Note! This project has moved to github.com/armmbed/mbed-events
This repository has been superceded
This project has moved to mbed-events
Composable event loops combine the cheap synchronicity of event loops with the composability of preempted threads.
Two modular event queue classes are provided:
- EventLoop - for loops coupled with a c++ managed thread
- EventQueue - for manually managed event queues
The Event class takes advantage of the extensibility of FuncPtr to allow an event to be passed through APIs as a normal function.
More information on composable event loops.
EventLoop.h@14:5abf2ccf2dbf, 2016-05-10 (annotated)
- Committer:
- Christopher Haster
- Date:
- Tue May 10 07:51:44 2016 -0500
- Revision:
- 14:5abf2ccf2dbf
- Parent:
- 11:6721568592e5
- Child:
- 16:ff5d48fcce1b
Move to internal memory management
Allows an event to be enqueued multiple times before being handled.
Downside is cancel is non-trivial to support.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
8:3c2a014bd907 | 1 | /* EventLoop |
Christopher Haster |
8:3c2a014bd907 | 2 | * |
Christopher Haster |
8:3c2a014bd907 | 3 | * EventQueue wrapped in a thread |
Christopher Haster |
8:3c2a014bd907 | 4 | */ |
Christopher Haster |
8:3c2a014bd907 | 5 | #ifndef EVENT_LOOP_H |
Christopher Haster |
8:3c2a014bd907 | 6 | #define EVENT_LOOP_H |
Christopher Haster |
8:3c2a014bd907 | 7 | #ifndef EVENTS_NO_RTOS |
Christopher Haster |
8:3c2a014bd907 | 8 | |
Christopher Haster |
8:3c2a014bd907 | 9 | #include "EventQueue.h" |
Christopher Haster |
8:3c2a014bd907 | 10 | #include "Thread.h" |
Christopher Haster |
8:3c2a014bd907 | 11 | |
Christopher Haster |
11:6721568592e5 | 12 | /** Managed queue coupled with its own thread |
Christopher Haster |
11:6721568592e5 | 13 | */ |
Christopher Haster |
8:3c2a014bd907 | 14 | class EventLoop : public EventQueue { |
Christopher Haster |
8:3c2a014bd907 | 15 | public: |
Christopher Haster |
8:3c2a014bd907 | 16 | /** Create an event loop without starting execution |
Christopher Haster |
14:5abf2ccf2dbf | 17 | * @param priority Initial priority of the thread |
Christopher Haster |
14:5abf2ccf2dbf | 18 | * (default: osPriorityNormal) |
Christopher Haster |
14:5abf2ccf2dbf | 19 | * @param event_count Number of events to allow enqueueing at once |
Christopher Haster |
14:5abf2ccf2dbf | 20 | * @param event_context Max size of arguments passed with an event |
Christopher Haster |
14:5abf2ccf2dbf | 21 | * @param stack_size Stack size (in bytes) requirements for the thread |
Christopher Haster |
14:5abf2ccf2dbf | 22 | * (default: DEFAULT_STACK_SIZE) |
Christopher Haster |
14:5abf2ccf2dbf | 23 | * @param stack_pointer Pointer to stack area to be used by the thread |
Christopher Haster |
14:5abf2ccf2dbf | 24 | * (default: NULL) |
Christopher Haster |
8:3c2a014bd907 | 25 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 26 | EventLoop(osPriority priority=osPriorityNormal, |
Christopher Haster |
14:5abf2ccf2dbf | 27 | unsigned event_count=32, |
Christopher Haster |
14:5abf2ccf2dbf | 28 | unsigned event_context=0, |
Christopher Haster |
14:5abf2ccf2dbf | 29 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
Christopher Haster |
14:5abf2ccf2dbf | 30 | unsigned char *stack_pointer=NULL); |
Christopher Haster |
8:3c2a014bd907 | 31 | |
Christopher Haster |
8:3c2a014bd907 | 32 | /** Create an event loop running in a dedicated thread |
Christopher Haster |
14:5abf2ccf2dbf | 33 | * @param start True to start on construction |
Christopher Haster |
8:3c2a014bd907 | 34 | * @param priority Initial priority of the thread |
Christopher Haster |
8:3c2a014bd907 | 35 | * (default: osPriorityNormal) |
Christopher Haster |
14:5abf2ccf2dbf | 36 | * @param event_count Number of events to allow enqueueing at once |
Christopher Haster |
14:5abf2ccf2dbf | 37 | * @param event_context Max size of arguments passed with an event |
Christopher Haster |
8:3c2a014bd907 | 38 | * @param stack_size Stack size (in bytes) requirements for the thread |
Christopher Haster |
8:3c2a014bd907 | 39 | * (default: DEFAULT_STACK_SIZE) |
Christopher Haster |
8:3c2a014bd907 | 40 | * @param stack_pointer Pointer to stack area to be used by the thread |
Christopher Haster |
8:3c2a014bd907 | 41 | * (default: NULL) |
Christopher Haster |
8:3c2a014bd907 | 42 | */ |
Christopher Haster |
8:3c2a014bd907 | 43 | EventLoop(bool start, |
Christopher Haster |
8:3c2a014bd907 | 44 | osPriority priority=osPriorityNormal, |
Christopher Haster |
14:5abf2ccf2dbf | 45 | unsigned event_count=32, |
Christopher Haster |
14:5abf2ccf2dbf | 46 | unsigned event_context=0, |
Christopher Haster |
8:3c2a014bd907 | 47 | uint32_t stack_size=DEFAULT_STACK_SIZE, |
Christopher Haster |
8:3c2a014bd907 | 48 | unsigned char *stack_pointer=NULL); |
Christopher Haster |
8:3c2a014bd907 | 49 | |
Christopher Haster |
8:3c2a014bd907 | 50 | /** Clean up event loop |
Christopher Haster |
8:3c2a014bd907 | 51 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 52 | virtual ~EventLoop(); |
Christopher Haster |
8:3c2a014bd907 | 53 | |
Christopher Haster |
8:3c2a014bd907 | 54 | /** Starts an event loop running in a dedicated thread |
Christopher Haster |
8:3c2a014bd907 | 55 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 56 | void start(); |
Christopher Haster |
8:3c2a014bd907 | 57 | |
Christopher Haster |
8:3c2a014bd907 | 58 | /** Stops an event loop cleanly, waiting for any currently executing events |
Christopher Haster |
8:3c2a014bd907 | 59 | */ |
Christopher Haster |
8:3c2a014bd907 | 60 | void stop(); |
Christopher Haster |
8:3c2a014bd907 | 61 | |
Christopher Haster |
8:3c2a014bd907 | 62 | private: |
Christopher Haster |
8:3c2a014bd907 | 63 | static void run(const void *p); |
Christopher Haster |
8:3c2a014bd907 | 64 | |
Christopher Haster |
8:3c2a014bd907 | 65 | bool _running; |
Christopher Haster |
14:5abf2ccf2dbf | 66 | osPriority _priority; |
Christopher Haster |
14:5abf2ccf2dbf | 67 | uint32_t _stack_size; |
Christopher Haster |
14:5abf2ccf2dbf | 68 | unsigned char *_stack_pointer; |
Christopher Haster |
8:3c2a014bd907 | 69 | rtos::Thread *_thread; |
Christopher Haster |
8:3c2a014bd907 | 70 | }; |
Christopher Haster |
8:3c2a014bd907 | 71 | |
Christopher Haster |
8:3c2a014bd907 | 72 | #endif |
Christopher Haster |
8:3c2a014bd907 | 73 | #endif |