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.
Diff: EventQueue.h
- Revision:
- 14:5abf2ccf2dbf
- Parent:
- 10:62767e708bb6
- Child:
- 15:92d7c0b8a0f5
--- a/EventQueue.h Fri Apr 22 22:32:35 2016 -0500 +++ b/EventQueue.h Tue May 10 07:51:44 2016 -0500 @@ -7,6 +7,8 @@ #include "Timer.h" #include "Ticker.h" +#include "FuncPtr.h" +#include "Binder.h" /** Flexible queue for managing events @@ -14,8 +16,12 @@ class EventQueue { public: /** Create an event queue + * @param event_count Number of events to allow enqueueing at once + * @param event_context Max size of arguments passed with an event */ - EventQueue(); + EventQueue(unsigned event_count=32, + unsigned event_context=0); + virtual ~EventQueue(); /** Dispatch pending events * @param ms Time to wait for events in milliseconds, @@ -33,22 +39,43 @@ struct event { struct event *volatile next; unsigned target; - bool registered; - - int delay; int period; - void (*callback)(void *); - void *data; + void (*dispatch)(void *p); + // data follows }; - void event_register(struct event *event, int ms); - void event_unregister(struct event *event); + struct event *alloc(unsigned size, int ms=-1); + void dealloc(struct event *); + + void trigger(struct event *e, int delay); + + template <typename F> + bool trigger(F func, int delay, int period, int tolerance, int ms=-1) { + struct event *e = alloc(sizeof(F), ms); + if (!e) { + return false; + } + + e->period = period; + (void)tolerance; // unused + e->dispatch = &F::thunk; + *reinterpret_cast<F*>(e+1) = func; + + trigger(e, delay); + return true; + } void tick(); void wakeup(); + unsigned _event_count; + unsigned _event_context; + struct event *volatile _queue; + struct event *volatile _free; + void *_mem; + unsigned _tick; mbed::Ticker _ticker; mbed::Timer _timer;