diff options
author | gVisor bot <gvisor-bot@google.com> | 2020-01-22 03:03:09 +0000 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-01-22 03:03:09 +0000 |
commit | ca8d1cb6db085a700627f613d37b66ec2d1313e7 (patch) | |
tree | 50ccca80587413ea3617aef16624f6987001348c /pkg/sync | |
parent | a6468f1765f7dcc4dc850897e755901811f04345 (diff) | |
parent | 1effdc091b441c4b1ada4327c1422cd360f80f98 (diff) |
Merge release-20200115.0-67-g1effdc0 (automated)
Diffstat (limited to 'pkg/sync')
-rwxr-xr-x | pkg/sync/tmutex_unsafe.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/pkg/sync/tmutex_unsafe.go b/pkg/sync/tmutex_unsafe.go new file mode 100755 index 000000000..3c32f8371 --- /dev/null +++ b/pkg/sync/tmutex_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" +) + +// TMutex is a try lock. +type TMutex struct { + sync.Mutex +} + +type syncMutex struct { + state int32 + sema uint32 +} + +func (m *TMutex) 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 *TMutex) TryLock() bool { + if atomic.CompareAndSwapInt32(m.state(), mutexUnlocked, mutexLocked) { + if RaceEnabled { + RaceAcquire(unsafe.Pointer(&m.Mutex)) + } + return true + } + return false +} |