Skip to content

Commit 65971f5

Browse files
committed
unix: Add "uselect" module, with poll() function.
Underlyingly, uses standard POSIX poll() for portability.
1 parent 79c4ec1 commit 65971f5

5 files changed

Lines changed: 207 additions & 0 deletions

File tree

unix/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ SRC_C = \
115115
input.c \
116116
file.c \
117117
modos.c \
118+
moduselect.c \
118119
alloc.c \
119120
coverage.c \
120121
$(SRC_MOD)

unix/moduselect.c

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Damien P. George
7+
* Copyright (c) 2015 Paul Sokolovsky
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <stdio.h>
29+
#include <errno.h>
30+
#include <poll.h>
31+
32+
#include "py/nlr.h"
33+
#include "py/obj.h"
34+
#include "py/objlist.h"
35+
#include "py/objtuple.h"
36+
#include "py/mphal.h"
37+
38+
/// \class Poll - poll class
39+
40+
typedef struct _mp_obj_poll_t {
41+
mp_obj_base_t base;
42+
unsigned short alloc;
43+
unsigned short len;
44+
struct pollfd *entries;
45+
} mp_obj_poll_t;
46+
47+
/// \method register(obj[, eventmask])
48+
STATIC mp_obj_t poll_register(uint n_args, const mp_obj_t *args) {
49+
mp_obj_poll_t *self = args[0];
50+
mp_uint_t flags;
51+
if (n_args == 3) {
52+
flags = mp_obj_get_int(args[2]);
53+
} else {
54+
flags = POLLIN | POLLOUT;
55+
}
56+
57+
int i;
58+
if (self->len < self->alloc) {
59+
i = self->len++;
60+
} else {
61+
assert(0);
62+
}
63+
64+
self->entries[i].fd = mp_obj_get_int(args[1]);
65+
self->entries[i].events = flags;
66+
self->entries[i].revents = 0;
67+
68+
return mp_const_none;
69+
}
70+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register);
71+
72+
/// \method unregister(obj)
73+
STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) {
74+
mp_obj_poll_t *self = self_in;
75+
struct pollfd *entries = self->entries;
76+
int fd = mp_obj_get_int(obj_in);
77+
for (int i = self->len - 1; i >= 0; i--) {
78+
if (entries->fd == fd) {
79+
entries->fd = -1;
80+
break;
81+
}
82+
entries++;
83+
}
84+
85+
// TODO raise KeyError if obj didn't exist in map
86+
return mp_const_none;
87+
}
88+
MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister);
89+
90+
/// \method modify(obj, eventmask)
91+
STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) {
92+
mp_obj_poll_t *self = self_in;
93+
struct pollfd *entries = self->entries;
94+
int fd = mp_obj_get_int(obj_in);
95+
for (int i = self->len - 1; i >= 0; i--) {
96+
if (entries->fd == fd) {
97+
entries->events = mp_obj_get_int(eventmask_in);
98+
break;
99+
}
100+
entries++;
101+
}
102+
103+
// TODO raise KeyError if obj didn't exist in map
104+
return mp_const_none;
105+
}
106+
MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify);
107+
108+
/// \method poll([timeout])
109+
/// Timeout is in milliseconds.
110+
STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) {
111+
mp_obj_poll_t *self = args[0];
112+
113+
// work out timeout (its given already in ms)
114+
int timeout = -1;
115+
if (n_args == 2) {
116+
if (args[1] != mp_const_none) {
117+
mp_int_t timeout_i = mp_obj_get_int(args[1]);
118+
if (timeout_i >= 0) {
119+
timeout = timeout_i;
120+
}
121+
}
122+
}
123+
124+
int n_ready = poll(self->entries, self->len, timeout);
125+
RAISE_ERRNO(n_ready, errno);
126+
if (n_ready == 0) {
127+
return mp_const_empty_tuple;
128+
}
129+
130+
mp_obj_list_t *ret_list = mp_obj_new_list(n_ready, NULL);
131+
int ret_i = 0;
132+
struct pollfd *entries = self->entries;
133+
for (int i = 0; i < self->len; i++) {
134+
if (entries->revents != 0) {
135+
mp_obj_tuple_t *t = mp_obj_new_tuple(2, NULL);
136+
t->items[0] = MP_OBJ_NEW_SMALL_INT(entries->fd);
137+
t->items[1] = MP_OBJ_NEW_SMALL_INT(entries->revents);
138+
ret_list->items[ret_i++] = t;
139+
entries++;
140+
}
141+
}
142+
143+
return ret_list;
144+
}
145+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 2, poll_poll);
146+
147+
STATIC const mp_map_elem_t poll_locals_dict_table[] = {
148+
{ MP_OBJ_NEW_QSTR(MP_QSTR_register), (mp_obj_t)&poll_register_obj },
149+
{ MP_OBJ_NEW_QSTR(MP_QSTR_unregister), (mp_obj_t)&poll_unregister_obj },
150+
{ MP_OBJ_NEW_QSTR(MP_QSTR_modify), (mp_obj_t)&poll_modify_obj },
151+
{ MP_OBJ_NEW_QSTR(MP_QSTR_poll), (mp_obj_t)&poll_poll_obj },
152+
};
153+
STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table);
154+
155+
STATIC const mp_obj_type_t mp_type_poll = {
156+
{ &mp_type_type },
157+
.name = MP_QSTR_poll,
158+
.locals_dict = (mp_obj_t)&poll_locals_dict,
159+
};
160+
161+
STATIC mp_obj_t select_poll(mp_uint_t n_args, const mp_obj_t *args) {
162+
int alloc = 4;
163+
if (n_args > 0) {
164+
alloc = mp_obj_get_int(args[0]);
165+
}
166+
mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t);
167+
poll->base.type = &mp_type_poll;
168+
poll->entries = m_new(struct pollfd, alloc);
169+
poll->alloc = alloc;
170+
poll->len = 0;
171+
return poll;
172+
}
173+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_poll_obj, 0, 1, select_poll);
174+
175+
STATIC const mp_map_elem_t mp_module_select_globals_table[] = {
176+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uselect) },
177+
{ MP_OBJ_NEW_QSTR(MP_QSTR_poll), (mp_obj_t)&mp_select_poll_obj },
178+
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLIN), MP_OBJ_NEW_SMALL_INT(POLLIN) },
179+
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLOUT), MP_OBJ_NEW_SMALL_INT(POLLOUT) },
180+
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLERR), MP_OBJ_NEW_SMALL_INT(POLLERR) },
181+
{ MP_OBJ_NEW_QSTR(MP_QSTR_POLLHUP), MP_OBJ_NEW_SMALL_INT(POLLHUP) },
182+
};
183+
184+
STATIC MP_DEFINE_CONST_DICT(mp_module_select_globals, mp_module_select_globals_table);
185+
186+
const mp_obj_module_t mp_module_uselect = {
187+
.base = { &mp_type_module },
188+
.name = MP_QSTR_uselect,
189+
.globals = (mp_obj_dict_t*)&mp_module_select_globals,
190+
};

