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.

Revision:
2:11cda6bead99
Parent:
0:b1b901ae3696
Child:
3:6dccdc36651f
--- a/Event.h	Wed Apr 20 12:40:18 2016 -0500
+++ b/Event.h	Wed Apr 20 14:15:54 2016 -0500
@@ -22,11 +22,18 @@
 public:
     /** Create an unbound event
      */
-    Event() {}
+    Event() {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
+    }
 
     /** Create an event bound to a queue
      */
     Event(EventQueue *queue, FuncPtr<void(A0, A1, A2, A3)> callback) {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
         attach(queue, callback);
     }
 
@@ -43,16 +50,37 @@
         _callback.attach(callback);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Set delay for when the event is dispatched after it is posted
+     *  @param ms   Delay in milliseconds
+     */
+    void delay(int ms) {
+        _event.delay = ms;
+    }
+
+    /** Set event to repeat periodically after it is posted
+     *  @param ms   period in milliseconds
+     */
+    void period(int ms) {
+        _event.period = ms;
+    }
+
+    /** Set tolerance hint on when the event must be called, defaults to 0
+     *  @param ms   tolerance in milliseconds
+     */
+    void tolerance(int ms) {
+        (void)ms; // currently ignored
+    }
+
+    /** Post the event onto the bound queue
      */
     void call(A0 a0, A1 a1, A2 a2, A3 a3) {
         _bind.attach(_callback, a0, a1, a2, a3);
         _event.callback = Binder<void(A0, A1, A2, A3), A0, A1, A2, A3>::thunk;
         _event.data = &_bind;
-        _queue->event_register(&_event);
+        _queue->event_register(&_event, _event.delay);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Post the event onto the bound queue
      */
     void operator()(A0 a0, A1 a1, A2 a2, A3 a3) {
         return call(a0, a1, a2, a3);
@@ -92,11 +120,18 @@
 public:
     /** Create an unbound event
      */
-    Event() {}
+    Event() {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
+    }
 
     /** Create an event bound to a queue
      */
     Event(EventQueue *queue, FuncPtr<void(A0, A1, A2)> callback) {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
         attach(queue, callback);
     }
 
@@ -113,16 +148,37 @@
         _callback.attach(callback);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Set delay for when the event is dispatched after it is posted
+     *  @param ms   Delay in milliseconds
+     */
+    void delay(int ms) {
+        _event.delay = ms;
+    }
+
+    /** Set event to repeat periodically after it is posted
+     *  @param ms   period in milliseconds
+     */
+    void period(int ms) {
+        _event.period = ms;
+    }
+
+    /** Set tolerance hint on when the event must be called, defaults to 0
+     *  @param ms   tolerance in milliseconds
+     */
+    void tolerance(int ms) {
+        (void)ms; // currently ignored
+    }
+
+    /** Post the event onto the bound queue
      */
     void call(A0 a0, A1 a1, A2 a2) {
         _bind.attach(_callback, a0, a1, a2);
         _event.callback = Binder<void(A0, A1, A2), A0, A1, A2>::thunk;
         _event.data = &_bind;
-        _queue->event_register(&_event);
+        _queue->event_register(&_event, _event.delay);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Post the event onto the bound queue
      */
     void operator()(A0 a0, A1 a1, A2 a2) {
         return call(a0, a1, a2);
@@ -162,11 +218,18 @@
 public:
     /** Create an unbound event
      */
-    Event() {}
+    Event() {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
+    }
 
     /** Create an event bound to a queue
      */
     Event(EventQueue *queue, FuncPtr<void(A0, A1)> callback) {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
         attach(queue, callback);
     }
 
@@ -183,16 +246,37 @@
         _callback.attach(callback);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Set delay for when the event is dispatched after it is posted
+     *  @param ms   Delay in milliseconds
+     */
+    void delay(int ms) {
+        _event.delay = ms;
+    }
+
+    /** Set event to repeat periodically after it is posted
+     *  @param ms   period in milliseconds
+     */
+    void period(int ms) {
+        _event.period = ms;
+    }
+
+    /** Set tolerance hint on when the event must be called, defaults to 0
+     *  @param ms   tolerance in milliseconds
+     */
+    void tolerance(int ms) {
+        (void)ms; // currently ignored
+    }
+
+    /** Post the event onto the bound queue
      */
     void call(A0 a0, A1 a1) {
         _bind.attach(_callback, a0, a1);
         _event.callback = Binder<void(A0, A1), A0, A1>::thunk;
         _event.data = &_bind;
-        _queue->event_register(&_event);
+        _queue->event_register(&_event, _event.delay);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Post the event onto the bound queue
      */
     void operator()(A0 a0, A1 a1) {
         return call(a0, a1);
@@ -232,11 +316,18 @@
 public:
     /** Create an unbound event
      */
-    Event() {}
+    Event() {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
+    }
 
     /** Create an event bound to a queue
      */
     Event(EventQueue *queue, FuncPtr<void(A0)> callback) {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
         attach(queue, callback);
     }
 
@@ -253,16 +344,37 @@
         _callback.attach(callback);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Set delay for when the event is dispatched after it is posted
+     *  @param ms   Delay in milliseconds
+     */
+    void delay(int ms) {
+        _event.delay = ms;
+    }
+
+    /** Set event to repeat periodically after it is posted
+     *  @param ms   period in milliseconds
+     */
+    void period(int ms) {
+        _event.period = ms;
+    }
+
+    /** Set tolerance hint on when the event must be called, defaults to 0
+     *  @param ms   tolerance in milliseconds
+     */
+    void tolerance(int ms) {
+        (void)ms; // currently ignored
+    }
+
+    /** Post the event onto the bound queue
      */
     void call(A0 a0) {
         _bind.attach(_callback, a0);
         _event.callback = Binder<void(A0), A0>::thunk;
         _event.data = &_bind;
-        _queue->event_register(&_event);
+        _queue->event_register(&_event, _event.delay);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Post the event onto the bound queue
      */
     void operator()(A0 a0) {
         return call(a0);
@@ -302,11 +414,18 @@
 public:
     /** Create an unbound event
      */
-    Event() {}
+    Event() {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
+    }
 
     /** Create an event bound to a queue
      */
     Event(EventQueue *queue, FuncPtr<void()> callback) {
+        memset(&_event, 0, sizeof _event);
+        _event.delay = 0;
+        _event.period = -1;
         attach(queue, callback);
     }
 
@@ -323,15 +442,36 @@
         _callback.attach(callback);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Set delay for when the event is dispatched after it is posted
+     *  @param ms   Delay in milliseconds
+     */
+    void delay(int ms) {
+        _event.delay = ms;
+    }
+
+    /** Set event to repeat periodically after it is posted
+     *  @param ms   period in milliseconds
+     */
+    void period(int ms) {
+        _event.period = ms;
+    }
+
+    /** Set tolerance hint on when the event must be called, defaults to 0
+     *  @param ms   tolerance in milliseconds
+     */
+    void tolerance(int ms) {
+        (void)ms; // currently ignored
+    }
+
+    /** Post the event onto the bound queue
      */
     void call() {
         _event.callback = FuncPtr<void()>::thunk;
         _event.data = &_callback;
-        _queue->event_register(&_event);
+        _queue->event_register(&_event, _event.delay);
     }
 
-    /** Dispatch the event onto the bound queue
+    /** Post the event onto the bound queue
      */
     void operator()() {
         return call();