-
Notifications
You must be signed in to change notification settings - Fork 1
/
locker.go
74 lines (64 loc) · 1.39 KB
/
locker.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
65
66
67
68
69
70
71
72
73
74
// Package namedlocker implements in-memory named locks.
package namedlocker
import (
"errors"
"sync"
)
var (
// ErrUnlockOfUnlockedKey is the error reported when unlocking an unlocked key.
ErrUnlockOfUnlockedKey = errors.New("unlock of unlocked key")
pool = sync.Pool{New: func() interface{} { return &ref{} }}
)
type ref struct {
sync.Mutex
cnt int32
}
// Store is an in-memory store of named locks.
//
// The zero-value is usable as-is.
type Store struct {
mu sync.Mutex
refs map[string]*ref
}
// Lock acquires a lock on key.
// If key is locked, it blocks until it can be acquired.
func (s *Store) Lock(key string) {
s.mu.Lock()
r, ok := s.refs[key]
if !ok {
r = pool.Get().(*ref)
if s.refs == nil {
s.refs = map[string]*ref{}
}
s.refs[key] = r
}
r.cnt++
s.mu.Unlock()
r.Lock()
}
// TryUnlock releases the lock on key.
//
// If key is not locked, ErrUnlockOfUnlockedKey is returned.
func (s *Store) TryUnlock(key string) error {
s.mu.Lock()
defer s.mu.Unlock()
r, ok := s.refs[key]
if !ok || r.cnt <= 0 {
// should we panic if cnt is < 0? that suggests some state got corrupted.
return ErrUnlockOfUnlockedKey
}
r.Unlock()
r.cnt--
if r.cnt == 0 {
delete(s.refs, key)
pool.Put(r)
}
return nil
}
// Unlock is a wrapper around TryUnlock that panics if it returns an error.
func (s *Store) Unlock(key string) {
err := s.TryUnlock(key)
if err != nil {
panic(err)
}
}