-
Notifications
You must be signed in to change notification settings - Fork 2
/
mutex.go
58 lines (51 loc) · 1.06 KB
/
mutex.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
package xsync
import (
"sync"
)
// A Mutex is a mutual exclusion lock with ability to be locked with priority
// and/or cancellation.
// The zero value for a Mutex is an unlocked mutex.
//
// A Mutex must not be copied after first use.
type Mutex struct {
mu sync.Mutex
once sync.Once
cond Cond
locked bool
}
func (m *Mutex) init() {
m.once.Do(func() {
m.cond = Cond{
L: &m.mu,
}
})
}
// Lock locks m.
//
// Unlike sync.Mutex if lock is already in use Lock() can return before
// Unlock() if and only if given demand's cancellation channel became filled of
// closed. In that case returned err is ErrCanceled.
func (m *Mutex) Lock(d Demand) error {
m.init()
m.mu.Lock()
defer m.mu.Unlock()
for m.locked {
if err := m.cond.Wait(d); err != nil {
return ErrCanceled
}
}
m.locked = true
return nil
}
// Unlock unlocks m.
// It panics if m is not locked on entry to Unlock().
func (m *Mutex) Unlock() {
m.mu.Lock()
if !m.locked {
m.mu.Unlock()
panic("xsync: Unlock() of unlocked mutex")
}
m.locked = false
m.mu.Unlock()
m.cond.Signal()
}