1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-15 12:08:59 +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:
if __debug__:
result = await loop.wait(dialog, input_signal)
result = await loop.spawn(dialog, input_signal)
if isinstance(result, str):
return result
else:

View File

@ -231,7 +231,7 @@ class Cmd:
async def read_cmd(iface: io.HID) -> Cmd:
desc_init = frame_init()
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
@ -304,7 +304,7 @@ async def send_cmd(cmd: Cmd, iface: io.HID) -> None:
if offset < datalen:
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:
frm.seq = seq
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
`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
@ -145,7 +145,7 @@ class sleep(Syscall):
schedule(task, deadline, deadline)
class select(Syscall):
class wait(Syscall):
'''
Pause current task, and resume only after a message on `msg_iface` is
received. Messages are received either from an USB interface, or the
@ -153,8 +153,8 @@ class select(Syscall):
Example:
>>> hid_report, = await loop.select(0xABCD) # await USB HID report
>>> event, x, y = await loop.select(io.TOUCH) # await touch event
>>> hid_report, = await loop.wait(0xABCD) # await USB HID report
>>> event, x, y = await loop.wait(io.TOUCH) # await touch event
'''
def __init__(self, msg_iface):
@ -209,11 +209,11 @@ class signal(Syscall):
raise
class wait(Syscall):
class spawn(Syscall):
'''
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
completion. By default, `wait` returns after the first child completes, and
Return value of `spawn` is the return value of task that triggered the
completion. By default, `spawn` returns after the first child completes, and
other running children are killed (by cancelling any pending schedules and
calling `close()`).
@ -223,15 +223,15 @@ class wait(Syscall):
>>> # async def animate_logo(): ...
>>> touch_task = wait_for_touch()
>>> animation_task = animate_logo()
>>> waiter = loop.wait(touch_task, animation_task)
>>> waiter = loop.spawn(touch_task, animation_task)
>>> result = await waiter
>>> if animation_task in waiter.finished:
>>> print('animation task returned', result)
>>> else:
>>> print('touch task returned', result)
Note: You should not directly `yield` a `wait` instance, see logic in
`wait.__iter__` for explanation. Always use `await`.
Note: You should not directly `yield` a `spawn` instance, see logic in
`spawn.__iter__` for explanation. Always use `await`.
'''
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:
touch = loop.select(io.TOUCH)
touch = loop.wait(io.TOUCH)
while True:
ev, *pos = yield touch
if ev == io.TOUCH_START:
@ -161,7 +161,7 @@ class Widget:
pass
def __iter__(self):
touch = loop.select(io.TOUCH)
touch = loop.wait(io.TOUCH)
result = None
while result is None:
self.render()

View File

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

View File

@ -26,4 +26,4 @@ class EntrySelector(Widget):
return HOST
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):
if __debug__:
return await loop.wait(self.edit_loop(), input_signal)
return await loop.spawn(self.edit_loop(), input_signal)
else:
return await self.edit_loop()
async def edit_loop(self):
timeout = loop.sleep(1000 * 1000 * 1)
touch = loop.select(io.TOUCH)
wait_timeout = loop.wait(touch, timeout)
wait_touch = loop.wait(touch)
touch = loop.wait(io.TOUCH)
wait_timeout = loop.spawn(touch, timeout)
wait_touch = loop.spawn(touch)
content = None
self.back.taint()

View File

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

View File

@ -16,7 +16,7 @@ async def change_page(page, page_count):
d = SWIPE_VERTICAL
swipe = Swipe(directions=d)
if __debug__:
s = await loop.wait(swipe, swipe_signal)
s = await loop.spawn(swipe, swipe_signal)
else:
s = await swipe
if s == SWIPE_UP:
@ -29,7 +29,7 @@ async def paginate(render_page, page_count, page=0, *args):
while True:
changer = change_page(page, page_count)
renderer = render_page(page, page_count, *args)
waiter = loop.wait(changer, renderer)
waiter = loop.spawn(changer, renderer)
result = await waiter
if changer in waiter.finished:
page = result
@ -82,4 +82,4 @@ class Scrollpage(ui.Widget):
render_scrollbar(self.page, self.page_count)
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):
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):
return int(result)
else:
return result
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
tasks ends, `UnexpectedMessageError` is raised.
'''
return loop.wait(self.read(()), *tasks)
return loop.spawn(self.read(()), *tasks)
def getreader(self):
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
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:
# wait for initial report
report = await read
@ -64,7 +64,7 @@ class Reader:
if self.size < len(buf):
raise EOFError
read = loop.select(self.iface.iface_num() | io.POLL_READ)
read = loop.wait(self.iface.iface_num() | io.POLL_READ)
nread = 0
while nread < len(buf):
if self.ofs == len(self.data):
@ -123,7 +123,7 @@ class Writer:
if self.size < len(buf):
raise EOFError
write = loop.select(self.iface.iface_num() | io.POLL_WRITE)
write = loop.wait(self.iface.iface_num() | io.POLL_WRITE)
nwritten = 0
while nwritten < len(buf):
# copy as much as possible to report buffer
@ -152,7 +152,7 @@ class Writer:
self.data[self.ofs] = 0x00
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:
await write
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
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:
# wait for initial report
report = await read
@ -84,7 +84,7 @@ class Reader:
if self.size < len(buf):
raise EOFError
read = loop.select(self.iface.iface_num() | io.POLL_READ)
read = loop.wait(self.iface.iface_num() | io.POLL_READ)
nread = 0
while nread < len(buf):
if self.ofs == len(self.data):
@ -149,7 +149,7 @@ class Writer:
if self.size < len(buf):
raise EOFError
write = loop.select(self.iface.iface_num() | io.POLL_WRITE)
write = loop.wait(self.iface.iface_num() | io.POLL_WRITE)
nwritten = 0
while nwritten < len(buf):
# copy as much as possible to report buffer
@ -178,7 +178,7 @@ class Writer:
self.data[self.ofs] = 0x00
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)
@ -198,8 +198,8 @@ class SesssionSupervisor:
After close request, the handling task is closed and session terminated.
Both requests receive responses confirming the operation.
'''
read = loop.select(self.iface.iface_num() | io.POLL_READ)
write = loop.select(self.iface.iface_num() | io.POLL_WRITE)
read = loop.wait(self.iface.iface_num() | io.POLL_READ)
write = loop.wait(self.iface.iface_num() | io.POLL_WRITE)
while True:
report = await read
repmarker, repsid = ustruct.unpack(_REP, report)

View File

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