8000 extmod: implement basic "uevent" module (RFC, WIP) by dpgeorge · Pull Request #6125 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

extmod: implement basic "uevent" module (RFC, WIP) #6125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/library/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ the following libraries.
ubluetooth.rst
ucryptolib.rst
uctypes.rst
uevent.rst


Port-specific libraries
Expand Down
35 changes: 35 additions & 0 deletions docs/library/uevent.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
:mod:`uevent` -- wait for events on a set of objects
====================================================

.. module:: uevent
:synopsis: wait for events on a set of objects

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth some intro?

The main purpose of the uevent module is to enable fully-event driven operation using uasyncio such that whenever any form of event needs to be awaited it can be done by pausing the processor until the event occurs. The benefits are that the processor and other parts of the system can be put to sleep to save power and that an event, when it comes in, gets handled immediately without having to wait for a polling loop to come around.

The uevent functionality is loosely patterned after the select and selectors modules with the major distinction that it is not tied to file descriptors or stream I/O. Instead, an event is an abstract concept represented by a ... [oops, can't continue until I actually understand some more...]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds pretty good. Will add.

This class is similar to the ``select`` and ``selectors`` modules but:
- poll.register() will update flags, not overwrite
- one-shot behaviour
- poll_entry can have a user method set on it
- everything can be O(1) in a native implementation
- poll.notify() for async wake-up (TODO)
- helps to minimise heap allocation, both in this module and for the user's use of it
- unregister operates on the poll_entry (also on poll)

Functions
---------

.. function:: poll()

Create an instance of the Poll class.

.. _class: Poll

class ``Poll``
--------------

Methods
~~~~~~~

.. method:: poll.register(obj[, eventmask])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is obj? I assume it has to have a certain interface?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the port. On unix it needs to respond to MP_STREAM_GET_FILENO. On bare-metal in the current code it must implement the MP_STREAM_POLL ioctl. But when it moves to an event-based system the obj needs to respond to MP_STREAM_SET_EVENT_CALLBACK (or whatever i'll be called).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can some of your reply be captured in this doc or perhaps in the code since the interface described in the doc is still in flux?


.. method:: poll.unregister(obj[, eventmask])

.. method:: poll.poll_ms(timeout=-1, /)
100 changes: 100 additions & 0 deletions extmod/moduevent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "py/runtime.h"
#include "extmod/moduevent.h"

#if MICROPY_PY_UEVENT != MICROPY_PY_UEVENT_IMPL_NONE

STATIC const mp_obj_fun_builtin_var_t poll_unregister_obj;

STATIC void mp_uevent_poll_entry_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
mp_obj_poll_entry_base_t *self = MP_OBJ_TO_PTR(self_in);
if (dest[0] == MP_OBJ_NULL) {
// Load attribute.
if (attr == MP_QSTR_data) {
dest[0] = self->user_data;
} else if (attr == MP_QSTR_flags) {
dest[0] = MP_OBJ_NEW_SMALL_INT(self->user_flags);
} else if (attr == MP_QSTR_unregister) {
dest[0] = MP_OBJ_FROM_PTR(&poll_unregister_obj);
dest[1] = MP_OBJ_FROM_PTR(self);
} else if (attr == self->user_method_name) {
dest[0] = self->user_method_obj;
dest[1] = self_in;
}
} else if (dest[1] != MP_OBJ_NULL) {
// Store attribute.
if (attr == MP_QSTR_data) {
self->user_data = dest[1];
dest[0] = MP_OBJ_NULL; // success
} else if (attr != MP_QSTR_flags) {
self->user_method_ 9E81 name = attr;
self->user_method_obj = dest[1];
dest[0] = MP_OBJ_NULL; // success
}
}
}

const mp_obj_type_t mp_type_poll_entry = {
{ &mp_type_type },
.name = MP_QSTR_poll_entry,
.attr = mp_uevent_poll_entry_attr,
};

STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_register_obj, 2, 3, mp_uevent_poll_register);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_unregister_obj, 1, 2, mp_uevent_poll_unregister);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_ms_obj, 1, 2, mp_uevent_poll_poll_ms);

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_unregister), MP_ROM_PTR(&poll_unregister_obj) },
{ MP_ROM_QSTR(MP_QSTR_poll_ms), MP_ROM_PTR(&poll_poll_ms_obj) },
};
STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table);

const mp_obj_type_t mp_type_poll = {
{ &mp_type_type },
.name = MP_QSTR_poll,
.getiter = mp_identity_getiter,
.iternext = mp_uevent_poll_iternext,
.locals_dict = (mp_obj_dict_t *)&poll_locals_dict,
};

STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_uevent_poll_obj, mp_uevent_poll);

STATIC const mp_rom_map_elem_t mp_module_uevent_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uevent) },
{ MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mp_uevent_poll_obj) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_uevent_globals, mp_module_uevent_globals_table);

const mp_obj_module_t mp_module_uevent = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&mp_module_uevent_globals,
};

#endif // MICROPY_PY_UEVENT != MICROPY_PY_UEVENT_IMPL_NONE
59 changes: 59 additions & 0 deletions extmod/moduevent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Damien P. George
*
* Permission is F438 hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_EXTMOD_MODUEVENT_H
#define MICROPY_INCLUDED_EXTMOD_MODUEVENT_H

#include "py/obj.h"

#define MICROPY_ALLOC_UEVENT_POLL_INIT (4)
#define MICROPY_ALLOC_UEVENT_POLL_INC (4)

#define UEVENT_READ (1)
#define UEVENT_WRITE (2)

typedef struct _mp_obj_poll_t mp_obj_poll_t;

typedef struct _mp_obj_poll_entry_base_t {
mp_obj_base_t base;
mp_obj_poll_t *poller;
mp_obj_t obj;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be really, really helpful to have a comment somewhere about this mysterious "obj", e.g. what does it need to implement or look like...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See reply above.

uint16_t user_flags;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// copy of ready_flags: set of events on this object being signaled to the user

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ready_flags doesn't exist on unix, it's more like "set of events ready on this object since the last time the user got them by iterating the poller"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha. Mind adding that as a comment?

uint16_t user_method_name; // a qstr
mp_obj_t user_method_obj;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user_method_name and _obj seem unused? Maybe I missed something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are used by mp_uevent_poll_entry_attr

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that has getters and setters, but I don't see them being actually used anywhere. They were used in #6110. Maybe I'm overlooking something.

mp_obj_t user_data;
} mp_obj_poll_entry_base_t;

extern const mp_obj_type_t mp_type_poll;
extern const mp_obj_type_t mp_type_poll_entry;

// These should all be provided by a specific uevent implementation.
mp_obj_t mp_uevent_poll_iternext(mp_obj_t self_in);
mp_obj_t mp_uevent_poll_register(size_t n_args, const mp_obj_t *args);
mp_obj_t mp_uevent_poll_unregister(size_t n_args, const mp_obj_t *args);
mp_obj_t mp_uevent_poll_poll_ms(size_t n_args, const mp_obj_t *args);
mp_obj_t mp_uevent_poll(void);

#endif // MICROPY_INCLUDED_EXTMOD_MODUEVENT_H
Loading
0