2014-04-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / libgo / go / sync / runtime_sema_test.go
blob57a8dbee78398f7173384c49f849f17af3c6cb0b
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 package sync_test
7 import (
8 "runtime"
9 . "sync"
10 "sync/atomic"
11 "testing"
14 func BenchmarkSemaUncontended(b *testing.B) {
15 type PaddedSem struct {
16 sem uint32
17 pad [32]uint32
19 const CallsPerSched = 1000
20 procs := runtime.GOMAXPROCS(-1)
21 N := int32(b.N / CallsPerSched)
22 c := make(chan bool, procs)
23 for p := 0; p < procs; p++ {
24 go func() {
25 sem := new(PaddedSem)
26 for atomic.AddInt32(&N, -1) >= 0 {
27 runtime.Gosched()
28 for g := 0; g < CallsPerSched; g++ {
29 Runtime_Semrelease(&sem.sem)
30 Runtime_Semacquire(&sem.sem)
33 c <- true
34 }()
36 for p := 0; p < procs; p++ {
37 <-c
41 func benchmarkSema(b *testing.B, block, work bool) {
42 const CallsPerSched = 1000
43 const LocalWork = 100
44 procs := runtime.GOMAXPROCS(-1)
45 N := int32(b.N / CallsPerSched)
46 c := make(chan bool, procs)
47 c2 := make(chan bool, procs/2)
48 sem := uint32(0)
49 if block {
50 for p := 0; p < procs/2; p++ {
51 go func() {
52 Runtime_Semacquire(&sem)
53 c2 <- true
54 }()
57 for p := 0; p < procs; p++ {
58 go func() {
59 foo := 0
60 for atomic.AddInt32(&N, -1) >= 0 {
61 runtime.Gosched()
62 for g := 0; g < CallsPerSched; g++ {
63 Runtime_Semrelease(&sem)
64 if work {
65 for i := 0; i < LocalWork; i++ {
66 foo *= 2
67 foo /= 2
70 Runtime_Semacquire(&sem)
73 c <- foo == 42
74 Runtime_Semrelease(&sem)
75 }()
77 if block {
78 for p := 0; p < procs/2; p++ {
79 <-c2
82 for p := 0; p < procs; p++ {
83 <-c
87 func BenchmarkSemaSyntNonblock(b *testing.B) {
88 benchmarkSema(b, false, false)
91 func BenchmarkSemaSyntBlock(b *testing.B) {
92 benchmarkSema(b, true, false)
95 func BenchmarkSemaWorkNonblock(b *testing.B) {
96 benchmarkSema(b, false, true)
99 func BenchmarkSemaWorkBlock(b *testing.B) {
100 benchmarkSema(b, true, true)