SummerGift 8 лет назад
Родитель
Сommit
d4bdd4fa81
1 измененных файлов с 207 добавлено и 183 удалено
  1. 207 183
      port/moduselect.c

+ 207 - 183
port/moduselect.c

@@ -4,7 +4,6 @@
  * The MIT License (MIT)
  * The MIT License (MIT)
  *
  *
  * Copyright (c) 2014 Damien P. George
  * Copyright (c) 2014 Damien P. George
- * Copyright (c) 2015-2017 Paul Sokolovsky
  *
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * of this software and associated documentation files (the "Software"), to deal
@@ -26,128 +25,188 @@
  */
  */
 
 
 #include "py/mpconfig.h"
 #include "py/mpconfig.h"
-
-#if MICROPY_PY_USELECT_POSIX
+#if MICROPY_PY_USELECT
 
 
 #include <stdio.h>
 #include <stdio.h>
-#include <errno.h>
-#include <poll.h>
 
 
 #include "py/runtime.h"
 #include "py/runtime.h"
 #include "py/obj.h"
 #include "py/obj.h"
 #include "py/objlist.h"
 #include "py/objlist.h"
-#include "py/objtuple.h"
+#include "py/stream.h"
+#include "py/mperrno.h"
 #include "py/mphal.h"
 #include "py/mphal.h"
-#include "fdfile.h"
-
-#define DEBUG 0
-
-#if MICROPY_PY_SOCKET
-extern const mp_obj_type_t mp_type_socket;
-#endif
 
 
 // Flags for poll()
 // Flags for poll()
 #define FLAG_ONESHOT (1)
 #define FLAG_ONESHOT (1)
 
 
-/// \class Poll - poll class
-
-typedef struct _mp_obj_poll_t {
-    mp_obj_base_t base;
-    unsigned short alloc;
-    unsigned short len;
-    struct pollfd *entries;
-    mp_obj_t *obj_map;
-    short iter_cnt;
-    short iter_idx;
-    int flags;
-    // callee-owned tuple
-    mp_obj_t ret_tuple;
-} mp_obj_poll_t;
+/// \module select - Provides select function to wait for events on a stream
+///
+/// This module provides the select function.
 
 
-STATIC int get_fd(mp_obj_t fdlike) {
-    int fd;
-    // Shortcut for fdfile compatible types
-    if (MP_OBJ_IS_TYPE(fdlike, &mp_type_fileio)
-        #if MICROPY_PY_SOCKET
-        || MP_OBJ_IS_TYPE(fdlike, &mp_type_socket)
-        #endif
-        ) {
-        mp_obj_fdfile_t *fdfile = MP_OBJ_TO_PTR(fdlike);
-        fd = fdfile->fd;
-    } else {
-        fd = mp_obj_get_int(fdlike);
+typedef struct _poll_obj_t {
+    mp_obj_t obj;
+    mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, mp_uint_t arg, int *errcode);
+    mp_uint_t flags;
+    mp_uint_t flags_ret;
+} poll_obj_t;
+
+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) {
+    for (mp_uint_t i = 0; i < obj_len; i++) {
+        mp_map_elem_t *elem = mp_map_lookup(poll_map, mp_obj_id(obj[i]), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
+        if (elem->value == NULL) {
+            // object not found; get its ioctl and add it to the poll list
+            const mp_stream_p_t *stream_p = mp_get_stream_raise(obj[i], MP_STREAM_OP_IOCTL);
+            poll_obj_t *poll_obj = m_new_obj(poll_obj_t);
+            poll_obj->obj = obj[i];
+            poll_obj->ioctl = stream_p->ioctl;
+            poll_obj->flags = flags;
+            poll_obj->flags_ret = 0;
+            elem->value = poll_obj;
+        } else {
+            // object exists; update its flags
+            if (or_flags) {
+                ((poll_obj_t*)elem->value)->flags |= flags;
+            } else {
+                ((poll_obj_t*)elem->value)->flags = flags;
+            }
+        }
     }
     }
-    return fd;
 }
 }
 
 
