8000 extmod: Switch to use new event functions. · micropython/micropython@df3948d · GitHub
[go: up one dir, main page]

Skip to content

Commit df3948d

Browse files
projectgusdpgeorge
authored andcommitted
extmod: Switch to use new event functions.
See previous commit for details of these functions. As of this commit, these still call the old hook macros on all ports. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent f5be012 commit df3948d

File tree

7 files changed

+29
-27
lines changed

7 files changed

+29
-27
lines changed

extmod/btstack/modbluetooth_btstack.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ STATIC void set_random_address(void) {
552552
volatile bool ready = false;
553553
btstack_crypto_random_generate(&sm_crypto_random_request, static_addr, 6, &btstack_static_address_ready, (void *)&ready);
554554
while (!ready) {
555-
MICROPY_EVENT_POLL_HOOK
555+
mp_event_wait_indefinite();
556556
}
557557

558558
#endif // MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS
@@ -574,7 +574,7 @@ STATIC void set_random_address(void) {
574574
break;
575575
}
576576

577-
MICROPY_EVENT_POLL_HOOK
577+
mp_event_wait_indefinite();
578578
}
579579
DEBUG_printf("set_random_address: Address loaded by controller\n");
580580
}
@@ -654,7 +654,7 @@ int mp_bluetooth_init(void) {
654654
// Either the HCI event will set state to ACTIVE, or the timeout will set it to TIMEOUT.
655655
mp_bluetooth_btstack_port_start();
656656
while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING) {
657-
MICROPY_EVENT_POLL_HOOK
657+
mp_event_wait_indefinite();
658658
}
659659
btstack_run_loop_remove_timer(&btstack_init_deinit_timeout);
660660

@@ -727,7 +727,7 @@ void mp_bluetooth_deinit(void) {
727727
// either timeout or clean shutdown.
728728
mp_bluetooth_btstack_port_deinit();
729729
while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
730-
MICROPY_EVENT_POLL_HOOK
730+
mp_event_wait_indefinite();
731731
}
732732
btstack_run_loop_remove_timer(&btstack_init_deinit_timeout);
733733

extmod/machine_i2c.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,7 @@ STATIC mp_obj_t machine_i2c_scan(mp_obj_t self_in) {
331331
// This scan loop may run for some time, so process any pending events/exceptions,
332332
// or allow the port to run any necessary background tasks. But do it as fast as
333333
// possible, in particular we are not waiting on any events.
334-
#if defined(MICROPY_EVENT_POLL_HOOK_FAST)
335-
MICROPY_EVENT_POLL_HOOK_FAST;
336-
#elif defined(MICROPY_EVENT_POLL_HOOK)
337-
MICROPY_EVENT_POLL_HOOK
338-
#endif
334+
mp_event_handle_nowait();
339335
}
340336
return list;
341337
}

extmod/modlwip.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,7 @@ typedef struct _lwip_socket_obj_t {
318318
} lwip_socket_obj_t;
319319

320320
static inline void poll_sockets(void) {
321-
#ifdef MICROPY_EVENT_POLL_HOOK
322-
MICROPY_EVENT_POLL_HOOK;
323-
#else
324-
mp_hal_delay_ms(1);
325-
#endif
321+
mp_event_wait_ms(1);
326322
}
327323

