uasyncio.synchro: New submodule for synchronization primitives, Lock added.

pull/227/head
Paul Sokolovsky 2017-11-03 00:39:35 +02:00
rodzic 2829d4adc9
commit 3c805874d7
1 zmienionych plików z 28 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,28 @@
from uasyncio import core
class Lock:
def __init__(self):
self.locked = False
self.wlist = []
def release(self):
assert self.locked
self.locked = False
if self.wlist:
#print(self.wlist)
coro = self.wlist.pop(0)
core.get_event_loop().call_soon(coro)
def acquire(self):
# As release() is not coro, assume we just released and going to acquire again
# so, yield first to let someone else to acquire it first
yield
#print("acquire:", self.locked)
while 1:
if not self.locked:
self.locked = True
return True
#print("putting", core.get_event_loop().cur_coro, "on waiting list")
self.wlist.append(core.get_event_loop().cur_coro)
yield False