Skip to content

Commit 11064fe

Browse files
committed
pybricks.tools: Use new awaitable for pbio tasks.
This new awaitable is introduced so we can eventually drop queued tasks and safely await protothreads. For now, just keep the pbio tasks compiling.
1 parent b7d7703 commit 11064fe

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

pybricks/tools/pb_module_tools.c

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -145,34 +145,20 @@ void pb_module_tools_pbio_task_do_blocking(pbio_task_t *task, mp_int_t timeout)
145145
}
146146
}
147147

148-
// The awaitables associated with pbio tasks can originate from different
149-
// objects. At the moment, they are only associated with Bluetooth tasks, and
150-
// they cannot run at the same time. So we keep a single list of awaitables
151-
// here instead of with each Bluetooth-related MicroPython object.
152-
MP_REGISTER_ROOT_POINTER(mp_obj_t pbio_task_awaitables);
153-
154-
static bool pb_module_tools_pbio_task_test_completion(mp_obj_t obj, uint32_t end_time) {
155-
pbio_task_t *task = MP_OBJ_TO_PTR(obj);
156-
157-
// Keep going if not done yet.
158-
if (task->status == PBIO_ERROR_AGAIN) {
159-
return false;
160-
}
161-
162-
// If done, make sure it was successful.
163-
pb_assert(task->status);
164-
return true;
148+
static pbio_error_t pb_module_tools_pbio_task_iterate_once(pbio_os_state_t *state, mp_obj_t parent_obj) {
149+
pbio_task_t *task = MP_OBJ_TO_PTR(parent_obj);
150+
return task->status;
165151
}
166152

167153
mp_obj_t pb_module_tools_pbio_task_wait_or_await(pbio_task_t *task) {
168-
return pb_type_awaitable_await_or_wait(
169-
MP_OBJ_FROM_PTR(task),
170-
MP_STATE_PORT(pbio_task_awaitables),
171-
pb_type_awaitable_end_time_none,
172-
pb_module_tools_pbio_task_test_completion,
173-
pb_type_awaitable_return_none,
174-
pb_type_awaitable_cancel_none,
175-
PB_TYPE_AWAITABLE_OPT_RAISE_ON_BUSY);
154+
pb_type_async_t config = {
155+
.parent_obj = MP_OBJ_FROM_PTR(task),
156+
.iter_once = pb_module_tools_pbio_task_iterate_once,
157+
};
158+
159+
// REVISIT: pbio tasks will be deprecated. Instead, protothreads can now
160+
// be safely awaited.
161+
return pb_type_async_wait_or_await(&config, NULL);
176162
}
177163

178164
/**
@@ -268,7 +254,6 @@ static MP_DEFINE_CONST_FUN_OBJ_KW(pb_module_tools_run_task_obj, 0, pb_module_too
268254
// Reset global awaitable state when user program starts.
269255
void pb_module_tools_init(void) {
270256
memset(waits, 0, sizeof(waits));
271-
MP_STATE_PORT(pbio_task_awaitables) = mp_obj_new_list(0, NULL);
272257
run_loop_is_active = false;
273258
}
274259

0 commit comments

Comments
 (0)