diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-10-23 04:31:11 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-10-23 04:31:11 +0000 |
commit | 4ccad563d2a3559f0557bfb177bcf45144219bdf (patch) | |
tree | 46bb86f514fbf6bad82da48e69a18fb09d878834 /libgo/go/sync | |
parent | 0b7463235f0e23c624d1911c9b15f531108cc5a6 (diff) | |
download | gcc-4ccad563d2a3559f0557bfb177bcf45144219bdf.zip gcc-4ccad563d2a3559f0557bfb177bcf45144219bdf.tar.gz gcc-4ccad563d2a3559f0557bfb177bcf45144219bdf.tar.bz2 |
libgo: Update to current sources.
From-SVN: r192704
Diffstat (limited to 'libgo/go/sync')
-rw-r--r-- | libgo/go/sync/atomic/64bit_linux_arm.go | 36 | ||||
-rw-r--r-- | libgo/go/sync/atomic/atomic_test.go | 140 | ||||
-rw-r--r-- | libgo/go/sync/atomic/doc.go | 48 | ||||
-rw-r--r-- | libgo/go/sync/atomic/race.go | 191 | ||||
-rw-r--r-- | libgo/go/sync/cond.go | 18 | ||||
-rw-r--r-- | libgo/go/sync/mutex.go | 16 | ||||
-rw-r--r-- | libgo/go/sync/once.go | 2 | ||||
-rw-r--r-- | libgo/go/sync/once_test.go | 29 | ||||
-rw-r--r-- | libgo/go/sync/race.go | 34 | ||||
-rw-r--r-- | libgo/go/sync/race0.go | 28 | ||||
-rw-r--r-- | libgo/go/sync/rwmutex.go | 36 | ||||
-rw-r--r-- | libgo/go/sync/waitgroup.go | 29 |
12 files changed, 512 insertions, 95 deletions
diff --git a/libgo/go/sync/atomic/64bit_linux_arm.go b/libgo/go/sync/atomic/64bit_linux_arm.go new file mode 100644 index 0000000..f070e78 --- /dev/null +++ b/libgo/go/sync/atomic/64bit_linux_arm.go @@ -0,0 +1,36 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package atomic + +func loadUint64(addr *uint64) (val uint64) { + for { + val = *addr + if CompareAndSwapUint64(addr, val, val) { + break + } + } + return +} + +func storeUint64(addr *uint64, val uint64) { + for { + old := *addr + if CompareAndSwapUint64(addr, old, val) { + break + } + } + return +} + +func addUint64(val *uint64, delta uint64) (new uint64) { + for { + old := *val + new = old + delta + if CompareAndSwapUint64(val, old, new) { + break + } + } + return +} diff --git a/libgo/go/sync/atomic/atomic_test.go b/libgo/go/sync/atomic/atomic_test.go index f60d997..53dfdbf40 100644 --- a/libgo/go/sync/atomic/atomic_test.go +++ b/libgo/go/sync/atomic/atomic_test.go @@ -640,73 +640,73 @@ func init() { } } -func hammerAddInt32(uval *uint32, count int) { - val := (*int32)(unsafe.Pointer(uval)) +func hammerAddInt32(uaddr *uint32, count int) { + addr := (*int32)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { - AddInt32(val, 1) + AddInt32(addr, 1) } } -func hammerAddUint32(val *uint32, count int) { +func hammerAddUint32(addr *uint32, count int) { for i := 0; i < count; i++ { - AddUint32(val, 1) + AddUint32(addr, 1) } } -func hammerAddUintptr32(uval *uint32, count int) { +func hammerAddUintptr32(uaddr *uint32, count int) { // only safe when uintptr is 32-bit. // not called on 64-bit systems. - val := (*uintptr)(unsafe.Pointer(uval)) + addr := (*uintptr)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { - AddUintptr(val, 1) + AddUintptr(addr, 1) } } -func hammerCompareAndSwapInt32(uval *uint32, count int) { - val := (*int32)(unsafe.Pointer(uval)) +func hammerCompareAndSwapInt32(uaddr *uint32, count int) { + addr := (*int32)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { for { - v := *val - if CompareAndSwapInt32(val, v, v+1) { + v := *addr + if CompareAndSwapInt32(addr, v, v+1) { break } } } } -func hammerCompareAndSwapUint32(val *uint32, count int) { +func hammerCompareAndSwapUint32(addr *uint32, count int) { for i := 0; i < count; i++ { for { - v := *val - if CompareAndSwapUint32(val, v, v+1) { + v := *addr + if CompareAndSwapUint32(addr, v, v+1) { break } } } } -func hammerCompareAndSwapUintptr32(uval *uint32, count int) { +func hammerCompareAndSwapUintptr32(uaddr *uint32, count int) { // only safe when uintptr is 32-bit. // not called on 64-bit systems. - val := (*uintptr)(unsafe.Pointer(uval)) + addr := (*uintptr)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { for { - v := *val - if CompareAndSwapUintptr(val, v, v+1) { + v := *addr + if CompareAndSwapUintptr(addr, v, v+1) { break } } } } -func hammerCompareAndSwapPointer32(uval *uint32, count int) { +func hammerCompareAndSwapPointer32(uaddr *uint32, count int) { // only safe when uintptr is 32-bit. // not called on 64-bit systems. - val := (*unsafe.Pointer)(unsafe.Pointer(uval)) + addr := (*unsafe.Pointer)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { for { - v := *val - if CompareAndSwapPointer(val, v, unsafe.Pointer(uintptr(v)+1)) { + v := *addr + if CompareAndSwapPointer(addr, v, unsafe.Pointer(uintptr(v)+1)) { break } } @@ -765,73 +765,73 @@ func init() { } } -func hammerAddInt64(uval *uint64, count int) { - val := (*int64)(unsafe.Pointer(uval)) +func hammerAddInt64(uaddr *uint64, count int) { + addr := (*int64)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { - AddInt64(val, 1) + AddInt64(addr, 1) } } -func hammerAddUint64(val *uint64, count int) { +func hammerAddUint64(addr *uint64, count int) { for i := 0; i < count; i++ { - AddUint64(val, 1) + AddUint64(addr, 1) } } -func hammerAddUintptr64(uval *uint64, count int) { +func hammerAddUintptr64(uaddr *uint64, count int) { // only safe when uintptr is 64-bit. // not called on 32-bit systems. - val := (*uintptr)(unsafe.Pointer(uval)) + addr := (*uintptr)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { - AddUintptr(val, 1) + AddUintptr(addr, 1) } } -func hammerCompareAndSwapInt64(uval *uint64, count int) { - val := (*int64)(unsafe.Pointer(uval)) +func hammerCompareAndSwapInt64(uaddr *uint64, count int) { + addr := (*int64)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { for { - v := *val - if CompareAndSwapInt64(val, v, v+1) { + v := *addr + if CompareAndSwapInt64(addr, v, v+1) { break } } } } -func hammerCompareAndSwapUint64(val *uint64, count int) { +func hammerCompareAndSwapUint64(addr *uint64, count int) { for i := 0; i < count; i++ { for { - v := *val - if CompareAndSwapUint64(val, v, v+1) { + v := *addr + if CompareAndSwapUint64(addr, v, v+1) { break } } } } -func hammerCompareAndSwapUintptr64(uval *uint64, count int) { +func hammerCompareAndSwapUintptr64(uaddr *uint64, count int) { // only safe when uintptr is 64-bit. // not called on 32-bit systems. - val := (*uintptr)(unsafe.Pointer(uval)) + addr := (*uintptr)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { for { - v := *val - if CompareAndSwapUintptr(val, v, v+1) { + v := *addr + if CompareAndSwapUintptr(addr, v, v+1) { break } } } } -func hammerCompareAndSwapPointer64(uval *uint64, count int) { +func hammerCompareAndSwapPointer64(uaddr *uint64, count int) { // only safe when uintptr is 64-bit. // not called on 32-bit systems. - val := (*unsafe.Pointer)(unsafe.Pointer(uval)) + addr := (*unsafe.Pointer)(unsafe.Pointer(uaddr)) for i := 0; i < count; i++ { for { - v := *val - if CompareAndSwapPointer(val, v, unsafe.Pointer(uintptr(v)+1)) { + v := *addr + if CompareAndSwapPointer(addr, v, unsafe.Pointer(uintptr(v)+1)) { break } } @@ -871,9 +871,9 @@ func TestHammer64(t *testing.T) { } } -func hammerStoreLoadInt32(t *testing.T, valp unsafe.Pointer) { - val := (*int32)(valp) - v := LoadInt32(val) +func hammerStoreLoadInt32(t *testing.T, paddr unsafe.Pointer) { + addr := (*int32)(paddr) + v := LoadInt32(addr) vlo := v & ((1 << 16) - 1) vhi := v >> 16 if vlo != vhi { @@ -883,12 +883,12 @@ func hammerStoreLoadInt32(t *testing.T, valp unsafe.Pointer) { if vlo == 1e4 { new = 0 } - StoreInt32(val, new) + StoreInt32(addr, new) } -func hammerStoreLoadUint32(t *testing.T, valp unsafe.Pointer) { - val := (*uint32)(valp) - v := LoadUint32(val) +func hammerStoreLoadUint32(t *testing.T, paddr unsafe.Pointer) { + addr := (*uint32)(paddr) + v := LoadUint32(addr) vlo := v & ((1 << 16) - 1) vhi := v >> 16 if vlo != vhi { @@ -898,38 +898,38 @@ func hammerStoreLoadUint32(t *testing.T, valp unsafe.Pointer) { if vlo == 1e4 { new = 0 } - StoreUint32(val, new) + StoreUint32(addr, new) } -func hammerStoreLoadInt64(t *testing.T, valp unsafe.Pointer) { - val := (*int64)(valp) - v := LoadInt64(val) +func hammerStoreLoadInt64(t *testing.T, paddr unsafe.Pointer) { + addr := (*int64)(paddr) + v := LoadInt64(addr) vlo := v & ((1 << 32) - 1) vhi := v >> 32 if vlo != vhi { t.Fatalf("Int64: %#x != %#x", vlo, vhi) } new := v + 1 + 1<<32 - StoreInt64(val, new) + StoreInt64(addr, new) } -func hammerStoreLoadUint64(t *testing.T, valp unsafe.Pointer) { - val := (*uint64)(valp) - v := LoadUint64(val) +func hammerStoreLoadUint64(t *testing.T, paddr unsafe.Pointer) { + addr := (*uint64)(paddr) + v := LoadUint64(addr) vlo := v & ((1 << 32) - 1) vhi := v >> 32 if vlo != vhi { t.Fatalf("Uint64: %#x != %#x", vlo, vhi) } new := v + 1 + 1<<32 - StoreUint64(val, new) + StoreUint64(addr, new) } -func hammerStoreLoadUintptr(t *testing.T, valp unsafe.Pointer) { - val := (*uintptr)(valp) +func hammerStoreLoadUintptr(t *testing.T, paddr unsafe.Pointer) { + addr := (*uintptr)(paddr) var test64 uint64 = 1 << 50 arch32 := uintptr(test64) == 0 - v := LoadUintptr(val) + v := LoadUintptr(addr) new := v if arch32 { vlo := v & ((1 << 16) - 1) @@ -950,14 +950,14 @@ func hammerStoreLoadUintptr(t *testing.T, valp unsafe.Pointer) { inc := uint64(1 + 1<<32) new = v + uintptr(inc) } - StoreUintptr(val, new) + StoreUintptr(addr, new) } -func hammerStoreLoadPointer(t *testing.T, valp unsafe.Pointer) { - val := (*unsafe.Pointer)(valp) +func hammerStoreLoadPointer(t *testing.T, paddr unsafe.Pointer) { + addr := (*unsafe.Pointer)(paddr) var test64 uint64 = 1 << 50 arch32 := uintptr(test64) == 0 - v := uintptr(LoadPointer(val)) + v := uintptr(LoadPointer(addr)) new := v if arch32 { vlo := v & ((1 << 16) - 1) @@ -978,7 +978,7 @@ func hammerStoreLoadPointer(t *testing.T, valp unsafe.Pointer) { inc := uint64(1 + 1<<32) new = v + uintptr(inc) } - StorePointer(val, unsafe.Pointer(new)) + StorePointer(addr, unsafe.Pointer(new)) } func TestHammerStoreLoad(t *testing.T) { diff --git a/libgo/go/sync/atomic/doc.go b/libgo/go/sync/atomic/doc.go index ecb4808..33e1bcf 100644 --- a/libgo/go/sync/atomic/doc.go +++ b/libgo/go/sync/atomic/doc.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build !race + // Package atomic provides low-level atomic memory primitives // useful for implementing synchronization algorithms. // @@ -14,12 +16,22 @@ // The compare-and-swap operation, implemented by the CompareAndSwapT // functions, is the atomic equivalent of: // -// if *val == old { -// *val = new +// if *addr == old { +// *addr = new // return true // } // return false // +// The add operation, implemented by the AddT functions, is the atomic +// equivalent of: +// +// *addr += delta +// return *addr +// +// The load and store operations, implemented by the LoadT and StoreT +// functions, are the atomic equivalents of "return *addr" and +// "*addr = val". +// package atomic import ( @@ -31,37 +43,37 @@ import ( // On x86-32, the 64-bit functions use instructions unavailable before the Pentium MMX. // CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value. -func CompareAndSwapInt32(val *int32, old, new int32) (swapped bool) +func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool) // CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value. -func CompareAndSwapInt64(val *int64, old, new int64) (swapped bool) +func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool) // CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value. -func CompareAndSwapUint32(val *uint32, old, new uint32) (swapped bool) +func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool) // CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value. -func CompareAndSwapUint64(val *uint64, old, new uint64) (swapped bool) +func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool) // CompareAndSwapUintptr executes the compare-and-swap operation for a uintptr value. -func CompareAndSwapUintptr(val *uintptr, old, new uintptr) (swapped bool) +func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool) // CompareAndSwapPointer executes the compare-and-swap operation for a unsafe.Pointer value. -func CompareAndSwapPointer(val *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool) +func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool) -// AddInt32 atomically adds delta to *val and returns the new value. -func AddInt32(val *int32, delta int32) (new int32) +// AddInt32 atomically adds delta to *addr and returns the new value. +func AddInt32(addr *int32, delta int32) (new int32) -// AddUint32 atomically adds delta to *val and returns the new value. -func AddUint32(val *uint32, delta uint32) (new uint32) +// AddUint32 atomically adds delta to *addr and returns the new value. +func AddUint32(addr *uint32, delta uint32) (new uint32) -// AddInt64 atomically adds delta to *val and returns the new value. -func AddInt64(val *int64, delta int64) (new int64) +// AddInt64 atomically adds delta to *addr and returns the new value. +func AddInt64(addr *int64, delta int64) (new int64) -// AddUint64 atomically adds delta to *val and returns the new value. -func AddUint64(val *uint64, delta uint64) (new uint64) +// AddUint64 atomically adds delta to *addr and returns the new value. +func AddUint64(addr *uint64, delta uint64) (new uint64) -// AddUintptr atomically adds delta to *val and returns the new value. -func AddUintptr(val *uintptr, delta uintptr) (new uintptr) +// AddUintptr atomically adds delta to *addr and returns the new value. +func AddUintptr(addr *uintptr, delta uintptr) (new uintptr) // LoadInt32 atomically loads *addr. func LoadInt32(addr *int32) (val int32) diff --git a/libgo/go/sync/atomic/race.go b/libgo/go/sync/atomic/race.go new file mode 100644 index 0000000..c362765 --- /dev/null +++ b/libgo/go/sync/atomic/race.go @@ -0,0 +1,191 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build race + +package atomic + +import ( + "runtime" + "unsafe" +) + +var mtx uint32 = 1 // same for all + +func CompareAndSwapInt32(val *int32, old, new int32) bool { + return CompareAndSwapUint32((*uint32)(unsafe.Pointer(val)), uint32(old), uint32(new)) +} + +func CompareAndSwapUint32(val *uint32, old, new uint32) (swapped bool) { + swapped = false + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(val)) + if *val == old { + *val = new + swapped = true + runtime.RaceReleaseMerge(unsafe.Pointer(val)) + } + runtime.RaceSemrelease(&mtx) + return +} + +func CompareAndSwapInt64(val *int64, old, new int64) bool { + return CompareAndSwapUint64((*uint64)(unsafe.Pointer(val)), uint64(old), uint64(new)) +} + +func CompareAndSwapUint64(val *uint64, old, new uint64) (swapped bool) { + swapped = false + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(val)) + if *val == old { + *val = new + swapped = true + runtime.RaceReleaseMerge(unsafe.Pointer(val)) + } + runtime.RaceSemrelease(&mtx) + return +} + +func CompareAndSwapPointer(val *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool) { + swapped = false + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(val)) + if *val == old { + *val = new + swapped = true + runtime.RaceReleaseMerge(unsafe.Pointer(val)) + } + runtime.RaceSemrelease(&mtx) + return +} + +func CompareAndSwapUintptr(val *uintptr, old, new uintptr) (swapped bool) { + swapped = false + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(val)) + if *val == old { + *val = new + swapped = true + runtime.RaceReleaseMerge(unsafe.Pointer(val)) + } + runtime.RaceSemrelease(&mtx) + return +} + +func AddInt32(val *int32, delta int32) int32 { + return int32(AddUint32((*uint32)(unsafe.Pointer(val)), uint32(delta))) +} + +func AddUint32(val *uint32, delta uint32) (new uint32) { + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(val)) + *val = *val + delta + new = *val + runtime.RaceReleaseMerge(unsafe.Pointer(val)) + runtime.RaceSemrelease(&mtx) + + return +} + +func AddInt64(val *int64, delta int64) int64 { + return int64(AddUint64((*uint64)(unsafe.Pointer(val)), uint64(delta))) +} + +func AddUint64(val *uint64, delta uint64) (new uint64) { + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(val)) + *val = *val + delta + new = *val + runtime.RaceReleaseMerge(unsafe.Pointer(val)) + runtime.RaceSemrelease(&mtx) + + return +} + +func AddUintptr(val *uintptr, delta uintptr) (new uintptr) { + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(val)) + *val = *val + delta + new = *val + runtime.RaceReleaseMerge(unsafe.Pointer(val)) + runtime.RaceSemrelease(&mtx) + + return +} + +func LoadInt32(addr *int32) int32 { + return int32(LoadUint32((*uint32)(unsafe.Pointer(addr)))) +} + +func LoadUint32(addr *uint32) (val uint32) { + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(addr)) + val = *addr + runtime.RaceSemrelease(&mtx) + return +} + +func LoadInt64(addr *int64) int64 { + return int64(LoadUint64((*uint64)(unsafe.Pointer(addr)))) +} + +func LoadUint64(addr *uint64) (val uint64) { + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(addr)) + val = *addr + runtime.RaceSemrelease(&mtx) + return +} + +func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer) { + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(addr)) + val = *addr + runtime.RaceSemrelease(&mtx) + return +} + +func LoadUintptr(addr *uintptr) (val uintptr) { + runtime.RaceSemacquire(&mtx) + runtime.RaceAcquire(unsafe.Pointer(addr)) + val = *addr + runtime.RaceSemrelease(&mtx) + return +} + +func StoreInt32(addr *int32, val int32) { + StoreUint32((*uint32)(unsafe.Pointer(addr)), uint32(val)) +} + +func StoreUint32(addr *uint32, val uint32) { + runtime.RaceSemacquire(&mtx) + *addr = val + runtime.RaceRelease(unsafe.Pointer(addr)) + runtime.RaceSemrelease(&mtx) +} + +func StoreInt64(addr *int64, val int64) { + StoreUint64((*uint64)(unsafe.Pointer(addr)), uint64(val)) +} + +func StoreUint64(addr *uint64, val uint64) { + runtime.RaceSemacquire(&mtx) + *addr = val + runtime.RaceRelease(unsafe.Pointer(addr)) + runtime.RaceSemrelease(&mtx) +} + +func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer) { + runtime.RaceSemacquire(&mtx) + *addr = val + runtime.RaceRelease(unsafe.Pointer(addr)) + runtime.RaceSemrelease(&mtx) +} + +func StoreUintptr(addr *uintptr, val uintptr) { + runtime.RaceSemacquire(&mtx) + *addr = val + runtime.RaceRelease(unsafe.Pointer(addr)) + runtime.RaceSemrelease(&mtx) +} diff --git a/libgo/go/sync/cond.go b/libgo/go/sync/cond.go index 1fc3dea..491b985 100644 --- a/libgo/go/sync/cond.go +++ b/libgo/go/sync/cond.go @@ -56,6 +56,9 @@ func NewCond(l Locker) *Cond { // c.L.Unlock() // func (c *Cond) Wait() { + if raceenabled { + raceDisable() + } c.m.Lock() if c.newSema == nil { c.newSema = new(uint32) @@ -63,6 +66,9 @@ func (c *Cond) Wait() { s := c.newSema c.newWaiters++ c.m.Unlock() + if raceenabled { + raceEnable() + } c.L.Unlock() runtime_Semacquire(s) c.L.Lock() @@ -73,6 +79,9 @@ func (c *Cond) Wait() { // It is allowed but not required for the caller to hold c.L // during the call. func (c *Cond) Signal() { + if raceenabled { + raceDisable() + } c.m.Lock() if c.oldWaiters == 0 && c.newWaiters > 0 { // Retire old generation; rename new to old. @@ -86,6 +95,9 @@ func (c *Cond) Signal() { runtime_Semrelease(c.oldSema) } c.m.Unlock() + if raceenabled { + raceEnable() + } } // Broadcast wakes all goroutines waiting on c. @@ -93,6 +105,9 @@ func (c *Cond) Signal() { // It is allowed but not required for the caller to hold c.L // during the call. func (c *Cond) Broadcast() { + if raceenabled { + raceDisable() + } c.m.Lock() // Wake both generations. if c.oldWaiters > 0 { @@ -109,4 +124,7 @@ func (c *Cond) Broadcast() { c.newSema = nil } c.m.Unlock() + if raceenabled { + raceEnable() + } } diff --git a/libgo/go/sync/mutex.go b/libgo/go/sync/mutex.go index 9494cc3..b4629eb 100644 --- a/libgo/go/sync/mutex.go +++ b/libgo/go/sync/mutex.go @@ -10,7 +10,10 @@ // Values containing the types defined in this package should not be copied. package sync -import "sync/atomic" +import ( + "sync/atomic" + "unsafe" +) // A Mutex is a mutual exclusion lock. // Mutexes can be created as part of other structures; @@ -38,6 +41,9 @@ const ( func (m *Mutex) Lock() { // Fast path: grab unlocked mutex. if atomic.CompareAndSwapInt32(&m.state, 0, mutexLocked) { + if raceenabled { + raceAcquire(unsafe.Pointer(m)) + } return } @@ -61,6 +67,10 @@ func (m *Mutex) Lock() { awoke = true } } + + if raceenabled { + raceAcquire(unsafe.Pointer(m)) + } } // Unlock unlocks m. @@ -70,6 +80,10 @@ func (m *Mutex) Lock() { // It is allowed for one goroutine to lock a Mutex and then // arrange for another goroutine to unlock it. func (m *Mutex) Unlock() { + if raceenabled { + raceRelease(unsafe.Pointer(m)) + } + // Fast path: drop lock bit. new := atomic.AddInt32(&m.state, -mutexLocked) if (new+mutexLocked)&mutexLocked == 0 { diff --git a/libgo/go/sync/once.go b/libgo/go/sync/once.go index 04b714a..1699e86 100644 --- a/libgo/go/sync/once.go +++ b/libgo/go/sync/once.go @@ -38,6 +38,6 @@ func (o *Once) Do(f func()) { defer o.m.Unlock() if o.done == 0 { f() - atomic.CompareAndSwapUint32(&o.done, 0, 1) + atomic.StoreUint32(&o.done, 1) } } diff --git a/libgo/go/sync/once_test.go b/libgo/go/sync/once_test.go index 37075af..183069a 100644 --- a/libgo/go/sync/once_test.go +++ b/libgo/go/sync/once_test.go @@ -17,8 +17,11 @@ func (o *one) Increment() { *o++ } -func run(once *Once, o *one, c chan bool) { +func run(t *testing.T, once *Once, o *one, c chan bool) { once.Do(func() { o.Increment() }) + if v := *o; v != 1 { + t.Errorf("once failed inside run: %d is not 1", v) + } c <- true } @@ -28,14 +31,34 @@ func TestOnce(t *testing.T) { c := make(chan bool) const N = 10 for i := 0; i < N; i++ { - go run(once, o, c) + go run(t, once, o, c) } for i := 0; i < N; i++ { <-c } if *o != 1 { - t.Errorf("once failed: %d is not 1", *o) + t.Errorf("once failed outside run: %d is not 1", *o) + } +} + +func TestOncePanic(t *testing.T) { + once := new(Once) + for i := 0; i < 2; i++ { + func() { + defer func() { + if recover() == nil { + t.Fatalf("Once.Do() has not panic'ed") + } + }() + once.Do(func() { + panic("failed") + }) + }() } + once.Do(func() {}) + once.Do(func() { + t.Fatalf("Once called twice") + }) } func BenchmarkOnce(b *testing.B) { diff --git a/libgo/go/sync/race.go b/libgo/go/sync/race.go new file mode 100644 index 0000000..d9431af --- /dev/null +++ b/libgo/go/sync/race.go @@ -0,0 +1,34 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build race + +package sync + +import ( + "runtime" + "unsafe" +) + +const raceenabled = true + +func raceAcquire(addr unsafe.Pointer) { + runtime.RaceAcquire(addr) +} + +func raceRelease(addr unsafe.Pointer) { + runtime.RaceRelease(addr) +} + +func raceReleaseMerge(addr unsafe.Pointer) { + runtime.RaceReleaseMerge(addr) +} + +func raceDisable() { + runtime.RaceDisable() +} + +func raceEnable() { + runtime.RaceEnable() +} diff --git a/libgo/go/sync/race0.go b/libgo/go/sync/race0.go new file mode 100644 index 0000000..bef14f9 --- /dev/null +++ b/libgo/go/sync/race0.go @@ -0,0 +1,28 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build !race + +package sync + +import ( + "unsafe" +) + +const raceenabled = false + +func raceAcquire(addr unsafe.Pointer) { +} + +func raceRelease(addr unsafe.Pointer) { +} + +func raceReleaseMerge(addr unsafe.Pointer) { +} + +func raceDisable() { +} + +func raceEnable() { +} diff --git a/libgo/go/sync/rwmutex.go b/libgo/go/sync/rwmutex.go index 782a9c3..b494c64 100644 --- a/libgo/go/sync/rwmutex.go +++ b/libgo/go/sync/rwmutex.go @@ -4,7 +4,10 @@ package sync -import "sync/atomic" +import ( + "sync/atomic" + "unsafe" +) // An RWMutex is a reader/writer mutual exclusion lock. // The lock can be held by an arbitrary number of readers @@ -24,10 +27,17 @@ const rwmutexMaxReaders = 1 << 30 // RLock locks rw for reading. func (rw *RWMutex) RLock() { + if raceenabled { + raceDisable() + } if atomic.AddInt32(&rw.readerCount, 1) < 0 { // A writer is pending, wait for it. runtime_Semacquire(&rw.readerSem) } + if raceenabled { + raceEnable() + raceAcquire(unsafe.Pointer(&rw.readerSem)) + } } // RUnlock undoes a single RLock call; @@ -35,6 +45,10 @@ func (rw *RWMutex) RLock() { // It is a run-time error if rw is not locked for reading // on entry to RUnlock. func (rw *RWMutex) RUnlock() { + if raceenabled { + raceReleaseMerge(unsafe.Pointer(&rw.writerSem)) + raceDisable() + } if atomic.AddInt32(&rw.readerCount, -1) < 0 { // A writer is pending. if atomic.AddInt32(&rw.readerWait, -1) == 0 { @@ -42,6 +56,9 @@ func (rw *RWMutex) RUnlock() { runtime_Semrelease(&rw.writerSem) } } + if raceenabled { + raceEnable() + } } // Lock locks rw for writing. @@ -51,6 +68,9 @@ func (rw *RWMutex) RUnlock() { // a blocked Lock call excludes new readers from acquiring // the lock. func (rw *RWMutex) Lock() { + if raceenabled { + raceDisable() + } // First, resolve competition with other writers. rw.w.Lock() // Announce to readers there is a pending writer. @@ -59,6 +79,11 @@ func (rw *RWMutex) Lock() { if r != 0 && atomic.AddInt32(&rw.readerWait, r) != 0 { runtime_Semacquire(&rw.writerSem) } + if raceenabled { + raceEnable() + raceAcquire(unsafe.Pointer(&rw.readerSem)) + raceAcquire(unsafe.Pointer(&rw.writerSem)) + } } // Unlock unlocks rw for writing. It is a run-time error if rw is @@ -68,6 +93,12 @@ func (rw *RWMutex) Lock() { // goroutine. One goroutine may RLock (Lock) an RWMutex and then // arrange for another goroutine to RUnlock (Unlock) it. func (rw *RWMutex) Unlock() { + if raceenabled { + raceRelease(unsafe.Pointer(&rw.readerSem)) + raceRelease(unsafe.Pointer(&rw.writerSem)) + raceDisable() + } + // Announce to readers there is no active writer. r := atomic.AddInt32(&rw.readerCount, rwmutexMaxReaders) // Unblock blocked readers, if any. @@ -76,6 +107,9 @@ func (rw *RWMutex) Unlock() { } // Allow other writers to proceed. rw.w.Unlock() + if raceenabled { + raceEnable() + } } // RLocker returns a Locker interface that implements diff --git a/libgo/go/sync/waitgroup.go b/libgo/go/sync/waitgroup.go index bc9e738..9b0ffec 100644 --- a/libgo/go/sync/waitgroup.go +++ b/libgo/go/sync/waitgroup.go @@ -4,7 +4,10 @@ package sync -import "sync/atomic" +import ( + "sync/atomic" + "unsafe" +) // A WaitGroup waits for a collection of goroutines to finish. // The main goroutine calls Add to set the number of @@ -34,6 +37,11 @@ type WaitGroup struct { // If the counter becomes zero, all goroutines blocked on Wait() are released. // If the counter goes negative, Add panics. func (wg *WaitGroup) Add(delta int) { + if raceenabled { + raceReleaseMerge(unsafe.Pointer(wg)) + raceDisable() + defer raceEnable() + } v := atomic.AddInt32(&wg.counter, int32(delta)) if v < 0 { panic("sync: negative WaitGroup counter") @@ -57,7 +65,14 @@ func (wg *WaitGroup) Done() { // Wait blocks until the WaitGroup counter is zero. func (wg *WaitGroup) Wait() { + if raceenabled { + raceDisable() + } if atomic.LoadInt32(&wg.counter) == 0 { + if raceenabled { + raceEnable() + raceAcquire(unsafe.Pointer(wg)) + } return } wg.m.Lock() @@ -68,7 +83,15 @@ func (wg *WaitGroup) Wait() { // to avoid missing an Add. if atomic.LoadInt32(&wg.counter) == 0 { atomic.AddInt32(&wg.waiters, -1) + if raceenabled { + raceEnable() + raceAcquire(unsafe.Pointer(wg)) + raceDisable() + } wg.m.Unlock() + if raceenabled { + raceEnable() + } return } if wg.sema == nil { @@ -77,4 +100,8 @@ func (wg *WaitGroup) Wait() { s := wg.sema wg.m.Unlock() runtime_Semacquire(s) + if raceenabled { + raceEnable() + raceAcquire(unsafe.Pointer(wg)) + } } |