328324
STATIC struct tcp_pcb *volatile *lwip_socket_incoming_array(lwip_socket_obj_t *socket) {

extmod/modselect.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ STATIC mp_uint_t poll_set_poll_once(poll_set_t *poll_set, size_t *rwx_num) {
306306

307307
STATIC mp_uint_t poll_set_poll_until_ready_or_timeout(poll_set_t *poll_set, size_t *rwx_num, mp_uint_t timeout) {
308308
mp_uint_t start_ticks = mp_hal_ticks_ms();
309+
bool has_timeout = timeout != (mp_uint_t)-1;
309310

310311
#if MICROPY_PY_SELECT_POSIX_OPTIMISATIONS
311312

@@ -350,23 +351,28 @@ STATIC mp_uint_t poll_set_poll_until_ready_or_timeout(poll_set_t *poll_set, size
350351
}
351352

352353
// Return if an object is ready, or if the timeout expired.
353-
if (n_ready > 0 || (timeout != (mp_uint_t)-1 && mp_hal_ticks_ms() - start_ticks >= timeout)) {
354+
if (n_ready > 0 || (has_timeout && mp_hal_ticks_ms() - start_ticks >= timeout)) {
354355
return n_ready;
355356
}
356357

357-
// This would be MICROPY_EVENT_POLL_HOOK but the call to poll() above already includes a delay.
358-
mp_handle_pending(true);
358+
// This would be mp_event_wait_ms() but the call to poll() above already includes a delay.
359+
mp_event_handle_nowait();
359360
}
360361

361362
#else
362363

363364
for (;;) {
364365
// poll the objects
365366
mp_uint_t n_ready = poll_set_poll_once(poll_set, rwx_num);
366-
if (n_ready > 0 || (timeout != (mp_uint_t)-1 && mp_hal_ticks_ms() - start_ticks >= timeout)) {
367+
uint32_t elapsed = mp_hal_ticks_ms() - start_ticks;
368+
if (n_ready > 0 || (has_timeout && elapsed >= timeout)) {
367369
return n_ready;
368370
}
369-
MICROPY_EVENT_POLL_HOOK
371+
if (has_timeout) {
372+
mp_event_wait_ms(timeout - elapsed);
373+
} else {
374+
mp_event_wait_indefinite();
375+
}
370376
}
371377

372378
#endif

extmod/modssl_mbedtls.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,7 @@ STATIC mp_obj_t ssl_socket_make_new(mp_obj_ssl_context_t *ssl_context, mp_obj_t
391391
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
392392
goto cleanup;
393393
}
394-
#ifdef MICROPY_EVENT_POLL_HOOK
395-
MICROPY_EVENT_POLL_HOOK
396-
#endif
394+
mp_event_wait_ms(1);
397395
}
398396
}
399397

extmod/network_cyw43.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,13 @@ STATIC mp_obj_t network_cyw43_scan(size_t n_args, const mp_obj_t *pos_args, mp_m
222222

223223
// Wait for scan to finish, with a 10s timeout
224224
uint32_t start = mp_hal_ticks_ms();
225-
while (cyw43_wifi_scan_active(self->cyw) && mp_hal_ticks_ms() - start < 10000) {
226-
MICROPY_EVENT_POLL_HOOK
225+
const uint32_t TIMEOUT = 10000;
226+
while (cyw43_wifi_scan_active(self->cyw)) {
227+
uint32_t elapsed = mp_hal_ticks_ms() - start;
228+
if (elapsed >= TIMEOUT) {
229+
break;
230+
}
231+
mp_event_wait_ms(TIMEOUT - elapsed);
227232
}
228233

229234
return res;

extmod/nimble/modbluetooth_nimble.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ void mp_bluetooth_nimble_port_shutdown(void) {
570570
ble_hs_stop(&ble_hs_shutdown_stop_listener, ble_hs_shutdown_stop_cb, NULL);
571571

572572
while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) {
573-
MICROPY_EVENT_POLL_HOOK
573+
mp_event_wait_indefinite();
574574
}
575575
}
576576

@@ -636,10 +636,11 @@ int mp_bluetooth_init(void) {
636636
// On non-ringbuffer builds (NimBLE on STM32/Unix) this will also poll the UART and run the event queue.
637637
mp_uint_t timeout_start_ticks_ms = mp_hal_ticks_ms();
638638
while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) {
639-
if (mp_hal_ticks_ms() - timeout_start_ticks_ms > NIMBLE_STARTUP_TIMEOUT) {
639+
uint32_t elapsed = mp_hal_ticks_ms() - timeout_start_ticks_ms;
640+
if (elapsed > NIMBLE_STARTUP_TIMEOUT) {
640641
break;
641642
}
642-
MICROPY_EVENT_POLL_HOOK
643+
mp_event_wait_ms(NIMBLE_STARTUP_TIMEOUT - elapsed);
643644
}
644645

645646
if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) {

0 commit comments

Comments
 (0)
0