-/// \method register(obj[, eventmask])
-STATIC mp_obj_t poll_register(size_t n_args, const mp_obj_t *args) {
-    mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
-    bool is_fd = MP_OBJ_IS_INT(args[1]);
-    int fd = get_fd(args[1]);
-
-    mp_uint_t flags;
-    if (n_args == 3) {
-        flags = mp_obj_get_int(args[2]);
-    } else {
-        flags = POLLIN | POLLOUT;
-    }
+// poll each object in the map
+STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map, mp_uint_t *rwx_num) {
+    mp_uint_t n_ready = 0;
+    for (mp_uint_t i = 0; i < poll_map->alloc; ++i) {
+        if (!MP_MAP_SLOT_IS_FILLED(poll_map, i)) {
+            continue;
+        }
 
 
-    struct pollfd *free_slot = NULL;
+        poll_obj_t *poll_obj = (poll_obj_t*)poll_map->table[i].value;
+        int errcode;
+        mp_int_t ret = poll_obj->ioctl(poll_obj->obj, MP_STREAM_POLL, poll_obj->flags, &errcode);
+        poll_obj->flags_ret = ret;
 
 
-    struct pollfd *entry = self->entries;
-    for (int i = 0; i < self->len; i++, entry++) {
-        int entry_fd = entry->fd;
-        if (entry_fd == fd) {
-            entry->events = flags;
-            return mp_const_false;
+        if (ret == -1) {
+            // error doing ioctl
+            mp_raise_OSError(errcode);
         }
         }
-        if (entry_fd == -1) {
-            free_slot = entry;
+
+        if (ret != 0) {
+            // object is ready
+            n_ready += 1;
+            if (rwx_num != NULL) {
+                if (ret & MP_STREAM_POLL_RD) {
+                    rwx_num[0] += 1;
+                }
+                if (ret & MP_STREAM_POLL_WR) {
+                    rwx_num[1] += 1;
+                }
+                if ((ret & ~(MP_STREAM_POLL_RD | MP_STREAM_POLL_WR)) != 0) {
+                    rwx_num[2] += 1;
+                }
+            }
         }
         }
     }
     }
+    return n_ready;
+}
 
 
-    if (free_slot == NULL) {
-        if (self->len >= self->alloc) {
-            self->entries = m_renew(struct pollfd, self->entries, self->alloc, self->alloc + 4);
-            if (self->obj_map) {
-                self->obj_map = m_renew(mp_obj_t, self->obj_map, self->alloc, self->alloc + 4);
+/// \function select(rlist, wlist, xlist[, timeout])
+STATIC mp_obj_t select_select(uint n_args, const mp_obj_t *args) {
+    // get array data from tuple/list arguments
+    size_t rwx_len[3];
+    mp_obj_t *r_array, *w_array, *x_array;
+    mp_obj_get_array(args[0], &rwx_len[0], &r_array);
+    mp_obj_get_array(args[1], &rwx_len[1], &w_array);
+    mp_obj_get_array(args[2], &rwx_len[2], &x_array);
+
+    // get timeout
+    mp_uint_t timeout = -1;
+    if (n_args == 4) {
+        if (args[3] != mp_const_none) {
+            #if MICROPY_PY_BUILTINS_FLOAT
+            float timeout_f = mp_obj_get_float(args[3]);
+            if (timeout_f >= 0) {
+                timeout = (mp_uint_t)(timeout_f * 1000);
             }
             }
-            self->alloc += 4;
+            #else
+            timeout = mp_obj_get_int(args[3]) * 1000;
+            #endif
         }
         }
-        free_slot = &self->entries[self->len++];
     }
     }
 
 
-    if (!is_fd) {
-        if (self->obj_map == NULL) {
-            self->obj_map = m_new0(mp_obj_t, self->alloc);
+    // merge separate lists and get the ioctl function for each object
+    mp_map_t poll_map;
+    mp_map_init(&poll_map, rwx_len[0] + rwx_len[1] + rwx_len[2]);
+    poll_map_add(&poll_map, r_array, rwx_len[0], MP_STREAM_POLL_RD, true);
+    poll_map_add(&poll_map, w_array, rwx_len[1], MP_STREAM_POLL_WR, true);
+    poll_map_add(&poll_map, x_array, rwx_len[2], MP_STREAM_POLL_ERR | MP_STREAM_POLL_HUP, true);
+
+    mp_uint_t start_tick = mp_hal_ticks_ms();
+    rwx_len[0] = rwx_len[1] = rwx_len[2] = 0;
+    for (;;) {
+        // poll the objects
+        mp_uint_t n_ready = poll_map_poll(&poll_map, rwx_len);
+
+        if (n_ready > 0 || (timeout != -1 && mp_hal_ticks_ms() - start_tick >= timeout)) {
+            // one or more objects are ready, or we had a timeout
+            mp_obj_t list_array[3];
+            list_array[0] = mp_obj_new_list(rwx_len[0], NULL);
+            list_array[1] = mp_obj_new_list(rwx_len[1], NULL);
+            list_array[2] = mp_obj_new_list(rwx_len[2], NULL);
+            rwx_len[0] = rwx_len[1] = rwx_len[2] = 0;
+            for (mp_uint_t i = 0; i < poll_map.alloc; ++i) {
+                if (!MP_MAP_SLOT_IS_FILLED(&poll_map, i)) {
+                    continue;
+                }
+                poll_obj_t *poll_obj = (poll_obj_t*)poll_map.table[i].value;
+                if (poll_obj->flags_ret & MP_STREAM_POLL_RD) {
+                    ((mp_obj_list_t*)list_array[0])->items[rwx_len[0]++] = poll_obj->obj;
+                }
+                if (poll_obj->flags_ret & MP_STREAM_POLL_WR) {
+                    ((mp_obj_list_t*)list_array[1])->items[rwx_len[1]++] = poll_obj->obj;
+                }
+                if ((poll_obj->flags_ret & ~(MP_STREAM_POLL_RD | MP_STREAM_POLL_WR)) != 0) {
+                    ((mp_obj_list_t*)list_array[2])->items[rwx_len[2]++] = poll_obj->obj;
+                }
+            }
+            mp_map_deinit(&poll_map);
+            return mp_obj_new_tuple(3, list_array);
         }
         }
-        self->obj_map[free_slot - self->entries] = args[1];
+        MICROPY_EVENT_POLL_HOOK
     }
     }
+}
+MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select);
+
+/// \class Poll - poll class
+
+typedef struct _mp_obj_poll_t {
+    mp_obj_base_t base;
+    mp_map_t poll_map;
+    short iter_cnt;
+    short iter_idx;
+    int flags;
+    // callee-owned tuple
+    mp_obj_t ret_tuple;
+} mp_obj_poll_t;
 
 
-    free_slot->fd = fd;
-    free_slot->events = flags;
-    free_slot->revents = 0;
-    return mp_const_true;
+/// \method register(obj[, eventmask])
+STATIC mp_obj_t poll_register(uint n_args, const mp_obj_t *args) {
+    mp_obj_poll_t *self = args[0];
+    mp_uint_t flags;
+    if (n_args == 3) {
+        flags = mp_obj_get_int(args[2]);
+    } else {
+        flags = MP_STREAM_POLL_RD | MP_STREAM_POLL_WR;
+    }
+    poll_map_add(&self->poll_map, &args[1], 1, flags, false);
+    return mp_const_none;
 }
 }
 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register);
 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, poll_register);
 
 
 /// \method unregister(obj)
 /// \method unregister(obj)
 STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) {
 STATIC mp_obj_t poll_unregister(mp_obj_t self_in, mp_obj_t obj_in) {
-    mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
-    struct pollfd *entries = self->entries;
-    int fd = get_fd(obj_in);
-    for (int i = self->len - 1; i >= 0; i--) {
-        if (entries->fd == fd) {
-            entries->fd = -1;
-            if (self->obj_map) {
-                self->obj_map[entries - self->entries] = MP_OBJ_NULL;
-            }
-            break;
-        }
-        entries++;
-    }
-
+    mp_obj_poll_t *self = self_in;
+    mp_map_lookup(&self->poll_map, mp_obj_id(obj_in), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
     // TODO raise KeyError if obj didn't exist in map
     // TODO raise KeyError if obj didn't exist in map
     return mp_const_none;
     return mp_const_none;
 }
 }
@@ -155,27 +214,21 @@ MP_DEFINE_CONST_FUN_OBJ_2(poll_unregister_obj, poll_unregister);
 
 
 /// \method modify(obj, eventmask)
 /// \method modify(obj, eventmask)
 STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) {
 STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmask_in) {
-    mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
-    struct pollfd *entries = self->entries;
-    int fd = get_fd(obj_in);
-    for (int i = self->len - 1; i >= 0; i--) {
-        if (entries->fd == fd) {
-            entries->events = mp_obj_get_int(eventmask_in);
-            break;
-        }
-        entries++;
+    mp_obj_poll_t *self = self_in;
+    mp_map_elem_t *elem = mp_map_lookup(&self->poll_map, mp_obj_id(obj_in), MP_MAP_LOOKUP);
+    if (elem == NULL) {
+        mp_raise_OSError(MP_ENOENT);
     }
     }
-
-    // TODO raise KeyError if obj didn't exist in map
+    ((poll_obj_t*)elem->value)->flags = mp_obj_get_int(eventmask_in);
     return mp_const_none;
     return mp_const_none;
 }
 }
 MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify);
 MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify);
 
 
