Skip to content

Commit d513587

Browse files
committed
events - Fixed unchaining of event queues
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.
1 parent aeabcc9 commit d513587

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

events/equeue/equeue.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,11 @@ static void equeue_chain_update(void *p, int ms) {
558558
}
559559

560560
void equeue_chain(equeue_t *q, equeue_t *target) {
561+
if (!target) {
562+
equeue_background(q, 0, 0);
563+
return;
564+
}
565+
561566
struct equeue_chain_context *c = equeue_alloc(q,
562567
sizeof(struct equeue_chain_context));
563568

events/equeue/tests/tests.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,42 @@ void chain_test(void) {
557557
equeue_dispatch(&q1, 30);
558558

559559
test_assert(touched == 6);
560+
561+
equeue_destroy(&q1);
562+
equeue_destroy(&q2);
563+
}
564+
565+
void unchain_test(void) {
566+
equeue_t q1;
567+
int err = equeue_create(&q1, 2048);
568+
test_assert(!err);
569+
570+
equeue_t q2;
571+
err = equeue_create(&q2, 2048);
572+
test_assert(!err);
573+
574+
equeue_chain(&q2, &q1);
575+
576+
int touched = 0;
577+
int id1 = equeue_call(&q1, simple_func, &touched);
578+
int id2 = equeue_call(&q2, simple_func, &touched);
579+
test_assert(id1 && id2);
580+
581+
equeue_dispatch(&q1, 0);
582+
test_assert(touched == 2);
583+
584+
equeue_chain(&q2, 0);
585+
equeue_chain(&q1, &q2);
586+
587+
id1 = equeue_call(&q1, simple_func, &touched);
588+
id2 = equeue_call(&q2, simple_func, &touched);
589+
test_assert(id1 && id2);
590+
591+
equeue_dispatch(&q2, 0);
592+
test_assert(touched == 4);
593+
594+
equeue_destroy(&q1);
595+
equeue_destroy(&q2);
560596
}
561597

562598
// Barrage tests
@@ -671,6 +707,7 @@ int main() {
671707
test_run(sloth_test);
672708
test_run(background_test);
673709
test_run(chain_test);
710+
test_run(unchain_test);
674711
test_run(multithread_test);
675712
test_run(simple_barrage_test, 20);
676713
test_run(fragmenting_barrage_test, 20);

0 commit comments

Comments
 (0)