-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_rwlock.py
356 lines (293 loc) · 9.32 KB
/
test_rwlock.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# Copyright (c) Meta Platforms, Inc. and affiliates.
# pyre-unsafe
import threading
import time
import unittest
from ft_utils.concurrent import AtomicFlag, AtomicInt64
from ft_utils.lock_test_utils import run_interrupt_handling
from ft_utils.synchronization import RWLock, RWReadContext, RWWriteContext
class TestRWLock(unittest.TestCase):
def execute(self, what):
def runnit():
for _ in range(5):
what()
threads = [threading.Thread(target=runnit) for _ in range(10)]
for t in threads:
t.start()
for t in threads:
t.join()
def test_simple_read_lock(self):
lock = RWLock()
lock.lock_read()
time.sleep(0.01)
lock.unlock_read()
lock.lock_read()
time.sleep(0.01)
lock.unlock_read()
def test_simple_write_lock(self):
lock = RWLock()
lock.lock_write()
time.sleep(0.01)
lock.unlock_write()
lock.lock_write()
time.sleep(0.01)
lock.unlock_write()
def test_lock_write(self):
lock = RWLock()
count = AtomicInt64()
ok = AtomicFlag(True)
ran = AtomicFlag(False)
def check_serialized():
try:
lock.lock_write()
count.incr()
if count > 1:
ok.set(False)
time.sleep(0.01)
count.decr()
finally:
lock.unlock_write()
ran.set(True)
self.execute(check_serialized)
self.assertTrue(ok)
self.assertTrue(ran)
def test_lock_read(self):
lock = RWLock()
count = AtomicInt64()
ok = AtomicFlag(False)
ran = AtomicFlag(False)
def check_notserialized():
try:
lock.lock_read()
count.incr()
if count > 1:
ok.set(True)
time.sleep(0.1)
count.decr()
finally:
lock.unlock_read()
ran.set(True)
self.execute(check_notserialized)
self.assertTrue(ok)
self.assertTrue(ran)
def test_lock_read_write(self):
lock = RWLock()
rcount = AtomicInt64()
wcount = AtomicInt64()
rok = AtomicFlag(False)
wokr = AtomicFlag(True)
wokw = AtomicFlag(True)
rran = AtomicFlag(False)
wran = AtomicFlag(False)
def check_both():
try:
lock.lock_read()
rcount.incr()
if rcount > 1:
rok.set(True)
if wcount > 0:
rok.set(True)
time.sleep(0.01)
rcount.decr()
rran.set(True)
finally:
lock.unlock_read()
time.sleep(0.01)
try:
lock.lock_write()
wcount.incr()
if wcount > 1:
wokw.set(False)
if rcount > 0:
wokr.set(False)
time.sleep(0.01)
wcount.decr()
wran.set(True)
finally:
lock.unlock_write()
self.execute(check_both)
self.assertTrue(rok)
self.assertTrue(wokr)
self.assertTrue(wokw)
self.assertTrue(rran)
self.assertTrue(wran)
def test_simple_context(self):
lock = RWLock()
with RWReadContext(lock):
pass
with RWWriteContext(lock):
pass
def test_lock_context(self):
lock = RWLock()
rcount = AtomicInt64()
wcount = AtomicInt64()
rok = AtomicFlag(False)
wokr = AtomicFlag(True)
wokw = AtomicFlag(True)
rran = AtomicFlag(False)
wran = AtomicFlag(False)
def check_both():
with RWReadContext(lock):
rcount.incr()
if rcount > 1:
rok.set(True)
if wcount > 0:
rok.set(False)
return
time.sleep(0.01)
rcount.decr()
rran.set(True)
time.sleep(0.01)
with RWWriteContext(lock):
wcount.incr()
if wcount > 1:
wokw.set(False)
return
if rcount > 0:
wokr.set(False)
return
time.sleep(0.01)
wcount.decr()
wran.set(True)
self.execute(check_both)
self.assertTrue(rok)
self.assertTrue(wokr)
self.assertTrue(wokw)
self.assertTrue(rran)
self.assertTrue(wran)
def test_readers(self):
lock = RWLock()
count = AtomicInt64()
done = AtomicFlag(False)
def read_wait():
with RWReadContext(lock):
count.incr()
while not done:
time.sleep(0.01)
threads = [threading.Thread(target=read_wait) for _ in range(10)]
for t in threads:
t.start()
while count < 10:
time.sleep(0.01)
readers = lock.readers()
writers_waiting = lock.writers_waiting()
writer_locked = lock.writer_locked()
done.set(True)
for t in threads:
t.join()
self.assertEqual(readers, 10)
self.assertEqual(writers_waiting, 0)
self.assertEqual(writer_locked, 0)
def test_writers_block_readers(self):
lock = RWLock()
done = AtomicFlag(False)
locked = AtomicFlag(False)
new_start = AtomicInt64()
started = AtomicFlag(False)
def read_wait1():
with RWReadContext(lock):
while not done:
time.sleep(0.01)
def read_wait2():
new_start.incr()
with RWReadContext(lock):
while not done:
time.sleep(0.01)
new_start.decr()
def write_wait():
started.set(True)
with RWWriteContext(lock):
pass
# Get 5 read locks
threads = [threading.Thread(target=read_wait1) for _ in range(5)]
for t in threads:
t.start()
while lock.readers() < 5:
time.sleep(0.01)
# Ask for a write lock even though read is held.
t = threading.Thread(target=write_wait)
t.start()
threads.append(t)
while not started:
time.sleep(0.01)
# Now start 5 new read threads which should not get the read lock as the write waiting blocks.
new_threads = [threading.Thread(target=read_wait2) for _ in range(5)]
for t in new_threads:
t.start()
threads.append(t)
while new_start < 5:
time.sleep(0.01)
readers = lock.readers()
locked = lock.writer_locked()
waiting = lock.writers_waiting()
done.set(True)
# Drain the second set of readers.
while new_start > 0:
time.sleep(0.01)
for t in threads:
t.join()
self.assertEqual(readers, 5)
self.assertEqual(waiting, 1)
self.assertFalse(locked)
self.assertEqual(lock.readers(), 0)
def test_writers_waiting(self):
lock = RWLock()
done = AtomicFlag(False)
started = AtomicFlag(False)
locked = AtomicFlag(False)
unlocked = AtomicFlag(True)
def read_wait():
with RWReadContext(lock):
while not done:
time.sleep(0.01)
def write_wait():
unlocked.set(not lock.writer_locked())
started.set(True)
with RWWriteContext(lock):
locked.set(lock.writer_locked())
threads = [threading.Thread(target=read_wait) for _ in range(10)]
for t in threads:
t.start()
while lock.readers() < 0:
time.sleep(0.01)
t = threading.Thread(target=write_wait)
t.start()
threads.append(t)
while lock.readers() < 10 or (not started):
time.sleep(0.01)
time.sleep(0.1)
writers_waiting = lock.writers_waiting()
done.set(True)
for t in threads:
t.join()
self.assertEqual(writers_waiting, 1)
self.assertTrue(locked)
self.assertTrue(unlocked)
class TestRWLockSignals(unittest.TestCase):
def test_interrupt_handling_write(self):
def acquire(lock):
lock.lock_write()
def release(lock):
lock.unlock_write()
run_interrupt_handling(self, RWLock(), acquire, release)
def test_interrupt_handling_read(self):
phase = AtomicInt64(0)
def acquire(lock):
if phase == 0:
lock.lock_write()
elif phase == 1:
lock.lock_read()
else:
raise RuntimeError("Acquire lock phase error")
phase.incr()
def release(lock):
if phase == 1:
lock.unlock_write()
if phase == 2:
lock.unlock_read()
else:
raise RuntimeError("Release lock phase error")
phase.incr()
run_interrupt_handling(self, RWLock(), acquire, release)
if __name__ == "__main__":
unittest.main()