-STATIC int poll_poll_internal(size_t n_args, const mp_obj_t *args) {
-    mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
+STATIC mp_uint_t poll_poll_internal(uint n_args, const mp_obj_t *args) {
+    mp_obj_poll_t *self = args[0];
 
 
-    // work out timeout (it's given already in ms)
-    int timeout = -1;
+    // work out timeout (its given already in ms)
+    mp_uint_t timeout = -1;
     int flags = 0;
     int flags = 0;
     if (n_args >= 2) {
     if (n_args >= 2) {
         if (args[1] != mp_const_none) {
         if (args[1] != mp_const_none) {
@@ -191,43 +244,42 @@ STATIC int poll_poll_internal(size_t n_args, const mp_obj_t *args) {
 
 
     self->flags = flags;
     self->flags = flags;
 
 
-    int n_ready = poll(self->entries, self->len, timeout);
-    RAISE_ERRNO(n_ready, errno);
+    mp_uint_t start_tick = mp_hal_ticks_ms();
+    mp_uint_t n_ready;
+    for (;;) {
+        // poll the objects
+        n_ready = poll_map_poll(&self->poll_map, NULL);
+        if (n_ready > 0 || (timeout != -1 && mp_hal_ticks_ms() - start_tick >= timeout)) {
+            break;
+        }
+        MICROPY_EVENT_POLL_HOOK
+    }
+
     return n_ready;
     return n_ready;
 }
 }
 
 
-/// \method poll([timeout])
-/// Timeout is in milliseconds.
-STATIC mp_obj_t poll_poll(size_t n_args, const mp_obj_t *args) {
-    int n_ready = poll_poll_internal(n_args, args);
-
-    if (n_ready == 0) {
-        return mp_const_empty_tuple;
-    }
-
-    mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]);
+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);
 
 
-    mp_obj_list_t *ret_list = MP_OBJ_TO_PTR(mp_obj_new_list(n_ready, NULL));
-    int ret_i = 0;
-    struct pollfd *entries = self->entries;
-    for (int i = 0; i < self->len; i++, entries++) {
-        if (entries->revents != 0) {
-            mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
-            // If there's an object stored, return it, otherwise raw fd
-            if (self->obj_map && self->obj_map[i] != MP_OBJ_NULL) {
-                t->items[0] = self->obj_map[i];
-            } else {
-                t->items[0] = MP_OBJ_NEW_SMALL_INT(entries->fd);
-            }
-            t->items[1] = MP_OBJ_NEW_SMALL_INT(entries->revents);
-            ret_list->items[ret_i++] = MP_OBJ_FROM_PTR(t);
+    // one or more objects are ready, or we had a timeout
+    mp_obj_list_t *ret_list = mp_obj_new_list(n_ready, NULL);
+    n_ready = 0;
+    for (mp_uint_t i = 0; i < self->poll_map.alloc; ++i) {
+        if (!MP_MAP_SLOT_IS_FILLED(&self->poll_map, i)) {
+            continue;
+        }
+        poll_obj_t *poll_obj = (poll_obj_t*)self->poll_map.table[i].value;
+        if (poll_obj->flags_ret != 0) {
+            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);
             if (self->flags & FLAG_ONESHOT) {
             if (self->flags & FLAG_ONESHOT) {
-                entries->events = 0;
+                // Don't poll next time, until new event flags will be set explicitly
+                poll_obj->flags = 0;
             }
             }
         }
         }
     }
     }
-
-    return MP_OBJ_FROM_PTR(ret_list);
+    return ret_list;
 }
 }
 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);
 
 
