Skip to content
This repository was archived by the owner on Aug 19, 2021. It is now read-only.

Commit ee453c9

Browse files
committed
Rename event -> equeue for functions operating on queues
Indicates primary object these functions operate on, which is the queue. event_call -> equeue_call event_call_in -> equeue_call_in event_call_every -> equeue_call_every event_alloc -> equeue_alloc event_dealloc -> equeue_dealloc event_post -> equeue_post event_cancel -> equeue_cancel These were initially prefixed with event, since they associated with single events in the queue. However it became quickly apparent that this just led to confusion. Unfortunately, the event prefix hasn't been completely removed and remains in the funtions that directly operate on events: - event_delay - event_period - event_dtor
1 parent ed105c9 commit ee453c9

File tree

5 files changed

+132
-137
lines changed

5 files changed

+132
-137
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ int main() {
1818
equeue_create(&queue, 32*EVENTS_EVENT_SIZE);
1919

2020
// events are simple callbacks
21-
event_call(&queue, print, "called immediately");
22-
event_call_in(&queue, print, "called in 2 seconds", 2000);
23-
event_call_every(&queue, print, "called every 1 seconds", 1000);
21+
equeue_call(&queue, print, "called immediately");
22+
equeue_call_in(&queue, print, "called in 2 seconds", 2000);
23+
equeue_call_every(&queue, print, "called every 1 seconds", 1000);
2424

2525
// events are executed when dispatch is called
2626
equeue_dispatch(&queue, 3000);

events.c

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void equeue_destroy(equeue_t *q) {
5757
while (q->queue) {
5858
struct event *e = q->queue;
5959
q->queue = e->next;
60-
event_dealloc(q, e+1);
60+
equeue_dealloc(q, e+1);
6161
}
6262

6363
events_mutex_destroy(&q->freelock);
@@ -67,7 +67,7 @@ void equeue_destroy(equeue_t *q) {
6767
}
6868

6969
// equeue allocation functions
70-
static void *equeue_alloc(equeue_t *q, unsigned size) {
70+
static void *equeue_mem_alloc(equeue_t *q, unsigned size) {
7171
size = size + sizeof(unsigned);
7272
size = (size + sizeof(unsigned)-1) & ~(sizeof(unsigned)-1);
7373
if (size < sizeof(struct equeue_chunk)) {
@@ -103,7 +103,7 @@ static void *equeue_alloc(equeue_t *q, unsigned size) {
103103
return 0;
104104
}
105105

106-
static void equeue_dealloc(equeue_t *q, void *e) {
106+
static void equeue_mem_dealloc(equeue_t *q, void *e) {
107107
struct equeue_chunk *c = (struct equeue_chunk *)((unsigned *)e - 1);
108108

109109
events_mutex_lock(&q->freelock);
@@ -126,36 +126,36 @@ static void equeue_dealloc(equeue_t *q, void *e) {
126126
}
127127

128128
// event allocation functions
129-
static inline int event_next_id(equeue_t *q) {
129+
static inline int equeue_next_id(equeue_t *q) {
130130
int id = q->next_id++;
131131
if (q->next_id < 0) {
132132
q->next_id = 42;
133133
}
134134
return id;
135135
}
136136

137-
void *event_alloc(equeue_t *q, unsigned size) {
138-
struct event *e = equeue_alloc(q, sizeof(struct event) + size);
137+
void *equeue_alloc(equeue_t *q, unsigned size) {
138+
struct event *e = equeue_mem_alloc(q, sizeof(struct event) + size);
139139
if (!e) {
140140
return 0;
141141
}
142142

143-
e->id = event_next_id(q);
143+
e->id = equeue_next_id(q);
144144
e->target = 0;
145145
e->period = -1;
146146
e->dtor = 0;
147147

148148
return e + 1;
149149
}
150150

151-
void event_dealloc(equeue_t *q, void *p) {
151+
void equeue_dealloc(equeue_t *q, void *p) {
152152
struct event *e = (struct event*)p - 1;
153153

154154
if (e->dtor) {
155155
e->dtor(e+1);
156156
}
157157

158-
equeue_dealloc(q, e);
158+
equeue_mem_dealloc(q, e);
159159
}
160160

161161
// equeue scheduling functions
@@ -187,10 +187,10 @@ static struct event *equeue_dequeue(equeue_t *q, int id) {
187187
return 0;
188188
}
189189

190-
static int equeue_post(equeue_t *q, struct event *e, int ms) {
190+
static int equeue_post_in(equeue_t *q, struct event *e, int ms) {
191191
int id = e->id;
192192
if (ms < 0) {
193-
event_dealloc(q, e+1);
193+
equeue_dealloc(q, e+1);
194194
return id;
195195
}
196196

@@ -202,23 +202,30 @@ static int equeue_post(equeue_t *q, struct event *e, int ms) {
202202
return id;
203203
}
204204

205-
static void equeue_cancel(equeue_t *q, int id) {
205+
int equeue_post(equeue_t *q, void (*cb)(void*), void *p) {
206+
struct event *e = (struct event*)p - 1;
207+
e->cb = cb;
208+
int id = equeue_post_in(q, e, e->target);
209+
return id;
210+
}
211+
212+
void equeue_cancel(equeue_t *q, int id) {
206213
events_mutex_lock(&q->queuelock);
207214
struct event *e = equeue_dequeue(q, id);
208215
events_mutex_unlock(&q->queuelock);
209216

210217
if (e) {
211-
event_dealloc(q, e+1);
218+
equeue_dealloc(q, e+1);
212219
}
213220
}
214221

215222
void equeue_break(equeue_t *q) {
216-
equeue_post(q, &q->break_, 0);
223+
equeue_post_in(q, &q->break_, 0);
217224
}
218225

219226
void equeue_dispatch(equeue_t *q, int ms) {
220227
if (ms >= 0) {
221-
equeue_post(q, &q->break_, ms);
228+
equeue_post_in(q, &q->break_, ms);
222229
}
223230

224231
while (1) {
@@ -244,7 +251,7 @@ void equeue_dispatch(equeue_t *q, int ms) {
244251

245252
if (e->period >= 0) {
246253
// requeue periodic tasks to avoid race conditions
247-
// in event_cancel
254+
// in equeue_cancel
248255
equeue_enqueue(q, e, e->period);
249256
}
250257
events_mutex_unlock(&q->queuelock);
@@ -257,7 +264,7 @@ void equeue_dispatch(equeue_t *q, int ms) {
257264
e->cb(e + 1);
258265

259266
if (e->period < 0) {
260-
event_dealloc(q, e+1);
267+
equeue_dealloc(q, e+1);
261268
}
262269
}
263270

@@ -281,18 +288,6 @@ void event_dtor(void *p, void (*dtor)(void *)) {
281288
e->dtor = dtor;
282289
}
283290

284-
// event operations
285-
int event_post(equeue_t *q, void (*cb)(void*), void *p) {
286-
struct event *e = (struct event*)p - 1;
287-
e->cb = cb;
288-
int id = equeue_post(q, e, e->target);
289-
return id;
290-
}
291-
292-
void event_cancel(equeue_t *q, int id) {
293-
equeue_cancel(q, id);
294-
}
295-
296291
// simple callbacks
297292
struct ecallback {
298293
void (*cb)(void*);
@@ -304,31 +299,31 @@ static void ecallback_dispatch(void *p) {
304299
e->cb(e->data);
305300
}
306301

307-
int event_call(equeue_t *q, void (*cb)(void*), void *data) {
308-
struct ecallback *e = event_alloc(q, sizeof(struct ecallback));
302+
int equeue_call(equeue_t *q, void (*cb)(void*), void *data) {
303+
struct ecallback *e = equeue_alloc(q, sizeof(struct ecallback));
309304
if (!e) {
310305
return 0;
311306
}
312307

313308
e->cb = cb;
314309
e->data = data;
315-
return event_post(q, ecallback_dispatch, e);
310+
return equeue_post(q, ecallback_dispatch, e);
316311
}
317312

318-
int event_call_in(equeue_t *q, int ms, void (*cb)(void*), void *data) {
319-
struct ecallback *e = event_alloc(q, sizeof(struct ecallback));
313+
int equeue_call_in(equeue_t *q, int ms, void (*cb)(void*), void *data) {
314+
struct ecallback *e = equeue_alloc(q, sizeof(struct ecallback));
320315
if (!e) {
321316
return 0;
322317
}
323318

324319
event_delay(e, ms);
325320
e->cb = cb;
326321
e->data = data;
327-
return event_post(q, ecallback_dispatch, e);
322+
return equeue_post(q, ecallback_dispatch, e);
328323
}
329324

330-
int event_call_every(equeue_t *q, int ms, void (*cb)(void*), void *data) {
331-
struct ecallback *e = event_alloc(q, sizeof(struct ecallback));
325+
int equeue_call_every(equeue_t *q, int ms, void (*cb)(void*), void *data) {
326+
struct ecallback *e = equeue_alloc(q, sizeof(struct ecallback));
332327
if (!e) {
333328
return 0;
334329
}
@@ -337,5 +332,5 @@ int event_call_every(equeue_t *q, int ms, void (*cb)(void*), void *data) {
337332
event_period(e, ms);
338333
e->cb = cb;
339334
e->data = data;
340-
return event_post(q, ecallback_dispatch, e);
335+
return equeue_post(q, ecallback_dispatch, e);
341336
}

events.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,26 @@ void equeue_break(equeue_t *queue);
8080
// Passed callback will be executed in the associated equeue's
8181
// dispatch call with the data pointer passed unmodified
8282
//
83-
// event_call - Immediately post an event to the queue
84-
// event_call_in - Post an event after a specified time in milliseconds
85-
// event_call_every - Post an event periodically in milliseconds
83+
// equeue_call - Immediately post an event to the queue
84+
// equeue_call_in - Post an event after a specified time in milliseconds
85+
// equeue_call_every - Post an event periodically in milliseconds
8686
//
8787
// These calls will result in 0 if no memory is available, otherwise they
88-
// will result in a unique identifier that can be passed to event_cancel.
89-
int event_call(equeue_t *queue, void (*cb)(void *), void *data);
90-
int event_call_in(equeue_t *queue, int ms, void (*cb)(void *), void *data);
91-
int event_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data);
88+
// will result in a unique identifier that can be passed to equeue_cancel.
89+
int equeue_call(equeue_t *queue, void (*cb)(void *), void *data);
90+
int equeue_call_in(equeue_t *queue, int ms, void (*cb)(void *), void *data);
91+
int equeue_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data);
9292

9393
// Events with queue handled blocks of memory
9494
//
95-
// Argument to event_post must point to a result of a event_alloc call
95+
// Argument to equeue_post must point to a result of a equeue_alloc call
9696
// and the associated memory is automatically freed after the event
9797
// is dispatched.
9898
//
99-
// event_alloc will result in null if no memory is available
99+
// equeue_alloc will result in null if no memory is available
100100
// or the requested size is less than the size passed to equeue_create.
101-
void *event_alloc(equeue_t *queue, unsigned size);
102-
void event_dealloc(equeue_t *queue, void *event);
101+
void *equeue_alloc(equeue_t *queue, unsigned size);
102+
void equeue_dealloc(equeue_t *queue, void *event);
103103

104104
// Configure an allocated event
105105
//
@@ -112,21 +112,21 @@ void event_dtor(void *event, void (*dtor)(void *));
112112

113113
// Post an allocted event to the event queue
114114
//
115-
// Argument to event_post must point to a result of a event_alloc call
115+
// Argument to equeue_post must point to a result of a equeue_alloc call
116116
// and the associated memory is automatically freed after the event
117117
// is dispatched.
118118
//
119119
// This call results in an unique identifier that can be passed to
120-
// event_cancel.
121-
int event_post(equeue_t *queue, void (*cb)(void *), void *event);
120+
// equeue_cancel.
121+
int equeue_post(equeue_t *queue, void (*cb)(void *), void *event);
122122

123123
// Cancel events that are in flight
124124
//
125-
// Every event_call function returns a non-negative identifier on success
125+
// Every equeue_call function returns a non-negative identifier on success
126126
// that can be used to cancel an in-flight event. If the event has already
127127
// been dispatched or does not exist, no error occurs. Note, this can not
128128
// stop a currently executing event
129-
void event_cancel(equeue_t *queue, int event);
129+
void equeue_cancel(equeue_t *queue, int event);
130130

131131

132132
#ifdef __cplusplus

0 commit comments

Comments
 (0)