Skip to content

Commit

Permalink
Fixed unchaining of event queues
Browse files Browse the repository at this point in the history
The equeue_chain function is supposed to unchain the event queue
from whatever queue it is chained to when passed a null target.
Internally, this is accomplished by just calling equeue_background
with null and letting the previously registered update function
clean up the chaining.

However, equeue_chain did not appropriately check for null, causing
it to unnecessarily allocate memory and leaving the update function
in a bad state. Fixed with a simple null check.
  • Loading branch information
geky committed Nov 20, 2016
1 parent 454cb02 commit 74649bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions equeue.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,11 @@ static void equeue_chain_update(void *p, int ms) {
}

void equeue_chain(equeue_t *q, equeue_t *target) {
if (!target) {
equeue_background(q, 0, 0);
return;
}

struct equeue_chain_context *c = equeue_alloc(q,
sizeof(struct equeue_chain_context));

Expand Down
37 changes: 37 additions & 0 deletions tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,42 @@ void chain_test(void) {
equeue_dispatch(&q1, 30);

test_assert(touched == 6);

equeue_destroy(&q1);
equeue_destroy(&q2);
}

void unchain_test(void) {
equeue_t q1;
int err = equeue_create(&q1, 2048);
test_assert(!err);

equeue_t q2;
err = equeue_create(&q2, 2048);
test_assert(!err);

equeue_chain(&q2, &q1);

int touched = 0;
int id1 = equeue_call(&q1, simple_func, &touched);
int id2 = equeue_call(&q2, simple_func, &touched);
test_assert(id1 && id2);

equeue_dispatch(&q1, 0);
test_assert(touched == 2);

equeue_chain(&q2, 0);
equeue_chain(&q1, &q2);

id1 = equeue_call(&q1, simple_func, &touched);
id2 = equeue_call(&q2, simple_func, &touched);
test_assert(id1 && id2);

equeue_dispatch(&q2, 0);
test_assert(touched == 4);

equeue_destroy(&q1);
equeue_destroy(&q2);
}

// Barrage tests
Expand Down Expand Up @@ -660,6 +696,7 @@ int main() {
test_run(sloth_test);
test_run(background_test);
test_run(chain_test);
test_run(unchain_test);
test_run(multithread_test);
test_run(simple_barrage_test, 20);
test_run(fragmenting_barrage_test, 20);
Expand Down

0 comments on commit 74649bd

Please sign in to comment.