Delta / Mbed OS Delta_CLI

Fork of NNN40_CLI by Delta

Committer:
silviaChen
Date:
Fri Nov 11 09:02:51 2016 +0000
Revision:
24:838a0b25934b
Parent:
17:03c8af30087a
CLI V1.18, modify ERROR message format

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tsungta 17:03c8af30087a 1 /*
tsungta 17:03c8af30087a 2 * Flexible event queue for dispatching events
tsungta 17:03c8af30087a 3 */
tsungta 17:03c8af30087a 4 #ifndef EVENTS_H
tsungta 17:03c8af30087a 5 #define EVENTS_H
tsungta 17:03c8af30087a 6
tsungta 17:03c8af30087a 7 #ifdef __cplusplus
tsungta 17:03c8af30087a 8 extern "C" {
tsungta 17:03c8af30087a 9 #endif
tsungta 17:03c8af30087a 10
tsungta 17:03c8af30087a 11 // System specific files
tsungta 17:03c8af30087a 12 #include "events_tick.h"
tsungta 17:03c8af30087a 13 #include "events_mutex.h"
tsungta 17:03c8af30087a 14 #include "events_sema.h"
tsungta 17:03c8af30087a 15
tsungta 17:03c8af30087a 16
tsungta 17:03c8af30087a 17 // Event/queue structures
tsungta 17:03c8af30087a 18 struct event {
tsungta 17:03c8af30087a 19 struct event *next;
tsungta 17:03c8af30087a 20 int id;
tsungta 17:03c8af30087a 21 unsigned target;
tsungta 17:03c8af30087a 22 int period;
tsungta 17:03c8af30087a 23 void (*dtor)(void *);
tsungta 17:03c8af30087a 24
tsungta 17:03c8af30087a 25 void (*cb)(void *);
tsungta 17:03c8af30087a 26 // data follows
tsungta 17:03c8af30087a 27 };
tsungta 17:03c8af30087a 28
tsungta 17:03c8af30087a 29 typedef struct equeue {
tsungta 17:03c8af30087a 30 struct event *queue;
tsungta 17:03c8af30087a 31 int next_id;
tsungta 17:03c8af30087a 32
tsungta 17:03c8af30087a 33 void *buffer;
tsungta 17:03c8af30087a 34 struct equeue_chunk {
tsungta 17:03c8af30087a 35 unsigned size;
tsungta 17:03c8af30087a 36 struct equeue_chunk *next;
tsungta 17:03c8af30087a 37 struct equeue_chunk *nchunk;
tsungta 17:03c8af30087a 38 } *chunks;
tsungta 17:03c8af30087a 39 struct equeue_slab {
tsungta 17:03c8af30087a 40 unsigned size;
tsungta 17:03c8af30087a 41 unsigned char *data;
tsungta 17:03c8af30087a 42 } slab;
tsungta 17:03c8af30087a 43
tsungta 17:03c8af30087a 44 struct event break_;
tsungta 17:03c8af30087a 45
tsungta 17:03c8af30087a 46 events_sema_t eventsema;
tsungta 17:03c8af30087a 47 events_mutex_t queuelock;
tsungta 17:03c8af30087a 48 events_mutex_t freelock;
tsungta 17:03c8af30087a 49 } equeue_t;
tsungta 17:03c8af30087a 50
tsungta 17:03c8af30087a 51 // Queue operations
tsungta 17:03c8af30087a 52 //
tsungta 17:03c8af30087a 53 // Creation results in negative value on failure.
tsungta 17:03c8af30087a 54 int equeue_create(equeue_t *queue, unsigned size);
tsungta 17:03c8af30087a 55 int equeue_create_inplace(equeue_t *queue, unsigned size, void *buffer);
tsungta 17:03c8af30087a 56 void equeue_destroy(equeue_t *queue);
tsungta 17:03c8af30087a 57
tsungta 17:03c8af30087a 58 // Dispatch events
tsungta 17:03c8af30087a 59 //
tsungta 17:03c8af30087a 60 // Executes any callbacks enqueued for the specified time in milliseconds,
tsungta 17:03c8af30087a 61 // or forever if ms is negative
tsungta 17:03c8af30087a 62 void equeue_dispatch(equeue_t *queue, int ms);
tsungta 17:03c8af30087a 63
tsungta 17:03c8af30087a 64 // Break a running event loop
tsungta 17:03c8af30087a 65 //
tsungta 17:03c8af30087a 66 // Shuts down an unbounded event loop. Already pending events may finish
tsungta 17:03c8af30087a 67 // executing, but the queue will not continue looping indefinitely.
tsungta 17:03c8af30087a 68 void equeue_break(equeue_t *queue);
tsungta 17:03c8af30087a 69
tsungta 17:03c8af30087a 70 // Simple event calls
tsungta 17:03c8af30087a 71 //
tsungta 17:03c8af30087a 72 // Passed callback will be executed in the associated equeue's
tsungta 17:03c8af30087a 73 // dispatch call with the data pointer passed unmodified
tsungta 17:03c8af30087a 74 //
tsungta 17:03c8af30087a 75 // event_call - Immediately post an event to the queue
tsungta 17:03c8af30087a 76 // event_call_in - Post an event after a specified time in milliseconds
tsungta 17:03c8af30087a 77 // event_call_every - Post an event periodically in milliseconds
tsungta 17:03c8af30087a 78 //
tsungta 17:03c8af30087a 79 // These calls will result in 0 if no memory is available, otherwise they
tsungta 17:03c8af30087a 80 // will result in a unique identifier that can be passed to event_cancel.
tsungta 17:03c8af30087a 81 int event_call(equeue_t *queue, void (*cb)(void *), void *data);
tsungta 17:03c8af30087a 82 int event_call_in(equeue_t *queue, int ms, void (*cb)(void *), void *data);
tsungta 17:03c8af30087a 83 int event_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data);
tsungta 17:03c8af30087a 84
tsungta 17:03c8af30087a 85 // Events with queue handled blocks of memory
tsungta 17:03c8af30087a 86 //
tsungta 17:03c8af30087a 87 // Argument to event_post must point to a result of a event_alloc call
tsungta 17:03c8af30087a 88 // and the associated memory is automatically freed after the event
tsungta 17:03c8af30087a 89 // is dispatched.
tsungta 17:03c8af30087a 90 //
tsungta 17:03c8af30087a 91 // event_alloc will result in null if no memory is available
tsungta 17:03c8af30087a 92 // or the requested size is less than the size passed to equeue_create.
tsungta 17:03c8af30087a 93 void *event_alloc(equeue_t *queue, unsigned size);
tsungta 17:03c8af30087a 94 void event_dealloc(equeue_t *queue, void *event);
tsungta 17:03c8af30087a 95
tsungta 17:03c8af30087a 96 // Configure an allocated event
tsungta 17:03c8af30087a 97 //
tsungta 17:03c8af30087a 98 // event_delay - Specify a millisecond delay before posting an event
tsungta 17:03c8af30087a 99 // event_period - Specify a millisecond period to repeatedly post an event
tsungta 17:03c8af30087a 100 // event_dtor - Specify a destructor to run before the memory is deallocated
tsungta 17:03c8af30087a 101 void event_delay(void *event, int ms);
tsungta 17:03c8af30087a 102 void event_period(void *event, int ms);
tsungta 17:03c8af30087a 103 void event_dtor(void *event, void (*dtor)(void *));
tsungta 17:03c8af30087a 104
tsungta 17:03c8af30087a 105 // Post an allocted event to the event queue
tsungta 17:03c8af30087a 106 //
tsungta 17:03c8af30087a 107 // Argument to event_post must point to a result of a event_alloc call
tsungta 17:03c8af30087a 108 // and the associated memory is automatically freed after the event
tsungta 17:03c8af30087a 109 // is dispatched.
tsungta 17:03c8af30087a 110 //
tsungta 17:03c8af30087a 111 // This call results in an unique identifier that can be passed to
tsungta 17:03c8af30087a 112 // event_cancel.
tsungta 17:03c8af30087a 113 int event_post(equeue_t *queue, void (*cb)(void *), void *event);
tsungta 17:03c8af30087a 114
tsungta 17:03c8af30087a 115 // Cancel events that are in flight
tsungta 17:03c8af30087a 116 //
tsungta 17:03c8af30087a 117 // Every event_call function returns a non-negative identifier on success
tsungta 17:03c8af30087a 118 // that can be used to cancel an in-flight event. If the event has already
tsungta 17:03c8af30087a 119 // been dispatched or does not exist, no error occurs. Note, this can not
tsungta 17:03c8af30087a 120 // stop a currently executing event
tsungta 17:03c8af30087a 121 void event_cancel(equeue_t *queue, int event);
tsungta 17:03c8af30087a 122
tsungta 17:03c8af30087a 123
tsungta 17:03c8af30087a 124 #ifdef __cplusplus
tsungta 17:03c8af30087a 125 }
tsungta 17:03c8af30087a 126 #endif
tsungta 17:03c8af30087a 127
tsungta 17:03c8af30087a 128 #endif
tsungta 17:03c8af30087a 129