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.

EventQueue.cpp

Committer:
Christopher Haster
Date:
2016-04-20
Revision:
0:b1b901ae3696
Child:
1:2202c19570e5

File content as of revision 0:b1b901ae3696:

#include <EventQueue.h>
#include "mbed.h"


static void wakeup() {}

void EventQueue::dispatch(unsigned milli) {
    mbed::Timer timer;
    mbed::Timeout timeout;
    timer.start();
    timeout.attach_us(wakeup, milli * 1000);

    while (true) {
        while (_queue) {
            _queue->callback(_queue->data);
            _queue = _queue->next;
        }

        if ((unsigned)timer.read_ms() > milli) {
            break;
        }

        __WFI();
    }
}

void EventQueue::event_register(struct event *event) {
    __disable_irq();
    for (struct event **p = &_queue;; p = &(*p)->next) {
        if (!*p) {
            event->next = 0;
            *p = event;
            break;
        } else if (*p == event) {
            break;
        }
    }
    __enable_irq();
}

void EventQueue::event_unregister(struct event *event) {
    __disable_irq();
    for (struct event **p = &_queue; *p; p = &(*p)->next) {
        if (*p == event) {
            *p = event->next;
            break;
        }
    }
    __enable_irq();
}