1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 func TestMutexLock(t
*testing
.T
) {
27 if mu
.RWUnlock(true) {
31 if !mu
.RWLock(false) {
34 if mu
.RWUnlock(false) {
39 func TestMutexClose(t
*testing
.T
) {
41 if !mu
.IncrefAndClose() {
54 if mu
.IncrefAndClose() {
59 func TestMutexCloseUnblock(t
*testing
.T
) {
63 for i
:= 0; i
< 4; i
++ {
72 // Concurrent goroutines must not be able to read lock the mutex.
73 time
.Sleep(time
.Millisecond
)
79 mu
.IncrefAndClose() // Must unblock the readers.
80 for i
:= 0; i
< 4; i
++ {
83 case <-time
.After(10 * time
.Second
):
90 if !mu
.RWUnlock(true) {
95 func TestMutexPanic(t
*testing
.T
) {
96 ensurePanics
:= func(f
func()) {
99 t
.Fatal("does not panic")
106 ensurePanics(func() { mu
.Decref() })
107 ensurePanics(func() { mu
.RWUnlock(true) })
108 ensurePanics(func() { mu
.RWUnlock(false) })
110 ensurePanics(func() { mu
.Incref(); mu
.Decref(); mu
.Decref() })
111 ensurePanics(func() { mu
.RWLock(true); mu
.RWUnlock(true); mu
.RWUnlock(true) })
112 ensurePanics(func() { mu
.RWLock(false); mu
.RWUnlock(false); mu
.RWUnlock(false) })
114 // ensure that it's still not broken
123 func TestMutexStress(t
*testing
.T
) {
130 defer runtime
.GOMAXPROCS(runtime
.GOMAXPROCS(P
))
131 done
:= make(chan bool)
133 var readState
[2]uint64
134 var writeState
[2]uint64
135 for p
:= 0; p
< P
; p
++ {
137 r
:= rand
.New(rand
.NewSource(rand
.Int63()))
138 for i
:= 0; i
< N
; i
++ {
150 if !mu
.RWLock(true) {
154 // Ensure that it provides mutual exclusion for readers.
155 if readState
[0] != readState
[1] {
161 if mu
.RWUnlock(true) {
166 if !mu
.RWLock(false) {
170 // Ensure that it provides mutual exclusion for writers.
171 if writeState
[0] != writeState
[1] {
177 if mu
.RWUnlock(false) {
186 for p
:= 0; p
< P
; p
++ {
189 if !mu
.IncrefAndClose() {