timer.c revision ec5347e2c775f027573ce5648b910361aa926c01
/*
* Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1998-2002 Internet Software Consortium.
*
* 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 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.
*/
/* $Id: timer.c,v 1.79 2007/06/18 23:47:44 tbox Exp $ */
/*! \file */
#include <config.h>
#include <isc/condition.h>
#include <isc/platform.h>
#ifndef ISC_PLATFORM_USETHREADS
#include "timer_p.h"
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_TIMER_TRACE
(d).seconds, (d).nanoseconds)
(d).seconds, (d).nanoseconds)
#else
#define XTRACE(s)
#define XTRACEID(s, t)
#define XTRACETIME(s, d)
#define XTRACETIME2(s, d, n)
#define XTRACETIMER(s, t, d)
#endif /* ISC_TIMER_TRACE */
struct isc_timer {
/*! Not locked. */
unsigned int magic;
/*! Locked by timer lock. */
unsigned int references;
/*! Locked by manager lock. */
isc_task_t * task;
void * arg;
unsigned int index;
};
struct isc_timermgr {
/* Not locked. */
unsigned int magic;
/* Locked by manager lock. */
unsigned int nscheduled;
#ifdef ISC_PLATFORM_USETHREADS
#else /* ISC_PLATFORM_USETHREADS */
unsigned int refs;
#endif /* ISC_PLATFORM_USETHREADS */
isc_heap_t * heap;
};
#ifndef ISC_PLATFORM_USETHREADS
/*!
* If threads are not in use, there can be only one.
*/
#endif /* ISC_PLATFORM_USETHREADS */
static inline isc_result_t
int cmp;
#ifdef ISC_PLATFORM_USETHREADS
#endif
/*!
* Note: the caller must ensure locking.
*/
#ifndef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
/*!
* If the manager was timed wait, we may need to signal the
* manager to force a wakeup.
*/
#endif
/*
* Compute the new due time.
*/
if (result != ISC_R_SUCCESS)
return (result);
} else {
else
}
/*
* Schedule the timer.
*/
/*
* Already scheduled.
*/
switch (cmp) {
case -1:
break;
case 1:
break;
case 0:
/* Nothing to do. */
break;
}
} else {
if (result != ISC_R_SUCCESS) {
return (ISC_R_NOMEMORY);
}
manager->nscheduled++;
}
/*
* 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.
*/
#ifdef ISC_PLATFORM_USETHREADS
/*
* 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 (result == ISC_R_SUCCESS &&
"*** POKED TIMER ***");
}
}
"signal (schedule)"));
}
#else /* ISC_PLATFORM_USETHREADS */
#endif /* ISC_PLATFORM_USETHREADS */
return (ISC_R_SUCCESS);
}
static inline void
/*
* The caller must ensure locking.
*/
manager->nscheduled--;
#ifdef ISC_PLATFORM_USETHREADS
if (need_wakeup) {
"signal (deschedule)"));
}
#endif /* ISC_PLATFORM_USETHREADS */
}
}
static void
/*
* The caller must ensure it is safe to destroy the timer.
*/
NULL);
}
{
/*
* Create a new 'type' timer managed by 'manager'. The timers
* parameters are specified by 'expires' and 'interval'. Events
* 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'.
*/
/*
* Get current time.
*/
if (type != isc_timertype_inactive) {
} 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.
*/
}
return (ISC_R_NOMEMORY);
if (result != ISC_R_SUCCESS) {
return (result);
}
} else
/*
* 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.
*/
if (result != ISC_R_SUCCESS) {
return (result);
}
/*
* 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)
else
if (result == ISC_R_SUCCESS)
if (result != ISC_R_SUCCESS) {
return (result);
}
return (ISC_R_SUCCESS);
}
{
/*
* 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.
*/
/*
* Get current time.
*/
if (type != isc_timertype_inactive) {
} 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.
*/
}
if (purge)
NULL);
} else {
}
if (result == ISC_R_SUCCESS) {
if (type == isc_timertype_inactive) {
} else
}
return (result);
}
return (t);
}
/*
* Set the last-touched time of 'timer' to the current time.
*/
/*
* 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.
*/
return (result);
}
void
/*
* Attach *timerp to timer.
*/
timer->references++;
}
void
/*
* Detach *timerp from its timer.
*/
timer->references--;
if (timer->references == 0)
if (free_timer)
}
static void
isc_eventtype_t type = 0;
/*!
* The caller must be holding the manager lock.
*/
int cmp;
if (cmp >= 0) {
} else {
}
} else {
/*
* Idle timer has been touched; reschedule.
*/
"idle reschedule"),
timer);
}
if (post_event) {
"posting"), timer);
/*
* XXX We could preallocate this event.
*/
type,
sizeof(*event));
else
"couldn't "
"allocate event"));
}
manager->nscheduled--;
if (need_schedule) {
if (result != ISC_R_SUCCESS)
"couldn't "
"schedule timer: %u"),
result);
}
} else {
}
}
}
#ifdef ISC_PLATFORM_USETHREADS
static isc_threadresult_t
#ifdef _WIN32 /* XXXDCL */
#endif
"running"), now);
if (manager->nscheduled > 0) {
"waituntil"),
result == ISC_R_TIMEDOUT);
} else {
}
ISC_MSG_WAKEUP, "wakeup"));
}
return ((isc_threadresult_t)0);
}
#endif /* ISC_PLATFORM_USETHREADS */
static isc_boolean_t
return (ISC_TRUE);
return (ISC_FALSE);
}
static void
}
/*
* Create a timer manager.
*/
#ifndef ISC_PLATFORM_USETHREADS
return (ISC_R_SUCCESS);
}
#endif /* ISC_PLATFORM_USETHREADS */
return (ISC_R_NOMEMORY);
manager->nscheduled = 0;
if (result != ISC_R_SUCCESS) {
return (ISC_R_NOMEMORY);
}
if (result != ISC_R_SUCCESS) {
return (result);
}
#ifdef ISC_PLATFORM_USETHREADS
"isc_condition_init() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
"isc_thread_create() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
#else /* ISC_PLATFORM_USETHREADS */
#endif /* ISC_PLATFORM_USETHREADS */
return (ISC_R_SUCCESS);
}
void
#ifdef ISC_PLATFORM_USETHREADS
#else
#endif
}
void
/*
* Destroy a timer manager.
*/
#ifndef ISC_PLATFORM_USETHREADS
return;
}
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
ISC_MSG_SIGNALDESTROY, "signal (destroy)"));
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
/*
* Wait for thread to exit.
*/
"isc_thread_join() %s",
ISC_MSG_FAILED, "failed"));
#endif /* ISC_PLATFORM_USETHREADS */
/*
* Clean up.
*/
#ifdef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
}
#ifndef ISC_PLATFORM_USETHREADS
return (ISC_R_NOTFOUND);
return (ISC_R_SUCCESS);
}
void
isc__timermgr_dispatch(void) {
return;
}
#endif /* ISC_PLATFORM_USETHREADS */