-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtrylock.go
36 lines (30 loc) · 847 Bytes
/
trylock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package trylock
import (
"sync"
"sync/atomic"
"unsafe"
)
const mutexLocked = 1 << iota
// Mutex is simple sync.Mutex + ability to try to Lock.
type Mutex struct {
in sync.Mutex
}
// Lock locks m.
// If the lock is already in use, the calling goroutine
// blocks until the mutex is available.
func (m *Mutex) Lock() {
m.in.Lock()
}
// Unlock unlocks m.
// It is a run-time error if m is not locked on entry to Unlock.
//
// A locked Mutex is not associated with a particular goroutine.
// It is allowed for one goroutine to lock a Mutex and then
// arrange for another goroutine to unlock it.
func (m *Mutex) Unlock() {
m.in.Unlock()
}
// TryLock tries to lock m. It returns true in case of success, false otherwise.
func (m *Mutex) TryLock() bool {
return atomic.CompareAndSwapInt32((*int32)(unsafe.Pointer(&m.in)), 0, mutexLocked)
}