kopia lustrzana https://github.com/micropython/micropython-lib
uasyncio.synchro: New submodule for synchronization primitives, Lock added.
rodzic
2829d4adc9
commit
3c805874d7
|
@ -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
|
Ładowanie…
Reference in New Issue