forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.go
85 lines (73 loc) · 2.02 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package nosync
// Mutex is a dummy which is non-blocking.
type Mutex struct {
locked bool
}
// Lock locks m. It is a run-time error if m is already locked.
func (m *Mutex) Lock() {
if m.locked {
panic("nosync: mutex is already locked")
}
m.locked = true
}
// Unlock unlocks m. It is a run-time error if m is not locked.
func (m *Mutex) Unlock() {
if !m.locked {
panic("nosync: unlock of unlocked mutex")
}
m.locked = false
}
// RWMutex is a dummy which is non-blocking.
type RWMutex struct {
writeLocked bool
readLockCounter int
}
// Lock locks m for writing. It is a run-time error if rw is already locked for reading or writing.
func (rw *RWMutex) Lock() {
if rw.readLockCounter != 0 || rw.writeLocked {
panic("nosync: mutex is already locked")
}
rw.writeLocked = true
}
// Unlock unlocks rw for writing. It is a run-time error if rw is not locked for writing.
func (rw *RWMutex) Unlock() {
if !rw.writeLocked {
panic("nosync: unlock of unlocked mutex")
}
rw.writeLocked = false
}
// RLock locks m for reading. It is a run-time error if rw is already locked for reading or writing.
func (rw *RWMutex) RLock() {
if rw.writeLocked {
panic("nosync: mutex is already locked")
}
rw.readLockCounter++
}
// RUnlock undoes a single RLock call; it does not affect other simultaneous readers. It is a run-time error if rw is not locked for reading.
func (rw *RWMutex) RUnlock() {
if rw.readLockCounter == 0 {
panic("nosync: unlock of unlocked mutex")
}
rw.readLockCounter--
}
// WaitGroup is a dummy which is non-blocking.
type WaitGroup struct {
counter int
}
// Add adds delta, which may be negative, to the WaitGroup If the counter goes negative, Add panics.
func (wg *WaitGroup) Add(delta int) {
wg.counter += delta
if wg.counter < 0 {
panic("sync: negative WaitGroup counter")
}
}
// Done decrements the WaitGroup counter.
func (wg *WaitGroup) Done() {
wg.Add(-1)
}
// Wait panics if the WaitGroup counter is not zero.
func (wg *WaitGroup) Wait() {
if wg.counter != 0 {
panic("sync: WaitGroup counter not zero")
}
}