2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 18:49:54 +00:00
bind/lib/isc/timer.c

1073 lines
26 KiB
C
Raw Normal View History

1998-12-12 20:48:14 +00:00
/*
2012-03-10 23:45:53 +00:00
* Copyright (C) 2004, 2005, 2007-2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
2004-03-05 05:14:21 +00:00
* Copyright (C) 1998-2002 Internet Software Consortium.
*
2007-06-18 23:47:57 +00:00
* Permission to use, copy, modify, and/or distribute this software for any
1998-12-12 20:48:14 +00:00
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
2004-03-05 05:14:21 +00:00
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC 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-12-12 20:48:14 +00:00
*/
1998-10-15 22:22:50 +00:00
2012-03-08 00:21:15 +11:00
/* $Id$ */
/*! \file */
2000-06-22 22:00:42 +00:00
1998-12-12 19:25:20 +00:00
#include <config.h>
#include <isc/condition.h>
1998-10-16 01:18:31 +00:00
#include <isc/heap.h>
#include <isc/log.h>
#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/msgs.h>
#include <isc/platform.h>
2000-04-25 19:32:29 +00:00
#include <isc/task.h>
#include <isc/thread.h>
#include <isc/time.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
2009-10-05 17:30:49 +00:00
#ifdef OPENSSL_LEAKS
#include <openssl/err.h>
#endif
/* See task.c about the following definition: */
#ifdef BIND9
#ifdef ISC_PLATFORM_USETHREADS
#define USE_TIMER_THREAD
#else
#define USE_SHARED_MANAGER
#endif /* ISC_PLATFORM_USETHREADS */
#endif /* BIND9 */
#ifndef USE_TIMER_THREAD
#include "timer_p.h"
#endif /* USE_TIMER_THREAD */
1998-10-21 01:57:35 +00:00
#ifdef ISC_TIMER_TRACE
#define XTRACE(s) fprintf(stderr, "%s\n", (s))
#define XTRACEID(s, t) fprintf(stderr, "%s %p\n", (s), (t))
#define XTRACETIME(s, d) fprintf(stderr, "%s %u.%09u\n", (s), \
1998-10-16 21:53:23 +00:00
(d).seconds, (d).nanoseconds)
#define XTRACETIME2(s, d, n) fprintf(stderr, "%s %u.%09u %u.%09u\n", (s), \
(d).seconds, (d).nanoseconds, (n).seconds, (n).nanoseconds)
#define XTRACETIMER(s, t, d) fprintf(stderr, "%s %p %u.%09u\n", (s), (t), \
1998-10-16 21:53:23 +00:00
(d).seconds, (d).nanoseconds)
#else
#define XTRACE(s)
#define XTRACEID(s, t)
#define XTRACETIME(s, d)
#define XTRACETIME2(s, d, n)
1998-10-16 21:53:23 +00:00
#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
#define TIMER_MAGIC ISC_MAGIC('T', 'I', 'M', 'R')
#define VALID_TIMER(t) ISC_MAGIC_VALID(t, TIMER_MAGIC)
typedef struct isc__timer isc__timer_t;
typedef struct isc__timermgr isc__timermgr_t;
struct isc__timer {
/*! Not locked. */
isc_timer_t common;
isc__timermgr_t * manager;
1998-10-22 01:33:20 +00:00
isc_mutex_t lock;
/*! Locked by timer lock. */
1998-10-15 22:22:50 +00:00
unsigned int references;
1998-12-13 23:45:21 +00:00
isc_time_t idle;
/*! 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 ISC_MAGIC('T', 'I', 'M', 'M')
#define VALID_MANAGER(m) ISC_MAGIC_VALID(m, TIMER_MANAGER_MAGIC)
1998-10-15 22:22:50 +00:00
struct isc__timermgr {
1998-10-15 22:22:50 +00:00
/* Not locked. */
isc_timermgr_t common;
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;
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;
#ifdef USE_TIMER_THREAD
1998-10-22 01:33:20 +00:00
isc_condition_t wakeup;
isc_thread_t thread;
#endif /* USE_TIMER_THREAD */
#ifdef USE_SHARED_MANAGER
unsigned int refs;
#endif /* USE_SHARED_MANAGER */
1998-12-13 23:45:21 +00:00
isc_heap_t * heap;
1998-10-15 22:22:50 +00:00
};
/*%
* The followings can be either static or public, depending on build
* environment.
*/
#ifdef BIND9
#define ISC_TIMERFUNC_SCOPE
#else
#define ISC_TIMERFUNC_SCOPE static
#endif
ISC_TIMERFUNC_SCOPE isc_result_t
isc__timer_create(isc_timermgr_t *manager, isc_timertype_t type,
const isc_time_t *expires, const isc_interval_t *interval,
isc_task_t *task, isc_taskaction_t action, const void *arg,
isc_timer_t **timerp);
ISC_TIMERFUNC_SCOPE isc_result_t
isc__timer_reset(isc_timer_t *timer, isc_timertype_t type,
const isc_time_t *expires, const isc_interval_t *interval,
isc_boolean_t purge);
ISC_TIMERFUNC_SCOPE isc_timertype_t
isc__timer_gettype(isc_timer_t *timer);
ISC_TIMERFUNC_SCOPE isc_result_t
isc__timer_touch(isc_timer_t *timer);
ISC_TIMERFUNC_SCOPE void
isc__timer_attach(isc_timer_t *timer0, isc_timer_t **timerp);
ISC_TIMERFUNC_SCOPE void
isc__timer_detach(isc_timer_t **timerp);
ISC_TIMERFUNC_SCOPE isc_result_t
isc__timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp);
ISC_TIMERFUNC_SCOPE void
isc__timermgr_poke(isc_timermgr_t *manager0);
ISC_TIMERFUNC_SCOPE void
isc__timermgr_destroy(isc_timermgr_t **managerp);
static struct isc__timermethods {
isc_timermethods_t methods;
/*%
* The following are defined just for avoiding unused static functions.
*/
2009-09-02 04:25:19 +00:00
#ifndef BIND9
void *gettype;
2009-09-02 04:25:19 +00:00
#endif
} timermethods = {
{
isc__timer_attach,
isc__timer_detach,
isc__timer_reset,
isc__timer_touch
2009-09-02 04:25:19 +00:00
}
#ifndef BIND9
,
(void *)isc__timer_gettype
#endif
};
static struct isc__timermgrmethods {
isc_timermgrmethods_t methods;
#ifndef BIND9
void *poke; /* see above */
#endif
} timermgrmethods = {
{
isc__timermgr_destroy,
isc__timer_create
2009-09-02 04:25:19 +00:00
}
#ifndef BIND9
2009-09-02 04:25:19 +00:00
,
(void *)isc__timermgr_poke
#endif
};
#ifdef USE_SHARED_MANAGER
/*!
* If the manager is supposed to be shared, there can be only one.
*/
static isc__timermgr_t *timermgr = NULL;
#endif /* USE_SHARED_MANAGER */
1998-10-26 23:08:23 +00:00
static inline isc_result_t
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;
isc__timermgr_t *manager;
1998-12-13 23:45:21 +00:00
isc_time_t due;
1998-10-16 19:56:56 +00:00
int cmp;
#ifdef USE_TIMER_THREAD
isc_boolean_t timedwait;
#endif
1998-10-16 01:18:31 +00:00
/*!
1998-10-16 18:46:38 +00:00
* Note: the caller must ensure locking.
*/
REQUIRE(timer->type != isc_timertype_inactive);
#ifndef USE_TIMER_THREAD
UNUSED(signal_ok);
#endif /* USE_TIMER_THREAD */
manager = timer->manager;
#ifdef USE_TIMER_THREAD
/*!
* If the manager was timed wait, we may need to signal the
* manager to force a wakeup.
*/
2002-09-12 02:44:29 +00:00
timedwait = ISC_TF(manager->nscheduled > 0 &&
isc_time_seconds(&manager->due) != 0);
#endif
1998-10-16 18:46:38 +00:00
/*
* Compute the new due time.
1998-10-16 01:18:31 +00:00
*/
if (timer->type != isc_timertype_once) {
result = isc_time_add(now, &timer->interval, &due);
if (result != ISC_R_SUCCESS)
return (result);
if (timer->type == isc_timertype_limited &&
isc_time_compare(&timer->expires, &due) < 0)
due = timer->expires;
} else {
if (isc_time_isepoch(&timer->idle))
1998-10-16 19:56:56 +00:00
due = timer->expires;
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 18:46:38 +00:00
/*
* Schedule the timer.
*/
1998-10-16 01:18:31 +00:00
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
XTRACETIMER(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TIMER,
ISC_MSG_SCHEDULE, "schedule"), timer, due);
1998-10-16 01:18:31 +00:00
/*
* If this timer is at the head of the queue, we need to ensure
* that we won't miss it if it has a more recent due time than
* the current "next" timer. We do this either by waking up the
* run thread, or explicitly setting the value in the manager.
1998-10-16 01:18:31 +00:00
*/
#ifdef USE_TIMER_THREAD
/*
* This is a temporary (probably) hack to fix a bug on tru64 5.1
* and 5.1a. Sometimes, pthread_cond_timedwait() doesn't actually
* return when the time expires, so here, we check to see if
* we're 15 seconds or more behind, and if we are, we signal
* the dispatcher. This isn't such a bad idea as a general purpose
* watchdog, so perhaps we should just leave it in here.
*/
if (signal_ok && timedwait) {
isc_interval_t fifteen;
isc_time_t then;
isc_interval_set(&fifteen, 15, 0);
result = isc_time_add(&manager->due, &fifteen, &then);
if (result == ISC_R_SUCCESS &&
isc_time_compare(&then, now) < 0) {
SIGNAL(&manager->wakeup);
signal_ok = ISC_FALSE;
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_TIMER, ISC_LOG_WARNING,
"*** POKED TIMER ***");
}
}
2008-06-23 23:47:11 +00:00
if (timer->index == 1 && signal_ok) {
XTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TIMER,
ISC_MSG_SIGNALSCHED,
"signal (schedule)"));
SIGNAL(&manager->wakeup);
1998-10-16 19:56:56 +00:00
}
#else /* USE_TIMER_THREAD */
if (timer->index == 1 &&
isc_time_compare(&timer->due, &manager->due) < 0)
manager->due = timer->due;
#endif /* USE_TIMER_THREAD */
1998-10-16 01:18:31 +00:00
return (ISC_R_SUCCESS);
}
1998-10-15 22:22:50 +00:00
static inline void
deschedule(isc__timer_t *timer) {
#ifdef USE_TIMER_THREAD
1998-10-21 01:13:50 +00:00
isc_boolean_t need_wakeup = ISC_FALSE;
#endif
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) {
#ifdef USE_TIMER_THREAD
1998-10-16 01:18:31 +00:00
if (timer->index == 1)
1998-10-21 01:13:50 +00:00
need_wakeup = ISC_TRUE;
#endif
1998-10-21 01:13:50 +00:00
isc_heap_delete(manager->heap, timer->index);
1998-10-16 01:18:31 +00:00
timer->index = 0;
INSIST(manager->nscheduled > 0);
manager->nscheduled--;
#ifdef USE_TIMER_THREAD
1998-10-16 19:56:56 +00:00
if (need_wakeup) {
XTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TIMER,
ISC_MSG_SIGNALDESCHED,
"signal (deschedule)"));
SIGNAL(&manager->wakeup);
1998-10-16 19:56:56 +00:00
}
#endif /* USE_TIMER_THREAD */
1998-10-16 01:18:31 +00:00
}
1998-10-15 22:22:50 +00:00
}
static void
destroy(isc__timer_t *timer) {
isc__timermgr_t *manager = timer->manager;
1998-10-15 22:22:50 +00:00
/*
* The caller must ensure it is safe to destroy the timer.
1998-10-15 22:22:50 +00:00
*/
LOCK(&manager->lock);
(void)isc_task_purgerange(timer->task,
timer,
ISC_TIMEREVENT_FIRSTEVENT,
ISC_TIMEREVENT_LASTEVENT,
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);
2000-08-26 01:42:34 +00:00
DESTROYLOCK(&timer->lock);
timer->common.impmagic = 0;
timer->common.magic = 0;
2001-11-27 01:56:32 +00:00
isc_mem_put(manager->mctx, timer, sizeof(*timer));
1998-10-15 22:22:50 +00:00
}
ISC_TIMERFUNC_SCOPE isc_result_t
isc__timer_create(isc_timermgr_t *manager0, isc_timertype_t type,
const isc_time_t *expires, const isc_interval_t *interval,
isc_task_t *task, isc_taskaction_t action, const void *arg,
isc_timer_t **timerp)
1998-10-15 22:22:50 +00:00
{
isc__timermgr_t *manager = (isc__timermgr_t *)manager0;
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-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);
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)));
1998-10-15 22:22:50 +00:00
REQUIRE(timerp != NULL && *timerp == NULL);
REQUIRE(type != isc_timertype_limited ||
!(isc_time_isepoch(expires) || isc_interval_iszero(interval)));
1998-10-15 22:22:50 +00:00
/*
* 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);
1998-10-15 22:22:50 +00:00
}
2001-11-27 01:56:32 +00:00
timer = isc_mem_get(manager->mctx, sizeof(*timer));
1998-10-15 22:22:50 +00:00
if (timer == NULL)
return (ISC_R_NOMEMORY);
timer->manager = manager;
timer->references = 1;
if (type == isc_timertype_once && !isc_interval_iszero(interval)) {
result = isc_time_add(&now, interval, &timer->idle);
if (result != ISC_R_SUCCESS) {
isc_mem_put(manager->mctx, timer, sizeof(*timer));
return (result);
}
} 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;
/*
* Removing the const attribute from "arg" is the best of two
* evils here. If the timer->arg member is made const, then
* it affects a great many recipients of the timer event
* which did not pass in an "arg" that was truly const.
* Changing isc_timer_create() to not have "arg" prototyped as const,
* though, can cause compilers warnings for calls that *do*
* have a truly const arg. The caller will have to carefully
* keep track of whether arg started as a true const.
*/
DE_CONST(arg, timer->arg);
1998-10-16 01:18:31 +00:00
timer->index = 0;
result = isc_mutex_init(&timer->lock);
if (result != ISC_R_SUCCESS) {
isc_task_detach(&timer->task);
2001-11-27 01:56:32 +00:00
isc_mem_put(manager->mctx, timer, sizeof(*timer));
return (result);
1998-10-15 22:22:50 +00:00
}
2000-10-20 02:21:58 +00:00
ISC_LINK_INIT(timer, link);
timer->common.impmagic = TIMER_MAGIC;
timer->common.magic = ISCAPI_TIMER_MAGIC;
timer->common.methods = (isc_timermethods_t *)&timermethods;
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.
*/
if (type != isc_timertype_inactive)
result = schedule(timer, &now, ISC_TRUE);
else
result = ISC_R_SUCCESS;
if (result == ISC_R_SUCCESS)
APPEND(manager->timers, timer, link);
1998-10-15 22:22:50 +00:00
UNLOCK(&manager->lock);
if (result != ISC_R_SUCCESS) {
timer->common.impmagic = 0;
timer->common.magic = 0;
2000-08-26 01:42:34 +00:00
DESTROYLOCK(&timer->lock);
isc_task_detach(&timer->task);
2001-11-27 01:56:32 +00:00
isc_mem_put(manager->mctx, timer, sizeof(*timer));
return (result);
}
*timerp = (isc_timer_t *)timer;
1998-10-15 22:22:50 +00:00
return (ISC_R_SUCCESS);
1998-10-15 22:22:50 +00:00
}
ISC_TIMERFUNC_SCOPE isc_result_t
isc__timer_reset(isc_timer_t *timer0, isc_timertype_t type,
const isc_time_t *expires, const isc_interval_t *interval,
isc_boolean_t purge)
1998-10-15 22:22:50 +00:00
{
isc__timer_t *timer = (isc__timer_t *)timer0;
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
/*
* 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
* 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));
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)));
1998-10-15 22:22:50 +00:00
/*
* 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);
1998-10-15 22:22:50 +00:00
}
LOCK(&manager->lock);
LOCK(&timer->lock);
if (purge)
(void)isc_task_purgerange(timer->task,
timer,
ISC_TIMEREVENT_FIRSTEVENT,
ISC_TIMEREVENT_LASTEVENT,
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;
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);
}
1998-10-15 22:22:50 +00:00
UNLOCK(&timer->lock);
UNLOCK(&manager->lock);
return (result);
}
ISC_TIMERFUNC_SCOPE isc_timertype_t
isc__timer_gettype(isc_timer_t *timer0) {
isc__timer_t *timer = (isc__timer_t *)timer0;
isc_timertype_t t;
REQUIRE(VALID_TIMER(timer));
LOCK(&timer->lock);
t = timer->type;
UNLOCK(&timer->lock);
return (t);
}
ISC_TIMERFUNC_SCOPE isc_result_t
isc__timer_touch(isc_timer_t *timer0) {
isc__timer_t *timer = (isc__timer_t *)timer0;
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);
/*
* 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
TIME_NOW(&now);
result = isc_time_add(&now, &timer->interval, &timer->idle);
1998-10-15 22:22:50 +00:00
UNLOCK(&timer->lock);
return (result);
1998-10-15 22:22:50 +00:00
}
ISC_TIMERFUNC_SCOPE void
isc__timer_attach(isc_timer_t *timer0, isc_timer_t **timerp) {
isc__timer_t *timer = (isc__timer_t *)timer0;
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 = (isc_timer_t *)timer;
1998-10-15 22:22:50 +00:00
}
ISC_TIMERFUNC_SCOPE void
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 = (isc__timer_t *)*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);
1998-10-15 22:22:50 +00:00
if (free_timer)
destroy(timer);
*timerp = NULL;
}
1998-10-16 07:44:20 +00:00
static void
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;
isc_timerevent_t *event;
1998-10-21 02:26:57 +00:00
isc_eventtype_t type = 0;
isc__timer_t *timer;
1998-10-26 23:08:23 +00:00
isc_result_t result;
isc_boolean_t idle;
1998-10-16 07:44:20 +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);
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;
} else if (timer->type == isc_timertype_limited) {
int cmp;
cmp = isc_time_compare(now, &timer->expires);
if (cmp >= 0) {
type = ISC_TIMEREVENT_LIFE;
post_event = ISC_TRUE;
need_schedule = ISC_FALSE;
} else {
type = ISC_TIMEREVENT_TICK;
post_event = ISC_TRUE;
need_schedule = ISC_TRUE;
}
} else if (!isc_time_isepoch(&timer->expires) &&
1998-10-22 01:33:20 +00:00
isc_time_compare(now,
&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-16 07:44:20 +00:00
} else {
idle = ISC_FALSE;
LOCK(&timer->lock);
if (!isc_time_isepoch(&timer->idle) &&
isc_time_compare(now,
&timer->idle) >= 0) {
idle = ISC_TRUE;
}
UNLOCK(&timer->lock);
if (idle) {
type = ISC_TIMEREVENT_IDLE;
post_event = ISC_TRUE;
need_schedule = ISC_FALSE;
} else {
/*
* Idle timer has been touched;
* reschedule.
*/
XTRACEID(isc_msgcat_get(isc_msgcat,
ISC_MSGSET_TIMER,
ISC_MSG_IDLERESCHED,
"idle reschedule"),
timer);
post_event = ISC_FALSE;
need_schedule = ISC_TRUE;
}
1998-10-16 07:44:20 +00:00
}
if (post_event) {
XTRACEID(isc_msgcat_get(isc_msgcat,
ISC_MSGSET_TIMER,
ISC_MSG_POSTING,
"posting"), timer);
1999-01-15 02:14:55 +00:00
/*
* XXX We could preallocate this event.
*/
event = (isc_timerevent_t *)isc_event_allocate(manager->mctx,
1998-10-21 02:26:57 +00:00
timer,
type,
timer->action,
timer->arg,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-16 21:41:30 +00:00
if (event != NULL) {
event->due = timer->due;
isc_task_send(timer->task,
2008-08-22 05:57:53 +00:00
ISC_EVENT_PTR(&event));
} else
2009-01-22 23:47:54 +00:00
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s",
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_TIMER,
ISC_MSG_EVENTNOTALLOC,
"couldn't "
"allocate event"));
1998-10-16 07:44:20 +00:00
}
1998-10-16 07:44:20 +00:00
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)
2009-01-23 01:15:41 +00:00
UNEXPECTED_ERROR(__FILE__, __LINE__,
2009-01-23 01:27:12 +00:00
"%s: %u",
isc_msgcat_get(isc_msgcat,
ISC_MSGSET_TIMER,
ISC_MSG_SCHEDFAIL,
2009-01-23 01:27:12 +00:00
"couldn't schedule "
2009-01-23 23:47:54 +00:00
"timer"),
1998-10-16 07:44:20 +00:00
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-16 07:44:20 +00:00
}
#ifdef USE_TIMER_THREAD
1998-10-23 05:45:26 +00:00
static isc_threadresult_t
#ifdef _WIN32 /* XXXDCL */
WINAPI
#endif
1998-10-16 01:18:31 +00:00
run(void *uap) {
isc__timermgr_t *manager = uap;
1998-12-13 23:45:21 +00:00
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) {
TIME_NOW(&now);
1998-10-16 21:53:23 +00:00
XTRACETIME(isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_RUNNING,
"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) {
XTRACETIME2(isc_msgcat_get(isc_msgcat,
ISC_MSGSET_GENERAL,
ISC_MSG_WAITUNTIL,
"waituntil"),
manager->due, now);
result = WAITUNTIL(&manager->wakeup, &manager->lock, &manager->due);
1998-10-22 01:33:20 +00:00
INSIST(result == ISC_R_SUCCESS ||
result == ISC_R_TIMEDOUT);
1998-10-16 19:56:56 +00:00
} else {
XTRACETIME(isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_WAIT, "wait"), now);
1998-10-16 01:18:31 +00:00
WAIT(&manager->wakeup, &manager->lock);
1998-10-16 19:56:56 +00:00
}
XTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TIMER,
ISC_MSG_WAKEUP, "wakeup"));
1998-10-16 01:18:31 +00:00
}
UNLOCK(&manager->lock);
2009-10-05 17:30:49 +00:00
#ifdef OPENSSL_LEAKS
ERR_remove_state(0);
#endif
1998-10-23 05:45:26 +00:00
return ((isc_threadresult_t)0);
1998-10-16 01:18:31 +00:00
}
#endif /* USE_TIMER_THREAD */
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) {
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) {
isc__timer_t *timer;
1998-10-16 21:54:53 +00:00
timer = what;
REQUIRE(VALID_TIMER(timer));
timer->index = index;
}
ISC_TIMERFUNC_SCOPE isc_result_t
isc__timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
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);
#ifdef USE_SHARED_MANAGER
if (timermgr != NULL) {
timermgr->refs++;
*managerp = (isc_timermgr_t *)timermgr;
return (ISC_R_SUCCESS);
}
#endif /* USE_SHARED_MANAGER */
2001-11-27 01:56:32 +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->common.impmagic = TIMER_MANAGER_MAGIC;
manager->common.magic = ISCAPI_TIMERMGR_MAGIC;
manager->common.methods = (isc_timermgrmethods_t *)&timermgrmethods;
manager->mctx = NULL;
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;
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);
2001-11-27 01:56:32 +00:00
isc_mem_put(mctx, manager, sizeof(*manager));
1998-10-16 01:18:31 +00:00
return (ISC_R_NOMEMORY);
}
result = isc_mutex_init(&manager->lock);
if (result != ISC_R_SUCCESS) {
1998-10-21 01:13:50 +00:00
isc_heap_destroy(&manager->heap);
2001-11-27 01:56:32 +00:00
isc_mem_put(mctx, manager, sizeof(*manager));
return (result);
1998-10-16 01:18:31 +00:00
}
isc_mem_attach(mctx, &manager->mctx);
#ifdef USE_TIMER_THREAD
1998-10-22 01:33:20 +00:00
if (isc_condition_init(&manager->wakeup) != ISC_R_SUCCESS) {
isc_mem_detach(&manager->mctx);
2000-08-26 01:42:34 +00:00
DESTROYLOCK(&manager->lock);
1998-10-21 01:13:50 +00:00
isc_heap_destroy(&manager->heap);
2001-11-27 01:56:32 +00:00
isc_mem_put(mctx, manager, sizeof(*manager));
1998-10-21 01:13:50 +00:00
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_condition_init() %s",
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_FAILED, "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) {
isc_mem_detach(&manager->mctx);
1998-10-22 01:33:20 +00:00
(void)isc_condition_destroy(&manager->wakeup);
2000-08-26 01:42:34 +00:00
DESTROYLOCK(&manager->lock);
1998-10-21 01:13:50 +00:00
isc_heap_destroy(&manager->heap);
2001-11-27 01:56:32 +00:00
isc_mem_put(mctx, manager, sizeof(*manager));
1998-10-21 01:13:50 +00:00
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_thread_create() %s",
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_FAILED, "failed"));
1998-10-16 01:18:31 +00:00
return (ISC_R_UNEXPECTED);
}
#endif
#ifdef USE_SHARED_MANAGER
manager->refs = 1;
timermgr = manager;
#endif /* USE_SHARED_MANAGER */
1998-10-16 01:18:31 +00:00
*managerp = (isc_timermgr_t *)manager;
1998-10-16 01:18:31 +00:00
return (ISC_R_SUCCESS);
}
1998-10-15 22:22:50 +00:00
ISC_TIMERFUNC_SCOPE void
isc__timermgr_poke(isc_timermgr_t *manager0) {
#ifdef USE_TIMER_THREAD
isc__timermgr_t *manager = (isc__timermgr_t *)manager0;
2002-09-09 06:01:06 +00:00
REQUIRE(VALID_MANAGER(manager));
SIGNAL(&manager->wakeup);
2002-09-09 06:01:06 +00:00
#else
UNUSED(manager0);
2002-09-09 06:01:06 +00:00
#endif
}
ISC_TIMERFUNC_SCOPE void
isc__timermgr_destroy(isc_timermgr_t **managerp) {
isc__timermgr_t *manager;
isc_mem_t *mctx;
1998-10-16 01:18:31 +00:00
/*
* Destroy a timer manager.
*/
REQUIRE(managerp != NULL);
manager = (isc__timermgr_t *)*managerp;
1998-10-16 01:18:31 +00:00
REQUIRE(VALID_MANAGER(manager));
1998-10-15 22:22:50 +00:00
1998-10-16 01:18:31 +00:00
LOCK(&manager->lock);
#ifdef USE_SHARED_MANAGER
manager->refs--;
if (manager->refs > 0) {
UNLOCK(&manager->lock);
*managerp = NULL;
return;
}
timermgr = NULL;
#endif /* USE_SHARED_MANAGER */
#ifndef USE_TIMER_THREAD
isc__timermgr_dispatch((isc_timermgr_t *)manager);
#endif
1998-10-16 01:18:31 +00:00
REQUIRE(EMPTY(manager->timers));
1998-10-21 01:13:50 +00:00
manager->done = ISC_TRUE;
1998-10-16 01:18:31 +00:00
#ifdef USE_TIMER_THREAD
XTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TIMER,
ISC_MSG_SIGNALDESTROY, "signal (destroy)"));
SIGNAL(&manager->wakeup);
#endif /* USE_TIMER_THREAD */
1998-10-16 01:18:31 +00:00
UNLOCK(&manager->lock);
#ifdef USE_TIMER_THREAD
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__,
"isc_thread_join() %s",
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
ISC_MSG_FAILED, "failed"));
#endif /* USE_TIMER_THREAD */
1998-10-16 01:18:31 +00:00
/*
* Clean up.
*/
#ifdef USE_TIMER_THREAD
1998-10-22 01:33:20 +00:00
(void)isc_condition_destroy(&manager->wakeup);
#endif /* USE_TIMER_THREAD */
2000-08-26 01:42:34 +00:00
DESTROYLOCK(&manager->lock);
1998-10-21 01:13:50 +00:00
isc_heap_destroy(&manager->heap);
manager->common.impmagic = 0;
manager->common.magic = 0;
mctx = manager->mctx;
2001-11-27 01:56:32 +00:00
isc_mem_put(mctx, manager, sizeof(*manager));
isc_mem_detach(&mctx);
1998-10-16 01:18:31 +00:00
*managerp = NULL;
#ifdef USE_SHARED_MANAGER
timermgr = NULL;
#endif
1998-10-16 01:18:31 +00:00
}
#ifndef USE_TIMER_THREAD
isc_result_t
isc__timermgr_nextevent(isc_timermgr_t *manager0, isc_time_t *when) {
isc__timermgr_t *manager = (isc__timermgr_t *)manager0;
#ifdef USE_SHARED_MANAGER
if (manager == NULL)
manager = timermgr;
#endif
if (manager == NULL || manager->nscheduled == 0)
return (ISC_R_NOTFOUND);
*when = manager->due;
return (ISC_R_SUCCESS);
}
void
isc__timermgr_dispatch(isc_timermgr_t *manager0) {
isc__timermgr_t *manager = (isc__timermgr_t *)manager0;
isc_time_t now;
#ifdef USE_SHARED_MANAGER
if (manager == NULL)
manager = timermgr;
#endif
if (manager == NULL)
return;
TIME_NOW(&now);
dispatch(manager, &now);
}
#endif /* USE_TIMER_THREAD */
2009-09-01 23:47:45 +00:00
#ifdef USE_TIMERIMPREGISTER
isc_result_t
isc__timer_register() {
return (isc_timer_register(isc__timermgr_create));
}
#endif