diff --git a/lib/isc/include/isc/event.h b/lib/isc/include/isc/event.h index 75a528e4e9..2349f3140c 100644 --- a/lib/isc/include/isc/event.h +++ b/lib/isc/include/isc/event.h @@ -42,7 +42,6 @@ typedef void (*isc_eventdestructor_t)(isc_event_t *); * definition. Attributes of 0xffffff00 may be used by the application * or non-ISC libraries. */ -#define ISC_EVENTATTR_NOPURGE 0x00000001 /*% * The ISC_EVENTATTR_CANCELED attribute is intended to indicate diff --git a/lib/isc/include/isc/task.h b/lib/isc/include/isc/task.h index 1d995083c7..9bff6f8b60 100644 --- a/lib/isc/include/isc/task.h +++ b/lib/isc/include/isc/task.h @@ -234,63 +234,6 @@ isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp); * all resources used by the task will be freed. */ -unsigned int -isc_task_purgerange(isc_task_t *task, void *sender, isc_eventtype_t first, - isc_eventtype_t last, void *tag); -/*%< - * Purge events from a task's event queue. - * - * Requires: - * - *\li 'task' is a valid task. - * - *\li last >= first - * - * Ensures: - * - *\li Events in the event queue of 'task' whose sender is 'sender', whose - * type is >= first and <= last, and whose tag is 'tag' will be purged, - * unless they are marked as unpurgable. - * - *\li A sender of NULL will match any sender. A NULL tag matches any - * tag. - * - * Returns: - * - *\li The number of events purged. - */ - -unsigned int -isc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type, void *tag); -/*%< - * Purge events from a task's event queue. - * - * Notes: - * - *\li This function is equivalent to - * - *\code - * isc_task_purgerange(task, sender, type, type, tag); - *\endcode - * - * Requires: - * - *\li 'task' is a valid task. - * - * Ensures: - * - *\li Events in the event queue of 'task' whose sender is 'sender', whose - * type is 'type', and whose tag is 'tag' will be purged, unless they - * are marked as unpurgable. - * - *\li A sender of NULL will match any sender. A NULL tag matches any - * tag. - * - * Returns: - * - *\li The number of events purged. - */ - bool isc_task_purgeevent(isc_task_t *task, isc_event_t *event); /*%< diff --git a/lib/isc/task.c b/lib/isc/task.c index df1f4d565a..cc76a6541f 100644 --- a/lib/isc/task.c +++ b/lib/isc/task.c @@ -523,93 +523,6 @@ isc_task_sendtoanddetach(isc_task_t **taskp, isc_event_t **eventp, int c) { *taskp = NULL; } -#define PURGE_OK(event) (((event)->ev_attributes & ISC_EVENTATTR_NOPURGE) == 0) - -static unsigned int -dequeue_events(isc_task_t *task, void *sender, isc_eventtype_t first, - isc_eventtype_t last, void *tag, isc_eventlist_t *events, - bool purging) { - isc_event_t *event, *next_event; - unsigned int count = 0; - - REQUIRE(VALID_TASK(task)); - REQUIRE(last >= first); - - XTRACE("dequeue_events"); - - /* - * Events matching 'sender', whose type is >= first and <= last, and - * whose tag is 'tag' will be dequeued. If 'purging', matching events - * which are marked as unpurgable will not be dequeued. - * - * sender == NULL means "any sender", and tag == NULL means "any tag". - */ - - LOCK(&task->lock); - - for (event = HEAD(task->events); event != NULL; event = next_event) { - next_event = NEXT(event, ev_link); - if (event->ev_type >= first && event->ev_type <= last && - (sender == NULL || event->ev_sender == sender) && - (tag == NULL || event->ev_tag == tag) && - (!purging || PURGE_OK(event))) - { - DEQUEUE(task->events, event, ev_link); - task->nevents--; - ENQUEUE(*events, event, ev_link); - count++; - } - } - - UNLOCK(&task->lock); - - return (count); -} - -unsigned int -isc_task_purgerange(isc_task_t *task, void *sender, isc_eventtype_t first, - isc_eventtype_t last, void *tag) { - unsigned int count; - isc_eventlist_t events; - isc_event_t *event, *next_event; - REQUIRE(VALID_TASK(task)); - - /* - * Purge events from a task's event queue. - */ - - XTRACE("isc_task_purgerange"); - - ISC_LIST_INIT(events); - - count = dequeue_events(task, sender, first, last, tag, &events, true); - - for (event = HEAD(events); event != NULL; event = next_event) { - next_event = NEXT(event, ev_link); - ISC_LIST_UNLINK(events, event, ev_link); - isc_event_free(&event); - } - - /* - * Note that purging never changes the state of the task. - */ - - return (count); -} - -unsigned int -isc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type, - void *tag) { - /* - * Purge events from a task's event queue. - */ - REQUIRE(VALID_TASK(task)); - - XTRACE("isc_task_purge"); - - return (isc_task_purgerange(task, sender, type, type, tag)); -} - bool isc_task_purgeevent(isc_task_t *task, isc_event_t *event) { bool found = false; diff --git a/lib/isc/tests/task_test.c b/lib/isc/tests/task_test.c index 0a4d91e5e5..57a1bec477 100644 --- a/lib/isc/tests/task_test.c +++ b/lib/isc/tests/task_test.c @@ -534,8 +534,6 @@ basic(void **state) { isc_task_send(task1, &event); } - (void)isc_task_purge(task3, NULL, 0, 0); - isc_task_detach(&task1); isc_task_detach(&task2); isc_task_detach(&task3); @@ -959,349 +957,10 @@ post_shutdown(void **state) { #define TAGCNT 5 #define NEVENTS (SENDERCNT * TYPECNT * TAGCNT) -static bool testrange; -static void *purge_sender; -static isc_eventtype_t purge_type_first; -static isc_eventtype_t purge_type_last; -static void *purge_tag; static int eventcnt; atomic_bool started; -static void -pg_event1(isc_task_t *task, isc_event_t *event) { - UNUSED(task); - - LOCK(&lock); - while (!atomic_load(&started)) { - WAIT(&cv, &lock); - } - UNLOCK(&lock); - - isc_event_free(&event); -} - -static void -pg_event2(isc_task_t *task, isc_event_t *event) { - bool sender_match = false; - bool type_match = false; - bool tag_match = false; - - UNUSED(task); - - if ((purge_sender == NULL) || (purge_sender == event->ev_sender)) { - sender_match = true; - } - - if (testrange) { - if ((purge_type_first <= event->ev_type) && - (event->ev_type <= purge_type_last)) { - type_match = true; - } - } else { - if (purge_type_first == event->ev_type) { - type_match = true; - } - } - - if ((purge_tag == NULL) || (purge_tag == event->ev_tag)) { - tag_match = true; - } - - if (sender_match && type_match && tag_match) { - if ((event->ev_attributes & ISC_EVENTATTR_NOPURGE) != 0) { - if (verbose) { - print_message("# event %p,%d,%p " - "matched but was not " - "purgeable\n", - event->ev_sender, - (int)event->ev_type, - event->ev_tag); - } - ++eventcnt; - } else if (verbose) { - print_message("# event %p,%d,%p not purged\n", - event->ev_sender, (int)event->ev_type, - event->ev_tag); - } - } else { - ++eventcnt; - } - - isc_event_free(&event); -} - -static void -pg_sde(isc_task_t *task, isc_event_t *event) { - UNUSED(task); - - LOCK(&lock); - atomic_store(&done, true); - SIGNAL(&cv); - UNLOCK(&lock); - - isc_event_free(&event); -} - -static void -test_purge(int sender, int type, int tag, int exp_purged) { - isc_result_t result; - isc_task_t *task = NULL; - isc_event_t *eventtab[NEVENTS]; - isc_event_t *event = NULL; - isc_interval_t interval; - isc_time_t now; - int sender_cnt, type_cnt, tag_cnt, event_cnt, i; - int purged = 0; - - atomic_init(&started, false); - atomic_init(&done, false); - eventcnt = 0; - - isc_condition_init(&cv); - - result = isc_task_create(taskmgr, 0, &task); - assert_int_equal(result, ISC_R_SUCCESS); - - result = isc_task_onshutdown(task, pg_sde, NULL); - assert_int_equal(result, ISC_R_SUCCESS); - - /* - * Block the task on cv. - */ - event = isc_event_allocate(test_mctx, (void *)1, 9999, pg_event1, NULL, - sizeof(*event)); - - assert_non_null(event); - isc_task_send(task, &event); - - /* - * Fill the task's queue with some messages with varying - * sender, type, tag, and purgeable attribute values. - */ - event_cnt = 0; - for (sender_cnt = 0; sender_cnt < SENDERCNT; ++sender_cnt) { - for (type_cnt = 0; type_cnt < TYPECNT; ++type_cnt) { - for (tag_cnt = 0; tag_cnt < TAGCNT; ++tag_cnt) { - eventtab[event_cnt] = isc_event_allocate( - test_mctx, - &senders[sender + sender_cnt], - (isc_eventtype_t)(type + type_cnt), - pg_event2, NULL, sizeof(*event)); - - assert_non_null(eventtab[event_cnt]); - - eventtab[event_cnt]->ev_tag = - (void *)((uintptr_t)tag + tag_cnt); - - /* - * Mark events as non-purgeable if - * sender, type and tag are all - * odd-numbered. (There should be 4 - * of these out of 60 events total.) - */ - if (((sender_cnt % 2) != 0) && - ((type_cnt % 2) != 0) && - ((tag_cnt % 2) != 0)) { - eventtab[event_cnt]->ev_attributes |= - ISC_EVENTATTR_NOPURGE; - } - ++event_cnt; - } - } - } - - for (i = 0; i < event_cnt; ++i) { - isc_task_send(task, &eventtab[i]); - } - - if (testrange) { - /* - * We're testing isc_task_purgerange. - */ - purged = isc_task_purgerange( - task, purge_sender, (isc_eventtype_t)purge_type_first, - (isc_eventtype_t)purge_type_last, purge_tag); - assert_int_equal(purged, exp_purged); - } else { - /* - * We're testing isc_task_purge. - */ - if (verbose) { - print_message("# purge events %p,%u,%p\n", purge_sender, - purge_type_first, purge_tag); - } - purged = isc_task_purge(task, purge_sender, - (isc_eventtype_t)purge_type_first, - purge_tag); - if (verbose) { - print_message("# purged %d expected %d\n", purged, - exp_purged); - } - - assert_int_equal(purged, exp_purged); - } - - /* - * Unblock the task, allowing event processing. - */ - LOCK(&lock); - atomic_store(&started, true); - SIGNAL(&cv); - - isc_task_shutdown(task); - - isc_interval_set(&interval, 5, 0); - - /* - * Wait for shutdown processing to complete. - */ - while (!atomic_load(&done)) { - result = isc_time_nowplusinterval(&now, &interval); - assert_int_equal(result, ISC_R_SUCCESS); - - WAITUNTIL(&cv, &lock, &now); - } - - UNLOCK(&lock); - - isc_task_detach(&task); - - assert_int_equal(eventcnt, event_cnt - exp_purged); -} - -/* - * Purge test: - * A call to isc_task_purge(task, sender, type, tag) purges all events of - * type 'type' and with tag 'tag' not marked as unpurgeable from sender - * from the task's " queue and returns the number of events purged. - */ -static void -purge(void **state) { - UNUSED(state); - - /* Try purging on a specific sender. */ - if (verbose) { - print_message("# testing purge on 2,4,8 expecting 1\n"); - } - purge_sender = &senders[2]; - purge_type_first = 4; - purge_type_last = 4; - purge_tag = (void *)8; - testrange = false; - test_purge(1, 4, 7, 1); - - /* Try purging on all senders. */ - if (verbose) { - print_message("# testing purge on 0,4,8 expecting 3\n"); - } - purge_sender = NULL; - purge_type_first = 4; - purge_type_last = 4; - purge_tag = (void *)8; - testrange = false; - test_purge(1, 4, 7, 3); - - /* Try purging on all senders, specified type, all tags. */ - if (verbose) { - print_message("# testing purge on 0,4,0 expecting 15\n"); - } - purge_sender = NULL; - purge_type_first = 4; - purge_type_last = 4; - purge_tag = NULL; - testrange = false; - test_purge(1, 4, 7, 15); - - /* Try purging on a specified tag, no such type. */ - if (verbose) { - print_message("# testing purge on 0,99,8 expecting 0\n"); - } - purge_sender = NULL; - purge_type_first = 99; - purge_type_last = 99; - purge_tag = (void *)8; - testrange = false; - test_purge(1, 4, 7, 0); - - /* Try purging on specified sender, type, all tags. */ - if (verbose) { - print_message("# testing purge on 3,5,0 expecting 5\n"); - } - purge_sender = &senders[3]; - purge_type_first = 5; - purge_type_last = 5; - purge_tag = NULL; - testrange = false; - test_purge(1, 4, 7, 5); -} - -/* - * Purge range test: - * A call to isc_event_purgerange(task, sender, first, last, tag) purges - * all events not marked unpurgeable from sender 'sender' and of type within - * the range 'first' to 'last' inclusive from the task's event queue and - * returns the number of tasks purged. - */ - -static void -purgerange(void **state) { - UNUSED(state); - - /* Now let's try some ranges. */ - /* testing purgerange on 2,4-5,8 expecting 1 */ - purge_sender = &senders[2]; - purge_type_first = 4; - purge_type_last = 5; - purge_tag = (void *)8; - testrange = true; - test_purge(1, 4, 7, 1); - - /* Try purging on all senders. */ - if (verbose) { - print_message("# testing purge on 0,4-5,8 expecting 5\n"); - } - purge_sender = NULL; - purge_type_first = 4; - purge_type_last = 5; - purge_tag = (void *)8; - testrange = true; - test_purge(1, 4, 7, 5); - - /* Try purging on all senders, specified type, all tags. */ - if (verbose) { - print_message("# testing purge on 0,5-6,0 expecting 28\n"); - } - purge_sender = NULL; - purge_type_first = 5; - purge_type_last = 6; - purge_tag = NULL; - testrange = true; - test_purge(1, 4, 7, 28); - - /* Try purging on a specified tag, no such type. */ - if (verbose) { - print_message("# testing purge on 0,99-101,8 expecting 0\n"); - } - purge_sender = NULL; - purge_type_first = 99; - purge_type_last = 101; - purge_tag = (void *)8; - testrange = true; - test_purge(1, 4, 7, 0); - - /* Try purging on specified sender, type, all tags. */ - if (verbose) { - print_message("# testing purge on 3,5-6,0 expecting 10\n"); - } - purge_sender = &senders[3]; - purge_type_first = 5; - purge_type_last = 6; - purge_tag = NULL; - testrange = true; - test_purge(1, 4, 7, 10); -} - /* * Helpers for purge event tests */ @@ -1339,7 +998,7 @@ pge_sde(isc_task_t *task, isc_event_t *event) { } static void -try_purgeevent(bool purgeable) { +try_purgeevent(void) { isc_result_t result; isc_task_t *task = NULL; bool purged; @@ -1375,16 +1034,11 @@ try_purgeevent(bool purgeable) { event2_clone = event2; - if (purgeable) { - event2->ev_attributes &= ~ISC_EVENTATTR_NOPURGE; - } else { - event2->ev_attributes |= ISC_EVENTATTR_NOPURGE; - } - isc_task_send(task, &event2); purged = isc_task_purgeevent(task, event2_clone); - assert_int_equal(purgeable, purged); + + assert_true(purged); /* * Unblock the task, allowing event processing. @@ -1410,8 +1064,6 @@ try_purgeevent(bool purgeable) { UNLOCK(&lock); isc_task_detach(&task); - - assert_int_equal(eventcnt, (purgeable ? 0 : 1)); } /* @@ -1425,21 +1077,7 @@ static void purgeevent(void **state) { UNUSED(state); - try_purgeevent(true); -} - -/* - * Purge event not purgeable test: - * When the event is not marked as purgable, a call to - * isc_task_purgeevent(task, event) does not purge the event - * 'event' from the task's queue and returns false. - */ - -static void -purgeevent_notpurge(void **state) { - UNUSED(state); - - try_purgeevent(false); + try_purgeevent(); } int @@ -1455,11 +1093,7 @@ main(int argc, char **argv) { _teardown), cmocka_unit_test_setup_teardown(privileged_events, _setup, _teardown), - cmocka_unit_test_setup_teardown(purge, _setup2, _teardown), cmocka_unit_test_setup_teardown(purgeevent, _setup2, _teardown), - cmocka_unit_test_setup_teardown(purgeevent_notpurge, _setup, - _teardown), - cmocka_unit_test_setup_teardown(purgerange, _setup, _teardown), cmocka_unit_test_setup_teardown(task_shutdown, _setup4, _teardown), cmocka_unit_test_setup_teardown(task_exclusive, _setup4, diff --git a/lib/isc/timer.c b/lib/isc/timer.c index 136ed3276a..6ecb1b4c1b 100644 --- a/lib/isc/timer.c +++ b/lib/isc/timer.c @@ -192,8 +192,6 @@ deschedule(isc_timer_t *timer) { static void timerevent_unlink(isc_timer_t *timer, isc_timerevent_t *event) { - fprintf(stderr, "unlinking %p from %p\n", event, &timer->active); - REQUIRE(ISC_LINK_LINKED(event, ev_timerlink)); ISC_LIST_UNLINK(timer->active, event, ev_timerlink); } @@ -204,6 +202,7 @@ timerevent_destroy(isc_event_t *event0) { isc_timerevent_t *event = (isc_timerevent_t *)event0; if (ISC_LINK_LINKED(event, ev_timerlink)) { + /* The event was unlinked via timer_purge() */ timerevent_unlink(timer, event); } @@ -216,8 +215,15 @@ timer_purge(isc_timer_t *timer) { isc_timerevent_t *event = NULL; while ((event = ISC_LIST_HEAD(timer->active)) != NULL) { - (void)isc_task_purgeevent(timer->task, (isc_event_t *)event); - timerevent_unlink(timer, event); + bool purged = isc_task_purgeevent(timer->task, + (isc_event_t *)event); + if (!purged) { + /* + * The event has already been executed, but not + * yet destroyed. + */ + timerevent_unlink(timer, event); + } } } @@ -383,7 +389,6 @@ isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) { void isc_timer_detach(isc_timer_t **timerp) { isc_timer_t *timer; - /* * Detach *timerp from its timer. */ @@ -415,6 +420,7 @@ post_event(isc_timermgr_t *manager, isc_timer_t *timer, isc_eventtype_t type) { ((isc_event_t *)event)->ev_destroy_arg = timer; event->due = timer->due; + ISC_LIST_APPEND(timer->active, event, ev_timerlink); isc_task_send(timer->task, ISC_EVENT_PTR(&event));