Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multitasking #166

Merged
merged 59 commits into from
May 26, 2023
Merged
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
541f349
pybricks.tools: Add _set_run_loop_active().
laurensvalk Jan 10, 2023
49f453e
pybricks.common.Motor: Return generator object in run loop.
dlech May 31, 2021
8981245
pybricks.common.Motor: Async run_until_stalled.
laurensvalk Jan 12, 2023
07f0f9c
pybricks.common.Motor: Use generator for blocking run_until_stalled.
laurensvalk Jan 12, 2023
02fe7fc
pybricks.common.Motor: Move pb_type_MotorWait_obj_t.
laurensvalk Jan 12, 2023
ec73534
pybricks.common.Motor: Recycle free generators.
laurensvalk Jan 12, 2023
235d901
pybricks.common.Motor: Cancel ongoing generators on new task.
laurensvalk Jan 12, 2023
b0c2305
pybricks.common.Motor: Cancel ongoing generators on new non-blocking …
laurensvalk Jan 12, 2023
451115e
pybricks.tools: Make wait awaitable in run loop.
laurensvalk Jan 13, 2023
e5ca655
pybricks.robotics.DriveBase: Support run loop.
laurensvalk Jan 13, 2023
db707ed
pybricks.tools: Make wait reusable.
laurensvalk Jan 13, 2023
cd73dd5
pybricks.tools: Generalize await with callback.
laurensvalk Jan 13, 2023
aa04f53
pybricks.common.Speaker: Make beep awaitable.
laurensvalk Jan 13, 2023
78b9561
bricks/_common/modules: Add multi-tasking module.
laurensvalk Jan 14, 2023
31145b4
pybricks.tools: Import task from tools.
laurensvalk Mar 9, 2023
b93ca4b
bricks/virtualhub: Add async demo and test.
laurensvalk Mar 9, 2023
6375661
pybricks.tools.task: implement all() and race()
dlech Mar 16, 2023
497312f
pybricks.tools: Generalize awaitable.
laurensvalk May 4, 2023
9f23f0a
pybricks.tools: Use new awaitable for wait.
laurensvalk May 4, 2023
4e5706b
pybricks.tools: Use new awaitable for Motor.
laurensvalk May 4, 2023
e287f71
pybricks.tools: Use new awaitable for DriveBase.
laurensvalk May 4, 2023
4e8505d
pybricks.tools: Use new awaitable for beep.
laurensvalk May 4, 2023
d674b8d
pybricks.tools: Restore cancel handling on exception.
laurensvalk May 4, 2023
97d0f9d
pybricks.common.speaker: Make play_notes awaitable.
laurensvalk May 4, 2023
ee83f9c
pybricks.tools: Delete type_wait.
laurensvalk May 4, 2023
5689ed0
pybricks.tools.task: Add empty module and disable Python module.
laurensvalk May 4, 2023
a2f401e
pybricks.tools.task: Implement in C.
laurensvalk May 4, 2023
7ae0dcf
pybricks/tools/pb_type_awaitable: Refactor.
laurensvalk May 5, 2023
e11f4fe
pybricks.tools: Task is a type.
laurensvalk May 6, 2023
c9055bb
pybricks.tools.Task: Rename to multitask.
laurensvalk May 7, 2023
e4d04c7
pbio/control: Set temporary limits via pbio.
laurensvalk May 9, 2023
126c172
pbio/drivebase: Use function call to check for active.
laurensvalk May 9, 2023
eacf883
pbio/control: Fix control type enum names.
laurensvalk May 10, 2023
11ce5b7
pbio/control: Generalize control type.
laurensvalk May 10, 2023
80b8f83
pbio/control: Add stalling as control objective.
laurensvalk May 10, 2023
4040fe1
pbio/servo: Implement run_until_stalled.
laurensvalk May 10, 2023
d53c300
pbio/control: Add stalling as completion option.
laurensvalk May 11, 2023
236e21b
pybricks.common.Motor: Update run_until_stall.
laurensvalk May 11, 2023
93b15c7
pybricks/tools/pb_type_awaitable: Generalize return value.
laurensvalk May 11, 2023
87d9ab0
pybricks/tools/pb_type_awaitable: Simplify blocking call.
laurensvalk May 11, 2023
f961d55
pybricks.common.Motor: Drop temporary stall states.
laurensvalk May 11, 2023
994e72a
pybricks/tools/pb_type_awaitable: Drop config struct.
laurensvalk May 11, 2023
692df86
pybricks/tools/pb_type_awaitable: Fix allocation of first awaitable.
laurensvalk May 11, 2023
b758ac1
pybricks/tools/pb_type_awaitable: Fix cancellation of linked objects.
laurensvalk May 11, 2023
cd7304a
pybricks/tools/pb_type_awaitable: Use mp_obj_list_t instead of our ow…
laurensvalk May 12, 2023
8893a42
pybricks/tools/pb_type_awaitable: Generalize object type.
laurensvalk May 15, 2023
9c910ea
pybricks/tools/pb_type_awaitable: Refactor options.
laurensvalk May 15, 2023
54db661
pybricks/tools/pb_type_awaitable: Add force block option.
laurensvalk May 15, 2023
c62b0a9
pybricks/tools/pb_type_awaitable: Use end time.
laurensvalk May 15, 2023
1f7e87f
pybricks.common.ColorLight: Callbacks return objects.
laurensvalk May 16, 2023
7e3a73f
pybricks/tools/pb_type_awaitable: Share blocking assert.
laurensvalk May 16, 2023
019ed1e
pybricks.common.Motor: Restore duty_limit behavior.
laurensvalk May 22, 2023
6f099d2
.vscode/tasks: Add build task for Move Hub.
laurensvalk May 25, 2023
65cbbec
pybricks/tools/pb_type_awaitable: Revert generalizing object type.
laurensvalk May 25, 2023
54408e9
pbio/uartdev: Fix port_data not being static.
laurensvalk May 25, 2023
e8651fa
pybricks/pupdevices: Make sensors async-ready.
laurensvalk May 25, 2023
3cf50b3
pybricks/tools/awaitable: Refactor cancel_all.
laurensvalk May 26, 2023
3afe954
pybricks/tools/awaitable: Add raise on busy option.
laurensvalk May 26, 2023
4b939b0
pybricks.common.Speaker: Fix awaitable.
laurensvalk May 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pybricks/tools/pb_type_awaitable: Simplify blocking call.
With the return value available separately, we can get it directly
instead of through iterating.

