blob: 7a97475216616b0d28fb6f578bde2a64deb0a313 (
plain)
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
|
package mutexes
import "sync"
// WithSafety wrapps the supplied Mutex to protect unlock fns
// from being called multiple times
func WithSafety(mu Mutex) Mutex {
return &safeMutex{mu: mu}
}
// WithSafetyRW wrapps the supplied RWMutex to protect unlock
// fns from being called multiple times
func WithSafetyRW(mu RWMutex) RWMutex {
return &safeRWMutex{mu: mu}
}
// safeMutex simply wraps a Mutex to add multi-unlock safety
type safeMutex struct{ mu Mutex }
func (mu *safeMutex) Lock() func() {
unlock := mu.mu.Lock()
once := sync.Once{}
return func() { once.Do(unlock) }
}
// safeRWMutex simply wraps a RWMutex to add multi-unlock safety
type safeRWMutex struct{ mu RWMutex }
func (mu *safeRWMutex) Lock() func() {
unlock := mu.mu.Lock()
once := sync.Once{}
return func() { once.Do(unlock) }
}
func (mu *safeRWMutex) RLock() func() {
unlock := mu.mu.RLock()
once := sync.Once{}
return func() { once.Do(unlock) }
}
|