Skip to content

Commit e9d7c3e

Browse files
jpochylapfalcon
authored andcommitted
modutimeq: Add peektime() function (provisional).
Allows to get event time for a head item in the queue. The usecase if waiting for the next event *OR* I/O completion. I/O completion may happen before event triggers, and then wait should continue for the remaining event time (or I/O completion may schedule another earlier event altogether). The new function has a strongly provisional status - it may be converted to e.g. peek() function returning all of the event fields, not just time.
1 parent 6bfb344 commit e9d7c3e

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

extmod/modutimeq.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ STATIC mp_obj_t mod_utimeq_heappop(mp_obj_t heap_in, mp_obj_t list_ref) {
166166
}
167167
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_utimeq_heappop_obj, mod_utimeq_heappop);
168168

169+
STATIC mp_obj_t mod_utimeq_peektime(mp_obj_t heap_in) {
170+
mp_obj_utimeq_t *heap = get_heap(heap_in);
171+
if (heap->len == 0) {
172+
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
173+
}
174+
175+
struct qentry *item = &heap->items[0];
176+
return MP_OBJ_NEW_SMALL_INT(item->time);
177+
}
178+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_peektime_obj, mod_utimeq_peektime);
179+
169180
#if DEBUG
170181
STATIC mp_obj_t mod_utimeq_dump(mp_obj_t heap_in) {
171182
mp_obj_utimeq_t *heap = get_heap(heap_in);
@@ -190,6 +201,7 @@ STATIC mp_obj_t utimeq_unary_op(mp_uint_t op, mp_obj_t self_in) {
190201
STATIC const mp_rom_map_elem_t utimeq_locals_dict_table[] = {
191202
{ MP_ROM_QSTR(MP_QSTR_push), MP_ROM_PTR(&mod_utimeq_heappush_obj) },
192203
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&mod_utimeq_heappop_obj) },
204+
{ MP_ROM_QSTR(MP_QSTR_peektime), MP_ROM_PTR(&mod_utimeq_peektime_obj) },
193205
#if DEBUG
194206
{ MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&mod_utimeq_dump_obj) },
195207
#endif

0 commit comments

Comments
 (0)