]> Cypherpunks repositories - gostls13.git/commitdiff
sync: throw, not panic, for unlock of unlocked mutex
authorRhys Hiltner <rhys@justin.tv>
Thu, 7 Dec 2017 21:38:16 +0000 (13:38 -0800)
committerIan Lance Taylor <iant@golang.org>
Fri, 8 Dec 2017 13:40:21 +0000 (13:40 +0000)
This was originally done in https://golang.org/cl/31359 but partially
undone (apparently unintentionally) in https://golang.org/cl/34310

Fix it, and update tests to ensure the error is unrecoverable.

Fixes #23039

Change-Id: I923ebd613a05e67d8acce77f4a68c64c8574faa6
Reviewed-on: https://go-review.googlesource.com/82656
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
src/sync/mutex.go
src/sync/mutex_test.go

index 1232c629b180b1375e5e825b6a399445decb1fe1..4c5582c809457a9689f336695e3c3220de714bfc 100644 (file)
@@ -118,7 +118,7 @@ func (m *Mutex) Lock() {
                        // The goroutine has been woken from sleep,
                        // so we need to reset the flag in either case.
                        if new&mutexWoken == 0 {
-                               panic("sync: inconsistent mutex state")
+                               throw("sync: inconsistent mutex state")
                        }
                        new &^= mutexWoken
                }
@@ -140,7 +140,7 @@ func (m *Mutex) Lock() {
                                // inconsistent state: mutexLocked is not set and we are still
                                // accounted as waiter. Fix that.
                                if old&(mutexLocked|mutexWoken) != 0 || old>>mutexWaiterShift == 0 {
-                                       panic("sync: inconsistent mutex state")
+                                       throw("sync: inconsistent mutex state")
                                }
                                delta := int32(mutexLocked - 1<<mutexWaiterShift)
                                if !starving || old>>mutexWaiterShift == 1 {
@@ -181,7 +181,7 @@ func (m *Mutex) Unlock() {
        // Fast path: drop lock bit.
        new := atomic.AddInt32(&m.state, -mutexLocked)
        if (new+mutexLocked)&mutexLocked == 0 {
-               panic("sync: unlock of unlocked mutex")
+               throw("sync: unlock of unlocked mutex")
        }
        if new&mutexStarving == 0 {
                old := new
index 784471df12901c5d22df43bbce62bde2a9c9e31c..521468439abc2b5a5f4d7a73856edbbb509381d2 100644 (file)
@@ -155,7 +155,10 @@ func init() {
        if len(os.Args) == 3 && os.Args[1] == "TESTMISUSE" {
                for _, test := range misuseTests {
                        if test.name == os.Args[2] {
-                               test.f()
+                               func() {
+                                       defer func() { recover() }()
+                                       test.f()
+                               }()
                                fmt.Printf("test completed\n")
                                os.Exit(0)
                        }