unix/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256)
125125

126126
extern const struct _mp_obj_module_t mp_module_os;
127+
extern const struct _mp_obj_module_t mp_module_uselect;
127128
extern const struct _mp_obj_module_t mp_module_time;
128129
extern const struct _mp_obj_module_t mp_module_termios;
129130
extern const struct _mp_obj_module_t mp_module_socket;
@@ -162,6 +163,7 @@ extern const struct _mp_obj_module_t mp_module_jni;
162163
MICROPY_PY_TIME_DEF \
163164
MICROPY_PY_SOCKET_DEF \
164165
{ MP_OBJ_NEW_QSTR(MP_QSTR__os), (mp_obj_t)&mp_module_os }, \
166+
{ MP_OBJ_NEW_QSTR(MP_QSTR_uselect), (mp_obj_t)&mp_module_uselect }, \
165167
MICROPY_PY_TERMIOS_DEF \
166168

167169
// type definitions for the specific machine

unix/mphalport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ void mp_hal_stdio_mode_raw(void);
3535
void mp_hal_stdio_mode_orig(void);
3636

3737
static inline void mp_hal_delay_ms(mp_uint_t ms) { usleep((ms) * 1000); }
38+
39+
#define RAISE_ERRNO(err_flag, error_val) \
40+
{ if (err_flag == -1) \
41+
{ nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error_val))); } }

unix/qstrdefsport.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ Q(unlink)
4444
Q(getenv)
4545
Q(mkdir)
4646

47+
Q(uselect)
48+
Q(poll)
49+
Q(register)
50+
Q(unregister)
51+
Q(modify)
52+
Q(POLLIN)
53+
Q(POLLOUT)
54+
Q(POLLERR)
55+
Q(POLLHUP)
56+
4757
Q(ffi)
4858
Q(ffimod)
4959
Q(ffifunc)

0 commit comments

Comments
 (0)