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:
Fri Apr 22 02:06:42 2016 -0500
Revision:
8:3c2a014bd907
Child:
14:5abf2ccf2dbf
Added thread based EventLoop under EVENTS_NO_RTOS define

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 8:3c2a014bd907 1 #include "EventLoop.h"
Christopher Haster 8:3c2a014bd907 2
Christopher Haster 8:3c2a014bd907 3 #ifndef EVENTS_NO_RTOS
Christopher Haster 8:3c2a014bd907 4
Christopher Haster 8:3c2a014bd907 5 using namespace rtos;
Christopher Haster 8:3c2a014bd907 6
Christopher Haster 8:3c2a014bd907 7 EventLoop::EventLoop() {
Christopher Haster 8:3c2a014bd907 8 _running = false;
Christopher Haster 8:3c2a014bd907 9 }
Christopher Haster 8:3c2a014bd907 10
Christopher Haster 8:3c2a014bd907 11 EventLoop::EventLoop(bool start,
Christopher Haster 8:3c2a014bd907 12 osPriority priority,
Christopher Haster 8:3c2a014bd907 13 uint32_t stack_size,
Christopher Haster 8:3c2a014bd907 14 unsigned char *stack_pointer) {
Christopher Haster 8:3c2a014bd907 15 _running = false;
Christopher Haster 8:3c2a014bd907 16
Christopher Haster 8:3c2a014bd907 17 if (start) {
Christopher Haster 8:3c2a014bd907 18 EventLoop::start(priority, stack_size, stack_pointer);
Christopher Haster 8:3c2a014bd907 19 }
Christopher Haster 8:3c2a014bd907 20 }
Christopher Haster 8:3c2a014bd907 21
Christopher Haster 8:3c2a014bd907 22 EventLoop::~EventLoop() {
Christopher Haster 8:3c2a014bd907 23 stop();
Christopher Haster 8:3c2a014bd907 24 }
Christopher Haster 8:3c2a014bd907 25
Christopher Haster 8:3c2a014bd907 26 void EventLoop::start(osPriority priority,
Christopher Haster 8:3c2a014bd907 27 uint32_t stack_size,
Christopher Haster 8:3c2a014bd907 28 unsigned char *stack_pointer) {
Christopher Haster 8:3c2a014bd907 29 if (_running) {
Christopher Haster 8:3c2a014bd907 30 return;
Christopher Haster 8:3c2a014bd907 31 }
Christopher Haster 8:3c2a014bd907 32
Christopher Haster 8:3c2a014bd907 33 _running = true;
Christopher Haster 8:3c2a014bd907 34 _thread = new Thread(run, this, priority, stack_size, stack_pointer);
Christopher Haster 8:3c2a014bd907 35 }
Christopher Haster 8:3c2a014bd907 36
Christopher Haster 8:3c2a014bd907 37 void EventLoop::stop() {
Christopher Haster 8:3c2a014bd907 38 if (!_running) {
Christopher Haster 8:3c2a014bd907 39 return;
Christopher Haster 8:3c2a014bd907 40 }
Christopher Haster 8:3c2a014bd907 41
Christopher Haster 8:3c2a014bd907 42 Thread *thread = _thread;
Christopher Haster 8:3c2a014bd907 43 _thread = 0;
Christopher Haster 8:3c2a014bd907 44
Christopher Haster 8:3c2a014bd907 45 _running = false;
Christopher Haster 8:3c2a014bd907 46 while (thread->get_state() != Thread::Inactive) {
Christopher Haster 8:3c2a014bd907 47 Thread::yield();
Christopher Haster 8:3c2a014bd907 48 }
Christopher Haster 8:3c2a014bd907 49
Christopher Haster 8:3c2a014bd907 50 delete thread;
Christopher Haster 8:3c2a014bd907 51 }
Christopher Haster 8:3c2a014bd907 52
Christopher Haster 8:3c2a014bd907 53 void EventLoop::run(const void *p) {
Christopher Haster 8:3c2a014bd907 54 EventLoop *loop = (EventLoop *)p;
Christopher Haster 8:3c2a014bd907 55
Christopher Haster 8:3c2a014bd907 56 while (loop->_running) {
Christopher Haster 8:3c2a014bd907 57 loop->dispatch(0);
Christopher Haster 8:3c2a014bd907 58 Thread::yield();
Christopher Haster 8:3c2a014bd907 59 }
Christopher Haster 8:3c2a014bd907 60 }
Christopher Haster 8:3c2a014bd907 61
Christopher Haster 8:3c2a014bd907 62 #endif