1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-26 01:42:34 +00:00

loop: wait -> spawn, select -> wait

select was a bit confusing name, as we always block on only one
interface.
This commit is contained in:
Jan Pochyla 2018-04-13 14:57:04 +02:00
parent ebf4d2035e
commit e223173b4e
14 changed files with 45 additions and 45 deletions

View File

@ -46,7 +46,7 @@ async def request_pin(label=None, cancellable: bool=True) -> str:
while True: while True:
if __debug__: if __debug__:
result = await loop.wait(dialog, input_signal) result = await loop.spawn(dialog, input_signal)
if isinstance(result, str): if isinstance(result, str):
return result return result
else: else:

View File

@ -231,7 +231,7 @@ class Cmd:
async def read_cmd(iface: io.HID) -> Cmd: async def read_cmd(iface: io.HID) -> Cmd:
desc_init = frame_init() desc_init = frame_init()
desc_cont = frame_cont() desc_cont = frame_cont()
read = loop.select(iface.iface_num() | io.POLL_READ) read = loop.wait(iface.iface_num() | io.POLL_READ)
buf = await read buf = await read
@ -304,7 +304,7 @@ async def send_cmd(cmd: Cmd, iface: io.HID) -> None:
if offset < datalen: if offset < datalen:
frm = overlay_struct(buf, cont_desc) frm = overlay_struct(buf, cont_desc)
write = loop.select(iface.iface_num() | io.POLL_WRITE) write = loop.wait(iface.iface_num() | io.POLL_WRITE)
while offset < datalen: while offset < datalen:
frm.seq = seq frm.seq = seq
offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen) offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen)

View File

@ -4,7 +4,7 @@ the form of python coroutines (either plain generators or `async` functions) are
stepped through until completion, and can get asynchronously blocked by stepped through until completion, and can get asynchronously blocked by
`yield`ing or `await`ing a syscall. `yield`ing or `await`ing a syscall.
See `schedule`, `run`, and syscalls `sleep`, `select`, `signal` and `wait`. See `schedule`, `run`, and syscalls `sleep`, `wait`, `signal` and `spawn`.
''' '''
import utime import utime
@ -145,7 +145,7 @@ class sleep(Syscall):
schedule(task, deadline, deadline) schedule(task, deadline, deadline)
class select(Syscall): class wait(Syscall):
''' '''
Pause current task, and resume only after a message on `msg_iface` is Pause current task, and resume only after a message on `msg_iface` is
received. Messages are received either from an USB interface, or the received. Messages are received either from an USB interface, or the
@ -153,8 +153,8 @@ class select(Syscall):
Example: Example:
>>> hid_report, = await loop.select(0xABCD) # await USB HID report >>> hid_report, = await loop.wait(0xABCD) # await USB HID report
>>> event, x, y = await loop.select(io.TOUCH) # await touch event >>> event, x, y = await loop.wait(io.TOUCH) # await touch event
''' '''
def __init__(self, msg_iface): def __init__(self, msg_iface):
@ -209,11 +209,11 @@ class signal(Syscall):
raise raise
class wait(Syscall): class spawn(Syscall):
''' '''
Execute one or more children tasks and wait until one or more of them exit. Execute one or more children tasks and wait until one or more of them exit.
Return value of `wait` is the return value of task that triggered the Return value of `spawn` is the return value of task that triggered the
completion. By default, `wait` returns after the first child completes, and completion. By default, `spawn` returns after the first child completes, and
other running children are killed (by cancelling any pending schedules and other running children are killed (by cancelling any pending schedules and
calling `close()`). calling `close()`).
@ -223,15 +223,15 @@ class wait(Syscall):
>>> # async def animate_logo(): ... >>> # async def animate_logo(): ...
>>> touch_task = wait_for_touch() >>> touch_task = wait_for_touch()
>>> animation_task = animate_logo() >>> animation_task = animate_logo()
>>> waiter = loop.wait(touch_task, animation_task) >>> waiter = loop.spawn(touch_task, animation_task)
>>> result = await waiter >>> result = await waiter
>>> if animation_task in waiter.finished: >>> if animation_task in waiter.finished:
>>> print('animation task returned', result) >>> print('animation task returned', result)
>>> else: >>> else:
>>> print('touch task returned', result) >>> print('touch task returned', result)
Note: You should not directly `yield` a `wait` instance, see logic in Note: You should not directly `yield` a `spawn` instance, see logic in
`wait.__iter__` for explanation. Always use `await`. `spawn.__iter__` for explanation. Always use `await`.
''' '''
def __init__(self, *children, wait_for=1, exit_others=True): def __init__(self, *children, wait_for=1, exit_others=True):

View File

@ -85,7 +85,7 @@ async def alert(count: int=3):
async def click() -> tuple: async def click() -> tuple:
touch = loop.select(io.TOUCH) touch = loop.wait(io.TOUCH)
while True: while True:
ev, *pos = yield touch ev, *pos = yield touch
if ev == io.TOUCH_START: if ev == io.TOUCH_START:
@ -161,7 +161,7 @@ class Widget:
pass pass
def __iter__(self): def __iter__(self):
touch = loop.select(io.TOUCH) touch = loop.wait(io.TOUCH)
result = None result = None
while result is None: while result is None:
self.render() self.render()

