11## The equeue library ##
22
33The equeue library is designed as a simple but powerful library for scheduling
4- events on composable event queues.
4+ events on composable queues.
55
66``` c
77#include " equeue.h"
88#include < stdio.h>
99
10- void print (void * s) {
11- puts((const char * )s);
12- }
13-
1410int main () {
1511 // creates a queue with space for 32 basic events
1612 equeue_t queue;
@@ -31,26 +27,26 @@ int main() {
3127```
3228
3329The equeue library can be used as a normal event loop, or it can be
34- backgrounded on a single hardware timer or even another event loop.
35- The equeue library is both thread and irq safe, and provides functions
36- for easily composing multiple queues.
30+ backgrounded on a single hardware timer or even another event loop. It
31+ is both thread and irq safe, and provides functions for easily composing
32+ multiple queues.
3733
3834The equeue library can act as a drop-in scheduler, provide synchronization
3935between multiple threads, or just act as a mechanism for moving events
4036out of interrupt contexts.
4137
4238## Documentation ##
4339
44- Unless it is elaborated, the in-depth documentation of the specific functions
45- can be found in [equeue.h](equeue.h).
40+ The in-depth documentation on specific functions can be found in
41+ [ equeue.h] ( equeue.h ) .
4642
4743The core of the equeue library is the ` equeue_t ` type which represents a
48- single event queue, and the `equeue_dispath ` function which runs the equeue,
44+ single event queue, and the ` equeue_dispatch ` function which runs the equeue,
4945providing the context for executing events.
5046
5147On top of this, ` equeue_call ` , ` equeue_call_in ` , and ` equeue_call_every `
52- provide an easy method of posting events to be executed in the context
53- of the `equeue_dispatch` function.
48+ provide easy methods for posting events to execute in the context of the
49+ ` equeue_dispatch ` function.
5450
5551``` c
5652#include " equeue.h"
@@ -77,30 +73,30 @@ int main() {
7773}
7874```
7975
80- In addition to simple events , an event can be manually allocated with
76+ In addition to simple callbacks , an event can be manually allocated with
8177`equeue_alloc` and posted with `equeue_post` to allow passing an arbitrary
82- amount of data to the execution of the event. This memory is allocated out
78+ amount of context to the execution of the event. This memory is allocated out
8379of the equeue's buffer, and dynamic memory can be completely avoided.
8480
8581The equeue allocator is designed to minimize jitter in interrupt contexts as
8682well as avoid memory fragmentation on small devices. The allocator achieves
8783both constant-runtime and zero-fragmentation for fixed-size events, however
88- grows linearly as the quantity of different sized allocations increases.
84+ grows linearly as the quantity of differently- sized allocations increases.
8985
9086``` c
9187#include "equeue.h"
9288
9389equeue_t queue;
9490
9591// arbitrary data can be moved to a different context
96- int enet_callback (void * buffer, int size) {
92+ int enet_consume (void *buffer, int size) {
9793 if (size > 512) {
9894 size = 512;
9995 }
10096
101- void *event = equeue_alloc(&queue, 512);
102- memcpy(event , buffer, size);
103- equeue_post(&queue, event );
97+ void *data = equeue_alloc(&queue, 512);
98+ memcpy(data , buffer, size);
99+ equeue_post(&queue, handle_data_elsewhere, data );
104100
105101 return size;
106102}
@@ -133,12 +129,11 @@ void sonar_read(void) {
133129
134130From an architectural standpoint, event queues easily align with module
135131boundaries, where internal state can be implicitly synchronized through
136- event registration. Multiple modules can easily use event queues running
137- in separate threads.
132+ event dispatch.
138133
139- Alternatively, multiple event queues can be easily composed through the
140- ` equeue_chain ` function, which allows multiple event queues to share the
141- context of a single ` equeue_dispatch ` call .
134+ On platforms where multiple threads are unavailable, multiple modules
135+ can use independent event queues and still be composed through the
136+ `equeue_chain` function .
142137
143138``` c
144139#include "equeue.h"
@@ -166,8 +161,7 @@ void sonar_create(struct sonar *s, equeue_t *target) {
166161 equeue_call_in(&s->queue, 5, sonar_update, s);
167162}
168163
169- // although the above is perfectly synchronized, we can run these
170- // modules on a single event queue
164+ // all of the above queues can be combined into a single thread of execution
171165int main() {
172166 equeue_t queue;
173167 equeue_create(&queue, 1024);
@@ -180,7 +174,7 @@ int main() {
180174 struct slam slam;
181175 slam_create(&slam, &queue);
182176
183- // dispatches events for all of the modules
177+ // dispatches events from all of the modules
184178 equeue_dispatch(&queue, -1);
185179}
186180```
0 commit comments