diff --git a/uasyncio.synchro/uasyncio/synchro.py b/uasyncio.synchro/uasyncio/synchro.py new file mode 100644 index 00000000..dae4c061 --- /dev/null +++ b/uasyncio.synchro/uasyncio/synchro.py @@ -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