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: EventLoop.cpp
- Revision:
- 8:3c2a014bd907
- Child:
- 14:5abf2ccf2dbf
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EventLoop.cpp Fri Apr 22 02:06:42 2016 -0500 @@ -0,0 +1,62 @@ +#include "EventLoop.h" + +#ifndef EVENTS_NO_RTOS + +using namespace rtos; + +EventLoop::EventLoop() { + _running = false; +} + +EventLoop::EventLoop(bool start, + osPriority priority, + uint32_t stack_size, + unsigned char *stack_pointer) { + _running = false; + + if (start) { + EventLoop::start(priority, stack_size, stack_pointer); + } +} + +EventLoop::~EventLoop() { + stop(); +} + +void EventLoop::start(osPriority priority, + uint32_t stack_size, + unsigned char *stack_pointer) { + if (_running) { + return; + } + + _running = true; + _thread = new Thread(run, this, priority, stack_size, stack_pointer); +} + +void EventLoop::stop() { + if (!_running) { + return; + } + + Thread *thread = _thread; + _thread = 0; + + _running = false; + while (thread->get_state() != Thread::Inactive) { + Thread::yield(); + } + + delete thread; +} + +void EventLoop::run(const void *p) { + EventLoop *loop = (EventLoop *)p; + + while (loop->_running) { + loop->dispatch(0); + Thread::yield(); + } +} + +#endif