OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / libgo / go / sync / mutex_test.go
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.
4
5 // GOMAXPROCS=10 gotest
6
7 package sync_test
8
9 import (
10         "runtime"
11         . "sync"
12         "testing"
13 )
14
15 func HammerSemaphore(s *uint32, loops int, cdone chan bool) {
16         for i := 0; i < loops; i++ {
17                 runtime.Semacquire(s)
18                 runtime.Semrelease(s)
19         }
20         cdone <- true
21 }
22
23 func TestSemaphore(t *testing.T) {
24         s := new(uint32)
25         *s = 1
26         c := make(chan bool)
27         for i := 0; i < 10; i++ {
28                 go HammerSemaphore(s, 1000, c)
29         }
30         for i := 0; i < 10; i++ {
31                 <-c
32         }
33 }
34
35 func BenchmarkUncontendedSemaphore(b *testing.B) {
36         s := new(uint32)
37         *s = 1
38         HammerSemaphore(s, b.N, make(chan bool, 2))
39 }
40
41 func BenchmarkContendedSemaphore(b *testing.B) {
42         b.StopTimer()
43         s := new(uint32)
44         *s = 1
45         c := make(chan bool)
46         runtime.GOMAXPROCS(2)
47         b.StartTimer()
48
49         go HammerSemaphore(s, b.N/2, c)
50         go HammerSemaphore(s, b.N/2, c)
51         <-c
52         <-c
53 }
54
55
56 func HammerMutex(m *Mutex, loops int, cdone chan bool) {
57         for i := 0; i < loops; i++ {
58                 m.Lock()
59                 m.Unlock()
60         }
61         cdone <- true
62 }
63
64 func TestMutex(t *testing.T) {
65         m := new(Mutex)
66         c := make(chan bool)
67         for i := 0; i < 10; i++ {
68                 go HammerMutex(m, 1000, c)
69         }
70         for i := 0; i < 10; i++ {
71                 <-c
72         }
73 }
74
75 func BenchmarkUncontendedMutex(b *testing.B) {
76         m := new(Mutex)
77         HammerMutex(m, b.N, make(chan bool, 2))
78 }
79
80 func BenchmarkContendedMutex(b *testing.B) {
81         b.StopTimer()
82         m := new(Mutex)
83         c := make(chan bool)
84         runtime.GOMAXPROCS(2)
85         b.StartTimer()
86
87         go HammerMutex(m, b.N/2, c)
88         go HammerMutex(m, b.N/2, c)
89         <-c
90         <-c
91 }