2018-23-01 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / go / sync / runtime_sema_test.go
bloba680847edf87a310aeeefd52e1b86e8a0d71e5c8
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 "testing"
13 func BenchmarkSemaUncontended(b *testing.B) {
14 type PaddedSem struct {
15 sem uint32
16 pad [32]uint32
18 b.RunParallel(func(pb *testing.PB) {
19 sem := new(PaddedSem)
20 for pb.Next() {
21 Runtime_Semrelease(&sem.sem, false)
22 Runtime_Semacquire(&sem.sem)
27 func benchmarkSema(b *testing.B, block, work bool) {
28 if b.N == 0 {
29 return
31 sem := uint32(0)
32 if block {
33 done := make(chan bool)
34 go func() {
35 for p := 0; p < runtime.GOMAXPROCS(0)/2; p++ {
36 Runtime_Semacquire(&sem)
38 done <- true
39 }()
40 defer func() {
41 <-done
42 }()
44 b.RunParallel(func(pb *testing.PB) {
45 foo := 0
46 for pb.Next() {
47 Runtime_Semrelease(&sem, false)
48 if work {
49 for i := 0; i < 100; i++ {
50 foo *= 2
51 foo /= 2
54 Runtime_Semacquire(&sem)
56 _ = foo
57 Runtime_Semrelease(&sem, false)
61 func BenchmarkSemaSyntNonblock(b *testing.B) {
62 benchmarkSema(b, false, false)
65 func BenchmarkSemaSyntBlock(b *testing.B) {
66 benchmarkSema(b, true, false)
69 func BenchmarkSemaWorkNonblock(b *testing.B) {
70 benchmarkSema(b, false, true)
73 func BenchmarkSemaWorkBlock(b *testing.B) {
74 benchmarkSema(b, true, true)