1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2000-02-03 23:08:31 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000 Internet Software Consortium.
|
1998-12-12 20:48:14 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
1998-10-15 22:22:50 +00:00
|
|
|
|
1998-12-12 19:25:20 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
1998-12-04 20:00:26 +00:00
|
|
|
#include <isc/error.h>
|
1998-10-16 01:18:31 +00:00
|
|
|
#include <isc/thread.h>
|
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/condition.h>
|
|
|
|
#include <isc/heap.h>
|
1998-10-15 22:22:50 +00:00
|
|
|
#include <isc/timer.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1998-10-15 22:22:50 +00:00
|
|
|
|
1998-10-21 01:57:35 +00:00
|
|
|
#ifdef ISC_TIMER_TRACE
|
1998-10-16 21:53:23 +00:00
|
|
|
#define XTRACE(s) printf("%s\n", (s))
|
|
|
|
#define XTRACEID(s, t) printf("%s %p\n", (s), (t))
|
|
|
|
#define XTRACETIME(s, d) printf("%s %lu.%09lu\n", (s), \
|
|
|
|
(d).seconds, (d).nanoseconds)
|
|
|
|
#define XTRACETIMER(s, t, d) printf("%s %p %lu.%09lu\n", (s), (t), \
|
|
|
|
(d).seconds, (d).nanoseconds)
|
|
|
|
#else
|
|
|
|
#define XTRACE(s)
|
|
|
|
#define XTRACEID(s, t)
|
|
|
|
#define XTRACETIME(s, d)
|
|
|
|
#define XTRACETIMER(s, t, d)
|
1998-10-21 01:57:35 +00:00
|
|
|
#endif /* ISC_TIMER_TRACE */
|
1998-10-16 21:53:23 +00:00
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
#define TIMER_MAGIC 0x54494D52U /* TIMR. */
|
|
|
|
#define VALID_TIMER(t) ((t) != NULL && \
|
|
|
|
(t)->magic == TIMER_MAGIC)
|
1998-10-21 01:57:35 +00:00
|
|
|
struct isc_timer {
|
1998-10-15 22:22:50 +00:00
|
|
|
/* Not locked. */
|
|
|
|
unsigned int magic;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timermgr_t * manager;
|
1999-01-16 00:01:18 +00:00
|
|
|
isc_mem_t * mctx;
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_mutex_t lock;
|
1998-10-15 22:22:50 +00:00
|
|
|
/* Locked by timer lock. */
|
|
|
|
unsigned int references;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_time_t idle;
|
1998-10-15 22:22:50 +00:00
|
|
|
/* Locked by manager lock. */
|
1998-10-21 01:57:35 +00:00
|
|
|
isc_timertype_t type;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_time_t expires;
|
|
|
|
isc_interval_t interval;
|
|
|
|
isc_task_t * task;
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_taskaction_t action;
|
1998-10-15 22:22:50 +00:00
|
|
|
void * arg;
|
1998-10-16 01:18:31 +00:00
|
|
|
unsigned int index;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_time_t due;
|
|
|
|
LINK(isc_timer_t) link;
|
1998-10-15 22:22:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define TIMER_MANAGER_MAGIC 0x54494D4DU /* TIMM. */
|
|
|
|
#define VALID_MANAGER(m) ((m) != NULL && \
|
|
|
|
(m)->magic == TIMER_MANAGER_MAGIC)
|
|
|
|
|
1998-10-21 01:57:35 +00:00
|
|
|
struct isc_timermgr {
|
1998-10-15 22:22:50 +00:00
|
|
|
/* Not locked. */
|
|
|
|
unsigned int magic;
|
1998-12-18 19:14:37 +00:00
|
|
|
isc_mem_t * mctx;
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_mutex_t lock;
|
1998-10-15 22:22:50 +00:00
|
|
|
/* Locked by manager lock. */
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_boolean_t done;
|
1998-12-13 23:45:21 +00:00
|
|
|
LIST(isc_timer_t) timers;
|
1998-10-16 01:18:31 +00:00
|
|
|
unsigned int nscheduled;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_time_t due;
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_condition_t wakeup;
|
|
|
|
isc_thread_t thread;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_heap_t * heap;
|
1998-10-15 22:22:50 +00:00
|
|
|
};
|
|
|
|
|
1998-10-26 23:08:23 +00:00
|
|
|
static inline isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
schedule(isc_timer_t *timer, isc_time_t *now, isc_boolean_t signal_ok) {
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t result;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timermgr_t *manager;
|
|
|
|
isc_time_t due;
|
1998-10-16 19:56:56 +00:00
|
|
|
int cmp;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
1998-10-16 18:46:38 +00:00
|
|
|
* Note: the caller must ensure locking.
|
|
|
|
*/
|
|
|
|
|
1999-05-10 22:50:31 +00:00
|
|
|
REQUIRE(timer->type != isc_timertype_inactive);
|
|
|
|
|
1998-10-16 18:46:38 +00:00
|
|
|
/*
|
|
|
|
* Compute the new due time.
|
1998-10-16 01:18:31 +00:00
|
|
|
*/
|
1998-10-21 01:57:35 +00:00
|
|
|
if (timer->type == isc_timertype_ticker)
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_time_add(now, &timer->interval, &due);
|
1998-10-16 18:46:38 +00:00
|
|
|
else {
|
1998-10-23 22:59:44 +00:00
|
|
|
if (isc_time_isepoch(&timer->idle))
|
1998-10-16 19:56:56 +00:00
|
|
|
due = timer->expires;
|
1998-10-23 22:59:44 +00:00
|
|
|
else if (isc_time_isepoch(&timer->expires))
|
1998-10-16 19:56:56 +00:00
|
|
|
due = timer->idle;
|
1998-10-22 01:33:20 +00:00
|
|
|
else if (isc_time_compare(&timer->idle, &timer->expires) < 0)
|
1998-10-16 18:46:38 +00:00
|
|
|
due = timer->idle;
|
|
|
|
else
|
|
|
|
due = timer->expires;
|
|
|
|
}
|
1998-10-16 01:18:31 +00:00
|
|
|
|
1998-10-16 18:46:38 +00:00
|
|
|
/*
|
|
|
|
* Schedule the timer.
|
|
|
|
*/
|
1998-10-16 01:18:31 +00:00
|
|
|
manager = timer->manager;
|
|
|
|
if (timer->index > 0) {
|
|
|
|
/*
|
|
|
|
* Already scheduled.
|
|
|
|
*/
|
1998-10-22 01:33:20 +00:00
|
|
|
cmp = isc_time_compare(&due, &timer->due);
|
1998-10-16 19:56:56 +00:00
|
|
|
timer->due = due;
|
|
|
|
switch (cmp) {
|
1998-10-16 01:18:31 +00:00
|
|
|
case -1:
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_increased(manager->heap, timer->index);
|
1998-10-16 01:18:31 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_decreased(manager->heap, timer->index);
|
1998-10-16 01:18:31 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
/* Nothing to do. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
1998-10-16 19:56:56 +00:00
|
|
|
timer->due = due;
|
1998-10-21 01:13:50 +00:00
|
|
|
result = isc_heap_insert(manager->heap, timer);
|
1998-10-16 01:18:31 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
INSIST(result == ISC_R_NOMEMORY);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
manager->nscheduled++;
|
|
|
|
}
|
1998-10-16 19:56:56 +00:00
|
|
|
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACETIMER("schedule", timer, due);
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
1998-10-16 18:46:38 +00:00
|
|
|
* If this timer is at the head of the queue, we wake up the run
|
|
|
|
* thread. We do this, because we likely have set a more recent
|
|
|
|
* due time than the one the run thread is sleeping on, and we don't
|
|
|
|
* want it to oversleep.
|
1998-10-16 01:18:31 +00:00
|
|
|
*/
|
1998-10-22 19:23:26 +00:00
|
|
|
if (timer->index == 1 && signal_ok) {
|
|
|
|
XTRACE("signal (schedule)");
|
|
|
|
SIGNAL(&manager->wakeup);
|
1998-10-16 19:56:56 +00:00
|
|
|
}
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
static inline void
|
1998-12-13 23:45:21 +00:00
|
|
|
deschedule(isc_timer_t *timer) {
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_boolean_t need_wakeup = ISC_FALSE;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timermgr_t *manager;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
/*
|
|
|
|
* The caller must ensure locking.
|
|
|
|
*/
|
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
manager = timer->manager;
|
|
|
|
if (timer->index > 0) {
|
|
|
|
if (timer->index == 1)
|
1998-10-21 01:13:50 +00:00
|
|
|
need_wakeup = ISC_TRUE;
|
|
|
|
isc_heap_delete(manager->heap, timer->index);
|
1998-10-16 01:18:31 +00:00
|
|
|
timer->index = 0;
|
|
|
|
INSIST(manager->nscheduled > 0);
|
|
|
|
manager->nscheduled--;
|
1998-10-16 19:56:56 +00:00
|
|
|
if (need_wakeup) {
|
1998-10-22 19:23:26 +00:00
|
|
|
XTRACE("signal (deschedule)");
|
|
|
|
SIGNAL(&manager->wakeup);
|
1998-10-16 19:56:56 +00:00
|
|
|
}
|
1998-10-16 01:18:31 +00:00
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
destroy(isc_timer_t *timer) {
|
|
|
|
isc_timermgr_t *manager = timer->manager;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
1999-05-10 22:50:31 +00:00
|
|
|
* The caller must ensure it is safe to destroy the timer.
|
1998-10-15 22:22:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
1999-05-10 22:50:31 +00:00
|
|
|
isc_task_purgerange(timer->task,
|
|
|
|
timer,
|
1999-07-21 21:18:10 +00:00
|
|
|
ISC_TIMEREVENT_FIRSTEVENT,
|
|
|
|
ISC_TIMEREVENT_LASTEVENT,
|
1999-07-10 00:55:07 +00:00
|
|
|
NULL);
|
1998-10-15 22:22:50 +00:00
|
|
|
deschedule(timer);
|
|
|
|
UNLINK(manager->timers, timer, link);
|
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_detach(&timer->task);
|
1998-10-22 01:33:20 +00:00
|
|
|
(void)isc_mutex_destroy(&timer->lock);
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->magic = 0;
|
1999-01-16 00:01:18 +00:00
|
|
|
isc_mem_put(timer->mctx, timer, sizeof *timer);
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timer_create(isc_timermgr_t *manager, isc_timertype_t type,
|
|
|
|
isc_time_t *expires, isc_interval_t *interval,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, void *arg,
|
|
|
|
isc_timer_t **timerp)
|
1998-10-15 22:22:50 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timer_t *timer;
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t result;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_time_t now;
|
1999-01-16 00:01:18 +00:00
|
|
|
isc_mem_t *mctx;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new 'type' timer managed by 'manager'. The timers
|
1998-10-16 18:46:38 +00:00
|
|
|
* parameters are specified by 'expires' and 'interval'. Events
|
1998-10-16 01:18:31 +00:00
|
|
|
* will be posted to 'task' and when dispatched 'action' will be
|
|
|
|
* called with 'arg' as the arg value. The new timer is returned
|
|
|
|
* in 'timerp'.
|
1998-10-15 22:22:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(task != NULL);
|
1998-10-16 01:18:31 +00:00
|
|
|
REQUIRE(action != NULL);
|
1999-06-12 01:13:22 +00:00
|
|
|
if (expires == NULL)
|
|
|
|
expires = isc_time_epoch;
|
|
|
|
if (interval == NULL)
|
|
|
|
interval = isc_interval_zero;
|
|
|
|
REQUIRE(type == isc_timertype_inactive ||
|
1999-05-10 22:50:31 +00:00
|
|
|
!(isc_time_isepoch(expires) && isc_interval_iszero(interval)));
|
1998-10-15 22:22:50 +00:00
|
|
|
REQUIRE(timerp != NULL && *timerp == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get current time.
|
|
|
|
*/
|
1999-07-03 21:00:55 +00:00
|
|
|
if (type != isc_timertype_inactive) {
|
|
|
|
result = isc_time_now(&now);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_time_now() failed: %s",
|
|
|
|
isc_result_totext(result));
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
} 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);
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
1999-07-03 21:00:55 +00:00
|
|
|
|
1999-01-16 00:01:18 +00:00
|
|
|
mctx = isc_task_mem(task);
|
|
|
|
timer = isc_mem_get(mctx, sizeof *timer);
|
1998-10-15 22:22:50 +00:00
|
|
|
if (timer == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
timer->manager = manager;
|
1999-01-16 00:01:18 +00:00
|
|
|
timer->mctx = mctx;
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->references = 1;
|
1998-10-23 22:59:44 +00:00
|
|
|
if (type == isc_timertype_once && !isc_interval_iszero(interval))
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_time_add(&now, interval, &timer->idle);
|
1998-10-23 22:59:44 +00:00
|
|
|
else
|
|
|
|
isc_time_settoepoch(&timer->idle);
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->type = type;
|
1998-10-22 01:33:20 +00:00
|
|
|
timer->expires = *expires;
|
|
|
|
timer->interval = *interval;
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->task = NULL;
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_attach(task, &timer->task);
|
1998-10-16 01:18:31 +00:00
|
|
|
timer->action = action;
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->arg = arg;
|
1998-10-16 01:18:31 +00:00
|
|
|
timer->index = 0;
|
1998-10-22 01:33:20 +00:00
|
|
|
if (isc_mutex_init(&timer->lock) != ISC_R_SUCCESS) {
|
1998-12-13 02:03:46 +00:00
|
|
|
isc_task_detach(&timer->task);
|
1999-01-16 00:01:18 +00:00
|
|
|
isc_mem_put(mctx, timer, sizeof *timer);
|
1998-10-22 01:33:20 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_mutex_init() failed");
|
1998-10-15 22:22:50 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
1998-12-13 02:03:46 +00:00
|
|
|
timer->magic = TIMER_MAGIC;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note we don't have to lock the timer like we normally would because
|
|
|
|
* there are no external references to it yet.
|
|
|
|
*/
|
|
|
|
|
1999-05-10 22:50:31 +00:00
|
|
|
if (type != isc_timertype_inactive)
|
|
|
|
result = schedule(timer, &now, ISC_TRUE);
|
|
|
|
else
|
|
|
|
result = ISC_R_SUCCESS;
|
1998-12-13 02:03:46 +00:00
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
APPEND(manager->timers, timer, link);
|
1999-05-10 22:50:31 +00:00
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-12-13 02:03:46 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
timer->magic = 0;
|
|
|
|
(void)isc_mutex_destroy(&timer->lock);
|
|
|
|
isc_task_detach(&timer->task);
|
1999-01-16 00:01:18 +00:00
|
|
|
isc_mem_put(mctx, timer, sizeof *timer);
|
1998-12-13 02:03:46 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
*timerp = timer;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
1998-12-13 02:03:46 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timer_reset(isc_timer_t *timer, isc_timertype_t type,
|
|
|
|
isc_time_t *expires, isc_interval_t *interval,
|
1998-10-23 22:59:44 +00:00
|
|
|
isc_boolean_t purge)
|
1998-10-15 22:22:50 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_time_t now;
|
|
|
|
isc_timermgr_t *manager;
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t result;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
1998-10-16 23:57:51 +00:00
|
|
|
* Change the timer's type, expires, and interval values to the given
|
1998-10-21 01:13:50 +00:00
|
|
|
* values. If 'purge' is ISC_TRUE, any pending events from this timer
|
1998-10-16 23:57:51 +00:00
|
|
|
* are purged from its task's event queue.
|
1998-10-15 22:22:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_TIMER(timer));
|
|
|
|
manager = timer->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
1999-06-12 01:13:22 +00:00
|
|
|
if (expires == NULL)
|
|
|
|
expires = isc_time_epoch;
|
|
|
|
if (interval == NULL)
|
|
|
|
interval = isc_interval_zero;
|
|
|
|
REQUIRE(type == isc_timertype_inactive ||
|
1999-05-10 22:50:31 +00:00
|
|
|
!(isc_time_isepoch(expires) && isc_interval_iszero(interval)));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get current time.
|
|
|
|
*/
|
1999-07-03 21:00:55 +00:00
|
|
|
if (type != isc_timertype_inactive) {
|
|
|
|
result = isc_time_now(&now);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_time_now() failed: %s",
|
|
|
|
isc_result_totext(result));
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
} 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);
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
manager = timer->manager;
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
|
1998-10-16 23:57:51 +00:00
|
|
|
if (purge)
|
1999-05-10 22:50:31 +00:00
|
|
|
isc_task_purgerange(timer->task,
|
|
|
|
timer,
|
1999-07-21 21:18:10 +00:00
|
|
|
ISC_TIMEREVENT_FIRSTEVENT,
|
|
|
|
ISC_TIMEREVENT_LASTEVENT,
|
1999-07-10 00:55:07 +00:00
|
|
|
NULL);
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->type = type;
|
1998-10-22 01:33:20 +00:00
|
|
|
timer->expires = *expires;
|
|
|
|
timer->interval = *interval;
|
1998-10-23 22:59:44 +00:00
|
|
|
if (type == isc_timertype_once && !isc_interval_iszero(interval))
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_time_add(&now, interval, &timer->idle);
|
1998-10-23 22:59:44 +00:00
|
|
|
else
|
|
|
|
isc_time_settoepoch(&timer->idle);
|
1999-05-10 22:50:31 +00:00
|
|
|
if (type == isc_timertype_inactive) {
|
|
|
|
deschedule(timer);
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
} else
|
|
|
|
result = schedule(timer, &now, ISC_TRUE);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timer_touch(isc_timer_t *timer) {
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t result;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_time_t now;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
/*
|
|
|
|
* Set the last-touched time of 'timer' to the current time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_TIMER(timer));
|
|
|
|
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
|
1999-05-10 22:50:31 +00:00
|
|
|
/*
|
|
|
|
* We'd like to
|
|
|
|
*
|
|
|
|
* REQUIRE(timer->type == isc_timertype_once);
|
|
|
|
*
|
|
|
|
* but we cannot without locking the manager lock too, which we
|
|
|
|
* don't want to do.
|
|
|
|
*/
|
1998-10-15 22:22:50 +00:00
|
|
|
|
1999-06-12 01:13:22 +00:00
|
|
|
result = isc_time_now(&now);
|
1998-10-15 22:22:50 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
1998-10-21 01:13:50 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
1999-06-12 01:13:22 +00:00
|
|
|
"isc_time_now() failed: %s",
|
1998-10-21 01:16:18 +00:00
|
|
|
isc_result_totext(result));
|
1998-10-15 22:22:50 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_time_add(&now, &timer->interval, &timer->idle);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) {
|
1998-10-15 22:22:50 +00:00
|
|
|
/*
|
|
|
|
* Attach *timerp to timer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_TIMER(timer));
|
|
|
|
REQUIRE(timerp != NULL && *timerp == NULL);
|
|
|
|
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
timer->references++;
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
|
|
|
*timerp = timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timer_detach(isc_timer_t **timerp) {
|
|
|
|
isc_timer_t *timer;
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_boolean_t free_timer = ISC_FALSE;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Detach *timerp from its timer.
|
|
|
|
*/
|
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
REQUIRE(timerp != NULL);
|
|
|
|
timer = *timerp;
|
1998-10-15 22:22:50 +00:00
|
|
|
REQUIRE(VALID_TIMER(timer));
|
|
|
|
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
REQUIRE(timer->references > 0);
|
|
|
|
timer->references--;
|
|
|
|
if (timer->references == 0)
|
1998-10-21 01:13:50 +00:00
|
|
|
free_timer = ISC_TRUE;
|
1998-10-15 22:22:50 +00:00
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
|
|
|
if (free_timer)
|
|
|
|
destroy(timer);
|
|
|
|
|
|
|
|
*timerp = NULL;
|
|
|
|
}
|
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
dispatch(isc_timermgr_t *manager, isc_time_t *now) {
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_boolean_t done = ISC_FALSE, post_event, need_schedule;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_t *event;
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_eventtype_t type = 0;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timer_t *timer;
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t result;
|
1998-10-16 07:44:20 +00:00
|
|
|
|
1999-05-10 22:50:31 +00:00
|
|
|
/*
|
|
|
|
* The caller must be holding the manager lock.
|
|
|
|
*/
|
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
while (manager->nscheduled > 0 && !done) {
|
1998-10-21 01:13:50 +00:00
|
|
|
timer = isc_heap_element(manager->heap, 1);
|
1999-05-10 22:50:31 +00:00
|
|
|
INSIST(timer->type != isc_timertype_inactive);
|
1998-10-22 01:33:20 +00:00
|
|
|
if (isc_time_compare(now, &timer->due) >= 0) {
|
1998-10-21 01:57:35 +00:00
|
|
|
if (timer->type == isc_timertype_ticker) {
|
|
|
|
type = ISC_TIMEREVENT_TICK;
|
1998-10-21 01:13:50 +00:00
|
|
|
post_event = ISC_TRUE;
|
|
|
|
need_schedule = ISC_TRUE;
|
1998-10-23 22:59:44 +00:00
|
|
|
} else if (!isc_time_isepoch(&timer->expires) &&
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_time_compare(now,
|
1998-10-23 22:59:44 +00:00
|
|
|
&timer->expires) >= 0) {
|
1998-10-21 01:57:35 +00:00
|
|
|
type = ISC_TIMEREVENT_LIFE;
|
1998-10-21 01:13:50 +00:00
|
|
|
post_event = ISC_TRUE;
|
|
|
|
need_schedule = ISC_FALSE;
|
1998-10-23 22:59:44 +00:00
|
|
|
} else if (!isc_time_isepoch(&timer->idle) &&
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_time_compare(now,
|
1998-10-23 22:59:44 +00:00
|
|
|
&timer->idle) >= 0) {
|
1998-10-21 01:57:35 +00:00
|
|
|
type = ISC_TIMEREVENT_IDLE;
|
1998-10-21 01:13:50 +00:00
|
|
|
post_event = ISC_TRUE;
|
|
|
|
need_schedule = ISC_FALSE;
|
1998-10-16 07:44:20 +00:00
|
|
|
} else {
|
|
|
|
/*
|
1998-10-16 18:46:38 +00:00
|
|
|
* Idle timer has been touched; reschedule.
|
1998-10-16 07:44:20 +00:00
|
|
|
*/
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACEID("idle reschedule", timer);
|
1998-10-21 01:13:50 +00:00
|
|
|
post_event = ISC_FALSE;
|
|
|
|
need_schedule = ISC_TRUE;
|
1998-10-16 07:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (post_event) {
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACEID("posting", timer);
|
1999-01-15 02:14:55 +00:00
|
|
|
/*
|
|
|
|
* XXX We could preallocate this event.
|
|
|
|
*/
|
1999-01-16 00:01:18 +00:00
|
|
|
event = isc_event_allocate(timer->mctx,
|
1998-10-21 02:26:57 +00:00
|
|
|
timer,
|
|
|
|
type,
|
|
|
|
timer->action,
|
|
|
|
timer->arg,
|
|
|
|
sizeof *event);
|
1998-10-16 21:41:30 +00:00
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
if (event != NULL)
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_task_send(timer->task, &event);
|
1998-10-16 07:44:20 +00:00
|
|
|
else
|
1998-10-21 01:13:50 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
1998-10-16 07:44:20 +00:00
|
|
|
"couldn't allocate event");
|
|
|
|
}
|
|
|
|
|
|
|
|
timer->index = 0;
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_delete(manager->heap, 1);
|
1998-10-16 07:44:20 +00:00
|
|
|
manager->nscheduled--;
|
|
|
|
|
|
|
|
if (need_schedule) {
|
1998-10-22 01:33:20 +00:00
|
|
|
result = schedule(timer, now, ISC_FALSE);
|
1998-10-16 07:44:20 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
1998-10-21 01:13:50 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
1998-10-16 07:44:20 +00:00
|
|
|
"couldn't schedule timer: %s",
|
|
|
|
result);
|
|
|
|
}
|
|
|
|
} else {
|
1998-10-16 18:46:38 +00:00
|
|
|
manager->due = timer->due;
|
1998-10-21 01:13:50 +00:00
|
|
|
done = ISC_TRUE;
|
1998-10-16 07:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-23 05:45:26 +00:00
|
|
|
static isc_threadresult_t
|
1998-10-23 22:59:44 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
WINAPI
|
|
|
|
#endif
|
1998-10-16 01:18:31 +00:00
|
|
|
run(void *uap) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timermgr_t *manager = uap;
|
|
|
|
isc_time_t now;
|
1998-10-22 01:33:20 +00:00
|
|
|
isc_result_t result;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
while (!manager->done) {
|
1999-06-12 01:13:22 +00:00
|
|
|
RUNTIME_CHECK(isc_time_now(&now) == ISC_R_SUCCESS);
|
1998-10-16 21:53:23 +00:00
|
|
|
|
|
|
|
XTRACETIME("running", now);
|
1998-10-16 01:18:31 +00:00
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
dispatch(manager, &now);
|
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
if (manager->nscheduled > 0) {
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACETIME("waituntil", manager->due);
|
1998-10-22 01:33:20 +00:00
|
|
|
result = WAITUNTIL(&manager->wakeup, &manager->lock,
|
|
|
|
&manager->due);
|
|
|
|
INSIST(result == ISC_R_SUCCESS ||
|
|
|
|
result == ISC_R_TIMEDOUT);
|
1998-10-16 19:56:56 +00:00
|
|
|
} else {
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACE("wait");
|
1998-10-16 01:18:31 +00:00
|
|
|
WAIT(&manager->wakeup, &manager->lock);
|
1998-10-16 19:56:56 +00:00
|
|
|
}
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACE("wakeup");
|
1998-10-16 01:18:31 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-10-23 05:45:26 +00:00
|
|
|
return ((isc_threadresult_t)0);
|
1998-10-16 01:18:31 +00:00
|
|
|
}
|
|
|
|
|
1998-10-21 01:13:50 +00:00
|
|
|
static isc_boolean_t
|
1998-10-16 21:54:53 +00:00
|
|
|
sooner(void *v1, void *v2) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timer_t *t1, *t2;
|
1998-10-16 21:54:53 +00:00
|
|
|
|
|
|
|
t1 = v1;
|
|
|
|
t2 = v2;
|
|
|
|
REQUIRE(VALID_TIMER(t1));
|
|
|
|
REQUIRE(VALID_TIMER(t2));
|
|
|
|
|
1998-10-22 01:33:20 +00:00
|
|
|
if (isc_time_compare(&t1->due, &t2->due) < 0)
|
1998-10-21 01:13:50 +00:00
|
|
|
return (ISC_TRUE);
|
|
|
|
return (ISC_FALSE);
|
1998-10-16 21:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_index(void *what, unsigned int index) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timer_t *timer;
|
1998-10-16 21:54:53 +00:00
|
|
|
|
|
|
|
timer = what;
|
|
|
|
REQUIRE(VALID_TIMER(timer));
|
|
|
|
|
|
|
|
timer->index = index;
|
|
|
|
}
|
|
|
|
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t
|
1998-12-18 19:14:37 +00:00
|
|
|
isc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timermgr_t *manager;
|
1998-10-26 23:08:23 +00:00
|
|
|
isc_result_t result;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a timer manager.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(managerp != NULL && *managerp == NULL);
|
|
|
|
|
1998-10-21 22:01:08 +00:00
|
|
|
manager = isc_mem_get(mctx, sizeof *manager);
|
1998-10-16 01:18:31 +00:00
|
|
|
if (manager == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
manager->magic = TIMER_MANAGER_MAGIC;
|
|
|
|
manager->mctx = mctx;
|
1998-10-21 01:13:50 +00:00
|
|
|
manager->done = ISC_FALSE;
|
1998-10-16 01:18:31 +00:00
|
|
|
INIT_LIST(manager->timers);
|
|
|
|
manager->nscheduled = 0;
|
1998-10-23 22:59:44 +00:00
|
|
|
isc_time_settoepoch(&manager->due);
|
1998-10-16 01:18:31 +00:00
|
|
|
manager->heap = NULL;
|
1998-10-21 01:13:50 +00:00
|
|
|
result = isc_heap_create(mctx, sooner, set_index, 0, &manager->heap);
|
1998-10-16 01:18:31 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
INSIST(result == ISC_R_NOMEMORY);
|
1998-10-21 22:01:08 +00:00
|
|
|
isc_mem_put(mctx, manager, sizeof *manager);
|
1998-10-16 01:18:31 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
1998-10-22 01:33:20 +00:00
|
|
|
if (isc_mutex_init(&manager->lock) != ISC_R_SUCCESS) {
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_destroy(&manager->heap);
|
1998-10-21 22:01:08 +00:00
|
|
|
isc_mem_put(mctx, manager, sizeof *manager);
|
1998-10-22 01:33:20 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_mutex_init() failed");
|
1998-10-16 01:18:31 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
1998-10-22 01:33:20 +00:00
|
|
|
if (isc_condition_init(&manager->wakeup) != ISC_R_SUCCESS) {
|
|
|
|
(void)isc_mutex_destroy(&manager->lock);
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_destroy(&manager->heap);
|
1998-10-21 22:01:08 +00:00
|
|
|
isc_mem_put(mctx, manager, sizeof *manager);
|
1998-10-21 01:13:50 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
1998-10-22 01:33:20 +00:00
|
|
|
"isc_condition_init() failed");
|
1998-10-16 01:18:31 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
1998-10-22 01:33:20 +00:00
|
|
|
if (isc_thread_create(run, manager, &manager->thread) !=
|
|
|
|
ISC_R_SUCCESS) {
|
|
|
|
(void)isc_condition_destroy(&manager->wakeup);
|
|
|
|
(void)isc_mutex_destroy(&manager->lock);
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_destroy(&manager->heap);
|
1998-10-21 22:01:08 +00:00
|
|
|
isc_mem_put(mctx, manager, sizeof *manager);
|
1998-10-21 01:13:50 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
1998-10-22 01:33:20 +00:00
|
|
|
"isc_thread_create() failed");
|
1998-10-16 01:18:31 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
*managerp = manager;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_timermgr_destroy(isc_timermgr_t **managerp) {
|
|
|
|
isc_timermgr_t *manager;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a timer manager.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(managerp != NULL);
|
|
|
|
manager = *managerp;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
|
|
|
REQUIRE(EMPTY(manager->timers));
|
1998-10-21 01:13:50 +00:00
|
|
|
manager->done = ISC_TRUE;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
1998-10-22 19:23:26 +00:00
|
|
|
XTRACE("signal (destroy)");
|
|
|
|
SIGNAL(&manager->wakeup);
|
1998-10-16 01:18:31 +00:00
|
|
|
|
1998-12-16 02:04:43 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
/*
|
|
|
|
* Wait for thread to exit.
|
|
|
|
*/
|
1998-10-23 05:45:26 +00:00
|
|
|
if (isc_thread_join(manager->thread, NULL) != ISC_R_SUCCESS)
|
1998-10-21 01:13:50 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
1998-10-22 01:33:20 +00:00
|
|
|
"isc_thread_join() failed");
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up.
|
|
|
|
*/
|
1998-10-22 01:33:20 +00:00
|
|
|
(void)isc_condition_destroy(&manager->wakeup);
|
|
|
|
(void)isc_mutex_destroy(&manager->lock);
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_destroy(&manager->heap);
|
1998-10-16 01:18:31 +00:00
|
|
|
manager->magic = 0;
|
1998-10-21 22:01:08 +00:00
|
|
|
isc_mem_put(manager->mctx, manager, sizeof *manager);
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
*managerp = NULL;
|
|
|
|
}
|