Commit edc0dcb5 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

extmod/moduselect: Refactor towards introduction of poll.ipoll().

This follows previous refactor made to unix/moduselect.
parent b0828694
...@@ -182,6 +182,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select); ...@@ -182,6 +182,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select);
typedef struct _mp_obj_poll_t { typedef struct _mp_obj_poll_t {
mp_obj_base_t base; mp_obj_base_t base;
mp_map_t poll_map; mp_map_t poll_map;
int flags;
} mp_obj_poll_t; } mp_obj_poll_t;
/// \method register(obj[, eventmask]) /// \method register(obj[, eventmask])
...@@ -219,9 +220,7 @@ STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmas ...@@ -219,9 +220,7 @@ STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmas
} }
MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify); MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify);
/// \method poll([timeout]) STATIC mp_uint_t poll_poll_internal(uint n_args, const mp_obj_t *args) {
/// Timeout is in milliseconds.
STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
mp_obj_poll_t *self = args[0]; mp_obj_poll_t *self = args[0];
// work out timeout (its given already in ms) // work out timeout (its given already in ms)
...@@ -239,12 +238,26 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) { ...@@ -239,12 +238,26 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
} }
} }
self->flags = flags;
mp_uint_t start_tick = mp_hal_ticks_ms(); mp_uint_t start_tick = mp_hal_ticks_ms();
mp_uint_t n_ready;
for (;;) { for (;;) {
// poll the objects // poll the objects
mp_uint_t n_ready = poll_map_poll(&self->poll_map, NULL); n_ready = poll_map_poll(&self->poll_map, NULL);
if (n_ready > 0 || (timeout != -1 && mp_hal_ticks_ms() - start_tick >= timeout)) { if (n_ready > 0 || (timeout != -1 && mp_hal_ticks_ms() - start_tick >= timeout)) {
break;
}
MICROPY_EVENT_POLL_HOOK
}
return n_ready;
}
STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
mp_obj_poll_t *self = args[0];
mp_uint_t n_ready = poll_poll_internal(n_args, args);
// one or more objects are ready, or we had a timeout // one or more objects are ready, or we had a timeout
mp_obj_list_t *ret_list = mp_obj_new_list(n_ready, NULL); mp_obj_list_t *ret_list = mp_obj_new_list(n_ready, NULL);
n_ready = 0; n_ready = 0;
...@@ -256,16 +269,13 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) { ...@@ -256,16 +269,13 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
if (poll_obj->flags_ret != 0) { if (poll_obj->flags_ret != 0) {
mp_obj_t tuple[2] = {poll_obj->obj, MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret)}; mp_obj_t tuple[2] = {poll_obj->obj, MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret)};
ret_list->items[n_ready++] = mp_obj_new_tuple(2, tuple); ret_list->items[n_ready++] = mp_obj_new_tuple(2, tuple);
if (flags & FLAG_ONESHOT) { if (self->flags & FLAG_ONESHOT) {
// Don't poll next time, until new event flags will be set explicitly // Don't poll next time, until new event flags will be set explicitly
poll_obj->flags = 0; poll_obj->flags = 0;
} }
} }
} }
return ret_list; return ret_list;
}
MICROPY_EVENT_POLL_HOOK
}
} }
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment