-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadsync.py
202 lines (158 loc) · 5.39 KB
/
threadsync.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
# coding: utf-8
import threading
import contextlib
# A private extension to threading.Event
class EventTAS(type(threading.Event())):
def __init__(self, *args, **kwargs):
super(EventTAS, self).__init__(*args, **kwargs)
self.mutex = threading.Lock()
def set(self, *args, **kwargs):
with self.mutex:
prevflag = self.is_set()
super(EventTAS, self).set(*args, **kwargs)
return prevflag
def clear(self, *args, **kwargs):
with self.mutex:
prevflag = self.is_set()
super(EventTAS, self).clear(*args, **kwargs)
return prevflag
# Still no RW Lock in python...
# FIXME: Make the lock recursive?
class RWLock(object):
"""A read-write lock with priority to writers. Usable with "with". The
default behavior is to acquire the read lock."""
class ThreadLockingInfos(threading.local):
def __init__(self):
super(RWLock.ThreadLockingInfos, self).__init__()
self.reading = False
self.writing = False
def __init__(self, mutex = None):
self.mutex = mutex if mutex is not None else threading.Lock()
self.readq = threading.Condition(self.mutex)
self.writeq = threading.Condition(self.mutex)
self.readercount = 0
self.writercount = 0
self.writing = False
# Thread-local storage
self.tls = self.ThreadLockingInfos()
# Must be called with self.mutex locked
def _acquire_read(self):
# Just wait for writers to wake us
while self.writercount > 0:
self.readq.wait()
self.readercount += 1
self.tls.reading = True
def acquire_read(self):
"""Acquire a reading lock."""
assert(not self.tls.reading)
with self.mutex:
self._acquire_read()
__enter__ = acquire_read
# Must be called with self.mutex locked
def _release_read(self):
self.tls.reading = False
self.readercount -= 1
# Last reader wake the writers
if self.readercount == 0:
self.writeq.notify()
def release_read(self):
"""Release a reading lock."""
assert(self.tls.reading)
with self.mutex:
self._release_read()
def __exit__(self, t, v, tb):
self.release_read()
# Must be called with self.mutex locked
def _acquire_write(self):
self.writercount += 1
while self.readercount > 0 or self.writing:
self.writeq.wait()
self.writing = True
self.tls.writing = True
def acquire_write(self):
"""Acquire a writing lock."""
assert(not self.tls.writing)
with self.mutex:
self._acquire_write()
# Must be called with self.mutex locked
def _release_write(self):
self.writercount -= 1
self.writing = False
self.tls.writing = False
if self.writercount > 0:
self.writeq.notify()
else:
self.readq.notify_all()
def release_write(self):
"""Release a writing lock."""
assert(self.tls.writing)
with self.mutex:
self._release_write()
def upgrade_write(self):
"""Change a reading lock to a writer lock."""
assert(self.tls.reading)
assert(not self.tls.writing)
with self.mutex:
self._release_read()
self._acquire_write()
def downgrade_write(self):
"""Change a writing lock to a reader lock."""
assert(self.tls.writing)
assert(not self.tls.reading)
with self.mutex:
self._release_write()
self._acquire_read()
@contextlib.contextmanager
def read(self):
"""Context manager to be used in a "with" statement.
Acquire a read lock for the duration of the execution of the block."""
self.acquire_read()
try:
yield self
finally:
self.release_read()
@contextlib.contextmanager
def write(self):
"""Context manager to be used in a "with" statement.
Acquire a write lock for the duration of the execution of the block.
Upgrade if the lock was held for reading."""
is_upgrade = self.tls.reading
if is_upgrade:
self.upgrade_write()
else:
self.acquire_write()
try:
yield self
finally:
if is_upgrade:
self.downgrade_write()
else:
self.release_write()
@contextlib.contextmanager
def downgrade(self):
"""Context manager to be used in a "with" statement.
Change a write lock to a read lock for the duration of the execution of
the block."""
self.downgrade_write()
try:
yield self
finally:
self.upgrade_write()
@contextlib.contextmanager
def unlock(self):
"""Context manager to be used in a "with" statement.
Release completely a lock for the duration of the execution of the
block. Reacquires it as before afterwards."""
was_reading = self.tls.reading
was_writing = self.tls.writing
if was_writing:
self.release_write()
if was_reading:
self.release_read()
try:
yield self
finally:
if was_reading:
self.acquire_read()
if was_writing:
self.acquiwre_write()