-
Notifications
You must be signed in to change notification settings - Fork 0
/
rw_lock.go
162 lines (124 loc) · 3 KB
/
rw_lock.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Read / Write lock for storage files
// Read sequence:
// 1 - StartRead()
// 2 - Perform read
// 3 - EndRead()
// Write sequence:
// 1 - RequestWrite()
// 2 - Copy resource to temp file
// 3 - Perform changes
// 4 - StartWrite()
// 5 - Copy temp files into original files
// 6 - EndWrite()
package main
import (
"sync"
)
// Read / Write lock
// Controls access to a resource
type ReadWriteLock struct {
lock *sync.Mutex
// Read
read_count int
read_wait_count int
read_wait_group *sync.WaitGroup
// Write
writing bool
write_mutex *sync.Mutex
write_wait bool
write_wait_group *sync.WaitGroup
}
// Creates a lock to manage a resource
func CreateReadWriteLock() *ReadWriteLock {
return &ReadWriteLock{
lock: &sync.Mutex{},
read_count: 0,
read_wait_count: 0,
read_wait_group: nil,
writing: false,
write_wait: false,
write_mutex: &sync.Mutex{},
write_wait_group: nil,
}
}
// Request a write operation
// This locks the resource from writing
// Only one write thread is allowed
func (lock *ReadWriteLock) RequestWrite() {
lock.write_mutex.Lock()
}
// Starts a write operation
// Waits for pending read threads to finish
// Locks the resource so only the write thread can use it
func (lock *ReadWriteLock) StartWrite() {
lock.lock.Lock()
var waitGroup *sync.WaitGroup = nil
if lock.read_count > 0 {
lock.write_wait = true
if lock.write_wait_group == nil {
lock.write_wait_group = &sync.WaitGroup{}
lock.write_wait_group.Add(1)
}
waitGroup = lock.write_wait_group
} else {
lock.writing = true
}
lock.lock.Unlock()
if waitGroup != nil {
// Wait for read threads
waitGroup.Wait()
}
}
// Finish a write operation, unlocking the resource
func (lock *ReadWriteLock) EndWrite() {
lock.lock.Lock()
lock.writing = false
if lock.read_wait_count > 0 {
lock.read_count += lock.read_wait_count
lock.read_wait_count = 0
// Release read threads
if lock.read_wait_group != nil {
lock.read_wait_group.Done()
lock.read_wait_group = nil
}
}
lock.lock.Unlock()
// Unlock write mutex, so other write threads can continue
lock.write_mutex.Unlock()
}
// Starts a read operation
func (lock *ReadWriteLock) StartRead() {
lock.lock.Lock()
var waitGroup *sync.WaitGroup = nil
if lock.writing || lock.write_wait {
// We cannot read, we have to wait for a write thread
lock.read_wait_count++
if lock.read_wait_group == nil {
lock.read_wait_group = &sync.WaitGroup{}
lock.read_wait_group.Add(1)
}
waitGroup = lock.read_wait_group
} else {
lock.read_count++
}
lock.lock.Unlock()
if waitGroup != nil {
// Wait for the write threads to finish
waitGroup.Wait()
}
}
// Ends a read operation
func (lock *ReadWriteLock) EndRead() {
lock.lock.Lock()
lock.read_count--
if lock.write_wait && lock.read_count <= 0 {
lock.write_wait = false
lock.writing = true
if lock.write_wait_group != nil {
// Release write wait group
lock.write_wait_group.Done()
lock.write_wait_group = nil
}
}
lock.lock.Unlock()
}