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.
EventQueue.h@16:ff5d48fcce1b, 2016-04-18 (annotated)
- Committer:
- Christopher Haster
- Date:
- Mon Apr 18 09:47:21 2016 -0500
- Revision:
- 16:ff5d48fcce1b
- Parent:
- 15:92d7c0b8a0f5
- Child:
- 17:6d564266850e
Make rtos requirement
Move to yield over wfi
Remove problematic timeout logic as optimization
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
0:b1b901ae3696 | 1 | /* EventQueue |
Christopher Haster |
0:b1b901ae3696 | 2 | * |
Christopher Haster |
0:b1b901ae3696 | 3 | * Flexible queue for managing events |
Christopher Haster |
0:b1b901ae3696 | 4 | */ |
Christopher Haster |
0:b1b901ae3696 | 5 | #ifndef EVENT_QUEUE_H |
Christopher Haster |
0:b1b901ae3696 | 6 | #define EVENT_QUEUE_H |
Christopher Haster |
0:b1b901ae3696 | 7 | |
Christopher Haster |
10:62767e708bb6 | 8 | #include "Timer.h" |
Christopher Haster |
10:62767e708bb6 | 9 | #include "Ticker.h" |
Christopher Haster |
14:5abf2ccf2dbf | 10 | #include "FuncPtr.h" |
Christopher Haster |
14:5abf2ccf2dbf | 11 | #include "Binder.h" |
Christopher Haster |
2:11cda6bead99 | 12 | |
Christopher Haster |
0:b1b901ae3696 | 13 | |
Christopher Haster |
0:b1b901ae3696 | 14 | /** Flexible queue for managing events |
Christopher Haster |
0:b1b901ae3696 | 15 | */ |
Christopher Haster |
0:b1b901ae3696 | 16 | class EventQueue { |
Christopher Haster |
0:b1b901ae3696 | 17 | public: |
Christopher Haster |
2:11cda6bead99 | 18 | /** Create an event queue |
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 |
2:11cda6bead99 | 21 | */ |
Christopher Haster |
14:5abf2ccf2dbf | 22 | EventQueue(unsigned event_count=32, |
Christopher Haster |
14:5abf2ccf2dbf | 23 | unsigned event_context=0); |
Christopher Haster |
16:ff5d48fcce1b | 24 | |
Christopher Haster |
16:ff5d48fcce1b | 25 | /** Clean up event queue |
Christopher Haster |
16:ff5d48fcce1b | 26 | */ |
Christopher Haster |
16:ff5d48fcce1b | 27 | ~EventQueue(); |
Christopher Haster |
2:11cda6bead99 | 28 | |
Christopher Haster |
0:b1b901ae3696 | 29 | /** Dispatch pending events |
Christopher Haster |
1:2202c19570e5 | 30 | * @param ms Time to wait for events in milliseconds, |
Christopher Haster |
0:b1b901ae3696 | 31 | * 0 indicates to return immediately if no events are pending |
Christopher Haster |
0:b1b901ae3696 | 32 | */ |
Christopher Haster |
1:2202c19570e5 | 33 | void dispatch(int ms=-1); |
Christopher Haster |
0:b1b901ae3696 | 34 | |
Christopher Haster |
2:11cda6bead99 | 35 | /** Get current tick of the event queue |
Christopher Haster |
2:11cda6bead99 | 36 | * @return Number of milliseconds since the queue was instantiated, |
Christopher Haster |
2:11cda6bead99 | 37 | * this count intentionally overflows to 0 after 2^32-1 |
Christopher Haster |
2:11cda6bead99 | 38 | */ |
Christopher Haster |
2:11cda6bead99 | 39 | unsigned get_tick(); |
Christopher Haster |
2:11cda6bead99 | 40 | |
Christopher Haster |
16:ff5d48fcce1b | 41 | /** Determine if tick has been passed |
Christopher Haster |
16:ff5d48fcce1b | 42 | * @param Tick to check |
Christopher Haster |
16:ff5d48fcce1b | 43 | * @param True if tick has been passed |
Christopher Haster |
16:ff5d48fcce1b | 44 | */ |
Christopher Haster |
16:ff5d48fcce1b | 45 | bool past_tick(unsigned tick); |
Christopher Haster |
16:ff5d48fcce1b | 46 | |
Christopher Haster |
0:b1b901ae3696 | 47 | private: |
Christopher Haster |
0:b1b901ae3696 | 48 | struct event { |
Christopher Haster |
1:2202c19570e5 | 49 | struct event *volatile next; |
Christopher Haster |
2:11cda6bead99 | 50 | unsigned target; |
Christopher Haster |
2:11cda6bead99 | 51 | int period; |
Christopher Haster |
2:11cda6bead99 | 52 | |
Christopher Haster |
14:5abf2ccf2dbf | 53 | void (*dispatch)(void *p); |
Christopher Haster |
14:5abf2ccf2dbf | 54 | // data follows |
Christopher Haster |
0:b1b901ae3696 | 55 | }; |
Christopher Haster |
0:b1b901ae3696 | 56 | |
Christopher Haster |
14:5abf2ccf2dbf | 57 | struct event *alloc(unsigned size, int ms=-1); |
Christopher Haster |
14:5abf2ccf2dbf | 58 | void dealloc(struct event *); |
Christopher Haster |
14:5abf2ccf2dbf | 59 | |
Christopher Haster |
14:5abf2ccf2dbf | 60 | void trigger(struct event *e, int delay); |
Christopher Haster |
14:5abf2ccf2dbf | 61 | |
Christopher Haster |
14:5abf2ccf2dbf | 62 | template <typename F> |
Christopher Haster |
14:5abf2ccf2dbf | 63 | bool trigger(F func, int delay, int period, int tolerance, int ms=-1) { |
Christopher Haster |
14:5abf2ccf2dbf | 64 | struct event *e = alloc(sizeof(F), ms); |
Christopher Haster |
14:5abf2ccf2dbf | 65 | if (!e) { |
Christopher Haster |
14:5abf2ccf2dbf | 66 | return false; |
Christopher Haster |
14:5abf2ccf2dbf | 67 | } |
Christopher Haster |
14:5abf2ccf2dbf | 68 | |
Christopher Haster |
14:5abf2ccf2dbf | 69 | e->period = period; |
Christopher Haster |
14:5abf2ccf2dbf | 70 | (void)tolerance; // unused |
Christopher Haster |
14:5abf2ccf2dbf | 71 | e->dispatch = &F::thunk; |
Christopher Haster |
14:5abf2ccf2dbf | 72 | *reinterpret_cast<F*>(e+1) = func; |
Christopher Haster |
14:5abf2ccf2dbf | 73 | |
Christopher Haster |
14:5abf2ccf2dbf | 74 | trigger(e, delay); |
Christopher Haster |
14:5abf2ccf2dbf | 75 | return true; |
Christopher Haster |
14:5abf2ccf2dbf | 76 | } |
Christopher Haster |
0:b1b901ae3696 | 77 | |
Christopher Haster |
2:11cda6bead99 | 78 | void tick(); |
Christopher Haster |
2:11cda6bead99 | 79 | |
Christopher Haster |
14:5abf2ccf2dbf | 80 | unsigned _event_context; |
Christopher Haster |
16:ff5d48fcce1b | 81 | void *_mem; |
Christopher Haster |
16:ff5d48fcce1b | 82 | struct event *volatile _free; |
Christopher Haster |
2:11cda6bead99 | 83 | struct event *volatile _queue; |
Christopher Haster |
14:5abf2ccf2dbf | 84 | |
Christopher Haster |
2:11cda6bead99 | 85 | unsigned _tick; |
Christopher Haster |
2:11cda6bead99 | 86 | mbed::Ticker _ticker; |
Christopher Haster |
2:11cda6bead99 | 87 | mbed::Timer _timer; |
Christopher Haster |
2:11cda6bead99 | 88 | |
Christopher Haster |
0:b1b901ae3696 | 89 | template <typename F> |
Christopher Haster |
0:b1b901ae3696 | 90 | friend class Event; |
Christopher Haster |
0:b1b901ae3696 | 91 | }; |
Christopher Haster |
0:b1b901ae3696 | 92 | |
Christopher Haster |
0:b1b901ae3696 | 93 | |
Christopher Haster |
0:b1b901ae3696 | 94 | #endif |