forked from NebulousLabs/Sia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrymutex.go
64 lines (54 loc) · 1.29 KB
/
trymutex.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package sync
import (
"sync"
"time"
)
// TryMutex provides a mutex that allows you to attempt to grab a mutex, and
// then fail if the mutex is either not grabbed immediately or is not grabbed
// by the specified duration.
type TryMutex struct {
once sync.Once
lock chan struct{}
}
// init will create the channel that manages the lock.
func (tm *TryMutex) init() {
tm.lock = make(chan struct{}, 1)
tm.lock <- struct{}{}
}
// Lock grabs a lock on the TryMutex, blocking until the lock is obtained.
func (tm *TryMutex) Lock() {
tm.once.Do(tm.init)
<-tm.lock
}
// TryLock grabs a lock on the TryMutex, returning an error if the mutex is
// already locked.
func (tm *TryMutex) TryLock() bool {
tm.once.Do(tm.init)
select {
case <-tm.lock:
return true
default:
return false
}
}
// TryLockTimed grabs a lock on the TryMutex, returning an error if the mutex
// is not grabbed after the provided duration.
func (tm *TryMutex) TryLockTimed(t time.Duration) bool {
tm.once.Do(tm.init)
select {
case <-tm.lock:
return true
case <-time.After(t):
return false
}
}
// Unlock releases a lock on the TryMutex.
func (tm *TryMutex) Unlock() {
tm.once.Do(tm.init)
select {
case tm.lock <- struct{}{}:
// Success - do nothing.
default:
panic("unlock called when TryMutex is not locked")
}
}