-
Notifications
You must be signed in to change notification settings - Fork 0
/
rumutex.go
48 lines (41 loc) · 1.1 KB
/
rumutex.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
// Package rumutex implements an upgradable reader/writer mutual exclusion lock.
package rumutex
import (
"sync"
"sync/atomic"
)
// An RUMutex is an upgradable reader/writer mutual exclusion lock. The lock can be held by an
// arbitrary number of readers or a single writer.
type RUMutex struct {
rw sync.RWMutex
upgraded int32
}
// RLock locks ru for shared reading.
func (ru *RUMutex) RLock() {
ru.rw.RLock()
}
// Upgrade attempts to upgrade a read locked ru to exclusive writing.
// Returns false if the lock is already being upgraded.
func (ru *RUMutex) Upgrade() bool {
if !atomic.CompareAndSwapInt32(&ru.upgraded, 0, 1) {
return false
}
ru.rw.RUnlock()
ru.rw.Lock()
return true
}
// Downgrade downgrades a write locked ru back to shared reading.
func (ru *RUMutex) Downgrade() {
ru.rw.Unlock()
ru.rw.RLock()
atomic.StoreInt32(&ru.upgraded, 0) // don't allow new upgrades until we have the read lock
}
// RUnlock unlocks a read locked ru.
func (ru *RUMutex) RUnlock() {
ru.rw.RUnlock()
}
// Unlock unlocks a write locked ru.
func (ru *RUMutex) Unlock() {
ru.upgraded = 0
ru.rw.Unlock()
}