View File

@ -45,9 +45,9 @@ class ConfirmDialog(Widget):
async def __iter__(self): async def __iter__(self):
if __debug__: if __debug__:
return await loop.wait(super().__iter__(), self.content, confirm_signal) return await loop.spawn(super().__iter__(), self.content, confirm_signal)
else: else:
return await loop.wait(super().__iter__(), self.content) return await loop.spawn(super().__iter__(), self.content)
_STARTED = const(-1) _STARTED = const(-1)
@ -92,7 +92,7 @@ class HoldToConfirmDialog(Widget):
content_loop = self.content content_loop = self.content
confirm_loop = super().__iter__() # default loop (render on touch) confirm_loop = super().__iter__() # default loop (render on touch)
if __debug__: if __debug__:
result = await loop.wait(content_loop, confirm_loop, confirm_signal) result = await loop.spawn(content_loop, confirm_loop, confirm_signal)
else: else:
result = await loop.wait(content_loop, confirm_loop) result = await loop.spawn(content_loop, confirm_loop)
return result return result

View File

@ -26,4 +26,4 @@ class EntrySelector(Widget):
return HOST return HOST
async def __iter__(self): async def __iter__(self):
return await loop.wait(super().__iter__(), self.content) return await loop.spawn(super().__iter__(), self.content)

View File

@ -149,15 +149,15 @@ class MnemonicKeyboard(ui.Widget):
async def __iter__(self): async def __iter__(self):
if __debug__: if __debug__:
return await loop.wait(self.edit_loop(), input_signal) return await loop.spawn(self.edit_loop(), input_signal)
else: else:
return await self.edit_loop() return await self.edit_loop()
async def edit_loop(self): async def edit_loop(self):
timeout = loop.sleep(1000 * 1000 * 1) timeout = loop.sleep(1000 * 1000 * 1)
touch = loop.select(io.TOUCH) touch = loop.wait(io.TOUCH)
wait_timeout = loop.wait(touch, timeout) wait_timeout = loop.spawn(touch, timeout)
wait_touch = loop.wait(touch) wait_touch = loop.spawn(touch)
content = None content = None
self.back.taint() self.back.taint()

View File

@ -170,7 +170,7 @@ class PassphraseKeyboard(ui.Widget):
while True: while True:
change = self.change_page() change = self.change_page()
enter = self.enter_text() enter = self.enter_text()
wait = loop.wait(change, enter) wait = loop.spawn(change, enter)
result = await wait result = await wait
if enter in wait.finished: if enter in wait.finished:
return result return result
@ -178,9 +178,9 @@ class PassphraseKeyboard(ui.Widget):
@ui.layout @ui.layout
async def enter_text(self): async def enter_text(self):
timeout = loop.sleep(1000 * 1000 * 1) timeout = loop.sleep(1000 * 1000 * 1)
touch = loop.select(io.TOUCH) touch = loop.wait(io.TOUCH)
wait_timeout = loop.wait(touch, timeout) wait_timeout = loop.spawn(touch, timeout)
wait_touch = loop.wait(touch) wait_touch = loop.spawn(touch)
content = None content = None
while content is None: while content is None:
self.render() self.render()

View File

@ -16,7 +16,7 @@ async def change_page(page, page_count):
d = SWIPE_VERTICAL d = SWIPE_VERTICAL
swipe = Swipe(directions=d) swipe = Swipe(directions=d)
if __debug__: if __debug__:
s = await loop.wait(swipe, swipe_signal) s = await loop.spawn(swipe, swipe_signal)
else: else:
s = await swipe s = await swipe
if s == SWIPE_UP: if s == SWIPE_UP:
@ -29,7 +29,7 @@ async def paginate(render_page, page_count, page=0, *args):
while True: while True:
changer = change_page(page, page_count) changer = change_page(page, page_count)
renderer = render_page(page, page_count, *args) renderer = render_page(page, page_count, *args)
waiter = loop.wait(changer, renderer) waiter = loop.spawn(changer, renderer)
result = await waiter result = await waiter
if changer in waiter.finished: if changer in waiter.finished:
page = result page = result
@ -82,4 +82,4 @@ class Scrollpage(ui.Widget):
render_scrollbar(self.page, self.page_count) render_scrollbar(self.page, self.page_count)
async def __iter__(self): async def __iter__(self):
return await loop.wait(super().__iter__(), self.content) return await loop.spawn(super().__iter__(), self.content)

View File

@ -38,10 +38,10 @@ class WordSelector(Widget):
async def __iter__(self): async def __iter__(self):
if __debug__: if __debug__:
result = await loop.wait(super().__iter__(), self.content, input_signal) result = await loop.spawn(super().__iter__(), self.content, input_signal)
if isinstance(result, str): if isinstance(result, str):
return int(result) return int(result)
else: else:
return result return result
else: else:
return await loop.wait(super().__iter__(), self.content) return await loop.spawn(super().__iter__(), self.content)

