diff --git a/README.md b/README.md index 6cae4d8..5e5e031 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/events.c b/events.c index ea39324..3a2d8ac 100644 --- a/events.c +++ b/events.c @@ -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); @@ -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)) { @@ -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); @@ -126,7 +126,7 @@ 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; @@ -134,13 +134,13 @@ static inline int event_next_id(equeue_t *q) { 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; @@ -148,14 +148,14 @@ void *event_alloc(equeue_t *q, unsigned size) { 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 @@ -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; } @@ -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) { @@ -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); @@ -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); } } @@ -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*); @@ -304,19 +299,19 @@ 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; } @@ -324,11 +319,11 @@ int event_call_in(equeue_t *q, int ms, void (*cb)(void*), void *data) { 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; } @@ -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); } diff --git a/events.h b/events.h index 9c4d432..90ba945 100644 --- a/events.h +++ b/events.h @@ -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 // @@ -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 diff --git a/tests/prof.c b/tests/prof.c index 067d827..d570e4b 100644 --- a/tests/prof.c +++ b/tests/prof.c @@ -124,119 +124,119 @@ void events_tick_prof(void) { } } -void event_alloc_prof(void) { +void equeue_alloc_prof(void) { struct equeue q; equeue_create(&q, 32*EVENTS_EVENT_SIZE); prof_loop() { prof_start(); - void *e = event_alloc(&q, 8 * sizeof(int)); + void *e = equeue_alloc(&q, 8 * sizeof(int)); prof_stop(); - event_dealloc(&q, e); + equeue_dealloc(&q, e); } equeue_destroy(&q); } -void event_alloc_many_prof(int count) { +void equeue_alloc_many_prof(int count) { struct equeue q; equeue_create(&q, count*EVENTS_EVENT_SIZE); void *es[count]; for (int i = 0; i < count; i++) { - es[i] = event_alloc(&q, (i % 4) * sizeof(int)); + es[i] = equeue_alloc(&q, (i % 4) * sizeof(int)); } for (int i = 0; i < count; i++) { - event_dealloc(&q, es[i]); + equeue_dealloc(&q, es[i]); } prof_loop() { prof_start(); - void *e = event_alloc(&q, 8 * sizeof(int)); + void *e = equeue_alloc(&q, 8 * sizeof(int)); prof_stop(); - event_dealloc(&q, e); + equeue_dealloc(&q, e); } equeue_destroy(&q); } -void event_post_prof(void) { +void equeue_post_prof(void) { struct equeue q; equeue_create(&q, EVENTS_EVENT_SIZE); prof_loop() { - void *e = event_alloc(&q, 0); + void *e = equeue_alloc(&q, 0); prof_start(); - int id = event_post(&q, no_func, e); + int id = equeue_post(&q, no_func, e); prof_stop(); - event_cancel(&q, id); + equeue_cancel(&q, id); } equeue_destroy(&q); } -void event_post_many_prof(int count) { +void equeue_post_many_prof(int count) { struct equeue q; equeue_create(&q, count*EVENTS_EVENT_SIZE); for (int i = 0; i < count; i++) { - event_call(&q, no_func, 0); + equeue_call(&q, no_func, 0); } prof_loop() { - void *e = event_alloc(&q, 0); + void *e = equeue_alloc(&q, 0); prof_start(); - int id = event_post(&q, no_func, e); + int id = equeue_post(&q, no_func, e); prof_stop(); - event_cancel(&q, id); + equeue_cancel(&q, id); } equeue_destroy(&q); } -void event_post_future_prof(void) { +void equeue_post_future_prof(void) { struct equeue q; equeue_create(&q, EVENTS_EVENT_SIZE); prof_loop() { - void *e = event_alloc(&q, 0); + void *e = equeue_alloc(&q, 0); event_delay(e, 1000); prof_start(); - int id = event_post(&q, no_func, e); + int id = equeue_post(&q, no_func, e); prof_stop(); - event_cancel(&q, id); + equeue_cancel(&q, id); } equeue_destroy(&q); } -void event_post_future_many_prof(int count) { +void equeue_post_future_many_prof(int count) { struct equeue q; equeue_create(&q, count*EVENTS_EVENT_SIZE); for (int i = 0; i < count; i++) { - event_call(&q, no_func, 0); + equeue_call(&q, no_func, 0); } prof_loop() { - void *e = event_alloc(&q, 0); + void *e = equeue_alloc(&q, 0); event_delay(e, 1000); prof_start(); - int id = event_post(&q, no_func, e); + int id = equeue_post(&q, no_func, e); prof_stop(); - event_cancel(&q, id); + equeue_cancel(&q, id); } equeue_destroy(&q); @@ -247,7 +247,7 @@ void equeue_dispatch_prof(void) { equeue_create(&q, EVENTS_EVENT_SIZE); prof_loop() { - event_call(&q, no_func, 0); + equeue_call(&q, no_func, 0); prof_start(); equeue_dispatch(&q, 0); @@ -263,7 +263,7 @@ void equeue_dispatch_many_prof(int count) { prof_loop() { for (int i = 0; i < count; i++) { - event_call(&q, no_func, 0); + equeue_call(&q, no_func, 0); } prof_start(); @@ -274,60 +274,60 @@ void equeue_dispatch_many_prof(int count) { equeue_destroy(&q); } -void event_cancel_prof(void) { +void equeue_cancel_prof(void) { struct equeue q; equeue_create(&q, EVENTS_EVENT_SIZE); prof_loop() { - int id = event_call(&q, no_func, 0); + int id = equeue_call(&q, no_func, 0); prof_start(); - event_cancel(&q, id); + equeue_cancel(&q, id); prof_stop(); } equeue_destroy(&q); } -void event_cancel_many_prof(int count) { +void equeue_cancel_many_prof(int count) { struct equeue q; equeue_create(&q, count*EVENTS_EVENT_SIZE); for (int i = 0; i < count; i++) { - event_call(&q, no_func, 0); + equeue_call(&q, no_func, 0); } prof_loop() { - int id = event_call(&q, no_func, 0); + int id = equeue_call(&q, no_func, 0); prof_start(); - event_cancel(&q, id); + equeue_cancel(&q, id); prof_stop(); } equeue_destroy(&q); } -void event_alloc_size_prof(void) { +void equeue_alloc_size_prof(void) { size_t size = 32*EVENTS_EVENT_SIZE; struct equeue q; equeue_create(&q, size); - event_alloc(&q, 0); + equeue_alloc(&q, 0); prof_result(size - q.slab.size, "bytes"); equeue_destroy(&q); } -void event_alloc_many_size_prof(int count) { +void equeue_alloc_many_size_prof(int count) { size_t size = count*EVENTS_EVENT_SIZE; struct equeue q; equeue_create(&q, size); for (int i = 0; i < count; i++) { - event_alloc(&q, (i % 4) * sizeof(int)); + equeue_alloc(&q, (i % 4) * sizeof(int)); } prof_result(size - q.slab.size, "bytes"); @@ -335,7 +335,7 @@ void event_alloc_many_size_prof(int count) { equeue_destroy(&q); } -void event_alloc_fragmented_size_prof(int count) { +void equeue_alloc_fragmented_size_prof(int count) { size_t size = count*EVENTS_EVENT_SIZE; struct equeue q; @@ -344,23 +344,23 @@ void event_alloc_fragmented_size_prof(int count) { void *es[count]; for (int i = 0; i < count; i++) { - es[i] = event_alloc(&q, (i % 4) * sizeof(int)); + es[i] = equeue_alloc(&q, (i % 4) * sizeof(int)); } for (int i = 0; i < count; i++) { - event_dealloc(&q, es[i]); + equeue_dealloc(&q, es[i]); } for (int i = count-1; i >= 0; i--) { - es[i] = event_alloc(&q, (i % 4) * sizeof(int)); + es[i] = equeue_alloc(&q, (i % 4) * sizeof(int)); } for (int i = count-1; i >= 0; i--) { - event_dealloc(&q, es[i]); + equeue_dealloc(&q, es[i]); } for (int i = 0; i < count; i++) { - event_alloc(&q, (i % 4) * sizeof(int)); + equeue_alloc(&q, (i % 4) * sizeof(int)); } prof_result(size - q.slab.size, "bytes"); @@ -376,21 +376,21 @@ int main() { prof_baseline(baseline_prof); prof_measure(events_tick_prof); - prof_measure(event_alloc_prof); - prof_measure(event_post_prof); - prof_measure(event_post_future_prof); + prof_measure(equeue_alloc_prof); + prof_measure(equeue_post_prof); + prof_measure(equeue_post_future_prof); prof_measure(equeue_dispatch_prof); - prof_measure(event_cancel_prof); + prof_measure(equeue_cancel_prof); - prof_measure(event_alloc_many_prof, 1000); - prof_measure(event_post_many_prof, 1000); - prof_measure(event_post_future_many_prof, 1000); + prof_measure(equeue_alloc_many_prof, 1000); + prof_measure(equeue_post_many_prof, 1000); + prof_measure(equeue_post_future_many_prof, 1000); prof_measure(equeue_dispatch_many_prof, 100); - prof_measure(event_cancel_many_prof, 100); + prof_measure(equeue_cancel_many_prof, 100); - prof_measure(event_alloc_size_prof); - prof_measure(event_alloc_many_size_prof, 1000); - prof_measure(event_alloc_fragmented_size_prof, 1000); + prof_measure(equeue_alloc_size_prof); + prof_measure(equeue_alloc_many_size_prof, 1000); + prof_measure(equeue_alloc_fragmented_size_prof, 1000); printf("done!\n"); } diff --git a/tests/tests.c b/tests/tests.c index 73c7505..b832422 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -82,13 +82,13 @@ void fragment_func(void *p) { struct fragment *fragment = (struct fragment*)p; timing_func(&fragment->timing); - struct fragment *nfragment = event_alloc(fragment->q, fragment->size); + struct fragment *nfragment = equeue_alloc(fragment->q, fragment->size); test_assert(nfragment); *nfragment = *fragment; event_delay(nfragment, fragment->timing.delay); - int id = event_post(nfragment->q, fragment_func, nfragment); + int id = equeue_post(nfragment->q, fragment_func, nfragment); test_assert(id); } @@ -100,7 +100,7 @@ void simple_call_test(void) { test_assert(!err); bool touched = false; - event_call(&q, simple_func, &touched); + equeue_call(&q, simple_func, &touched); equeue_dispatch(&q, 0); test_assert(touched); @@ -113,7 +113,7 @@ void simple_call_in_test(void) { test_assert(!err); bool touched = false; - int id = event_call_in(&q, 5, simple_func, &touched); + int id = equeue_call_in(&q, 5, simple_func, &touched); test_assert(id); equeue_dispatch(&q, 10); @@ -128,7 +128,7 @@ void simple_call_every_test(void) { test_assert(!err); bool touched = false; - int id = event_call_every(&q, 5, simple_func, &touched); + int id = equeue_call_every(&q, 5, simple_func, &touched); test_assert(id); equeue_dispatch(&q, 10); @@ -143,11 +143,11 @@ void simple_post_test(void) { test_assert(!err); bool touched = false; - struct indirect *i = event_alloc(&q, sizeof(struct indirect)); + struct indirect *i = equeue_alloc(&q, sizeof(struct indirect)); test_assert(i); i->touched = &touched; - int id = event_post(&q, indirect_func, i); + int id = equeue_post(&q, indirect_func, i); test_assert(id); equeue_dispatch(&q, 0); @@ -163,24 +163,24 @@ void destructor_test(void) { test_assert(!err); bool touched = false; - struct indirect *i = event_alloc(&q, sizeof(struct indirect)); + struct indirect *i = equeue_alloc(&q, sizeof(struct indirect)); test_assert(i); i->touched = &touched; event_dtor(i, indirect_func); - int id = event_post(&q, pass_func, i); + int id = equeue_post(&q, pass_func, i); test_assert(id); equeue_dispatch(&q, 0); test_assert(touched); touched = false; - i = event_alloc(&q, sizeof(struct indirect)); + i = equeue_alloc(&q, sizeof(struct indirect)); test_assert(i); i->touched = &touched; event_dtor(i, indirect_func); - id = event_post(&q, pass_func, i); + id = equeue_post(&q, pass_func, i); test_assert(id); equeue_destroy(&q); @@ -192,11 +192,11 @@ void allocation_failure_test(void) { int err = equeue_create(&q, 2048); test_assert(!err); - void *p = event_alloc(&q, 4096); + void *p = equeue_alloc(&q, 4096); test_assert(!p); for (int i = 0; i < 100; i++) { - p = event_alloc(&q, 0); + p = equeue_alloc(&q, 0); } test_assert(!p); @@ -212,11 +212,11 @@ void cancel_test(int N) { int *ids = malloc(N*sizeof(int)); for (int i = 0; i < N; i++) { - ids[i] = event_call(&q, simple_func, &touched); + ids[i] = equeue_call(&q, simple_func, &touched); } for (int i = N-1; i >= 0; i--) { - event_cancel(&q, ids[i]); + equeue_cancel(&q, ids[i]); } free(ids); @@ -233,13 +233,13 @@ void loop_protect_test(void) { test_assert(!err); bool touched = false; - event_call_every(&q, 0, simple_func, &touched); + equeue_call_every(&q, 0, simple_func, &touched); equeue_dispatch(&q, 0); test_assert(touched); touched = false; - event_call_every(&q, 1, simple_func, &touched); + equeue_call_every(&q, 1, simple_func, &touched); equeue_dispatch(&q, 0); test_assert(touched); @@ -253,7 +253,7 @@ void break_test(void) { test_assert(!err); bool touched = false; - event_call_every(&q, 0, simple_func, &touched); + equeue_call_every(&q, 0, simple_func, &touched); equeue_break(&q); equeue_dispatch(&q, -1); @@ -269,7 +269,7 @@ void simple_barrage_test(int N) { test_assert(!err); for (int i = 0; i < N; i++) { - struct timing *timing = event_alloc(&q, sizeof(struct timing)); + struct timing *timing = equeue_alloc(&q, sizeof(struct timing)); test_assert(timing); timing->tick = events_tick(); @@ -277,7 +277,7 @@ void simple_barrage_test(int N) { event_delay(timing, timing->delay); event_period(timing, timing->delay); - int id = event_post(&q, timing_func, timing); + int id = equeue_post(&q, timing_func, timing); test_assert(id); } @@ -294,7 +294,7 @@ void fragmenting_barrage_test(int N) { for (int i = 0; i < N; i++) { unsigned size = sizeof(struct fragment) + i*sizeof(int); - struct fragment *fragment = event_alloc(&q, size); + struct fragment *fragment = equeue_alloc(&q, size); test_assert(fragment); fragment->q = &q; @@ -303,7 +303,7 @@ void fragmenting_barrage_test(int N) { fragment->timing.delay = (i+1)*100; event_delay(fragment, fragment->timing.delay); - int id = event_post(&q, fragment_func, fragment); + int id = equeue_post(&q, fragment_func, fragment); test_assert(id); } @@ -336,7 +336,7 @@ void multithreaded_barrage_test(int N) { test_assert(!err); for (int i = 0; i < N; i++) { - struct timing *timing = event_alloc(&q, sizeof(struct timing)); + struct timing *timing = equeue_alloc(&q, sizeof(struct timing)); test_assert(timing); timing->tick = events_tick(); @@ -344,7 +344,7 @@ void multithreaded_barrage_test(int N) { event_delay(timing, timing->delay); event_period(timing, timing->delay); - int id = event_post(&q, timing_func, timing); + int id = equeue_post(&q, timing_func, timing); test_assert(id); }