Skip to content

Commit 6ca39aa

Browse files
committed
_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)
1 parent ed7c714 commit 6ca39aa

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

extmod/modasyncio.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,29 @@ STATIC mp_obj_t task_queue_make_new(const mp_obj_type_t *type, size_t n_args, si
6767
/******************************************************************************/
6868
// Ticks for task ordering in pairing heap
6969

70+
// CIRCUITPY-CHANGE: ticks() must match adafruit_ticks()
71+
#define _TICKS_PERIOD (1lu << 29)
72+
#define _TICKS_MAX (_TICKS_PERIOD - 1)
73+
#define _TICKS_HALFPERIOD (_TICKS_PERIOD >> 1)
74+
75+
#if !CIRCUITPY || (defined(__unix__) || defined(__APPLE__))
7076
STATIC mp_obj_t ticks(void) {
71-
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
77+
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & _TICKS_MAX);
7278
}
79+
#else
80+
// We don't share the implementation above because our supervisor_ticks_ms
81+
// starts the epoch about 65 seconds before the first overflow (see
82+
// shared-bindings/supervisor/__init__.c). We assume/require that
83+
// supervisor.ticks_ms is picked as the ticks implementation under
84+
// CircuitPython for the Python-coded bits of asyncio.
85+
#define ticks() supervisor_ticks_ms()
86+
#endif
7387

88+
// CIRCUITPY-CHANGE: ticks_diff must match adafruit_ticks
7489
STATIC mp_int_t ticks_diff(mp_obj_t t1_in, mp_obj_t t0_in) {
7590
mp_uint_t t0 = MP_OBJ_SMALL_INT_VALUE(t0_in);
7691
mp_uint_t t1 = MP_OBJ_SMALL_INT_VALUE(t1_in);
77-
mp_int_t diff = ((t1 - t0 + MICROPY_PY_TIME_TICKS_PERIOD / 2) & (MICROPY_PY_TIME_TICKS_PERIOD - 1))
78-
- MICROPY_PY_TIME_TICKS_PERIOD / 2;
92+
mp_int_t diff = ((t1 - t0 + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD;
7993
return diff;
8094
}
8195

0 commit comments

Comments
 (0)