-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.go
42 lines (36 loc) · 1010 Bytes
/
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
package gonyan
import (
"sync"
)
// mutex wraps a standard sync.Mutex basic functionalities together with a bool
// for easy deactivation and ease possible feature extensions.
type mutex struct {
mtx sync.Mutex
disabled bool
}
// Lock proceeds with internal mutex lock if the mutex has not been disabled.
func (m *mutex) Lock() {
if !m.disabled {
m.mtx.Lock()
}
}
// Unlock proceeds with internal mutex unlock if the mutex has not been
// disabled.
func (m *mutex) Unlock() {
if !m.disabled {
m.mtx.Unlock()
}
}
// Disable sets the mutex state to disabled thus preventing the Lock and Unlock
// functions to actually make a difference.
// Beware of disabling the mutex while in actual use, especially during a
// critical section execution because, since Unlock won't work it will cause
// deadlocks.
func (m *mutex) Disable() {
m.disabled = true
}
// Enable sets the mutex state to enabled reactivating the Lock and Unlock
// functions.
func (m *mutex) Enable() {
m.disabled = false
}