timer.c revision 5e589b5356a4125b5af32605dead82ab8b467c88
/*
* Copyright (C) 1998-2000 Internet Software Consortium.
*
* 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.
*/
/* $Id: timer.c,v 1.58 2000/10/20 02:21:58 marka Exp $ */
#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 XTRACETIMER(s, t, d)
#endif /* ISC_TIMER_TRACE */
#define VALID_TIMER(t) ((t) != NULL && \
(t)->magic == TIMER_MAGIC)
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;
};
#define VALID_MANAGER(m) ((m) != NULL && \
(m)->magic == TIMER_MANAGER_MAGIC)
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;
/*
* Note: the caller must ensure locking.
*/
#ifndef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
/*
* 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
XTRACE("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) {
XTRACE("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) {
if (result != ISC_R_SUCCESS) {
"isc_time_now() failed: %s",
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.
*/
}
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.
*/
"isc_mutex_init() failed");
return (ISC_R_UNEXPECTED);
}
/*
* 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) {
if (result != ISC_R_SUCCESS) {
"isc_time_now() failed: %s",
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.
*/
}
if (purge)
NULL);
} else {
}
if (result == ISC_R_SUCCESS) {
if (type == isc_timertype_inactive) {
} else
}
return (result);
}
/*
* 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.
*/
if (result != ISC_R_SUCCESS) {
"isc_time_now() failed: %s",
} else
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.
*/
} else {
/*
* Idle timer has been touched; reschedule.
*/
}
if (post_event) {
/*
* 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
if (manager->nscheduled > 0) {
result == ISC_R_TIMEDOUT);
} else {
XTRACE("wait");
}
XTRACE("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);
}
"isc_mutex_init() failed");
return (ISC_R_UNEXPECTED);
}
#ifdef ISC_PLATFORM_USETHREADS
"isc_condition_init() failed");
return (ISC_R_UNEXPECTED);
}
"isc_thread_create() failed");
return (ISC_R_UNEXPECTED);
}
#else /* ISC_PLATFORM_USETHREADS */
#endif /* ISC_PLATFORM_USETHREADS */
return (ISC_R_SUCCESS);
}
void
/*
* Destroy a timer manager.
*/
#ifndef ISC_PLATFORM_USETHREADS
return;
}
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
XTRACE("signal (destroy)");
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
/*
* Wait for thread to exit.
*/
"isc_thread_join() 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);
}
isc__timermgr_dispatch(void) {
return (ISC_R_NOTFOUND);
isc_time_now(&now);
return (ISC_R_SUCCESS);
}
#endif /* ISC_PLATFORM_USETHREADS */