1 // Copyright 2009 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.
5 // GOMAXPROCS=10 go test
15 func HammerSemaphore(s
*uint32, loops
int, cdone
chan bool) {
16 for i
:= 0; i
< loops
; i
++ {
23 func TestSemaphore(t
*testing
.T
) {
27 for i
:= 0; i
< 10; i
++ {
28 go HammerSemaphore(s
, 1000, c
)
30 for i
:= 0; i
< 10; i
++ {
35 func BenchmarkUncontendedSemaphore(b
*testing
.B
) {
38 HammerSemaphore(s
, b
.N
, make(chan bool, 2))
41 func BenchmarkContendedSemaphore(b
*testing
.B
) {
46 defer runtime
.GOMAXPROCS(runtime
.GOMAXPROCS(2))
49 go HammerSemaphore(s
, b
.N
/2, c
)
50 go HammerSemaphore(s
, b
.N
/2, c
)
55 func HammerMutex(m
*Mutex
, loops
int, cdone
chan bool) {
56 for i
:= 0; i
< loops
; i
++ {
63 func TestMutex(t
*testing
.T
) {
66 for i
:= 0; i
< 10; i
++ {
67 go HammerMutex(m
, 1000, c
)
69 for i
:= 0; i
< 10; i
++ {
74 func TestMutexPanic(t
*testing
.T
) {
77 t
.Fatalf("unlock of unlocked mutex did not panic")
87 func BenchmarkMutexUncontended(b
*testing
.B
) {
88 type PaddedMutex
struct {
92 b
.RunParallel(func(pb
*testing
.PB
) {
101 func benchmarkMutex(b
*testing
.B
, slack
, work
bool) {
106 b
.RunParallel(func(pb
*testing
.PB
) {
112 for i
:= 0; i
< 100; i
++ {
122 func BenchmarkMutex(b
*testing
.B
) {
123 benchmarkMutex(b
, false, false)
126 func BenchmarkMutexSlack(b
*testing
.B
) {
127 benchmarkMutex(b
, true, false)
130 func BenchmarkMutexWork(b
*testing
.B
) {
131 benchmarkMutex(b
, false, true)
134 func BenchmarkMutexWorkSlack(b
*testing
.B
) {
135 benchmarkMutex(b
, true, true)