Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
26880d7e
Commit
26880d7e
authored
Feb 11, 2011
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sync: check Unlock of unlocked Mutex
R=r, adg CC=golang-dev
https://golang.org/cl/4180044
parent
ca5179d3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
1 deletion
+19
-1
mutex.go
src/pkg/sync/mutex.go
+6
-1
mutex_test.go
src/pkg/sync/mutex_test.go
+13
-0
No files found.
src/pkg/sync/mutex.go
View file @
26880d7e
...
@@ -53,9 +53,14 @@ func (m *Mutex) Lock() {
...
@@ -53,9 +53,14 @@ func (m *Mutex) Lock() {
// It is allowed for one goroutine to lock a Mutex and then
// It is allowed for one goroutine to lock a Mutex and then
// arrange for another goroutine to unlock it.
// arrange for another goroutine to unlock it.
func
(
m
*
Mutex
)
Unlock
()
{
func
(
m
*
Mutex
)
Unlock
()
{
if
xadd
(
&
m
.
key
,
-
1
)
==
0
{
switch
v
:=
xadd
(
&
m
.
key
,
-
1
);
{
case
v
==
0
:
// changed from 1 to 0; no contention
// changed from 1 to 0; no contention
return
return
case
int32
(
v
)
==
-
1
:
// changed from 0 to -1: wasn't locked
// (or there are 4 billion goroutines waiting)
panic
(
"sync: unlock of unlocked mutex"
)
}
}
runtime
.
Semrelease
(
&
m
.
sema
)
runtime
.
Semrelease
(
&
m
.
sema
)
}
}
src/pkg/sync/mutex_test.go
View file @
26880d7e
...
@@ -89,3 +89,16 @@ func BenchmarkContendedMutex(b *testing.B) {
...
@@ -89,3 +89,16 @@ func BenchmarkContendedMutex(b *testing.B) {
<-
c
<-
c
<-
c
<-
c
}
}
func
TestMutexPanic
(
t
*
testing
.
T
)
{
defer
func
()
{
if
recover
()
==
nil
{
t
.
Fatalf
(
"unlock of unlocked mutex did not panic"
)
}
}()
var
mu
Mutex
mu
.
Lock
()
mu
.
Unlock
()
mu
.
Unlock
()
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment