Skip to content

Commit 1580774

Browse files
authored
Merge pull request #6238 from pauluap/break_dispatch_flag
Remove windup behavior from break_dispatch
2 parents 0ae3580 + dc430ef commit 1580774

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

events/equeue/equeue.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int equeue_create_inplace(equeue_t *q, size_t size, void *buffer) {
7373
q->queue = 0;
7474
q->tick = equeue_tick();
7575
q->generation = 0;
76-
q->breaks = 0;
76+
q->break_requested = false;
7777

7878
q->background.active = false;
7979
q->background.update = 0;
@@ -366,7 +366,7 @@ void equeue_cancel(equeue_t *q, int id) {
366366

367367
void equeue_break(equeue_t *q) {
368368
equeue_mutex_lock(&q->queuelock);
369-
q->breaks++;
369+
q->break_requested = true;
370370
equeue_mutex_unlock(&q->queuelock);
371371
equeue_sema_signal(&q->eventsema);
372372
}
@@ -418,6 +418,7 @@ void equeue_dispatch(equeue_t *q, int ms) {
418418
q->background.active = true;
419419
equeue_mutex_unlock(&q->queuelock);
420420
}
421+
q->break_requested = false;
421422
return;
422423
}
423424
}
@@ -436,10 +437,10 @@ void equeue_dispatch(equeue_t *q, int ms) {
436437
equeue_sema_wait(&q->eventsema, deadline);
437438

438439
// check if we were notified to break out of dispatch
439-
if (q->breaks) {
440+
if (q->break_requested) {
440441
equeue_mutex_lock(&q->queuelock);
441-
if (q->breaks > 0) {
442-
q->breaks--;
442+
if (q->break_requested) {
443+
q->break_requested = false;
443444
equeue_mutex_unlock(&q->queuelock);
444445
return;
445446
}

events/equeue/equeue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct equeue_event {
5858
typedef struct equeue {
5959
struct equeue_event *queue;
6060
unsigned tick;
61-
unsigned breaks;
61+
bool break_requested;
6262
uint8_t generation;
6363

6464
unsigned char *buffer;

events/equeue/tests/tests.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,26 @@ void break_test(void) {
391391
equeue_destroy(&q);
392392
}
393393

394+
void break_no_windup_test(void) {
395+
equeue_t q;
396+
int err = equeue_create(&q, 2048);
397+
test_assert(!err);
398+
399+
int count = 0;
400+
equeue_call_every(&q, 0, simple_func, &count);
401+
402+
equeue_break(&q);
403+
equeue_break(&q);
404+
equeue_dispatch(&q, -1);
405+
test_assert(count == 1);
406+
407+
count = 0;
408+
equeue_dispatch(&q, 55);
409+
test_assert(count > 1);
410+
411+
equeue_destroy(&q);
412+
}
413+
394414
void period_test(void) {
395415
equeue_t q;
396416
int err = equeue_create(&q, 2048);
@@ -687,6 +707,43 @@ void multithreaded_barrage_test(int N) {
687707
equeue_destroy(&q);
688708
}
689709

710+
struct count_and_queue
711+
{
712+
int p;
713+
equeue_t* q;
714+
};
715+
716+
void simple_breaker(void *p) {
717+
struct count_and_queue* caq = (struct count_and_queue*)p;
718+
equeue_break(caq->q);
719+
usleep(10000);
720+
caq->p++;
721+
}
722+
723+
void break_request_cleared_on_timeout(void) {
724+
equeue_t q;
725+
int err = equeue_create(&q, 2048);
726+
test_assert(!err);
727+
728+
struct count_and_queue pq;
729+
pq.p = 0;
730+
pq.q = &q;
731+
732+
int id = equeue_call_every(&q, 10, simple_breaker, &pq);
733+
734+
equeue_dispatch(&q, 10);
735+
test_assert(pq.p == 1);
736+
737+
equeue_cancel(&q, id);
738+
739+
int count = 0;
740+
equeue_call_every(&q, 10, simple_func, &count);
741+
742+
equeue_dispatch(&q, 55);
743+
test_assert(count > 1);
744+
745+
equeue_destroy(&q);
746+
}
690747

691748
int main() {
692749
printf("beginning tests...\n");
@@ -702,6 +759,7 @@ int main() {
702759
test_run(cancel_unnecessarily_test);
703760
test_run(loop_protect_test);
704761
test_run(break_test);
762+
test_run(break_no_windup_test);
705763
test_run(period_test);
706764
test_run(nested_test);
707765
test_run(sloth_test);
@@ -712,6 +770,7 @@ int main() {
712770
test_run(simple_barrage_test, 20);
713771
test_run(fragmenting_barrage_test, 20);
714772
test_run(multithreaded_barrage_test, 20);
773+
test_run(break_request_cleared_on_timeout);
715774

716775
printf("done!\n");
717776
return test_failure;

0 commit comments

Comments
 (0)