Skip to content

Commit

Permalink
pybricks/tools/pb_type_awaitable: Share blocking assert.
Browse files Browse the repository at this point in the history
We want to call this from most class inits.
  • Loading branch information
laurensvalk committed May 26, 2023
1 parent 1f7e87f commit 7e3a73f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pybricks/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ void pb_module_tools_init(void);

bool pb_module_tools_run_loop_is_active(void);

void pb_module_tools_assert_blocking(void);

extern const mp_obj_type_t pb_type_StopWatch;

extern const mp_obj_type_t pb_type_Task;
Expand Down
6 changes: 6 additions & 0 deletions pybricks/tools/pb_module_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ bool pb_module_tools_run_loop_is_active() {
return run_loop_is_active;
}

void pb_module_tools_assert_blocking(void) {
if (run_loop_is_active) {
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("This can only be called before multitasking starts."));
}
}

// The awaitables for the wait() function have no object associated with
// it (unlike e.g. a motor), so we make a starting point here. These never
// have to cancel each other so shouldn't need to be in a list, but this lets
Expand Down
2 changes: 1 addition & 1 deletion pybricks/tools/pb_type_awaitable.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ mp_obj_t pb_type_awaitable_await_or_wait(

// Some operations are not allowed in async mode.
if (options & PB_TYPE_AWAITABLE_OPT_FORCE_BLOCK) {
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("This can only be called before multitasking starts."));
pb_module_tools_assert_blocking();
}

// First cancel linked awaitables if requested.
Expand Down

0 comments on commit 7e3a73f

Please sign in to comment.