Merge pull request #8091 from iangudger:stdlib-trylock

PiperOrigin-RevId: 482327634
This commit is contained in:
gVisor bot
2022-10-19 16:06:27 -07:00
4 changed files with 5 additions and 105 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.18
- run: tools/go_branch.sh
- run: git checkout go && git clean -xf . && go build ./...
- if: github.event_name == 'push'
-1
View File
@@ -42,7 +42,6 @@ go_test(
size = "small",
srcs = [
"gate_test.go",
"mutex_test.go",
"rwmutex_test.go",
"seqcount_test.go",
],
-72
View File
@@ -1,72 +0,0 @@
// Copyright 2019 The gVisor Authors.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +checkalignedignore
package sync
import (
"sync"
"testing"
"unsafe"
)
// TestStructSize verifies that syncMutex's size hasn't drifted from the
// standard library's version.
//
// The correctness of this package relies on these remaining in sync.
func TestStructSize(t *testing.T) {
const (
got = unsafe.Sizeof(syncMutex{})
want = unsafe.Sizeof(sync.Mutex{})
)
if got != want {
t.Errorf("got sizeof(syncMutex) = %d, want = sizeof(sync.Mutex) = %d", got, want)
}
}
// TestFieldValues verifies that the semantics of syncMutex.state from the
// standard library's implementation.
//
// The correctness of this package relies on these remaining in sync.
func TestFieldValues(t *testing.T) {
var m Mutex
m.Lock()
if got := *m.m.state(); got != mutexLocked {
t.Errorf("got locked sync.Mutex.state = %d, want = %d", got, mutexLocked)
}
m.Unlock()
if got := *m.m.state(); got != mutexUnlocked {
t.Errorf("got unlocked sync.Mutex.state = %d, want = %d", got, mutexUnlocked)
}
}
func TestDoubleTryLock(t *testing.T) {
var m Mutex
if !m.TryLock() {
t.Fatal("failed to aquire lock")
}
if m.TryLock() {
t.Fatal("unexpectedly succeeded in aquiring locked mutex")
}
}
func TestTryLockAfterLock(t *testing.T) {
var m Mutex
m.Lock()
if m.TryLock() {
t.Fatal("unexpectedly succeeded in aquiring locked mutex")
}
}
func TestTryLockUnlock(t *testing.T) {
var m Mutex
if !m.TryLock() {
t.Fatal("failed to aquire lock")
}
m.Unlock() // +checklocksforce
if !m.TryLock() {
t.Fatal("failed to aquire lock after unlock")
}
}
+4 -31
View File
@@ -3,62 +3,35 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.13 && !go1.21
// +build go1.13,!go1.21
// When updating the build constraint (above), check that syncMutex matches the
// standard library sync.Mutex definition.
package sync
import (
"sync"
"sync/atomic"
"unsafe"
)
// CrossGoroutineMutex is equivalent to Mutex, but it need not be unlocked by a
// the same goroutine that locked the mutex.
type CrossGoroutineMutex struct {
sync.Mutex
}
type syncMutex struct {
state int32
sema uint32
}
func (m *CrossGoroutineMutex) state() *int32 {
return &(*syncMutex)(unsafe.Pointer(&m.Mutex)).state
m sync.Mutex
}
// Lock locks the underlying Mutex.
// +checklocksignore
func (m *CrossGoroutineMutex) Lock() {
m.Mutex.Lock()
m.m.Lock()
}
// Unlock unlocks the underlying Mutex.
// +checklocksignore
func (m *CrossGoroutineMutex) Unlock() {
m.Mutex.Unlock()
m.m.Unlock()
}
const (
mutexUnlocked = 0
mutexLocked = 1
)
// TryLock tries to acquire the mutex. It returns true if it succeeds and false
// otherwise. TryLock does not block.
func (m *CrossGoroutineMutex) TryLock() bool {
if atomic.CompareAndSwapInt32(m.state(), mutexUnlocked, mutexLocked) {
if RaceEnabled {
RaceAcquire(unsafe.Pointer(&m.Mutex))
}
return true
}
return false
return m.m.TryLock()
}
// Mutex is a mutual exclusion lock. The zero value for a Mutex is an unlocked