Skip to content

Commit c1481bb

Browse files
committed
unix/moduselect: Implement "one-shot" flag for poll.poll().
After an I/O event is triggered for fd, event flags are automatically reset, so no further events are reported until new event flags are set. This is an optimization for uasyncio, required to account for coroutine semantics: each coroutine issues explicit read/write async call, and once that trigger, no events should be reported to coroutine, unless it again explicitly requests it. One-shot mode saves one linear scan over the poll array.
1 parent 4651c43 commit c1481bb

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

unix/moduselect.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#include "py/objtuple.h"
3636
#include "py/mphal.h"
3737

38+
// Flags for poll()
39+
#define FLAG_ONESHOT (1)
40+
3841
/// \class Poll - poll class
3942

4043
typedef struct _mp_obj_poll_t {
@@ -125,15 +128,19 @@ MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify);
125128
STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
126129
mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
127130

128-
// work out timeout (its given already in ms)
131+
// work out timeout (it's given already in ms)
129132
int timeout = -1;
130-
if (n_args == 2) {
133+
int flags = 0;
134+
if (n_args >= 2) {
131135
if (args[1] != mp_const_none) {
132136
mp_int_t timeout_i = mp_obj_get_int(args[1]);
133137
if (timeout_i >= 0) {
134138
timeout = timeout_i;
135139
}
136140
}
141+
if (n_args >= 3) {
142+
flags = mp_obj_get_int(args[2]);
143+
}
137144
}
138145

139146
int n_ready = poll(self->entries, self->len, timeout);
@@ -151,12 +158,15 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
151158
t->items[0] = MP_OBJ_NEW_SMALL_INT(entries->fd);
152159
t->items[1] = MP_OBJ_NEW_SMALL_INT(entries->revents);
153160
ret_list->items[ret_i++] = MP_OBJ_FROM_PTR(t);
161+
if (flags & FLAG_ONESHOT) {
162+
entries->events = 0;
163+
}
154164
}
155165
}
156166

157167
return MP_OBJ_FROM_PTR(ret_list);
158168
}
159-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 2, poll_poll);
169+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll);
160170

161171
STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = {
162172
{ MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&poll_register_obj) },

0 commit comments

Comments
 (0)