This reinstates behavior close to the way it was before introducing
async, in an effort not to reduce performance in the normal use case.
  • Loading branch information
laurensvalk committed May 26, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 87d9ab099136119ecedc7857bace1684c4af8e6d
25 changes: 8 additions & 17 deletions pybricks/tools/pb_type_awaitable.c
Original file line number Diff line number Diff line change
@@ -189,24 +189,15 @@ mp_obj_t pb_type_awaitable_await_or_block(mp_obj_t obj, const pb_type_awaitable_
return generator;
}

// Otherwise block and wait for it to complete.
nlr_buf_t nlr;
mp_obj_t ret = MP_OBJ_NULL;
if (nlr_push(&nlr) == 0) {
while (pb_type_awaitable_iternext(generator) == mp_const_none) {
mp_hal_delay_ms(5);
}
ret = MP_STATE_THREAD(stop_iteration_arg);
nlr_pop();
} else {
// Cancel the operation if an exception was raised.
pb_type_awaitable_obj_t *self = MP_OBJ_TO_PTR(generator);
if (self->cancel) {
self->cancel(self->obj);
}
nlr_jump(nlr.ret_val);
// Outside run loop, block until the operation is complete.
pb_type_awaitable_obj_t *awaitable = MP_OBJ_TO_PTR(generator);
while (!awaitable->test_completion(awaitable->obj, awaitable->start_time)) {
mp_hal_delay_ms(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 On ev3dev (at least with the current implementation) we need a delay here. But in general, MICROPY_EVENT_POLL_HOOK would be more appropriate here.

}
if (!awaitable->return_value) {
return mp_const_none;
}
return ret == MP_OBJ_NULL ? mp_const_none : ret;
return awaitable->return_value(awaitable->obj);
}

#endif // PYBRICKS_PY_TOOLS