-
Notifications
You must be signed in to change notification settings - Fork 8
/
lock.go
57 lines (53 loc) · 2.07 KB
/
lock.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
// Package go-lock is a Golang library implementing an effcient read-write lock with the following built-in mechanism:
//
// 1) Mutex with timeout mechanism
//
// 2) Trylock
//
// 3) No-starve read-write solution
//
package lock
import (
"context"
"sync"
"time"
)
// Mutex is a mutual exclusion lock, and must not be copied after first use.
type Mutex interface {
// Lock acquires the lock.
// If it is currently held by others, Lock will wait until it has a chance to acquire it.
Lock()
// TryLock attempts to acquire the lock without blocking.
// Return false if someone is holding it now.
TryLock() bool
// TryLockWithTimeout attempts to acquire the lock within a period of time.
// Return false if spending time is more than duration and no chance to acquire it.
TryLockWithTimeout(time.Duration) bool
// TryLockWithContext attempts to acquire the lock, blocking until resources
// are available or ctx is done (timeout or cancellation).
TryLockWithContext(ctx context.Context) bool
// Unlock releases the lock.
Unlock()
}
// RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of
// readers or a single writer. It must not be copied after first use.
type RWMutex interface {
Mutex
// RLock acquires the read lock.
// If it is currently held by others writing, RLock will wait until it has a chance to acquire it.
RLock()
// RTryLock attempts to acquire the read lock without blocking.
// Return false if someone is writing it now.
RTryLock() bool
// RTryLockWithTimeout attempts to acquire the read lock within a period of time.
// Return false if spending time is more than duration and no chance to acquire it.
RTryLockWithTimeout(time.Duration) bool
// RTryLockWithContext attempts to acquire the read lock, blocking until resources
// are available or ctx is done (timeout or cancellation).
RTryLockWithContext(ctx context.Context) bool
// RUnlock releases the read lock.
RUnlock()
// RLocker returns a Locker interface that implements the Lock and Unlock methods
// by calling rw.RLock and rw.RUnlock.
RLocker() sync.Locker
}