@@ -255,20 +307,19 @@ STATIC mp_obj_t poll_iternext(mp_obj_t self_in) {
 
 
     self->iter_cnt--;
     self->iter_cnt--;
 
 
-    struct pollfd *entries = self->entries + self->iter_idx;
-    for (int i = self->iter_idx; i < self->len; i++, entries++) {
+    for (mp_uint_t i = self->iter_idx; i < self->poll_map.alloc; ++i) {
         self->iter_idx++;
         self->iter_idx++;
-        if (entries->revents != 0) {
+        if (!MP_MAP_SLOT_IS_FILLED(&self->poll_map, i)) {
+            continue;
+        }
+        poll_obj_t *poll_obj = (poll_obj_t*)self->poll_map.table[i].value;
+        if (poll_obj->flags_ret != 0) {
             mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->ret_tuple);
             mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->ret_tuple);
-            // If there's an object stored, return it, otherwise raw fd
-            if (self->obj_map && self->obj_map[i] != MP_OBJ_NULL) {
-                t->items[0] = self->obj_map[i];
-            } else {
-                t->items[0] = MP_OBJ_NEW_SMALL_INT(entries->fd);
-            }
-            t->items[1] = MP_OBJ_NEW_SMALL_INT(entries->revents);
+            t->items[0] = poll_obj->obj;
+            t->items[1] = MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret);
             if (self->flags & FLAG_ONESHOT) {
             if (self->flags & FLAG_ONESHOT) {
-                entries->events = 0;
+                // Don't poll next time, until new event flags will be set explicitly
+                poll_obj->flags = 0;
             }
             }
             return MP_OBJ_FROM_PTR(t);
             return MP_OBJ_FROM_PTR(t);
         }
         }
