Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: add Mutex.MustBeLocked #1366

Closed
msolo opened this issue Dec 22, 2010 · 7 comments
Closed

sync: add Mutex.MustBeLocked #1366

msolo opened this issue Dec 22, 2010 · 7 comments

Comments

@msolo
Copy link
Contributor

msolo commented Dec 22, 2010

To prevent careless programming, I want to panic if a lock is not held at a certain
point.

I could see the rationale for making this panic and renaming the method MustBeLocked()
or PanicIfUnlocked(). I can't think of too many valid use cases other than panicking.

I can see people not reading it carefully and assuming that !IsUnlocked() means that
they hold the lock, or that if IsUnlocked() { Lock() } would only lock if you weren't
already.



diff -r 23006c94f1aa src/pkg/sync/mutex.go
--- a/src/pkg/sync/mutex.go Fri Dec 17 14:00:46 2010 -0800
+++ b/src/pkg/sync/mutex.go Tue Dec 21 15:12:49 2010 -0800
@@ -59,3 +59,13 @@
    }
    runtime.Semrelease(&m.sema)
 }
+
+// Check if the mutex lock is unlocked.
+// The most common case is where you want to fail code with a high probability
+// if a mutex is not locked. There is no false positive, but there is a
+// false negative if another goroutine is holding the lock.
+func (m *Mutex) IsUnlocked() bool {
+   return m.key == 0
+}
+
+
@adg
Copy link
Contributor

adg commented Dec 29, 2010

Comment 1:

It seems like a useful debugging tool, but I'm also a little horrified by the idea of
code complex enough to need it. As you observed, the IsUnlocked described here isn't
reliable - an AssertUnlocked that panics if the mutex is locked would be better.

Labels changed: added packagechange.

Owner changed to r...@golang.org.

Status changed to Thinking.

@rsc
Copy link
Contributor

rsc commented Jan 3, 2011

Comment 2:

Should be IsLocked I think, just to avoid double negatives.

Status changed to Accepted.

@gopherbot
Copy link

Comment 3 by cw@f00f.org:

Why not an atomic LockIfUnlocked? That's useful sometimes to grab a lock when it's
unlocked and fail otherwise. 
It's also race free. IsLocked for debugging can be done via other means.

@msolo
Copy link
Contributor Author

msolo commented Jan 15, 2011

Comment 4:

The code that motivated the request was complex and in the end, splitting the lock into
two and making a very clear policy simplified things to the point that I removed this
patch.

@bradfitz
Copy link
Contributor

bradfitz commented May 3, 2011

Comment 5:

I'd like a panicing mutex.AssertLocked() but I'd hate to provide an accessor to allow or
encourage people to do crazy stuff based on the lock status.

@rsc
Copy link
Contributor

rsc commented Dec 9, 2011

Comment 6:

Labels changed: added priority-later.

@rsc
Copy link
Contributor

rsc commented Jan 29, 2012

Comment 7:

I think we can keep trying to avoid this.

Status changed to WontFix.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants