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 09:47:21 2016 -0500
Revision:
16:ff5d48fcce1b
Parent:
14:5abf2ccf2dbf
Child:
20:2f9d9c53a5af
Make rtos requirement

Move to yield over wfi
Remove problematic timeout logic as optimization

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 14:5abf2ccf2dbf 19 * @param event_context Max size of arguments passed with an event
Christopher Haster 14:5abf2ccf2dbf 20 * @param stack_size Stack size (in bytes) requirements for the thread
Christopher Haster 14:5abf2ccf2dbf 21 * (default: DEFAULT_STACK_SIZE)
Christopher Haster 14:5abf2ccf2dbf 22 * @param stack_pointer Pointer to stack area to be used by the thread
Christopher Haster 14:5abf2ccf2dbf 23 * (default: NULL)
Christopher Haster 8:3c2a014bd907 24 */
Christopher Haster 14:5abf2ccf2dbf 25 EventLoop(osPriority priority=osPriorityNormal,
Christopher Haster 14:5abf2ccf2dbf 26 unsigned event_count=32,
Christopher Haster 14:5abf2ccf2dbf 27 unsigned event_context=0,
Christopher Haster 14:5abf2ccf2dbf 28 uint32_t stack_size=DEFAULT_STACK_SIZE,
Christopher Haster 14:5abf2ccf2dbf 29 unsigned char *stack_pointer=NULL);
Christopher Haster 8:3c2a014bd907 30
Christopher Haster 8:3c2a014bd907 31 /** Create an event loop running in a dedicated thread
Christopher Haster 14:5abf2ccf2dbf 32 * @param start True to start on construction
Christopher Haster 8:3c2a014bd907 33 * @param priority Initial priority of the thread
Christopher Haster 8:3c2a014bd907 34 * (default: osPriorityNormal)
Christopher Haster 14:5abf2ccf2dbf 35 * @param event_count Number of events to allow enqueueing at once
Christopher Haster 14:5abf2ccf2dbf 36 * @param event_context Max size of arguments passed with an event
Christopher Haster 8:3c2a014bd907 37 * @param stack_size Stack size (in bytes) requirements for the thread
Christopher Haster 8:3c2a014bd907 38 * (default: DEFAULT_STACK_SIZE)
Christopher Haster 8:3c2a014bd907 39 * @param stack_pointer Pointer to stack area to be used by the thread
Christopher Haster 8:3c2a014bd907 40 * (default: NULL)
Christopher Haster 8:3c2a014bd907 41 */
Christopher Haster 8:3c2a014bd907 42 EventLoop(bool start,
Christopher Haster 8:3c2a014bd907 43 osPriority priority=osPriorityNormal,
Christopher Haster 14:5abf2ccf2dbf 44 unsigned event_count=32,
Christopher Haster 14:5abf2ccf2dbf 45 unsigned event_context=0,
Christopher Haster 8:3c2a014bd907 46 uint32_t stack_size=DEFAULT_STACK_SIZE,
Christopher Haster 8:3c2a014bd907 47 unsigned char *stack_pointer=NULL);
Christopher Haster 8:3c2a014bd907 48
Christopher Haster 8:3c2a014bd907 49 /** Clean up event loop
Christopher Haster 8:3c2a014bd907 50 */
Christopher Haster 16:ff5d48fcce1b 51 ~EventLoop();
Christopher Haster 8:3c2a014bd907 52
Christopher Haster 8:3c2a014bd907 53 /** Starts an event loop running in a dedicated thread
Christopher Haster 8:3c2a014bd907 54 */
Christopher Haster 16:ff5d48fcce1b 55 osStatus start();
Christopher Haster 8:3c2a014bd907 56
Christopher Haster 8:3c2a014bd907 57 /** Stops an event loop cleanly, waiting for any currently executing events
Christopher Haster 8:3c2a014bd907 58 */
Christopher Haster 16:ff5d48fcce1b 59 osStatus stop();
Christopher Haster 8:3c2a014bd907 60
Christopher Haster 8:3c2a014bd907 61 private:
Christopher Haster 8:3c2a014bd907 62 static void run(const void *p);
Christopher Haster 8:3c2a014bd907 63
Christopher Haster 8:3c2a014bd907 64 bool _running;
Christopher Haster 14:5abf2ccf2dbf 65 osPriority _priority;
Christopher Haster 14:5abf2ccf2dbf 66 uint32_t _stack_size;
Christopher Haster 14:5abf2ccf2dbf 67 unsigned char *_stack_pointer;
Christopher Haster 8:3c2a014bd907 68 rtos::Thread *_thread;
Christopher Haster 8:3c2a014bd907 69 };
Christopher Haster 8:3c2a014bd907 70
Christopher Haster 8:3c2a014bd907 71 #endif