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:
12:1feb78280125
Parent:
4:30883e8633b4
Child:
13:b84e049b2d9c
--- a/Event.h	Fri Apr 22 20:57:16 2016 -0500
+++ b/Event.h	Fri Apr 22 22:18:55 2016 -0500
@@ -76,26 +76,38 @@
     }
 
     /** Set event to repeat periodically after it is posted
-     *  @param ms   period in milliseconds
+     *  @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
+     *  @param ms   Tolerance in milliseconds
      */
     void tolerance(int ms) {
         (void)ms; // currently ignored
     }
 
     /** Post the event onto the bound queue
+     *  @return     True if the event was posted successfully
      */
-    void call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
+    bool trigger(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
+        if (_event.registered) {
+            return false;
+        }
+
         _bind.attach(_callback, a0, a1, a2, a3, a4);
         _event.callback = Binder<void(A0, A1, A2, A3, A4), A0, A1, A2, A3, A4>::thunk;
         _event.data = &_bind;
         _queue->event_register(&_event, _event.delay);
+        return true;
+    }
+
+    /** Post the event onto the bound queue
+     */
+    void call(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
+        trigger(a0, a1, a2, a3, a4);
     }
 
     /** Post the event onto the bound queue
@@ -192,14 +204,14 @@
     }
 
     /** Set event to repeat periodically after it is posted
-     *  @param ms   period in milliseconds
+     *  @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
+     *  @param ms   Tolerance in milliseconds
      */
     void tolerance(int ms) {
         (void)ms; // currently ignored
@@ -207,11 +219,22 @@
 
     /** Post the event onto the bound queue
      */
-    void call(A0 a0, A1 a1, A2 a2, A3 a3) {
+    bool trigger(A0 a0, A1 a1, A2 a2, A3 a3) {
+        if (_event.registered) {
+            return false;
+        }
+
         _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, _event.delay);
+        return true;
+    }
+
+    /** Post the event onto the bound queue
+     */
+    void call(A0 a0, A1 a1, A2 a2, A3 a3) {
+        trigger(a0, a1, a2, a3);
     }
 
     /** Post the event onto the bound queue
@@ -308,14 +331,14 @@
     }
 
     /** Set event to repeat periodically after it is posted
-     *  @param ms   period in milliseconds
+     *  @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
+     *  @param ms   Tolerance in milliseconds
      */
     void tolerance(int ms) {
         (void)ms; // currently ignored
@@ -323,11 +346,22 @@
 
     /** Post the event onto the bound queue
      */
-    void call(A0 a0, A1 a1, A2 a2) {
+    bool trigger(A0 a0, A1 a1, A2 a2) {
+        if (_event.registered) {
+            return false;
+        }
+
         _bind.attach(_callback, a0, a1, a2);
         _event.callback = Binder<void(A0, A1, A2), A0, A1, A2>::thunk;
         _event.data = &_bind;
         _queue->event_register(&_event, _event.delay);
+        return true;
+    }
+
+    /** Post the event onto the bound queue
+     */
+    void call(A0 a0, A1 a1, A2 a2) {
+        trigger(a0, a1, a2);
     }
 
     /** Post the event onto the bound queue
@@ -424,14 +458,14 @@
     }
 
     /** Set event to repeat periodically after it is posted
-     *  @param ms   period in milliseconds
+     *  @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
+     *  @param ms   Tolerance in milliseconds
      */
     void tolerance(int ms) {
         (void)ms; // currently ignored
@@ -439,11 +473,22 @@
 
     /** Post the event onto the bound queue
      */
-    void call(A0 a0, A1 a1) {
+    bool trigger(A0 a0, A1 a1) {
+        if (_event.registered) {
+            return false;
+        }
+
         _bind.attach(_callback, a0, a1);
         _event.callback = Binder<void(A0, A1), A0, A1>::thunk;
         _event.data = &_bind;
         _queue->event_register(&_event, _event.delay);
+        return true;
+    }
+
+    /** Post the event onto the bound queue
+     */
+    void call(A0 a0, A1 a1) {
+        trigger(a0, a1);
     }
 
     /** Post the event onto the bound queue
@@ -540,14 +585,14 @@
     }
 
     /** Set event to repeat periodically after it is posted
-     *  @param ms   period in milliseconds
+     *  @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
+     *  @param ms   Tolerance in milliseconds
      */
     void tolerance(int ms) {
         (void)ms; // currently ignored
@@ -555,11 +600,22 @@
 
     /** Post the event onto the bound queue
      */
-    void call(A0 a0) {
+    bool trigger(A0 a0) {
+        if (_event.registered) {
+            return false;
+        }
+
         _bind.attach(_callback, a0);
         _event.callback = Binder<void(A0), A0>::thunk;
         _event.data = &_bind;
         _queue->event_register(&_event, _event.delay);
+        return true;
+    }
+
+    /** Post the event onto the bound queue
+     */
+    void call(A0 a0) {
+        trigger(a0);
     }
 
     /** Post the event onto the bound queue
@@ -656,14 +712,14 @@
     }
 
     /** Set event to repeat periodically after it is posted
-     *  @param ms   period in milliseconds
+     *  @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
+     *  @param ms   Tolerance in milliseconds
      */
     void tolerance(int ms) {
         (void)ms; // currently ignored
@@ -671,10 +727,21 @@
 
     /** Post the event onto the bound queue
      */
-    void call() {
+    bool trigger() {
+        if (_event.registered) {
+            return false;
+        }
+
         _event.callback = FuncPtr<void()>::thunk;
         _event.data = &_callback;
         _queue->event_register(&_event, _event.delay);
+        return true;
+    }
+
+    /** Post the event onto the bound queue
+     */
+    void call() {
+        trigger();
     }
 
     /** Post the event onto the bound queue