Note! This project has moved to github.com/armmbed/mbed-events

Dependents:   SimpleHTTPExample

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.

Committer:
Christopher Haster
Date:
Mon Apr 18 13:22:21 2016 -0500
Revision:
20:2f9d9c53a5af
Parent:
16:ff5d48fcce1b
Add event_pointer argument to avoid memory allocation

Who changed what in which revision?

UserRevisionLine numberNew 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
Christopher Haster 8:3c2a014bd907 8 #include "EventQueue.h"
Christopher Haster 8:3c2a014bd907 9 #include "Thread.h"
Christopher Haster 8:3c2a014bd907 10
Christopher Haster 11:6721568592e5 11 /** Managed queue coupled with its own thread
Christopher Haster 11:6721568592e5 12 */
Christopher Haster 8:3c2a014bd907 13 class EventLoop : public EventQueue {
Christopher Haster 8:3c2a014bd907 14 public:
Christopher Haster 8:3c2a014bd907 15 /** Create an event loop without starting execution
Christopher Haster 14:5abf2ccf2dbf 16 * @param priority Initial priority of the thread
Christopher Haster 14:5abf2ccf2dbf 17 * (default: osPriorityNormal)
Christopher Haster 14:5abf2ccf2dbf 18 * @param event_count Number of events to allow enqueueing at once
Christopher Haster 20:2f9d9c53a5af 19 * (default: 32)
Christopher Haster 14:5abf2ccf2dbf 20 * @param event_context Max size of arguments passed with an event
Christopher Haster 20:2f9d9c53a5af 21 * (default: 0)
Christopher Haster 20:2f9d9c53a5af 22 * @param event_pointer Pointer to memory area to be used for events
Christopher Haster 20:2f9d9c53a5af 23 * (default: NULL)
Christopher Haster 14:5abf2ccf2dbf 24 * @param stack_size Stack size (in bytes) requirements for the thread
Christopher Haster 14:5abf2ccf2dbf 25 * (default: DEFAULT_STACK_SIZE)
Christopher Haster 14:5abf2ccf2dbf 26 * @param stack_pointer Pointer to stack area to be used by the thread
Christopher Haster 14:5abf2ccf2dbf 27 * (default: NULL)
Christopher Haster 8:3c2a014bd907 28 */
Christopher Haster 14:5abf2ccf2dbf 29 EventLoop(osPriority priority=osPriorityNormal,
Christopher Haster 14:5abf2ccf2dbf 30 unsigned event_count=32,
Christopher Haster 14:5abf2ccf2dbf 31 unsigned event_context=0,
Christopher Haster 20:2f9d9c53a5af 32 unsigned char *event_pointer=NULL,
Christopher Haster 14:5abf2ccf2dbf 33 uint32_t stack_size=DEFAULT_STACK_SIZE,
Christopher Haster 14:5abf2ccf2dbf 34 unsigned char *stack_pointer=NULL);
Christopher Haster 8:3c2a014bd907 35
Christopher Haster 8:3c2a014bd907 36 /** Create an event loop running in a dedicated thread
Christopher Haster 14:5abf2ccf2dbf 37 * @param start True to start on construction
Christopher Haster 8:3c2a014bd907 38 * @param priority Initial priority of the thread
Christopher Haster 8:3c2a014bd907 39 * (default: osPriorityNormal)
Christopher Haster 14:5abf2ccf2dbf 40 * @param event_count Number of events to allow enqueueing at once
Christopher Haster 20:2f9d9c53a5af 41 * (default: 32)
Christopher Haster 14:5abf2ccf2dbf 42 * @param event_context Max size of arguments passed with an event
Christopher Haster 20:2f9d9c53a5af 43 * (default: 0)
Christopher Haster 20:2f9d9c53a5af 44 * @param event_pointer Pointer to memory area to be used for events
Christopher Haster 20:2f9d9c53a5af 45 * (default: NULL)
Christopher Haster 8:3c2a014bd907 46 * @param stack_size Stack size (in bytes) requirements for the thread
Christopher Haster 8:3c2a014bd907 47 * (default: DEFAULT_STACK_SIZE)
Christopher Haster 8:3c2a014bd907 48 * @param stack_pointer Pointer to stack area to be used by the thread
Christopher Haster 8:3c2a014bd907 49 * (default: NULL)
Christopher Haster 8:3c2a014bd907 50 */
Christopher Haster 8:3c2a014bd907 51 EventLoop(bool start,
Christopher Haster 8:3c2a014bd907 52 osPriority priority=osPriorityNormal,
Christopher Haster 14:5abf2ccf2dbf 53 unsigned event_count=32,
Christopher Haster 14:5abf2ccf2dbf 54 unsigned event_context=0,
Christopher Haster 20:2f9d9c53a5af 55 unsigned char *event_pointer=NULL,
Christopher Haster 8:3c2a014bd907 56 uint32_t stack_size=DEFAULT_STACK_SIZE,
Christopher Haster 8:3c2a014bd907 57 unsigned char *stack_pointer=NULL);
Christopher Haster 8:3c2a014bd907 58
Christopher Haster 8:3c2a014bd907 59 /** Clean up event loop
Christopher Haster 8:3c2a014bd907 60 */
Christopher Haster 16:ff5d48fcce1b 61 ~EventLoop();
Christopher Haster 8:3c2a014bd907 62
Christopher Haster 8:3c2a014bd907 63 /** Starts an event loop running in a dedicated thread
Christopher Haster 8:3c2a014bd907 64 */
Christopher Haster 16:ff5d48fcce1b 65 osStatus start();
Christopher Haster 8:3c2a014bd907 66
Christopher Haster 8:3c2a014bd907 67 /** Stops an event loop cleanly, waiting for any currently executing events
Christopher Haster 8:3c2a014bd907 68 */
Christopher Haster 16:ff5d48fcce1b 69 osStatus stop();
Christopher Haster 8:3c2a014bd907 70
Christopher Haster 8:3c2a014bd907 71 private:
Christopher Haster 8:3c2a014bd907 72 static void run(const void *p);
Christopher Haster 8:3c2a014bd907 73
Christopher Haster 8:3c2a014bd907 74 bool _running;
Christopher Haster 14:5abf2ccf2dbf 75 osPriority _priority;
Christopher Haster 14:5abf2ccf2dbf 76 uint32_t _stack_size;
Christopher Haster 14:5abf2ccf2dbf 77 unsigned char *_stack_pointer;
Christopher Haster 8:3c2a014bd907 78 rtos::Thread *_thread;
Christopher Haster 8:3c2a014bd907 79 };
Christopher Haster 8:3c2a014bd907 80
Christopher Haster 8:3c2a014bd907 81 #endif