Skip to content

Commit

Permalink
Rename event -> equeue for functions operating on queues
Browse files Browse the repository at this point in the history
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
  • Loading branch information
geky committed Jul 29, 2016
1 parent ed105c9 commit ee453c9
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 137 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ int main() {
equeue_create(&queue, 32*EVENTS_EVENT_SIZE);

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

// events are executed when dispatch is called
equeue_dispatch(&queue, 3000);
Expand Down
71 changes: 33 additions & 38 deletions events.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void equeue_destroy(equeue_t *q) {
while (q->queue) {
struct event *e = q->queue;
q->queue = e->next;
event_dealloc(q, e+1);
equeue_dealloc(q, e+1);
}

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

// equeue allocation functions
static void *equeue_alloc(equeue_t *q, unsigned size) {
static void *equeue_mem_alloc(equeue_t *q, unsigned size) {
size = size + sizeof(unsigned);
size = (size + sizeof(unsigned)-1) & ~(sizeof(unsigned)-1);
if (size < sizeof(struct equeue_chunk)) {
Expand Down Expand Up @@ -103,7 +103,7 @@ static void *equeue_alloc(equeue_t *q, unsigned size) {
return 0;
}

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

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

// event allocation functions
static inline int event_next_id(equeue_t *q) {
static inline int equeue_next_id(equeue_t *q) {
int id = q->next_id++;
if (q->next_id < 0) {
q->next_id = 42;
}
return id;
}

void *event_alloc(equeue_t *q, unsigned size) {
struct event *e = equeue_alloc(q, sizeof(struct event) + size);
void *equeue_alloc(equeue_t *q, unsigned size) {
struct event *e = equeue_mem_alloc(q, sizeof(struct event) + size);
if (!e) {
return 0;
}

e->id = event_next_id(q);
e->id = equeue_next_id(q);
e->target = 0;
e->period = -1;
e->dtor = 0;

return e + 1;
}

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

if (e->dtor) {
e->dtor(e+1);
}

equeue_dealloc(q, e);
equeue_mem_dealloc(q, e);
}

// equeue scheduling functions
Expand Down Expand Up @@ -187,10 +187,10 @@ static struct event *equeue_dequeue(equeue_t *q, int id) {
return 0;
}

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

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

static void equeue_cancel(equeue_t *q, int id) {
int equeue_post(equeue_t *q, void (*cb)(void*), void *p) {
struct event *e = (struct event*)p - 1;
e->cb = cb;
int id = equeue_post_in(q, e, e->target);
return id;
}

void equeue_cancel(equeue_t *q, int id) {
events_mutex_lock(&q->queuelock);
struct event *e = equeue_dequeue(q, id);
events_mutex_unlock(&q->queuelock);

if (e) {
event_dealloc(q, e+1);
equeue_dealloc(q, e+1);
}
}

void equeue_break(equeue_t *q) {
equeue_post(q, &q->break_, 0);
equeue_post_in(q, &q->break_, 0);
}

void equeue_dispatch(equeue_t *q, int ms) {
if (ms >= 0) {
equeue_post(q, &q->break_, ms);
equeue_post_in(q, &q->break_, ms);
}

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

if (e->period >= 0) {
// requeue periodic tasks to avoid race conditions
// in event_cancel
// in equeue_cancel
equeue_enqueue(q, e, e->period);
}
events_mutex_unlock(&q->queuelock);
Expand All @@ -257,7 +264,7 @@ void equeue_dispatch(equeue_t *q, int ms) {
e->cb(e + 1);

if (e->period < 0) {
event_dealloc(q, e+1);
equeue_dealloc(q, e+1);
}
}

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

// event operations
int event_post(equeue_t *q, void (*cb)(void*), void *p) {
struct event *e = (struct event*)p - 1;
e->cb = cb;
int id = equeue_post(q, e, e->target);
return id;
}

void event_cancel(equeue_t *q, int id) {
equeue_cancel(q, id);
}

// simple callbacks
struct ecallback {
void (*cb)(void*);
Expand All @@ -304,31 +299,31 @@ static void ecallback_dispatch(void *p) {
e->cb(e->data);
}

int event_call(equeue_t *q, void (*cb)(void*), void *data) {
struct ecallback *e = event_alloc(q, sizeof(struct ecallback));
int equeue_call(equeue_t *q, void (*cb)(void*), void *data) {
struct ecallback *e = equeue_alloc(q, sizeof(struct ecallback));
if (!e) {
return 0;
}

e->cb = cb;
e->data = data;
return event_post(q, ecallback_dispatch, e);
return equeue_post(q, ecallback_dispatch, e);
}

int event_call_in(equeue_t *q, int ms, void (*cb)(void*), void *data) {
struct ecallback *e = event_alloc(q, sizeof(struct ecallback));
int equeue_call_in(equeue_t *q, int ms, void (*cb)(void*), void *data) {
struct ecallback *e = equeue_alloc(q, sizeof(struct ecallback));
if (!e) {
return 0;
}

event_delay(e, ms);
e->cb = cb;
e->data = data;
return event_post(q, ecallback_dispatch, e);
return equeue_post(q, ecallback_dispatch, e);
}

int event_call_every(equeue_t *q, int ms, void (*cb)(void*), void *data) {
struct ecallback *e = event_alloc(q, sizeof(struct ecallback));
int equeue_call_every(equeue_t *q, int ms, void (*cb)(void*), void *data) {
struct ecallback *e = equeue_alloc(q, sizeof(struct ecallback));
if (!e) {
return 0;
}
Expand All @@ -337,5 +332,5 @@ int event_call_every(equeue_t *q, int ms, void (*cb)(void*), void *data) {
event_period(e, ms);
e->cb = cb;
e->data = data;
return event_post(q, ecallback_dispatch, e);
return equeue_post(q, ecallback_dispatch, e);
}
32 changes: 16 additions & 16 deletions events.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,26 @@ void equeue_break(equeue_t *queue);
// Passed callback will be executed in the associated equeue's
// dispatch call with the data pointer passed unmodified
//
// event_call - Immediately post an event to the queue
// event_call_in - Post an event after a specified time in milliseconds
// event_call_every - Post an event periodically in milliseconds
// equeue_call - Immediately post an event to the queue
// equeue_call_in - Post an event after a specified time in milliseconds
// equeue_call_every - Post an event periodically in milliseconds
//
// These calls will result in 0 if no memory is available, otherwise they
// will result in a unique identifier that can be passed to event_cancel.
int event_call(equeue_t *queue, void (*cb)(void *), void *data);
int event_call_in(equeue_t *queue, int ms, void (*cb)(void *), void *data);
int event_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data);
// will result in a unique identifier that can be passed to equeue_cancel.
int equeue_call(equeue_t *queue, void (*cb)(void *), void *data);
int equeue_call_in(equeue_t *queue, int ms, void (*cb)(void *), void *data);
int equeue_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data);

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

// Configure an allocated event
//
Expand All @@ -112,21 +112,21 @@ void event_dtor(void *event, void (*dtor)(void *));

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

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


#ifdef __cplusplus
Expand Down
Loading

0 comments on commit ee453c9

Please sign in to comment.