@@ -279,34 +330,12 @@ STATIC mp_obj_t poll_iternext(mp_obj_t self_in) {
     return MP_OBJ_STOP_ITERATION;
     return MP_OBJ_STOP_ITERATION;
 }
 }
 
 
-#if DEBUG
-STATIC mp_obj_t poll_dump(mp_obj_t self_in) {
-    mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in);
-
-    struct pollfd *entries = self->entries;
-    for (int i = self->len - 1; i >= 0; i--) {
-        printf("fd: %d ev: %x rev: %x", entries->fd, entries->events, entries->revents);
-        if (self->obj_map) {
-            printf(" obj: %p", self->obj_map[entries - self->entries]);
-        }
-        printf("\n");
-        entries++;
-    }
-
-    return mp_const_none;
-}
-MP_DEFINE_CONST_FUN_OBJ_1(poll_dump_obj, poll_dump);
-#endif
-
 STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = {
 STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = {
     { MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&poll_register_obj) },
     { MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&poll_register_obj) },
     { MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&poll_unregister_obj) },
     { MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&poll_unregister_obj) },
     { MP_ROM_QSTR(MP_QSTR_modify), MP_ROM_PTR(&poll_modify_obj) },
     { MP_ROM_QSTR(MP_QSTR_modify), MP_ROM_PTR(&poll_modify_obj) },
     { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&poll_poll_obj) },
     { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&poll_poll_obj) },
     { MP_ROM_QSTR(MP_QSTR_ipoll), MP_ROM_PTR(&poll_ipoll_obj) },
     { MP_ROM_QSTR(MP_QSTR_ipoll), MP_ROM_PTR(&poll_ipoll_obj) },
