Skip to content

Commit 0a62fa5

Browse files
committed
pybricks/tools/awaitable: Add raise on busy option.
Until we work out clever ways of cancellation or queueing, we can prevent certain resources from being used on more than one task at all.
1 parent e06101c commit 0a62fa5

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

pybricks/common/pb_type_speaker.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ STATIC mp_obj_t pb_type_Speaker_beep(size_t n_args, const mp_obj_t *pos_args, mp
172172
pb_type_Speaker_beep_test_completion,
173173
pb_type_awaitable_return_none,
174174
pb_type_Speaker_cancel,
175-
PB_TYPE_AWAITABLE_OPT_CANCEL_ALL | PB_TYPE_AWAITABLE_OPT_CANCEL_HARDWARE);
175+
PB_TYPE_AWAITABLE_OPT_RAISE_ON_BUSY);
176176
}
177177
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pb_type_Speaker_beep_obj, 1, pb_type_Speaker_beep);
178178

@@ -388,7 +388,7 @@ STATIC mp_obj_t pb_type_Speaker_play_notes(size_t n_args, const mp_obj_t *pos_ar
388388
pb_type_Speaker_notes_test_completion,
389389
pb_type_awaitable_return_none,
390390
pb_type_Speaker_cancel,
391-
PB_TYPE_AWAITABLE_OPT_CANCEL_ALL | PB_TYPE_AWAITABLE_OPT_CANCEL_HARDWARE);
391+
PB_TYPE_AWAITABLE_OPT_RAISE_ON_BUSY);
392392
}
393393
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pb_type_Speaker_play_notes_obj, 1, pb_type_Speaker_play_notes);
394394

pybricks/pupdevices/pb_module_pupdevices.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ mp_obj_t pb_pupdevices_set_data(pb_pupdevices_obj_base_t *sensor, uint8_t mode,
9696
pb_pup_device_test_completion,
9797
pb_type_awaitable_return_none,
9898
pb_type_awaitable_cancel_none,
99-
PB_TYPE_AWAITABLE_OPT_NONE); // choose opt raise on busy
99+
PB_TYPE_AWAITABLE_OPT_RAISE_ON_BUSY);
100100
}
101101

102102
void pb_pupdevices_init_class(pb_pupdevices_obj_base_t *self, mp_obj_t port_in, pbio_iodev_type_id_t valid_id) {

pybricks/tools/pb_type_awaitable.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,27 @@ void pb_type_awaitable_update_all(mp_obj_t awaitables_in, pb_type_awaitable_opt_
148148

149149
for (size_t i = 0; i < awaitables->len; i++) {
150150
pb_type_awaitable_obj_t *awaitable = MP_OBJ_TO_PTR(awaitables->items[i]);
151-
// Only cancel awaitables that are in use.
152-
if (awaitable->test_completion) {
153-
// Cancel hardware operation if requested and available.
154-
if (options & PB_TYPE_AWAITABLE_OPT_CANCEL_HARDWARE && awaitable->cancel) {
155-
awaitable->cancel(awaitable->obj);
156-
}
157-
// Set awaitable to done so it gets cancelled it gracefully on the
158-
// next iteration.
159-
if (options & PB_TYPE_AWAITABLE_OPT_CANCEL_ALL) {
160-
awaitable->test_completion = pb_type_awaitable_completed;
161-
}
151+
152+
// Skip awaitables that are not in use.
153+
if (!awaitable->test_completion) {
154+
continue;
155+
}
156+
157+
// Raise EBUSY if requested.
158+
if (options & PB_TYPE_AWAITABLE_OPT_RAISE_ON_BUSY) {
159+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("This resource cannot be used in two tasks at once."));
162160
}
161+
162+
// Cancel hardware operation if requested and available.
163+
if (options & PB_TYPE_AWAITABLE_OPT_CANCEL_HARDWARE && awaitable->cancel) {
164+
awaitable->cancel(awaitable->obj);
165+
}
166+
// Set awaitable to done so it gets cancelled it gracefully on the
167+
// next iteration.
168+
if (options & PB_TYPE_AWAITABLE_OPT_CANCEL_ALL) {
169+
awaitable->test_completion = pb_type_awaitable_completed;
170+
}
171+
163172
}
164173
}
165174

pybricks/tools/pb_type_awaitable.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ typedef enum _pb_type_awaitable_opt_t {
3434
* already cleaned up by lower level drivers (so not needed for motors).
3535
*/
3636
PB_TYPE_AWAITABLE_OPT_CANCEL_HARDWARE = 1 << 3,
37+
/**
38+
* Raises EBUSY if the resource is already in use. Used for resources that
39+
* do not support graceful cancellation.
40+
*/
41+
PB_TYPE_AWAITABLE_OPT_RAISE_ON_BUSY = 1 << 4,
3742
} pb_type_awaitable_opt_t;
3843

3944
/**

0 commit comments

Comments
 (0)