1 | /* |
2 | * This file is part of the MicroPython project, http://micropython.org/ |
3 | * |
4 | * The MIT License (MIT) |
5 | * |
6 | * Copyright (c) 2014 Damien P. George |
7 | * Copyright (c) 2015-2017 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 "py/mpconfig.h" |
29 | #if MICROPY_PY_USELECT |
30 | |
31 | #include <stdio.h> |
32 | |
33 | #include "py/runtime.h" |
34 | #include "py/obj.h" |
35 | #include "py/objlist.h" |
36 | #include "py/stream.h" |
37 | #include "py/mperrno.h" |
38 | #include "py/mphal.h" |
39 | |
40 | // Flags for poll() |
41 | #define FLAG_ONESHOT (1) |
42 | |
43 | /// \module select - Provides select function to wait for events on a stream |
44 | /// |
45 | /// This module provides the select function. |
46 | |
47 | typedef struct _poll_obj_t { |
48 | mp_obj_t obj; |
49 | mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode); |
50 | mp_uint_t flags; |
51 | mp_uint_t flags_ret; |
52 | } poll_obj_t; |
53 | |
54 | STATIC void poll_map_add(mp_map_t *poll_map, const mp_obj_t *obj, mp_uint_t obj_len, mp_uint_t flags, bool or_flags) { |
55 | for (mp_uint_t i = 0; i < obj_len; i++) { |
56 | mp_map_elem_t *elem = mp_map_lookup(poll_map, mp_obj_id(obj[i]), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); |
57 | if (elem->value == MP_OBJ_NULL) { |
58 | // object not found; get its ioctl and add it to the poll list |
59 | const mp_stream_p_t *stream_p = mp_get_stream_raise(obj[i], MP_STREAM_OP_IOCTL); |
60 | poll_obj_t *poll_obj = m_new_obj(poll_obj_t); |
61 | poll_obj->obj = obj[i]; |
62 | poll_obj->ioctl = stream_p->ioctl; |
63 | poll_obj->flags = flags; |
64 | poll_obj->flags_ret = 0; |
65 | elem->value = MP_OBJ_FROM_PTR(poll_obj); |
66 | } else { |
67 | // object exists; update its flags |
68 | if (or_flags) { |
69 | ((poll_obj_t *)MP_OBJ_TO_PTR(elem->value))->flags |= flags; |
70 | } else { |
71 | ((poll_obj_t *)MP_OBJ_TO_PTR(elem->value))->flags = flags; |
72 | } |
73 | } |
74 | } |
75 | } |
76 | |
77 | // poll each object in the map |
78 | STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map, size_t *rwx_num) { |
79 | mp_uint_t n_ready = 0; |
80 | for (mp_uint_t i = 0; i < poll_map->alloc; ++i) { |
81 | if (!mp_map_slot_is_filled(poll_map, i)) { |
82 | continue; |
83 | } |
84 | |
85 | poll_obj_t *poll_obj = MP_OBJ_TO_PTR(poll_map->table[i].value); |
86 | int errcode; |
87 | mp_int_t ret = poll_obj->ioctl(poll_obj->obj, MP_STREAM_POLL, poll_obj->flags, &errcode); |
88 | poll_obj->flags_ret = ret; |
89 | |
90 | if (ret == -1) { |
91 | // error doing ioctl |
92 | mp_raise_OSError(errcode); |
93 | } |
94 | |
95 | if (ret != 0) { |
96 | // object is ready |
97 | n_ready += 1; |
98 | if (rwx_num != NULL) { |
99 | if (ret & MP_STREAM_POLL_RD) { |
100 | rwx_num[0] += 1; |
101 | } |
102 | if (ret & MP_STREAM_POLL_WR) { |
103 | rwx_num[1] += 1; |
104 | } |
105 | if ((ret & ~(MP_STREAM_POLL_RD | MP_STREAM_POLL_WR)) != 0) { |
106 | rwx_num[2] += 1; |
107 | } |
108 | } |
109 | } |
110 | } |
111 | return n_ready; |
112 | } |
113 | |
114 | /// \function select(rlist, wlist, xlist[, timeout]) |
115 | STATIC mp_obj_t select_select(size_t n_args, const mp_obj_t *args) { |
116 | // get array data from tuple/list arguments |
117 | size_t rwx_len[3]; |
118 | mp_obj_t *r_array, *w_array, *x_array; |
119 | mp_obj_get_array(args[0], &rwx_len[0], &r_array); |
120 | mp_obj_get_array(args[1], &rwx_len[1], &w_array); |
121 | mp_obj_get_array(args[2], &rwx_len[2], &x_array); |
122 | |
123 | // get timeout |
124 | mp_uint_t timeout = -1; |
125 | if (n_args == 4) { |
126 | if (args[3] != mp_const_none) { |
127 | #if MICROPY_PY_BUILTINS_FLOAT |
128 | float timeout_f = mp_obj_get_float_to_f(args[3]); |
129 | if (timeout_f >= 0) { |
130 | timeout = (mp_uint_t)(timeout_f * 1000); |
131 | } |
132 | #else |
133 | timeout = mp_obj_get_int(args[3]) * 1000; |
134 | #endif |
135 | } |
136 | } |
137 | |
138 | // merge separate lists and get the ioctl function for each object |
139 | mp_map_t poll_map; |
140 | mp_map_init(&poll_map, rwx_len[0] + rwx_len[1] + rwx_len[2]); |
141 | poll_map_add(&poll_map, r_array, rwx_len[0], MP_STREAM_POLL_RD, true); |
142 | poll_map_add(&poll_map, w_array, rwx_len[1], MP_STREAM_POLL_WR, true); |
143 | poll_map_add(&poll_map, x_array, rwx_len[2], MP_STREAM_POLL_ERR | MP_STREAM_POLL_HUP, true); |
144 | |
145 | mp_uint_t start_tick = mp_hal_ticks_ms(); |
146 | rwx_len[0] = rwx_len[1] = rwx_len[2] = 0; |
147 | for (;;) { |
148 | // poll the objects |
149 | mp_uint_t n_ready = poll_map_poll(&poll_map, rwx_len); |
150 | |
151 | if (n_ready > 0 || (timeout != (mp_uint_t)-1 && mp_hal_ticks_ms() - start_tick >= timeout)) { |
152 | // one or more objects are ready, or we had a timeout |
153 | mp_obj_t list_array[3]; |
154 | list_array[0] = mp_obj_new_list(rwx_len[0], NULL); |
155 | list_array[1] = mp_obj_new_list(rwx_len[1], NULL); |
156 | list_array[2] = mp_obj_new_list(rwx_len[2], NULL); |
157 | rwx_len[0] = rwx_len[1] = rwx_len[2] = 0; |
158 | for (mp_uint_t i = 0; i < poll_map.alloc; ++i) { |
159 | if (!mp_map_slot_is_filled(&poll_map, i)) { |
160 | continue; |
161 | } |
162 | poll_obj_t *poll_obj = MP_OBJ_TO_PTR(poll_map.table[i].value); |
163 | if (poll_obj->flags_ret & MP_STREAM_POLL_RD) { |
164 | ((mp_obj_list_t *)MP_OBJ_TO_PTR(list_array[0]))->items[rwx_len[0]++] = poll_obj->obj; |
165 | } |
166 | if (poll_obj->flags_ret & MP_STREAM_POLL_WR) { |
167 | ((mp_obj_list_t *)MP_OBJ_TO_PTR(list_array[1]))->items[rwx_len[1]++] = poll_obj->obj; |
168 | } |
169 | if ((poll_obj->flags_ret & ~(MP_STREAM_POLL_RD | MP_STREAM_POLL_WR)) != 0) { |
170 | ((mp_obj_list_t *)MP_OBJ_TO_PTR(list_array[2]))->items[rwx_len[2]++] = poll_obj->obj; |
171 | } |
172 | } |
173 | mp_map_deinit(&poll_map); |
174 | return mp_obj_new_tuple(3, list_array); |
175 | } |
176 | MICROPY_EVENT_POLL_HOOK |
177 | } |
178 | } |
179 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select); |
180 | |
181 | /// \class Poll - poll class |
182 | |
183 | typedef struct _mp_obj_poll_t { |
184 | mp_obj_base_t base; |
185 | mp_map_t poll_map; |
186 | short iter_cnt; |
187 | short iter_idx; |
188 | int flags; |
189 | // callee-owned tuple |
190 | mp_obj_t ret_tuple; |
191 | } mp_obj_poll_t; |
192 | |
193 | /// \method register(obj[, eventmask]) |
194 | STATIC mp_obj_t poll_register(size_t n_args, const mp_obj_t *args) { |
195 | mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]); |
196 | mp_uint_t flags; |
197 | if (n_args == 3) { |
198 | flags = mp_obj_get_int(args[2]); |
199 | } else { |
200 | flags = MP_STREAM_POLL_RD | MP_STREAM_POLL_WR; |
201 | } |
202 | poll_map_add(&self->poll_map, &args[1], 1, flags, false); |
203 | return mp_const_none; |
204 | } |
205 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register); |
206 | |
207 | /// \method unregister(obj) |
208 | STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) { |
209 | mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in); |
210 | mp_map_lookup(&self->poll_map, mp_obj_id(obj_in), MP_MAP_LOOKUP_REMOVE_IF_FOUND); |
211 | // TODO raise KeyError if obj didn't exist in map |
212 | return mp_const_none; |
213 | } |
214 | MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister); |
215 | |
216 | /// \method modify(obj, eventmask) |
217 | STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) { |
218 | mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in); |
219 | mp_map_elem_t *elem = mp_map_lookup(&self->poll_map, mp_obj_id(obj_in), MP_MAP_LOOKUP); |
220 | if (elem == NULL) { |
221 | mp_raise_OSError(MP_ENOENT); |
222 | } |
223 | ((poll_obj_t *)MP_OBJ_TO_PTR(elem->value))->flags = mp_obj_get_int(eventmask_in); |
224 | return mp_const_none; |
225 | } |
226 | MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify); |
227 | |
228 | STATIC mp_uint_t poll_poll_internal(uint n_args, const mp_obj_t *args) { |
229 | mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]); |
230 | |
231 | // work out timeout (its given already in ms) |
232 | mp_uint_t timeout = -1; |
233 | int flags = 0; |
234 | if (n_args >= 2) { |
235 | if (args[1] != mp_const_none) { |
236 | mp_int_t timeout_i = mp_obj_get_int(args[1]); |
237 | if (timeout_i >= 0) { |
238 | timeout = timeout_i; |
239 | } |
240 | } |
241 | if (n_args >= 3) { |
242 | flags = mp_obj_get_int(args[2]); |
243 | } |
244 | } |
245 | |
246 | self->flags = flags; |
247 | |
248 | mp_uint_t start_tick = mp_hal_ticks_ms(); |
249 | mp_uint_t n_ready; |
250 | for (;;) { |
251 | // poll the objects |
252 | n_ready = poll_map_poll(&self->poll_map, NULL); |
253 | if (n_ready > 0 || (timeout != (mp_uint_t)-1 && mp_hal_ticks_ms() - start_tick >= timeout)) { |
254 | break; |
255 | } |
256 | MICROPY_EVENT_POLL_HOOK |
257 | } |
258 | |
259 | return n_ready; |
260 | } |
261 | |
262 | STATIC mp_obj_t poll_poll(size_t n_args, const mp_obj_t *args) { |
263 | mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]); |
264 | mp_uint_t n_ready = poll_poll_internal(n_args, args); |
265 | |
266 | // one or more objects are ready, or we had a timeout |
267 | mp_obj_list_t *ret_list = MP_OBJ_TO_PTR(mp_obj_new_list(n_ready, NULL)); |
268 | n_ready = 0; |
269 | for (mp_uint_t i = 0; i < self->poll_map.alloc; ++i) { |
270 | if (!mp_map_slot_is_filled(&self->poll_map, i)) { |
271 | continue; |
272 | } |
273 | poll_obj_t *poll_obj = MP_OBJ_TO_PTR(self->poll_map.table[i].value); |
274 | if (poll_obj->flags_ret != 0) { |
275 | mp_obj_t tuple[2] = {poll_obj->obj, MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret)}; |
276 | ret_list->items[n_ready++] = mp_obj_new_tuple(2, tuple); |
277 | if (self->flags & FLAG_ONESHOT) { |
278 | // Don't poll next time, until new event flags will be set explicitly |
279 | poll_obj->flags = 0; |
280 | } |
281 | } |
282 | } |
283 | return MP_OBJ_FROM_PTR(ret_list); |
284 | } |
285 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll); |
286 | |
287 | STATIC mp_obj_t poll_ipoll(size_t n_args, const mp_obj_t *args) { |
288 | mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]); |
289 | |
290 | if (self->ret_tuple == MP_OBJ_NULL) { |
291 | self->ret_tuple = mp_obj_new_tuple(2, NULL); |
292 | } |
293 | |
294 | int n_ready = poll_poll_internal(n_args, args); |
295 | self->iter_cnt = n_ready; |
296 | self->iter_idx = 0; |
297 | |
298 | return args[0]; |
299 | } |
300 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_ipoll_obj, 1, 3, poll_ipoll); |
301 | |
302 | STATIC mp_obj_t poll_iternext(mp_obj_t self_in) { |
303 | mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in); |
304 | |
305 | if (self->iter_cnt == 0) { |
306 | return MP_OBJ_STOP_ITERATION; |
307 | } |
308 | |
309 | self->iter_cnt--; |
310 | |
311 | for (mp_uint_t i = self->iter_idx; i < self->poll_map.alloc; ++i) { |
312 | self->iter_idx++; |
313 | if (!mp_map_slot_is_filled(&self->poll_map, i)) { |
314 | continue; |
315 | } |
316 | poll_obj_t *poll_obj = MP_OBJ_TO_PTR(self->poll_map.table[i].value); |
317 | if (poll_obj->flags_ret != 0) { |
318 | mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->ret_tuple); |
319 | t->items[0] = poll_obj->obj; |
320 | t->items[1] = MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret); |
321 | if (self->flags & FLAG_ONESHOT) { |
322 | // Don't poll next time, until new event flags will be set explicitly |
323 | poll_obj->flags = 0; |
324 | } |
325 | return MP_OBJ_FROM_PTR(t); |
326 | } |
327 | } |
328 | |
329 | assert(!"inconsistent number of poll active entries" ); |
330 | self->iter_cnt = 0; |
331 | return MP_OBJ_STOP_ITERATION; |
332 | } |
333 | |
334 | STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = { |
335 | { MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&poll_register_obj) }, |
336 | { MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&poll_unregister_obj) }, |
337 | { MP_ROM_QSTR(MP_QSTR_modify), MP_ROM_PTR(&poll_modify_obj) }, |
338 | { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&poll_poll_obj) }, |
339 | { MP_ROM_QSTR(MP_QSTR_ipoll), MP_ROM_PTR(&poll_ipoll_obj) }, |
340 | }; |
341 | STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); |
342 | |
343 | STATIC const mp_obj_type_t mp_type_poll = { |
344 | { &mp_type_type }, |
345 | .name = MP_QSTR_poll, |
346 | .getiter = mp_identity_getiter, |
347 | .iternext = poll_iternext, |
348 | .locals_dict = (void *)&poll_locals_dict, |
349 | }; |
350 | |
351 | /// \function poll() |
352 | STATIC mp_obj_t select_poll(void) { |
353 | mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t); |
354 | poll->base.type = &mp_type_poll; |
355 | mp_map_init(&poll->poll_map, 0); |
356 | poll->iter_cnt = 0; |
357 | poll->ret_tuple = MP_OBJ_NULL; |
358 | return MP_OBJ_FROM_PTR(poll); |
359 | } |
360 | MP_DEFINE_CONST_FUN_OBJ_0(mp_select_poll_obj, select_poll); |
361 | |
362 | STATIC const mp_rom_map_elem_t mp_module_select_globals_table[] = { |
363 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uselect) }, |
364 | { MP_ROM_QSTR(MP_QSTR_select), MP_ROM_PTR(&mp_select_select_obj) }, |
365 | { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mp_select_poll_obj) }, |
366 | { MP_ROM_QSTR(MP_QSTR_POLLIN), MP_ROM_INT(MP_STREAM_POLL_RD) }, |
367 | { MP_ROM_QSTR(MP_QSTR_POLLOUT), MP_ROM_INT(MP_STREAM_POLL_WR) }, |
368 | { MP_ROM_QSTR(MP_QSTR_POLLERR), MP_ROM_INT(MP_STREAM_POLL_ERR) }, |
369 | { MP_ROM_QSTR(MP_QSTR_POLLHUP), MP_ROM_INT(MP_STREAM_POLL_HUP) }, |
370 | }; |
371 | |
372 | STATIC MP_DEFINE_CONST_DICT(mp_module_select_globals, mp_module_select_globals_table); |
373 | |
374 | const mp_obj_module_t mp_module_uselect = { |
375 | .base = { &mp_type_module }, |
376 | .globals = (mp_obj_dict_t *)&mp_module_select_globals, |
377 | }; |
378 | |
379 | #endif // MICROPY_PY_USELECT |
380 | |