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.
15 func TestMutexLock(t
*testing
.T
) {
28 if mu
.RWUnlock(true) {
32 if !mu
.RWLock(false) {
35 if mu
.RWUnlock(false) {
40 func TestMutexClose(t
*testing
.T
) {
42 if !mu
.IncrefAndClose() {
55 if mu
.IncrefAndClose() {
60 func TestMutexCloseUnblock(t
*testing
.T
) {
64 for i
:= 0; i
< 4; i
++ {
73 // Concurrent goroutines must not be able to read lock the mutex.
74 time
.Sleep(time
.Millisecond
)
80 mu
.IncrefAndClose() // Must unblock the readers.
81 for i
:= 0; i
< 4; i
++ {
84 case <-time
.After(10 * time
.Second
):
91 if !mu
.RWUnlock(true) {
96 func TestMutexPanic(t
*testing
.T
) {
97 ensurePanics
:= func(f
func()) {
100 t
.Fatal("does not panic")
107 ensurePanics(func() { mu
.Decref() })
108 ensurePanics(func() { mu
.RWUnlock(true) })
109 ensurePanics(func() { mu
.RWUnlock(false) })
111 ensurePanics(func() { mu
.Incref(); mu
.Decref(); mu
.Decref() })
112 ensurePanics(func() { mu
.RWLock(true); mu
.RWUnlock(true); mu
.RWUnlock(true) })
113 ensurePanics(func() { mu
.RWLock(false); mu
.RWUnlock(false); mu
.RWUnlock(false) })
115 // ensure that it's still not broken
124 func TestMutexStress(t
*testing
.T
) {
131 defer runtime
.GOMAXPROCS(runtime
.GOMAXPROCS(P
))
132 done
:= make(chan bool)
134 var readState
[2]uint64
135 var writeState
[2]uint64
136 for p
:= 0; p
< P
; p
++ {
138 r
:= rand
.New(rand
.NewSource(rand
.Int63()))
139 for i
:= 0; i
< N
; i
++ {
151 if !mu
.RWLock(true) {
155 // Ensure that it provides mutual exclusion for readers.
156 if readState
[0] != readState
[1] {
162 if mu
.RWUnlock(true) {
167 if !mu
.RWLock(false) {
171 // Ensure that it provides mutual exclusion for writers.
172 if writeState
[0] != writeState
[1] {
178 if mu
.RWUnlock(false) {
187 for p
:= 0; p
< P
; p
++ {
190 if !mu
.IncrefAndClose() {