-
Notifications
You must be signed in to change notification settings - Fork 240
/
sync.py
245 lines (183 loc) · 6.1 KB
/
sync.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
# curio/sync.py
#
# Implementation of common task synchronization primitives such as
# events, locks, semaphores, and condition variables. These primitives
# are only safe to use within Curio--they are not thread safe
# unless otherwise indicated.
#
# The general implementation strategy is based on task scheduling.
# For example, if a task needs to wait on a lock, it goes to sleep on
# a queue. When a task releases a lock, it wakes a sleeping task.
# Task scheduling is provided by the SchedFIFO and SchedBarrier classes in sched.py
__all__ = ['Event', 'UniversalEvent', 'Lock', 'RLock', 'Semaphore', 'Condition' ]
# -- Standard library
import threading
# -- Curio
from .sched import SchedFIFO, SchedBarrier
from . import workers
from .task import current_task
from .meta import awaitable, iscoroutinefunction
from . import thread
class Event(object):
def __init__(self):
self._set = False
self._waiting = SchedBarrier()
def __repr__(self):
res = super().__repr__()
extra = 'set' if self._set else 'unset'
return f'<{res[1:-1]} [{extra},waiters:{len(self._waiting)}]>'
def is_set(self):
return self._set
def clear(self):
self._set = False
async def wait(self):
if self._set:
return
await self._waiting.suspend('EVENT_WAIT')
async def set(self):
self._set = True
await self._waiting.wake()
class UniversalEvent(object):
'''
An event that's safe to use from Curio and threads.
'''
def __init__(self):
self._evt = threading.Event()
def __repr__(self):
res = super().__repr__()
extra = 'set' if self.is_set() else 'unset'
return f'<{res[1:-1]} [{extra}]>'
def is_set(self):
return self._evt.is_set()
def clear(self):
self._evt.clear()
def wait(self):
self._evt.wait()
@awaitable(wait)
async def wait(self):
await workers.block_in_thread(self._evt.wait)
def set(self):
self._evt.set()
@awaitable(set)
async def set(self):
self._evt.set()
# Base class for all synchronization primitives that operate as context managers.
class _LockBase(object):
async def __aenter__(self):
await self.acquire()
return self
async def __aexit__(self, exc_type, exc, tb):
await self.release()
def __enter__(self):
return thread.AWAIT(self.__aenter__())
def __exit__(self, *args):
return thread.AWAIT(self.__aexit__(*args))
class Lock(_LockBase):
def __init__(self):
self._acquired = False
self._waiting = SchedFIFO()
def __repr__(self):
res = super().__repr__()
extra = 'locked' if self.locked() else 'unlocked'
return f'<{res[1:-1]} [{extra},waiters:{len(self._waiting)}]>'
async def acquire(self):
if self._acquired:
await self._waiting.suspend('LOCK_ACQUIRE')
self._acquired = True
return True
async def release(self):
assert self._acquired, 'Lock not acquired'
if self._waiting:
await self._waiting.wake()
else:
self._acquired = False
def locked(self):
return self._acquired
class RLock(_LockBase):
def __init__(self):
self._lock = Lock()
self._owner = None
self._count = 0
def __repr__(self):
res = super().__repr__()
extra = 'locked' if self.locked() else 'unlocked'
return f'<{res[1:-1]} [{extra},recursion:{self._count}]>'
async def acquire(self):
me = await current_task()
if self._owner is not me:
await self._lock.acquire()
self._owner = me
self._count += 1
return True
async def release(self):
if not self.locked():
raise RuntimeError('RLock is not locked')
if not await current_task() is self._owner:
raise RuntimeError('RLock can only be released by the owner')
self._count -= 1
if self._count == 0:
await self._lock.release()
self._owner = None
def locked(self):
return self._count > 0
class Semaphore(_LockBase):
def __init__(self, value=1):
self._value = value
self._waiting = SchedFIFO()
def __repr__(self):
res = super().__repr__()
extra = 'locked' if self.locked() else 'unlocked'
return f'<{res[1:-1]} [{extra},value:{self._value},waiters:{len(self._waiting)}]>'
@property
def value(self):
return self._value
async def acquire(self):
if self._value <= 0:
await self._waiting.suspend('SEMA_ACQUIRE')
else:
self._value -= 1
return True
async def release(self):
if self._waiting:
await self._waiting.wake()
else:
self._value += 1
def locked(self):
return self._value == 0
class Condition(_LockBase):
def __init__(self, lock=None):
if lock is None:
self._lock = Lock()
else:
self._lock = lock
self._waiting = SchedFIFO()
def __repr__(self):
res = super().__repr__()
extra = 'locked' if self.locked() else 'unlocked'
return f'<{res[1:-1]} [{extra},waiters:{len(self._waiting)}]>'
def locked(self):
return self._lock.locked()
async def acquire(self):
await self._lock.acquire()
async def release(self):
await self._lock.release()
async def wait(self):
if not self.locked():
raise RuntimeError("Can't wait on unacquired lock")
await self.release()
try:
await self._waiting.suspend('COND_WAIT')
finally:
await self.acquire()
async def wait_for(self, predicate):
while True:
result = predicate()
if result:
return result
await self.wait()
async def notify(self, n=1):
if not self.locked():
raise RuntimeError("Can't notify on unacquired lock")
await self._waiting.wake(n)
async def notify_all(self):
await self.notify(len(self._waiting))