aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/extmod/uasyncio/event.py
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
commit4fd287655a72b9aea14cdac715ad5b90ed082ed2 (patch)
tree65d393bc0e699dd12d05b29ba568e04cea666207 /circuitpython/extmod/uasyncio/event.py
parent0150f70ce9c39e9e6dd878766c0620c85e47bed0 (diff)
add circuitpython code
Diffstat (limited to 'circuitpython/extmod/uasyncio/event.py')
-rw-r--r--circuitpython/extmod/uasyncio/event.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/circuitpython/extmod/uasyncio/event.py b/circuitpython/extmod/uasyncio/event.py
new file mode 100644
index 0000000..a5b3bf9
--- /dev/null
+++ b/circuitpython/extmod/uasyncio/event.py
@@ -0,0 +1,33 @@
+# MicroPython uasyncio module
+# MIT license; Copyright (c) 2019-2020 Damien P. George
+
+from . import core
+
+# Event class for primitive events that can be waited on, set, and cleared
+class Event:
+ def __init__(self):
+ self.state = False # False=unset; True=set
+ self.waiting = core.TaskQueue() # Queue of Tasks waiting on completion of this event
+
+ def is_set(self):
+ return self.state
+
+ def set(self):
+ # Event becomes set, schedule any tasks waiting on it
+ # Note: This must not be called from anything except the thread running
+ # the asyncio loop (i.e. neither hard or soft IRQ, or a different thread).
+ while self.waiting.peek():
+ core._task_queue.push_head(self.waiting.pop_head())
+ self.state = True
+
+ def clear(self):
+ self.state = False
+
+ async def wait(self):
+ if not self.state:
+ # Event not set, put the calling task on the event's waiting queue
+ self.waiting.push_head(core.cur_task)
+ # Set calling task's data to the event's queue so it can be removed if needed
+ core.cur_task.data = self.waiting
+ yield
+ return True