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.

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Mon Apr 18 13:22:21 2016 -0500
Parent:
19:86ffaa34870b
Commit message:
Add event_pointer argument to avoid memory allocation

Changed in this revision

EventLoop.cpp Show annotated file Show diff for this revision Revisions of this file
EventLoop.h Show annotated file Show diff for this revision Revisions of this file
EventQueue.cpp Show annotated file Show diff for this revision Revisions of this file
EventQueue.h Show annotated file Show diff for this revision Revisions of this file
diff -r 86ffaa34870b -r 2f9d9c53a5af EventLoop.cpp
--- a/EventLoop.cpp	Mon Apr 18 12:59:37 2016 -0500
+++ b/EventLoop.cpp	Mon Apr 18 13:22:21 2016 -0500
@@ -5,9 +5,10 @@
 EventLoop::EventLoop(osPriority priority,
                      unsigned event_count,
                      unsigned event_context,
+                     unsigned char *event_pointer,
                      uint32_t stack_size,
                      unsigned char *stack_pointer)
-        : EventQueue(event_count, event_context) {
+        : EventQueue(event_count, event_context, event_pointer) {
     _running = false;
     _priority = priority;
     _stack_size = stack_size;
@@ -18,9 +19,10 @@
                      osPriority priority,
                      unsigned event_count,
                      unsigned event_context,
+                     unsigned char *event_pointer,
                      uint32_t stack_size,
                      unsigned char *stack_pointer)
-        : EventQueue(event_count, event_context) {
+        : EventQueue(event_count, event_context, event_pointer) {
     _running = false;
     _priority = priority;
     _stack_size = stack_size;
diff -r 86ffaa34870b -r 2f9d9c53a5af EventLoop.h
--- a/EventLoop.h	Mon Apr 18 12:59:37 2016 -0500
+++ b/EventLoop.h	Mon Apr 18 13:22:21 2016 -0500
@@ -16,7 +16,11 @@
      *  @param priority         Initial priority of the thread
      *                          (default: osPriorityNormal)
      *  @param event_count      Number of events to allow enqueueing at once
+     *                          (default: 32)
      *  @param event_context    Max size of arguments passed with an event
+     *                          (default: 0)
+     *  @param event_pointer    Pointer to memory area to be used for events
+     *                          (default: NULL)
      *  @param stack_size       Stack size (in bytes) requirements for the thread
      *                          (default: DEFAULT_STACK_SIZE)
      *  @param stack_pointer    Pointer to stack area to be used by the thread
@@ -25,6 +29,7 @@
     EventLoop(osPriority priority=osPriorityNormal,
               unsigned event_count=32,
               unsigned event_context=0,
+              unsigned char *event_pointer=NULL,
               uint32_t stack_size=DEFAULT_STACK_SIZE,
               unsigned char *stack_pointer=NULL);
 
@@ -33,7 +38,11 @@
      *  @param priority         Initial priority of the thread
      *                          (default: osPriorityNormal)
      *  @param event_count      Number of events to allow enqueueing at once
+     *                          (default: 32)
      *  @param event_context    Max size of arguments passed with an event
+     *                          (default: 0)
+     *  @param event_pointer    Pointer to memory area to be used for events
+     *                          (default: NULL)
      *  @param stack_size       Stack size (in bytes) requirements for the thread
      *                          (default: DEFAULT_STACK_SIZE)
      *  @param stack_pointer    Pointer to stack area to be used by the thread
@@ -43,6 +52,7 @@
               osPriority priority=osPriorityNormal,
               unsigned event_count=32,
               unsigned event_context=0,
+              unsigned char *event_pointer=NULL,
               uint32_t stack_size=DEFAULT_STACK_SIZE,
               unsigned char *stack_pointer=NULL);
 
diff -r 86ffaa34870b -r 2f9d9c53a5af EventQueue.cpp
--- a/EventQueue.cpp	Mon Apr 18 12:59:37 2016 -0500
+++ b/EventQueue.cpp	Mon Apr 18 13:22:21 2016 -0500
@@ -17,18 +17,26 @@
 
 
 // Event queue definitions
-EventQueue::EventQueue(unsigned event_count, unsigned event_context) {
+EventQueue::EventQueue(unsigned event_count,
+                       unsigned event_context, 
+                       unsigned char *event_pointer) {
     _event_context = sizeof(FuncPtr<void()>) + event_context;
     unsigned event_size = sizeof(struct event) + _event_context;
-    _mem = malloc(event_count * event_size);
-    _free = (struct event*)_mem;
 
-    if (_mem) {
+    if (!event_pointer) {
+        _mem = malloc(event_count * event_size);
+        _free = (struct event*)_mem;
+    } else {
+        _mem = 0;
+        _free = (struct event*)event_pointer;
+    }
+
+    if (_free) {
         for (unsigned i = 0; i < event_count-1; i++) {
-            ((struct event*)((char*)_mem + i*event_size))->next =
-                    (struct event*)((char*)_mem + (i+1)*event_size);
+            ((struct event*)((char*)_free + i*event_size))->next =
+                    (struct event*)((char*)_free + (i+1)*event_size);
         }
-        ((struct event*)((char*)_mem + (event_count-1)*event_size))->next = 0;
+        ((struct event*)((char*)_free + (event_count-1)*event_size))->next = 0;
     }
 
     _queue = 0;
diff -r 86ffaa34870b -r 2f9d9c53a5af EventQueue.h
--- a/EventQueue.h	Mon Apr 18 12:59:37 2016 -0500
+++ b/EventQueue.h	Mon Apr 18 13:22:21 2016 -0500
@@ -17,10 +17,15 @@
 public:
     /** Create an event queue
      *  @param event_count      Number of events to allow enqueueing at once
+     *                          (default: 32)
      *  @param event_context    Max size of arguments passed with an event
+     *                          (default: 0)
+     *  @param event_pointer    Pointer to memory area to be used for events
+     *                          (default: NULL)
      */
     EventQueue(unsigned event_count=32,
-               unsigned event_context=0);
+               unsigned event_context=0,
+               unsigned char *event_pointer=NULL);
 
     /** Clean up event queue
      */
@@ -79,6 +84,7 @@
 
     unsigned _event_context;
     void *_mem;
+
     struct event *volatile _free;
     struct event *volatile _queue;