diff options
| author | Katie Hockman <katie@golang.org> | 2020-12-14 10:03:05 -0500 |
|---|---|---|
| committer | Katie Hockman <katie@golang.org> | 2020-12-14 10:06:13 -0500 |
| commit | 0345ede87ee12698988973884cfc0fd3d499dffd (patch) | |
| tree | 7123cff141ee5661208d2f5f437b8f5252ac7f6a /src/sync | |
| parent | 4651d6b267818b0e0d128a5443289717c4bb8cbc (diff) | |
| parent | 0a02371b0576964e81c3b40d328db9a3ef3b031b (diff) | |
| download | go-0345ede87ee12698988973884cfc0fd3d499dffd.tar.xz | |
[dev.fuzz] all: merge master into dev.fuzz
Change-Id: I5d8c8329ccc9d747bd81ade6b1cb7cb8ae2e94b2
Diffstat (limited to 'src/sync')
| -rw-r--r-- | src/sync/atomic/atomic_test.go | 9 | ||||
| -rw-r--r-- | src/sync/atomic/doc.go | 11 | ||||
| -rw-r--r-- | src/sync/once.go | 4 | ||||
| -rw-r--r-- | src/sync/pool.go | 20 | ||||
| -rw-r--r-- | src/sync/poolqueue.go | 2 | ||||
| -rw-r--r-- | src/sync/rwmutex.go | 13 |
6 files changed, 45 insertions, 14 deletions
diff --git a/src/sync/atomic/atomic_test.go b/src/sync/atomic/atomic_test.go index 83e7c8d763..eadc962f70 100644 --- a/src/sync/atomic/atomic_test.go +++ b/src/sync/atomic/atomic_test.go @@ -1397,8 +1397,15 @@ func TestStoreLoadRelAcq64(t *testing.T) { func shouldPanic(t *testing.T, name string, f func()) { defer func() { - if recover() == nil { + // Check that all GC maps are sane. + runtime.GC() + + err := recover() + want := "unaligned 64-bit atomic operation" + if err == nil { t.Errorf("%s did not panic", name) + } else if s, _ := err.(string); s != want { + t.Errorf("%s: wanted panic %q, got %q", name, want, err) } }() f() diff --git a/src/sync/atomic/doc.go b/src/sync/atomic/doc.go index ff4ad80049..805ef956d5 100644 --- a/src/sync/atomic/doc.go +++ b/src/sync/atomic/doc.go @@ -43,15 +43,14 @@ import ( "unsafe" ) -// BUG(rsc): On x86-32, the 64-bit functions use instructions unavailable before the Pentium MMX. +// BUG(rsc): On 386, the 64-bit functions use instructions unavailable before the Pentium MMX. // // On non-Linux ARM, the 64-bit functions use instructions unavailable before the ARMv6k core. // -// On ARM, x86-32, and 32-bit MIPS, -// it is the caller's responsibility to arrange for 64-bit -// alignment of 64-bit words accessed atomically. The first word in a -// variable or in an allocated struct, array, or slice can be relied upon to be -// 64-bit aligned. +// On ARM, 386, and 32-bit MIPS, it is the caller's responsibility +// to arrange for 64-bit alignment of 64-bit words accessed atomically. +// The first word in a variable or in an allocated struct, array, or slice can +// be relied upon to be 64-bit aligned. // SwapInt32 atomically stores new into *addr and returns the previous *addr value. func SwapInt32(addr *int32, new int32) (old int32) diff --git a/src/sync/once.go b/src/sync/once.go index ca04408224..8844314e7e 100644 --- a/src/sync/once.go +++ b/src/sync/once.go @@ -9,11 +9,13 @@ import ( ) // Once is an object that will perform exactly one action. +// +// A Once must not be copied after first use. type Once struct { // done indicates whether the action has been performed. // It is first in the struct because it is used in the hot path. // The hot path is inlined at every call site. - // Placing done first allows more compact instructions on some architectures (amd64/x86), + // Placing done first allows more compact instructions on some architectures (amd64/386), // and fewer instructions (to calculate offset) on other architectures. done uint32 m Mutex diff --git a/src/sync/pool.go b/src/sync/pool.go index ca7afdb12f..1ae70127ac 100644 --- a/src/sync/pool.go +++ b/src/sync/pool.go @@ -152,8 +152,8 @@ func (p *Pool) Get() interface{} { func (p *Pool) getSlow(pid int) interface{} { // See the comment in pin regarding ordering of the loads. - size := atomic.LoadUintptr(&p.localSize) // load-acquire - locals := p.local // load-consume + size := runtime_LoadAcquintptr(&p.localSize) // load-acquire + locals := p.local // load-consume // Try to steal one element from other procs. for i := 0; i < int(size); i++ { l := indexLocal(locals, (pid+i+1)%int(size)) @@ -198,8 +198,8 @@ func (p *Pool) pin() (*poolLocal, int) { // Since we've disabled preemption, GC cannot happen in between. // Thus here we must observe local at least as large localSize. // We can observe a newer/larger local, it is fine (we must observe its zero-initialized-ness). - s := atomic.LoadUintptr(&p.localSize) // load-acquire - l := p.local // load-consume + s := runtime_LoadAcquintptr(&p.localSize) // load-acquire + l := p.local // load-consume if uintptr(pid) < s { return indexLocal(l, pid), pid } @@ -226,7 +226,7 @@ func (p *Pool) pinSlow() (*poolLocal, int) { size := runtime.GOMAXPROCS(0) local := make([]poolLocal, size) atomic.StorePointer(&p.local, unsafe.Pointer(&local[0])) // store-release - atomic.StoreUintptr(&p.localSize, uintptr(size)) // store-release + runtime_StoreReluintptr(&p.localSize, uintptr(size)) // store-release return &local[pid], pid } @@ -282,3 +282,13 @@ func indexLocal(l unsafe.Pointer, i int) *poolLocal { func runtime_registerPoolCleanup(cleanup func()) func runtime_procPin() int func runtime_procUnpin() + +// The below are implemented in runtime/internal/atomic and the +// compiler also knows to intrinsify the symbol we linkname into this +// package. + +//go:linkname runtime_LoadAcquintptr runtime/internal/atomic.LoadAcquintptr +func runtime_LoadAcquintptr(ptr *uintptr) uintptr + +//go:linkname runtime_StoreReluintptr runtime/internal/atomic.StoreReluintptr +func runtime_StoreReluintptr(ptr *uintptr, val uintptr) uintptr diff --git a/src/sync/poolqueue.go b/src/sync/poolqueue.go index 22f74969d9..9be83e9a43 100644 --- a/src/sync/poolqueue.go +++ b/src/sync/poolqueue.go @@ -57,7 +57,7 @@ const dequeueBits = 32 // the index. We divide by 4 so this fits in an int on 32-bit. const dequeueLimit = (1 << dequeueBits) / 4 -// dequeueNil is used in poolDeqeue to represent interface{}(nil). +// dequeueNil is used in poolDequeue to represent interface{}(nil). // Since we use nil to represent empty slots, we need a sentinel value // to represent nil. type dequeueNil *struct{} diff --git a/src/sync/rwmutex.go b/src/sync/rwmutex.go index dc0faf6a60..3012b5548e 100644 --- a/src/sync/rwmutex.go +++ b/src/sync/rwmutex.go @@ -35,6 +35,19 @@ type RWMutex struct { const rwmutexMaxReaders = 1 << 30 +// Happens-before relationships are indicated to the race detector via: +// - Unlock -> Lock: readerSem +// - Unlock -> RLock: readerSem +// - RUnlock -> Lock: writerSem +// +// The methods below temporarily disable handling of race synchronization +// events in order to provide the more precise model above to the race +// detector. +// +// For example, atomic.AddInt32 in RLock should not appear to provide +// acquire-release semantics, which would incorrectly synchronize racing +// readers, thus potentially missing races. + // RLock locks rw for reading. // // It should not be used for recursive read locking; a blocked Lock |
