Skip to content

Commit 42f40b4

Browse files
committed
uasyncio: Handle colliding sleep_ms calls.
If two tasks try to start sleeping at the same time, the SingletonGenerator will be in use, thus we need to allocate a new temporary handler on the heap. As the singleton isn't that single any more, it gets renamed. Found by taskgroup test micropython#12.
1 parent 26d62c8 commit 42f40b4

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

extmod/uasyncio/core.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TimeoutError(Exception):
4040

4141

4242
# "Yield" once, then raise StopIteration
43-
class SingletonGenerator:
43+
class SleepHandler:
4444
def __init__(self):
4545
self.state = None
4646
self.exc = StopIteration()
@@ -59,9 +59,10 @@ def __next__(self):
5959

6060

6161
# Pause task execution for the given time (integer in milliseconds, uPy extension)
62-
# Use a SingletonGenerator to do it without allocating on the heap
63-
def sleep_ms(t, sgen=SingletonGenerator()):
64-
assert sgen.state is None
62+
# Try not to allocate a SleepHandler on the heap if possible
63+
def sleep_ms(t, sgen=SleepHandler()):
64+
if sgen.state is not None: # the static one is busy
65+
sgen = SleepHandler()
6566
sgen.state = ticks_add(ticks(), max(0, t))
6667
return sgen
6768

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy