From 6ca39aa9eca58efd57b4351b5b3c37bff19a82a9 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 14 Nov 2023 21:47:47 -0600 Subject: [PATCH] _asyncio: fix definition of ticks to match adafruit_ticks Otherwise, the timing of tasks is inconsistent, because C code and userspace code are using incompatible epochs. For whatever reason, a bunch of asyncio tests would consistently fail in the Unix build locally; this change fixes it. I don't know why those same tests succeed in CI! Closes: #8500 (probably, didn't test) --- extmod/modasyncio.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/extmod/modasyncio.c b/extmod/modasyncio.c index 4a02ada261..4667e3de53 100644 --- a/extmod/modasyncio.c +++ b/extmod/modasyncio.c @@ -67,15 +67,29 @@ STATIC mp_obj_t task_queue_make_new(const mp_obj_type_t *type, size_t n_args, si /******************************************************************************/ // Ticks for task ordering in pairing heap -STATIC mp_obj_t ticks(void) { - return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_TIME_TICKS_PERIOD - 1)); -} +// CIRCUITPY-CHANGE: ticks() must match adafruit_ticks() +#define _TICKS_PERIOD (1lu << 29) +#define _TICKS_MAX (_TICKS_PERIOD - 1) +#define _TICKS_HALFPERIOD (_TICKS_PERIOD >> 1) +#if !CIRCUITPY || (defined(__unix__) || defined(__APPLE__)) +STATIC mp_obj_t ticks(void) { + return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & _TICKS_MAX); +} +#else +// We don't share the implementation above because our supervisor_ticks_ms +// starts the epoch about 65 seconds before the first overflow (see +// shared-bindings/supervisor/__init__.c). We assume/require that +// supervisor.ticks_ms is picked as the ticks implementation under +// CircuitPython for the Python-coded bits of asyncio. +#define ticks() supervisor_ticks_ms() +#endif + +// CIRCUITPY-CHANGE: ticks_diff must match adafruit_ticks STATIC mp_int_t ticks_diff(mp_obj_t t1_in, mp_obj_t t0_in) { mp_uint_t t0 = MP_OBJ_SMALL_INT_VALUE(t0_in); mp_uint_t t1 = MP_OBJ_SMALL_INT_VALUE(t1_in); - mp_int_t diff = ((t1 - t0 + MICROPY_PY_TIME_TICKS_PERIOD / 2) & (MICROPY_PY_TIME_TICKS_PERIOD - 1)) - - MICROPY_PY_TIME_TICKS_PERIOD / 2; + mp_int_t diff = ((t1 - t0 + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD; return diff; }