static void destroy(isc__timer_t *timer) { isc__timermgr_t *manager = timer->manager; /* * The caller must ensure it is safe to destroy the timer. */ LOCK(&manager->lock); (void)isc_task_purgerange(timer->task, timer, ISC_TIMEREVENT_FIRSTEVENT, ISC_TIMEREVENT_LASTEVENT, NULL); deschedule(timer); UNLINK(manager->timers, timer, link); UNLOCK(&manager->lock); isc_task_detach(&timer->task); DESTROYLOCK(&timer->lock); timer->common.impmagic = 0; timer->common.magic = 0; isc_mem_put(manager->mctx, timer, sizeof(*timer)); }
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. */ XTRACE("isc_task_purge"); return (isc_task_purgerange(task, sender, type, type, tag)); }
ISC_TIMERFUNC_SCOPE isc_result_t isc__timer_reset(isc_timer_t *timer0, isc_timertype_t type, isc_time_t *expires, isc_interval_t *interval, isc_boolean_t purge) { isc__timer_t *timer = (isc__timer_t *)timer0; isc_time_t now; isc__timermgr_t *manager; isc_result_t result; /* * Change the timer's type, expires, and interval values to the given * values. If 'purge' is ISC_TRUE, any pending events from this timer * are purged from its task's event queue. */ REQUIRE(VALID_TIMER(timer)); manager = timer->manager; REQUIRE(VALID_MANAGER(manager)); if (expires == NULL) expires = isc_time_epoch; if (interval == NULL) interval = isc_interval_zero; REQUIRE(type == isc_timertype_inactive || !(isc_time_isepoch(expires) && isc_interval_iszero(interval))); REQUIRE(type != isc_timertype_limited || !(isc_time_isepoch(expires) || isc_interval_iszero(interval))); /* * Get current time. */ if (type != isc_timertype_inactive) { TIME_NOW(&now); } else { /* * We don't have to do this, but it keeps the compiler from * complaining about "now" possibly being used without being * set, even though it will never actually happen. */ isc_time_settoepoch(&now); } LOCK(&manager->lock); LOCK(&timer->lock); if (purge) (void)isc_task_purgerange(timer->task, timer, ISC_TIMEREVENT_FIRSTEVENT, ISC_TIMEREVENT_LASTEVENT, NULL); timer->type = type; timer->expires = *expires; timer->interval = *interval; if (type == isc_timertype_once && !isc_interval_iszero(interval)) { result = isc_time_add(&now, interval, &timer->idle); } else { isc_time_settoepoch(&timer->idle); result = ISC_R_SUCCESS; } if (result == ISC_R_SUCCESS) { if (type == isc_timertype_inactive) { deschedule(timer); result = ISC_R_SUCCESS; } else result = schedule(timer, &now, ISC_TRUE); } UNLOCK(&timer->lock); UNLOCK(&manager->lock); return (result); }
int main(int argc, char *argv[]) { isc_taskmgr_t *manager = NULL; isc_task_t *t1 = NULL, *t2 = NULL; isc_task_t *t3 = NULL, *t4 = NULL; isc_event_t *event; unsigned int workers; isc_timermgr_t *timgr; isc_timer_t *ti1, *ti2; struct isc_interval interval; if (argc > 1) { workers = atoi(argv[1]); if (workers < 1) workers = 1; if (workers > 8192) workers = 8192; } else workers = 2; printf("%d workers\n", workers); RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_task_create(manager, 0, &t1) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_task_create(manager, 0, &t2) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_task_create(manager, 0, &t3) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_task_create(manager, 0, &t4) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_task_onshutdown(t1, my_shutdown, "1") == ISC_R_SUCCESS); RUNTIME_CHECK(isc_task_onshutdown(t2, my_shutdown, "2") == ISC_R_SUCCESS); RUNTIME_CHECK(isc_task_onshutdown(t3, my_shutdown, "3") == ISC_R_SUCCESS); RUNTIME_CHECK(isc_task_onshutdown(t4, my_shutdown, "4") == ISC_R_SUCCESS); timgr = NULL; RUNTIME_CHECK(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS); ti1 = NULL; isc_interval_set(&interval, 1, 0); RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL, &interval, t1, my_tick, "foo", &ti1) == ISC_R_SUCCESS); ti2 = NULL; isc_interval_set(&interval, 1, 0); RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL, &interval, t2, my_tick, "bar", &ti2) == ISC_R_SUCCESS); printf("task 1 = %p\n", t1); printf("task 2 = %p\n", t2); sleep(2); /* * Note: (void *)1 is used as a sender here, since some compilers * don't like casting a function pointer to a (void *). * * In a real use, it is more likely the sender would be a * structure (socket, timer, task, etc) but this is just a test * program. */ event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1", sizeof(*event)); isc_task_send(t1, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1", sizeof(*event)); isc_task_send(t1, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1", sizeof(*event)); isc_task_send(t1, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1", sizeof(*event)); isc_task_send(t1, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1", sizeof(*event)); isc_task_send(t1, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1", sizeof(*event)); isc_task_send(t1, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1", sizeof(*event)); isc_task_send(t1, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1", sizeof(*event)); isc_task_send(t1, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1", sizeof(*event)); isc_task_send(t1, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2", sizeof(*event)); isc_task_send(t2, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3", sizeof(*event)); isc_task_send(t3, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4", sizeof(*event)); isc_task_send(t4, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2", sizeof(*event)); isc_task_send(t2, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3", sizeof(*event)); isc_task_send(t3, &event); event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4", sizeof(*event)); isc_task_send(t4, &event); isc_task_purgerange(t3, NULL, ISC_EVENTTYPE_FIRSTEVENT, ISC_EVENTTYPE_LASTEVENT, NULL); isc_task_detach(&t1); isc_task_detach(&t2); isc_task_detach(&t3); isc_task_detach(&t4); sleep(10); printf("destroy\n"); isc_timer_detach(&ti1); isc_timer_detach(&ti2); isc_timermgr_destroy(&timgr); isc_taskmgr_destroy(&manager); printf("destroyed\n"); isc_mem_stats(mctx, stdout); isc_mem_destroy(&mctx); return (0); }