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() {