1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
1998-10-16 01:18:31 +00:00
|
|
|
#include <isc/unexpect.h>
|
|
|
|
#include <isc/thread.h>
|
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/condition.h>
|
|
|
|
#include <isc/heap.h>
|
1998-10-15 22:22:50 +00:00
|
|
|
#include <isc/timer.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use macros instead of calling the os_ 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.
|
|
|
|
*/
|
1998-10-16 01:18:31 +00:00
|
|
|
#define LOCK(lp) INSIST(os_mutex_lock((lp)))
|
|
|
|
#define UNLOCK(lp) INSIST(os_mutex_unlock((lp)))
|
|
|
|
#define BROADCAST(cvp) INSIST(os_condition_broadcast((cvp)))
|
|
|
|
#define WAIT(cvp, lp) INSIST(os_condition_wait((cvp), (lp)))
|
|
|
|
#define WAITUNTIL(cvp, lp, tp, bp) INSIST(os_condition_waituntil((cvp), \
|
|
|
|
(lp), (tp), (bp)))
|
1998-10-15 22:22:50 +00:00
|
|
|
|
1998-10-16 18:46:38 +00:00
|
|
|
#define ZERO(t) ((t).seconds == 0 && \
|
|
|
|
(t).nanoseconds == 0)
|
|
|
|
|
1998-10-16 21:53:23 +00:00
|
|
|
#ifdef TIMER_TRACE
|
|
|
|
#define XTRACE(s) printf("%s\n", (s))
|
|
|
|
#define XTRACEID(s, t) printf("%s %p\n", (s), (t))
|
|
|
|
#define XTRACETIME(s, d) printf("%s %lu.%09lu\n", (s), \
|
|
|
|
(d).seconds, (d).nanoseconds)
|
|
|
|
#define XTRACETIMER(s, t, d) printf("%s %p %lu.%09lu\n", (s), (t), \
|
|
|
|
(d).seconds, (d).nanoseconds)
|
|
|
|
#else
|
|
|
|
#define XTRACE(s)
|
|
|
|
#define XTRACEID(s, t)
|
|
|
|
#define XTRACETIME(s, d)
|
|
|
|
#define XTRACETIMER(s, t, d)
|
|
|
|
#endif /* TIMER_TRACE */
|
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
#define TIMER_MAGIC 0x54494D52U /* TIMR. */
|
|
|
|
#define VALID_TIMER(t) ((t) != NULL && \
|
|
|
|
(t)->magic == TIMER_MAGIC)
|
|
|
|
struct timer_t {
|
|
|
|
/* Not locked. */
|
|
|
|
unsigned int magic;
|
|
|
|
timer_manager_t manager;
|
|
|
|
os_mutex_t lock;
|
|
|
|
/* Locked by timer lock. */
|
|
|
|
unsigned int references;
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_t idle;
|
1998-10-15 22:22:50 +00:00
|
|
|
/* Locked by manager lock. */
|
|
|
|
timer_type_t type;
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_t expires;
|
1998-10-15 22:22:50 +00:00
|
|
|
os_time_t interval;
|
|
|
|
task_t task;
|
1998-10-16 01:18:31 +00:00
|
|
|
task_action_t action;
|
1998-10-15 22:22:50 +00:00
|
|
|
void * arg;
|
1998-10-16 01:18:31 +00:00
|
|
|
unsigned int index;
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_t due;
|
1998-10-15 22:22:50 +00:00
|
|
|
LINK(struct timer_t) link;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define TIMER_MANAGER_MAGIC 0x54494D4DU /* TIMM. */
|
|
|
|
#define VALID_MANAGER(m) ((m) != NULL && \
|
|
|
|
(m)->magic == TIMER_MANAGER_MAGIC)
|
|
|
|
|
|
|
|
struct timer_manager_t {
|
|
|
|
/* Not locked. */
|
|
|
|
unsigned int magic;
|
|
|
|
mem_context_t mctx;
|
|
|
|
os_mutex_t lock;
|
|
|
|
/* Locked by manager lock. */
|
1998-10-16 01:18:31 +00:00
|
|
|
boolean_t done;
|
1998-10-15 22:22:50 +00:00
|
|
|
LIST(struct timer_t) timers;
|
1998-10-16 01:18:31 +00:00
|
|
|
unsigned int nscheduled;
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_t due;
|
1998-10-16 01:18:31 +00:00
|
|
|
os_condition_t wakeup;
|
1998-10-15 22:22:50 +00:00
|
|
|
os_thread_t thread;
|
1998-10-16 01:18:31 +00:00
|
|
|
heap_t heap;
|
1998-10-15 22:22:50 +00:00
|
|
|
};
|
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
static inline isc_result
|
1998-10-16 19:56:56 +00:00
|
|
|
schedule(timer_t timer, os_time_t *nowp, boolean_t broadcast_ok) {
|
1998-10-16 01:18:31 +00:00
|
|
|
isc_result result;
|
|
|
|
timer_manager_t manager;
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_t due;
|
1998-10-16 19:56:56 +00:00
|
|
|
int cmp;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
1998-10-16 18:46:38 +00:00
|
|
|
* Note: the caller must ensure locking.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the new due time.
|
1998-10-16 01:18:31 +00:00
|
|
|
*/
|
1998-10-16 18:46:38 +00:00
|
|
|
if (timer->type == timer_type_ticker)
|
|
|
|
os_time_add(nowp, &timer->interval, &due);
|
|
|
|
else {
|
1998-10-16 19:56:56 +00:00
|
|
|
if (ZERO(timer->idle))
|
|
|
|
due = timer->expires;
|
|
|
|
else if (ZERO(timer->expires))
|
|
|
|
due = timer->idle;
|
|
|
|
else if (os_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 01:18:31 +00:00
|
|
|
|
1998-10-16 18:46:38 +00:00
|
|
|
/*
|
|
|
|
* Schedule the timer.
|
|
|
|
*/
|
1998-10-16 01:18:31 +00:00
|
|
|
manager = timer->manager;
|
|
|
|
if (timer->index > 0) {
|
|
|
|
/*
|
|
|
|
* Already scheduled.
|
|
|
|
*/
|
1998-10-16 19:56:56 +00:00
|
|
|
cmp = os_time_compare(&due, &timer->due);
|
|
|
|
timer->due = due;
|
|
|
|
switch (cmp) {
|
1998-10-16 01:18:31 +00:00
|
|
|
case -1:
|
|
|
|
heap_increased(manager->heap, timer->index);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
heap_decreased(manager->heap, timer->index);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
/* Nothing to do. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
1998-10-16 19:56:56 +00:00
|
|
|
timer->due = due;
|
1998-10-16 01:18:31 +00:00
|
|
|
result = heap_insert(manager->heap, timer);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
INSIST(result == ISC_R_NOMEMORY);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
manager->nscheduled++;
|
|
|
|
}
|
1998-10-16 19:56:56 +00:00
|
|
|
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACETIMER("schedule", timer, due);
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
1998-10-16 18:46:38 +00:00
|
|
|
* 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.
|
1998-10-16 01:18:31 +00:00
|
|
|
*/
|
1998-10-16 19:56:56 +00:00
|
|
|
if (timer->index == 1 && broadcast_ok) {
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACE("broadcast (schedule)");
|
1998-10-16 01:18:31 +00:00
|
|
|
BROADCAST(&manager->wakeup);
|
1998-10-16 19:56:56 +00:00
|
|
|
}
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
static inline void
|
1998-10-16 01:18:31 +00:00
|
|
|
deschedule(timer_t timer) {
|
|
|
|
boolean_t need_wakeup = FALSE;
|
|
|
|
timer_manager_t manager;
|
|
|
|
|
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) {
|
|
|
|
if (timer->index == 1)
|
|
|
|
need_wakeup = TRUE;
|
|
|
|
heap_delete(manager->heap, timer->index);
|
|
|
|
timer->index = 0;
|
|
|
|
INSIST(manager->nscheduled > 0);
|
|
|
|
manager->nscheduled--;
|
1998-10-16 19:56:56 +00:00
|
|
|
if (need_wakeup) {
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACE("broadcast (deschedule)");
|
1998-10-16 01:18:31 +00:00
|
|
|
BROADCAST(&manager->wakeup);
|
1998-10-16 19:56:56 +00:00
|
|
|
}
|
1998-10-16 01:18:31 +00:00
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
destroy(timer_t timer) {
|
|
|
|
timer_manager_t manager = timer->manager;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The caller must ensure locking.
|
|
|
|
*/
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
|
|
|
task_purge_events(timer->task, timer, TASK_EVENT_ANYEVENT);
|
|
|
|
deschedule(timer);
|
|
|
|
UNLINK(manager->timers, timer, link);
|
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
task_detach(&timer->task);
|
|
|
|
(void)os_mutex_destroy(&timer->lock);
|
|
|
|
timer->magic = 0;
|
|
|
|
mem_put(manager->mctx, timer, sizeof *timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result
|
|
|
|
timer_create(timer_manager_t manager, timer_type_t type,
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_t expires, os_time_t interval,
|
1998-10-16 01:18:31 +00:00
|
|
|
task_t task, task_action_t action, void *arg, timer_t *timerp)
|
1998-10-15 22:22:50 +00:00
|
|
|
{
|
|
|
|
timer_t timer;
|
|
|
|
isc_result result;
|
1998-10-16 18:46:38 +00:00
|
|
|
os_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);
|
1998-10-16 18:46:38 +00:00
|
|
|
REQUIRE(!(ZERO(expires) && ZERO(interval)));
|
1998-10-15 22:22:50 +00:00
|
|
|
REQUIRE(timerp != NULL && *timerp == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get current time.
|
|
|
|
*/
|
|
|
|
result = os_time_get(&now);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
unexpected_error(__FILE__, __LINE__,
|
|
|
|
"os_time_get() failed: %s",
|
|
|
|
isc_result_to_text(result));
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
timer = mem_get(manager->mctx, sizeof *timer);
|
|
|
|
if (timer == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
timer->magic = TIMER_MAGIC;
|
|
|
|
timer->manager = manager;
|
|
|
|
timer->references = 1;
|
1998-10-16 21:41:30 +00:00
|
|
|
if (type == timer_type_once && !ZERO(interval))
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_add(&now, &interval, &timer->idle);
|
|
|
|
else {
|
|
|
|
timer->idle.seconds = 0;
|
|
|
|
timer->idle.nanoseconds = 0;
|
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->type = type;
|
1998-10-16 18:46:38 +00:00
|
|
|
timer->expires = expires;
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->interval = interval;
|
|
|
|
timer->task = NULL;
|
|
|
|
task_attach(task, &timer->task);
|
1998-10-16 01:18:31 +00:00
|
|
|
timer->action = action;
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->arg = arg;
|
1998-10-16 01:18:31 +00:00
|
|
|
timer->index = 0;
|
1998-10-15 22:22:50 +00:00
|
|
|
if (!os_mutex_init(&timer->lock)) {
|
|
|
|
mem_put(manager->mctx, timer, sizeof *timer);
|
|
|
|
unexpected_error(__FILE__, __LINE__, "os_mutex_init() failed");
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
APPEND(manager->timers, timer, link);
|
1998-10-16 19:56:56 +00:00
|
|
|
result = schedule(timer, &now, TRUE);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
*timerp = timer;
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result
|
|
|
|
timer_reset(timer_t timer, timer_type_t type,
|
1998-10-16 23:57:51 +00:00
|
|
|
os_time_t expires, os_time_t interval, boolean_t purge)
|
1998-10-15 22:22:50 +00:00
|
|
|
{
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_t now;
|
1998-10-15 22:22:50 +00:00
|
|
|
timer_manager_t manager;
|
|
|
|
isc_result result;
|
|
|
|
|
|
|
|
/*
|
1998-10-16 23:57:51 +00:00
|
|
|
* Change the timer's type, expires, and interval values to the given
|
|
|
|
* values. If 'purge' is 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));
|
1998-10-16 18:46:38 +00:00
|
|
|
REQUIRE(!(ZERO(expires) && ZERO(interval)));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get current time.
|
|
|
|
*/
|
|
|
|
result = os_time_get(&now);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
unexpected_error(__FILE__, __LINE__,
|
|
|
|
"os_time_get() failed: %s",
|
|
|
|
isc_result_to_text(result));
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
manager = timer->manager;
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
|
1998-10-16 23:57:51 +00:00
|
|
|
if (purge)
|
|
|
|
task_purge_events(timer->task, timer, TASK_EVENT_ANYEVENT);
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->type = type;
|
1998-10-16 18:46:38 +00:00
|
|
|
timer->expires = expires;
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->interval = interval;
|
1998-10-16 21:41:30 +00:00
|
|
|
if (type == timer_type_once && !ZERO(interval))
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_add(&now, &interval, &timer->idle);
|
|
|
|
else {
|
|
|
|
timer->idle.seconds = 0;
|
|
|
|
timer->idle.nanoseconds = 0;
|
|
|
|
}
|
1998-10-16 19:56:56 +00:00
|
|
|
result = schedule(timer, &now, TRUE);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result
|
|
|
|
timer_touch(timer_t timer) {
|
1998-10-16 01:18:31 +00:00
|
|
|
isc_result result;
|
1998-10-16 18:46:38 +00:00
|
|
|
os_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);
|
|
|
|
|
1998-10-16 21:41:30 +00:00
|
|
|
INSIST(timer->type == timer_type_once);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
1998-10-16 18:46:38 +00:00
|
|
|
result = os_time_get(&now);
|
1998-10-15 22:22:50 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
unexpected_error(__FILE__, __LINE__,
|
|
|
|
"os_time_get() failed: %s",
|
|
|
|
isc_result_to_text(result));
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
1998-10-16 18:46:38 +00:00
|
|
|
os_time_add(&now, &timer->interval, &timer->idle);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
timer_attach(timer_t timer, timer_t *timerp) {
|
|
|
|
/*
|
|
|
|
* Attach *timerp to timer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_TIMER(timer));
|
|
|
|
REQUIRE(timerp != NULL && *timerp == NULL);
|
|
|
|
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
timer->references++;
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
|
|
|
*timerp = timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
timer_detach(timer_t *timerp) {
|
|
|
|
timer_t timer;
|
|
|
|
boolean_t free_timer = FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Detach *timerp from its timer.
|
|
|
|
*/
|
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
REQUIRE(timerp != NULL);
|
|
|
|
timer = *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)
|
|
|
|
free_timer = TRUE;
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
|
|
|
if (free_timer)
|
|
|
|
destroy(timer);
|
|
|
|
|
|
|
|
*timerp = NULL;
|
|
|
|
}
|
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
static void
|
|
|
|
dispatch(timer_manager_t manager, os_time_t *nowp) {
|
|
|
|
boolean_t done = FALSE, post_event, need_schedule;
|
|
|
|
task_event_t event;
|
1998-10-16 18:46:38 +00:00
|
|
|
task_eventtype_t type = 0;
|
1998-10-16 07:44:20 +00:00
|
|
|
timer_t timer;
|
|
|
|
isc_result result;
|
|
|
|
|
|
|
|
while (manager->nscheduled > 0 && !done) {
|
|
|
|
timer = heap_element(manager->heap, 1);
|
1998-10-16 18:46:38 +00:00
|
|
|
if (os_time_compare(nowp, &timer->due) >= 0) {
|
1998-10-16 07:44:20 +00:00
|
|
|
if (timer->type == timer_type_ticker) {
|
|
|
|
type = TIMER_EVENT_TICK;
|
|
|
|
post_event = TRUE;
|
|
|
|
need_schedule = TRUE;
|
1998-10-16 18:46:38 +00:00
|
|
|
} else if (!ZERO(timer->expires) &&
|
|
|
|
os_time_compare(nowp,
|
|
|
|
&timer->expires) >= 0) {
|
1998-10-16 07:44:20 +00:00
|
|
|
type = TIMER_EVENT_LIFE;
|
|
|
|
post_event = TRUE;
|
|
|
|
need_schedule = FALSE;
|
1998-10-16 18:46:38 +00:00
|
|
|
} else if (!ZERO(timer->idle) &&
|
|
|
|
os_time_compare(nowp,
|
|
|
|
&timer->idle) >= 0) {
|
|
|
|
type = TIMER_EVENT_IDLE;
|
|
|
|
post_event = TRUE;
|
|
|
|
need_schedule = FALSE;
|
1998-10-16 07:44:20 +00:00
|
|
|
} else {
|
|
|
|
/*
|
1998-10-16 18:46:38 +00:00
|
|
|
* Idle timer has been touched; reschedule.
|
1998-10-16 07:44:20 +00:00
|
|
|
*/
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACEID("idle reschedule", timer);
|
1998-10-16 07:44:20 +00:00
|
|
|
post_event = FALSE;
|
1998-10-16 18:46:38 +00:00
|
|
|
need_schedule = TRUE;
|
1998-10-16 07:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (post_event) {
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACEID("posting", timer);
|
1998-10-16 07:44:20 +00:00
|
|
|
event = task_event_allocate(manager->mctx,
|
|
|
|
timer,
|
|
|
|
type,
|
|
|
|
timer->action,
|
|
|
|
timer->arg,
|
|
|
|
sizeof *event);
|
1998-10-16 21:41:30 +00:00
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
if (event != NULL)
|
|
|
|
INSIST(task_send_event(timer->task,
|
|
|
|
&event));
|
|
|
|
else
|
|
|
|
unexpected_error(__FILE__, __LINE__,
|
|
|
|
"couldn't allocate event");
|
|
|
|
}
|
|
|
|
|
|
|
|
timer->index = 0;
|
|
|
|
heap_delete(manager->heap, 1);
|
|
|
|
manager->nscheduled--;
|
|
|
|
|
|
|
|
if (need_schedule) {
|
1998-10-16 19:56:56 +00:00
|
|
|
result = schedule(timer, nowp, FALSE);
|
1998-10-16 07:44:20 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
unexpected_error(__FILE__, __LINE__,
|
|
|
|
"couldn't schedule timer: %s",
|
|
|
|
result);
|
|
|
|
}
|
|
|
|
} else {
|
1998-10-16 18:46:38 +00:00
|
|
|
manager->due = timer->due;
|
1998-10-16 07:44:20 +00:00
|
|
|
done = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
static void *
|
|
|
|
run(void *uap) {
|
|
|
|
timer_manager_t manager = uap;
|
|
|
|
boolean_t timeout;
|
1998-10-16 01:54:04 +00:00
|
|
|
os_time_t now;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
while (!manager->done) {
|
1998-10-16 01:54:04 +00:00
|
|
|
INSIST(os_time_get(&now) == ISC_R_SUCCESS);
|
1998-10-16 21:53:23 +00:00
|
|
|
|
|
|
|
XTRACETIME("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) {
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACETIME("waituntil", manager->due);
|
1998-10-16 22:09:09 +00:00
|
|
|
WAITUNTIL(&manager->wakeup, &manager->lock,
|
|
|
|
&manager->due, &timeout);
|
1998-10-16 19:56:56 +00:00
|
|
|
} else {
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACE("wait");
|
1998-10-16 01:18:31 +00:00
|
|
|
WAIT(&manager->wakeup, &manager->lock);
|
1998-10-16 19:56:56 +00:00
|
|
|
}
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACE("wakeup");
|
1998-10-16 01:18:31 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
1998-10-16 21:54:53 +00:00
|
|
|
static boolean_t
|
|
|
|
sooner(void *v1, void *v2) {
|
|
|
|
timer_t t1, t2;
|
|
|
|
|
|
|
|
t1 = v1;
|
|
|
|
t2 = v2;
|
|
|
|
REQUIRE(VALID_TIMER(t1));
|
|
|
|
REQUIRE(VALID_TIMER(t2));
|
|
|
|
|
|
|
|
if (os_time_compare(&t1->due, &t2->due) < 0)
|
|
|
|
return (TRUE);
|
|
|
|
return (FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_index(void *what, unsigned int index) {
|
|
|
|
timer_t timer;
|
|
|
|
|
|
|
|
timer = what;
|
|
|
|
REQUIRE(VALID_TIMER(timer));
|
|
|
|
|
|
|
|
timer->index = index;
|
|
|
|
}
|
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
isc_result
|
1998-10-16 01:18:31 +00:00
|
|
|
timer_manager_create(mem_context_t mctx, timer_manager_t *managerp) {
|
|
|
|
timer_manager_t manager;
|
|
|
|
isc_result result;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a timer manager.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(managerp != NULL && *managerp == NULL);
|
|
|
|
|
|
|
|
manager = mem_get(mctx, sizeof *manager);
|
|
|
|
if (manager == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
manager->magic = TIMER_MANAGER_MAGIC;
|
|
|
|
manager->mctx = mctx;
|
|
|
|
manager->done = FALSE;
|
|
|
|
INIT_LIST(manager->timers);
|
|
|
|
manager->nscheduled = 0;
|
1998-10-16 18:46:38 +00:00
|
|
|
manager->due.seconds = 0;
|
|
|
|
manager->due.nanoseconds = 0;
|
1998-10-16 01:18:31 +00:00
|
|
|
manager->heap = NULL;
|
|
|
|
result = heap_create(mctx, sooner, set_index, 0, &manager->heap);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
INSIST(result == ISC_R_NOMEMORY);
|
|
|
|
mem_put(mctx, manager, sizeof *manager);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
if (!os_mutex_init(&manager->lock)) {
|
|
|
|
heap_destroy(&manager->heap);
|
|
|
|
mem_put(mctx, manager, sizeof *manager);
|
|
|
|
unexpected_error(__FILE__, __LINE__, "os_mutex_init() failed");
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
if (!os_condition_init(&manager->wakeup)) {
|
|
|
|
(void)os_mutex_destroy(&manager->lock);
|
|
|
|
heap_destroy(&manager->heap);
|
|
|
|
mem_put(mctx, manager, sizeof *manager);
|
|
|
|
unexpected_error(__FILE__, __LINE__,
|
|
|
|
"os_condition_init() failed");
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
if (!os_thread_create(run, manager, &manager->thread)) {
|
|
|
|
(void)os_condition_destroy(&manager->wakeup);
|
|
|
|
(void)os_mutex_destroy(&manager->lock);
|
|
|
|
heap_destroy(&manager->heap);
|
|
|
|
mem_put(mctx, manager, sizeof *manager);
|
|
|
|
unexpected_error(__FILE__, __LINE__,
|
|
|
|
"os_thread_create() failed");
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
*managerp = manager;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
void
|
1998-10-16 01:18:31 +00:00
|
|
|
timer_manager_destroy(timer_manager_t *managerp) {
|
|
|
|
timer_manager_t manager;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a timer manager.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(managerp != NULL);
|
|
|
|
manager = *managerp;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
|
|
|
REQUIRE(EMPTY(manager->timers));
|
|
|
|
manager->done = TRUE;
|
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-10-16 21:53:23 +00:00
|
|
|
XTRACE("broadcast (destroy)");
|
1998-10-16 01:18:31 +00:00
|
|
|
BROADCAST(&manager->wakeup);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for thread to exit.
|
|
|
|
*/
|
|
|
|
if (!os_thread_join(manager->thread))
|
|
|
|
unexpected_error(__FILE__, __LINE__,
|
|
|
|
"os_thread_join() failed");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up.
|
|
|
|
*/
|
|
|
|
(void)os_condition_destroy(&manager->wakeup);
|
|
|
|
(void)os_mutex_destroy(&manager->lock);
|
|
|
|
heap_destroy(&manager->heap);
|
|
|
|
manager->magic = 0;
|
|
|
|
mem_put(manager->mctx, manager, sizeof *manager);
|
|
|
|
|
|
|
|
*managerp = NULL;
|
|
|
|
}
|