View File

@ -86,7 +86,7 @@ class Context:
while servicing the wire context. If a message comes until one of the while servicing the wire context. If a message comes until one of the
tasks ends, `UnexpectedMessageError` is raised. tasks ends, `UnexpectedMessageError` is raised.
''' '''
return loop.wait(self.read(()), *tasks) return loop.spawn(self.read(()), *tasks)
def getreader(self): def getreader(self):
return codec_v1.Reader(self.iface) return codec_v1.Reader(self.iface)

View File

@ -38,7 +38,7 @@ class Reader:
on this session. `self.type` and `self.size` are initialized and on this session. `self.type` and `self.size` are initialized and
available after `aopen()` returns. available after `aopen()` returns.
''' '''
read = loop.select(self.iface.iface_num() | io.POLL_READ) read = loop.wait(self.iface.iface_num() | io.POLL_READ)
while True: while True:
# wait for initial report # wait for initial report
report = await read report = await read
@ -64,7 +64,7 @@ class Reader:
if self.size < len(buf): if self.size < len(buf):
raise EOFError raise EOFError
read = loop.select(self.iface.iface_num() | io.POLL_READ) read = loop.wait(self.iface.iface_num() | io.POLL_READ)
nread = 0 nread = 0
while nread < len(buf): while nread < len(buf):
if self.ofs == len(self.data): if self.ofs == len(self.data):
@ -123,7 +123,7 @@ class Writer:
if self.size < len(buf): if self.size < len(buf):
raise EOFError raise EOFError
write = loop.select(self.iface.iface_num() | io.POLL_WRITE) write = loop.wait(self.iface.iface_num() | io.POLL_WRITE)
nwritten = 0 nwritten = 0
while nwritten < len(buf): while nwritten < len(buf):
# copy as much as possible to report buffer # copy as much as possible to report buffer
@ -152,7 +152,7 @@ class Writer:
self.data[self.ofs] = 0x00 self.data[self.ofs] = 0x00
self.ofs += 1 self.ofs += 1
write = loop.select(self.iface.iface_num() | io.POLL_WRITE) write = loop.wait(self.iface.iface_num() | io.POLL_WRITE)
while True: while True:
await write await write
n = self.iface.write(self.data) n = self.iface.write(self.data)

View File

@ -60,7 +60,7 @@ class Reader:
on this session. `self.type` and `self.size` are initialized and on this session. `self.type` and `self.size` are initialized and
available after `aopen()` returns. available after `aopen()` returns.
''' '''
read = loop.select(self.iface.iface_num() | io.POLL_READ) read = loop.wait(self.iface.iface_num() | io.POLL_READ)
while True: while True:
# wait for initial report # wait for initial report
report = await read report = await read
@ -84,7 +84,7 @@ class Reader:
if self.size < len(buf): if self.size < len(buf):
raise EOFError raise EOFError
read = loop.select(self.iface.iface_num() | io.POLL_READ) read = loop.wait(self.iface.iface_num() | io.POLL_READ)
nread = 0 nread = 0
while nread < len(buf): while nread < len(buf):
if self.ofs == len(self.data): if self.ofs == len(self.data):
@ -149,7 +149,7 @@ class Writer:
if self.size < len(buf): if self.size < len(buf):
raise EOFError raise EOFError
write = loop.select(self.iface.iface_num() | io.POLL_WRITE) write = loop.wait(self.iface.iface_num() | io.POLL_WRITE)
nwritten = 0 nwritten = 0
while nwritten < len(buf): while nwritten < len(buf):
# copy as much as possible to report buffer # copy as much as possible to report buffer
@ -178,7 +178,7 @@ class Writer:
self.data[self.ofs] = 0x00 self.data[self.ofs] = 0x00
self.ofs += 1 self.ofs += 1
await loop.select(self.iface.iface_num() | io.POLL_WRITE) await loop.wait(self.iface.iface_num() | io.POLL_WRITE)
self.iface.write(self.data) self.iface.write(self.data)
@ -198,8 +198,8 @@ class SesssionSupervisor:
After close request, the handling task is closed and session terminated. After close request, the handling task is closed and session terminated.
Both requests receive responses confirming the operation. Both requests receive responses confirming the operation.
''' '''
read = loop.select(self.iface.iface_num() | io.POLL_READ) read = loop.wait(self.iface.iface_num() | io.POLL_READ)
write = loop.select(self.iface.iface_num() | io.POLL_WRITE) write = loop.wait(self.iface.iface_num() | io.POLL_WRITE)
while True: while True:
report = await read report = await read
repmarker, repsid = ustruct.unpack(_REP, report) repmarker, repsid = ustruct.unpack(_REP, report)

View File

@ -126,7 +126,7 @@ async def swipe_move():
async def layout_game(): async def layout_game():
while True: while True:
s.render() s.render()
await loop.wait(swipe_move()) await swipe_move()
workflow.startdefault(layout_game) workflow.startdefault(layout_game)