-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex.go
More file actions
52 lines (44 loc) · 988 Bytes
/
Copy pathmutex.go
File metadata and controls
52 lines (44 loc) · 988 Bytes
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
package mutex
import (
"sync"
"sync/atomic"
)
type Mutex struct {
sync.Mutex
before int32
after int32
}
func NewMutex() *Mutex {
return new(Mutex)
}
// Lock locks m.
// If the lock is already in use, the calling goroutine
// blocks until the mutex is available.
func (m *Mutex) lock() {
atomic.StoreInt32(&m.before, 1)
m.Mutex.Lock()
atomic.StoreInt32(&m.after, 1)
}
// Same as lock()
func (m *Mutex) Lock() {
m.lock()
}
// Unlock unlocks m.
// It is a run-time error if m is not locked on entry to Unlock.
//
// A locked Mutex is not associated with a particular goroutine.
// It is allowed for one goroutine to lock a Mutex and then
// arrange for another goroutine to unlock it.
func (m *Mutex) unlock() {
atomic.StoreInt32(&m.after, 0)
m.Mutex.Unlock()
atomic.StoreInt32(&m.before, 0)
}
// Same as unlock()
func (m *Mutex) Unlock() {
m.unlock()
}
func (m *Mutex) IsLocked() bool {
return atomic.LoadInt32(&m.before) == 1 &&
atomic.LoadInt32(&m.after) == 1
}