-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWriteLock.py
More file actions
27 lines (27 loc) · 883 Bytes
/
Copy pathReadWriteLock.py
File metadata and controls
27 lines (27 loc) · 883 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
from threading import Condition, Lock
class ReadWriteLock:
# What It Does: Lock with non-exclusive read locks, exclusive write-locks
# How It Works: Condition Variable and Read Lock Counter
def __init__(self):
self.condReadable = Condition(Lock())
self.nReaders = 0
def AcqRead(self):
self.condReadable.acquire()
try:
self.nReaders +=1
finally:
self.condReadable.release()
def RelRead(self):
self.condReadable.acquire()
try:
self.nReaders -= 1
if self.nReaders == 0:
self.condReadable.notifyAll()
finally:
self.condReadable.release()
def AcqWrite(self):
self.condReadable.acquire()
while self.nReaders > 0:
self.condReadable.wait()
def RelWrite(self):
self.condReadable.release()