timer.c revision da4242bf62503672dbabd04e1c84c62680393f25
#include "attribute.h"
#include <stddef.h>
#include <stdlib.h>
#include <isc/assertions.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.
*/
#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. */
struct isc_interval interval;
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_t
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("signal (schedule)");
}
return (ISC_R_SUCCESS);
}
static inline void
/*
* The caller must ensure locking.
*/
manager->nscheduled--;
if (need_wakeup) {
XTRACE("signal (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 isc_threadresult_t
#ifdef _WIN32
#endif
if (manager->nscheduled > 0) {
result == ISC_R_TIMEDOUT);
} else {
XTRACE("wait");
}
XTRACE("wakeup");
}
return ((isc_threadresult_t)0);
}
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("signal (destroy)");
/*
* Wait for thread to exit.
*/
"isc_thread_join() failed");
/*
* Clean up.
*/
}