Skip to content

Commit 3c80587

Browse files
committed
uasyncio.synchro: New submodule for synchronization primitives, Lock added.
1 parent 2829d4a commit 3c80587

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

uasyncio.synchro/uasyncio/synchro.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from uasyncio import core
2+
3+
class Lock:
4+
5+
def __init__(self):
6+
self.locked = False
7+
self.wlist = []
8+
9+
def release(self):
10+
assert self.locked
11+
self.locked = False
12+
if self.wlist:
13+
#print(self.wlist)
14+
coro = self.wlist.pop(0)
15+
core.get_event_loop().call_soon(coro)
16+
17+
def acquire(self):
18+
# As release() is not coro, assume we just released and going to acquire again
19+
# so, yield first to let someone else to acquire it first
20+
yield
21+
#print("acquire:", self.locked)
22+
while 1:
23+
if not self.locked:
24+
self.locked = True
25+
return True
26+
#print("putting", core.get_event_loop().cur_coro, "on waiting list")
27+
self.wlist.append(core.get_event_loop().cur_coro)
28+
yield False

0 commit comments

Comments
 (0)