timer.c revision bf6d2e39124ab3d51c253f7acad9a4abef059be6
#include <stddef.h>
#include <stdlib.h>
#include <isc/assertions.h>
#include <isc/unexpect.h>
#include <isc/condition.h>
/*
* We use macros instead of calling the routines directly because
* the capital letters make the locking stand out.
*
* We INSIST that they succeed since there's no way for us to continue
* if they fail.
*/
(t).nanoseconds == 0)
#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. */
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;
};
static inline isc_result
int cmp;
/*
* Note: the caller must ensure locking.
*/
/*
* Compute the new due time.
*/
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 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.
*/
XTRACE("broadcast (schedule)");
}
return (ISC_R_SUCCESS);
}
static inline void
/*
* The caller must ensure locking.
*/
manager->nscheduled--;
if (need_wakeup) {
XTRACE("broadcast (deschedule)");
}
}
}
static void
/*
* The caller must ensure locking.
*/
}
{
/*
* 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 (result != ISC_R_SUCCESS) {
"isc_time_get() failed: %s",
return (ISC_R_UNEXPECTED);
}
return (ISC_R_NOMEMORY);
else {
}
"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 (result == ISC_R_SUCCESS)
return (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.
*/
/*
* Get current time.
*/
if (result != ISC_R_SUCCESS) {
"isc_time_get() failed: %s",
return (ISC_R_UNEXPECTED);
}
if (purge)
else {
}
return (result);
}
/*
* Set the last-touched time of 'timer' to the current time.
*/
if (result != ISC_R_SUCCESS) {
"isc_time_get() failed: %s",
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
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;
} else {
/*
* Idle timer has been touched; reschedule.
*/
}
if (post_event) {
type,
sizeof *event);
else
"couldn't allocate event");
}
manager->nscheduled--;
if (need_schedule) {
if (result != ISC_R_SUCCESS)
"couldn't schedule timer: %s",
result);
}
} else {
}
}
}
static void *
if (manager->nscheduled > 0) {
result == ISC_R_TIMEDOUT);
} else {
XTRACE("wait");
}
XTRACE("wakeup");
}
return (NULL);
}
static isc_boolean_t
return (ISC_TRUE);
return (ISC_FALSE);
}
static void
}
/*
* Create a timer manager.
*/
return (ISC_R_NOMEMORY);
manager->nscheduled = 0;
if (result != ISC_R_SUCCESS) {
return (ISC_R_NOMEMORY);
}
"isc_mutex_init() failed");
return (ISC_R_UNEXPECTED);
}
"isc_condition_init() failed");
return (ISC_R_UNEXPECTED);
}
"isc_thread_create() failed");
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
void
/*
* Destroy a timer manager.
*/
XTRACE("broadcast (destroy)");
/*
* Wait for thread to exit.
*/
"isc_thread_join() failed");
/*
* Clean up.
*/
}