diff options
author | Jamie Liu <jamieliu@google.com> | 2020-04-02 11:55:55 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-04-02 11:57:06 -0700 |
commit | 30388ff5919df33e7184719dfc6c0d9cb110b2e2 (patch) | |
tree | ad7f89a55f7af62a33c434f44a7739e0795d37d3 /pkg/sync/mutex_unsafe.go | |
parent | 035836193e6d9e1fc9cce6a0161cb3907fbc2ef5 (diff) |
Rename files in //pkg/sync to better reflect what they contain.
PiperOrigin-RevId: 304447031
Diffstat (limited to 'pkg/sync/mutex_unsafe.go')
-rw-r--r-- | pkg/sync/mutex_unsafe.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/pkg/sync/mutex_unsafe.go b/pkg/sync/mutex_unsafe.go new file mode 100644 index 000000000..3dd15578b --- /dev/null +++ b/pkg/sync/mutex_unsafe.go @@ -0,0 +1,49 @@ +// 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. + +// +build go1.13 +// +build !go1.15 + +// When updating the build constraint (above), check that syncMutex matches the +// standard library sync.Mutex definition. + +package sync + +import ( + "sync" + "sync/atomic" + "unsafe" +) + +// Mutex is a try lock. +type Mutex struct { + sync.Mutex +} + +type syncMutex struct { + state int32 + sema uint32 +} + +func (m *Mutex) state() *int32 { + return &(*syncMutex)(unsafe.Pointer(&m.Mutex)).state +} + +const ( + mutexUnlocked = 0 + mutexLocked = 1 +) + +// TryLock tries to aquire the mutex. It returns true if it succeeds and false +// otherwise. TryLock does not block. +func (m *Mutex) TryLock() bool { + if atomic.CompareAndSwapInt32(m.state(), mutexUnlocked, mutexLocked) { + if RaceEnabled { + RaceAcquire(unsafe.Pointer(&m.Mutex)) + } + return true + } + return false +} |