-    #if DEBUG
-    { MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&poll_dump_obj) },
-    #endif
 };
 };
 STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table);
 STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table);
 
 
@@ -318,30 +347,25 @@ STATIC const mp_obj_type_t mp_type_poll = {
     .locals_dict = (void*)&poll_locals_dict,
     .locals_dict = (void*)&poll_locals_dict,
 };
 };
 
 
-STATIC mp_obj_t select_poll(size_t n_args, const mp_obj_t *args) {
-    int alloc = 4;
-    if (n_args > 0) {
-        alloc = mp_obj_get_int(args[0]);
-    }
+/// \function poll()
+STATIC mp_obj_t select_poll(void) {
     mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t);
     mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t);
     poll->base.type = &mp_type_poll;
     poll->base.type = &mp_type_poll;
-    poll->entries = m_new(struct pollfd, alloc);
-    poll->alloc = alloc;
-    poll->len = 0;
-    poll->obj_map = NULL;
+    mp_map_init(&poll->poll_map, 0);
     poll->iter_cnt = 0;
     poll->iter_cnt = 0;
     poll->ret_tuple = MP_OBJ_NULL;
     poll->ret_tuple = MP_OBJ_NULL;
-    return MP_OBJ_FROM_PTR(poll);
+    return poll;
 }
 }
-MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_poll_obj, 0, 1, select_poll);
+MP_DEFINE_CONST_FUN_OBJ_0(mp_select_poll_obj, select_poll);
 
 
 STATIC const mp_rom_map_elem_t mp_module_select_globals_table[] = {
 STATIC const mp_rom_map_elem_t mp_module_select_globals_table[] = {
     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uselect) },
     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uselect) },
+    { MP_ROM_QSTR(MP_QSTR_select), MP_ROM_PTR(&mp_select_select_obj) },
     { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mp_select_poll_obj) },
     { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mp_select_poll_obj) },
-    { MP_ROM_QSTR(MP_QSTR_POLLIN), MP_ROM_INT(POLLIN) },
-    { MP_ROM_QSTR(MP_QSTR_POLLOUT), MP_ROM_INT(POLLOUT) },
-    { MP_ROM_QSTR(MP_QSTR_POLLERR), MP_ROM_INT(POLLERR) },
-    { MP_ROM_QSTR(MP_QSTR_POLLHUP), MP_ROM_INT(POLLHUP) },
+    { MP_ROM_QSTR(MP_QSTR_POLLIN), MP_ROM_INT(MP_STREAM_POLL_RD) },
+    { MP_ROM_QSTR(MP_QSTR_POLLOUT), MP_ROM_INT(MP_STREAM_POLL_WR) },
+    { MP_ROM_QSTR(MP_QSTR_POLLERR), MP_ROM_INT(MP_STREAM_POLL_ERR) },
+    { MP_ROM_QSTR(MP_QSTR_POLLHUP), MP_ROM_INT(MP_STREAM_POLL_HUP) },
 };
 };
 
 
 STATIC MP_DEFINE_CONST_DICT(mp_module_select_globals, mp_module_select_globals_table);
 STATIC MP_DEFINE_CONST_DICT(mp_module_select_globals, mp_module_select_globals_table);
@@ -351,4 +375,4 @@ const mp_obj_module_t mp_module_uselect = {
     .globals = (mp_obj_dict_t*)&mp_module_select_globals,
     .globals = (mp_obj_dict_t*)&mp_module_select_globals,
 };
 };
 
 
-#endif // MICROPY_PY_USELECT_POSIX
+#endif // MICROPY_PY_USELECT