Home | History | Annotate | Line # | Download | only in isc
timer.c revision 1.9
      1  1.8  christos /*	$NetBSD: timer.c,v 1.9 2021/08/19 11:50:18 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      7  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  1.7  christos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
      9  1.1  christos  *
     10  1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     11  1.1  christos  * information regarding copyright ownership.
     12  1.1  christos  */
     13  1.1  christos 
     14  1.1  christos /*! \file */
     15  1.1  christos 
     16  1.3  christos #include <stdbool.h>
     17  1.3  christos 
     18  1.1  christos #include <isc/app.h>
     19  1.1  christos #include <isc/condition.h>
     20  1.1  christos #include <isc/heap.h>
     21  1.1  christos #include <isc/log.h>
     22  1.1  christos #include <isc/magic.h>
     23  1.1  christos #include <isc/mem.h>
     24  1.1  christos #include <isc/once.h>
     25  1.1  christos #include <isc/platform.h>
     26  1.1  christos #include <isc/print.h>
     27  1.6  christos #include <isc/refcount.h>
     28  1.1  christos #include <isc/task.h>
     29  1.1  christos #include <isc/thread.h>
     30  1.1  christos #include <isc/time.h>
     31  1.1  christos #include <isc/timer.h>
     32  1.1  christos #include <isc/util.h>
     33  1.1  christos 
     34  1.1  christos #ifdef OPENSSL_LEAKS
     35  1.1  christos #include <openssl/err.h>
     36  1.6  christos #endif /* ifdef OPENSSL_LEAKS */
     37  1.1  christos 
     38  1.1  christos #ifdef ISC_TIMER_TRACE
     39  1.6  christos #define XTRACE(s)      fprintf(stderr, "%s\n", (s))
     40  1.6  christos #define XTRACEID(s, t) fprintf(stderr, "%s %p\n", (s), (t))
     41  1.6  christos #define XTRACETIME(s, d) \
     42  1.6  christos 	fprintf(stderr, "%s %u.%09u\n", (s), (d).seconds, (d).nanoseconds)
     43  1.6  christos #define XTRACETIME2(s, d, n)                                      \
     44  1.6  christos 	fprintf(stderr, "%s %u.%09u %u.%09u\n", (s), (d).seconds, \
     45  1.6  christos 		(d).nanoseconds, (n).seconds, (n).nanoseconds)
     46  1.6  christos #define XTRACETIMER(s, t, d)                                      \
     47  1.6  christos 	fprintf(stderr, "%s %p %u.%09u\n", (s), (t), (d).seconds, \
     48  1.6  christos 		(d).nanoseconds)
     49  1.6  christos #else /* ifdef ISC_TIMER_TRACE */
     50  1.1  christos #define XTRACE(s)
     51  1.1  christos #define XTRACEID(s, t)
     52  1.1  christos #define XTRACETIME(s, d)
     53  1.1  christos #define XTRACETIME2(s, d, n)
     54  1.1  christos #define XTRACETIMER(s, t, d)
     55  1.1  christos #endif /* ISC_TIMER_TRACE */
     56  1.1  christos 
     57  1.6  christos #define TIMER_MAGIC    ISC_MAGIC('T', 'I', 'M', 'R')
     58  1.6  christos #define VALID_TIMER(t) ISC_MAGIC_VALID(t, TIMER_MAGIC)
     59  1.1  christos 
     60  1.9  christos struct isc_timer {
     61  1.1  christos 	/*! Not locked. */
     62  1.9  christos 	unsigned int magic;
     63  1.9  christos 	isc_timermgr_t *manager;
     64  1.6  christos 	isc_mutex_t lock;
     65  1.6  christos 	isc_refcount_t references;
     66  1.1  christos 	/*! Locked by timer lock. */
     67  1.6  christos 	isc_time_t idle;
     68  1.1  christos 	/*! Locked by manager lock. */
     69  1.6  christos 	isc_timertype_t type;
     70  1.6  christos 	isc_time_t expires;
     71  1.6  christos 	isc_interval_t interval;
     72  1.6  christos 	isc_task_t *task;
     73  1.6  christos 	isc_taskaction_t action;
     74  1.6  christos 	void *arg;
     75  1.6  christos 	unsigned int index;
     76  1.6  christos 	isc_time_t due;
     77  1.9  christos 	LINK(isc_timer_t) link;
     78  1.1  christos };
     79  1.1  christos 
     80  1.6  christos #define TIMER_MANAGER_MAGIC ISC_MAGIC('T', 'I', 'M', 'M')
     81  1.6  christos #define VALID_MANAGER(m)    ISC_MAGIC_VALID(m, TIMER_MANAGER_MAGIC)
     82  1.1  christos 
     83  1.9  christos struct isc_timermgr {
     84  1.1  christos 	/* Not locked. */
     85  1.9  christos 	unsigned int magic;
     86  1.6  christos 	isc_mem_t *mctx;
     87  1.6  christos 	isc_mutex_t lock;
     88  1.1  christos 	/* Locked by manager lock. */
     89  1.6  christos 	bool done;
     90  1.9  christos 	LIST(isc_timer_t) timers;
     91  1.6  christos 	unsigned int nscheduled;
     92  1.6  christos 	isc_time_t due;
     93  1.6  christos 	isc_condition_t wakeup;
     94  1.6  christos 	isc_thread_t thread;
     95  1.6  christos 	isc_heap_t *heap;
     96  1.1  christos };
     97  1.1  christos 
     98  1.1  christos void
     99  1.1  christos isc_timermgr_poke(isc_timermgr_t *manager0);
    100  1.1  christos 
    101  1.1  christos static inline isc_result_t
    102  1.9  christos schedule(isc_timer_t *timer, isc_time_t *now, bool signal_ok) {
    103  1.1  christos 	isc_result_t result;
    104  1.9  christos 	isc_timermgr_t *manager;
    105  1.1  christos 	isc_time_t due;
    106  1.1  christos 	int cmp;
    107  1.1  christos 
    108  1.1  christos 	/*!
    109  1.1  christos 	 * Note: the caller must ensure locking.
    110  1.1  christos 	 */
    111  1.1  christos 
    112  1.1  christos 	REQUIRE(timer->type != isc_timertype_inactive);
    113  1.1  christos 
    114  1.1  christos 	manager = timer->manager;
    115  1.1  christos 
    116  1.1  christos 	/*
    117  1.1  christos 	 * Compute the new due time.
    118  1.1  christos 	 */
    119  1.1  christos 	if (timer->type != isc_timertype_once) {
    120  1.1  christos 		result = isc_time_add(now, &timer->interval, &due);
    121  1.6  christos 		if (result != ISC_R_SUCCESS) {
    122  1.1  christos 			return (result);
    123  1.6  christos 		}
    124  1.1  christos 		if (timer->type == isc_timertype_limited &&
    125  1.1  christos 		    isc_time_compare(&timer->expires, &due) < 0)
    126  1.6  christos 		{
    127  1.1  christos 			due = timer->expires;
    128  1.6  christos 		}
    129  1.1  christos 	} else {
    130  1.6  christos 		if (isc_time_isepoch(&timer->idle)) {
    131  1.1  christos 			due = timer->expires;
    132  1.6  christos 		} else if (isc_time_isepoch(&timer->expires)) {
    133  1.1  christos 			due = timer->idle;
    134  1.6  christos 		} else if (isc_time_compare(&timer->idle, &timer->expires) < 0)
    135  1.6  christos 		{
    136  1.1  christos 			due = timer->idle;
    137  1.6  christos 		} else {
    138  1.1  christos 			due = timer->expires;
    139  1.6  christos 		}
    140  1.1  christos 	}
    141  1.1  christos 
    142  1.1  christos 	/*
    143  1.1  christos 	 * Schedule the timer.
    144  1.1  christos 	 */
    145  1.1  christos 
    146  1.1  christos 	if (timer->index > 0) {
    147  1.1  christos 		/*
    148  1.1  christos 		 * Already scheduled.
    149  1.1  christos 		 */
    150  1.1  christos 		cmp = isc_time_compare(&due, &timer->due);
    151  1.1  christos 		timer->due = due;
    152  1.1  christos 		switch (cmp) {
    153  1.1  christos 		case -1:
    154  1.1  christos 			isc_heap_increased(manager->heap, timer->index);
    155  1.1  christos 			break;
    156  1.1  christos 		case 1:
    157  1.1  christos 			isc_heap_decreased(manager->heap, timer->index);
    158  1.1  christos 			break;
    159  1.1  christos 		case 0:
    160  1.1  christos 			/* Nothing to do. */
    161  1.1  christos 			break;
    162  1.1  christos 		}
    163  1.1  christos 	} else {
    164  1.1  christos 		timer->due = due;
    165  1.1  christos 		result = isc_heap_insert(manager->heap, timer);
    166  1.1  christos 		if (result != ISC_R_SUCCESS) {
    167  1.1  christos 			INSIST(result == ISC_R_NOMEMORY);
    168  1.1  christos 			return (ISC_R_NOMEMORY);
    169  1.1  christos 		}
    170  1.1  christos 		manager->nscheduled++;
    171  1.1  christos 	}
    172  1.1  christos 
    173  1.4  christos 	XTRACETIMER("schedule", timer, due);
    174  1.1  christos 
    175  1.1  christos 	/*
    176  1.1  christos 	 * If this timer is at the head of the queue, we need to ensure
    177  1.1  christos 	 * that we won't miss it if it has a more recent due time than
    178  1.1  christos 	 * the current "next" timer.  We do this either by waking up the
    179  1.1  christos 	 * run thread, or explicitly setting the value in the manager.
    180  1.1  christos 	 */
    181  1.1  christos 
    182  1.1  christos 	if (timer->index == 1 && signal_ok) {
    183  1.4  christos 		XTRACE("signal (schedule)");
    184  1.1  christos 		SIGNAL(&manager->wakeup);
    185  1.1  christos 	}
    186  1.1  christos 
    187  1.1  christos 	return (ISC_R_SUCCESS);
    188  1.1  christos }
    189  1.1  christos 
    190  1.1  christos static inline void
    191  1.9  christos deschedule(isc_timer_t *timer) {
    192  1.3  christos 	bool need_wakeup = false;
    193  1.9  christos 	isc_timermgr_t *manager;
    194  1.1  christos 
    195  1.1  christos 	/*
    196  1.1  christos 	 * The caller must ensure locking.
    197  1.1  christos 	 */
    198  1.1  christos 
    199  1.1  christos 	manager = timer->manager;
    200  1.1  christos 	if (timer->index > 0) {
    201  1.6  christos 		if (timer->index == 1) {
    202  1.3  christos 			need_wakeup = true;
    203  1.6  christos 		}
    204  1.1  christos 		isc_heap_delete(manager->heap, timer->index);
    205  1.1  christos 		timer->index = 0;
    206  1.1  christos 		INSIST(manager->nscheduled > 0);
    207  1.1  christos 		manager->nscheduled--;
    208  1.1  christos 		if (need_wakeup) {
    209  1.4  christos 			XTRACE("signal (deschedule)");
    210  1.1  christos 			SIGNAL(&manager->wakeup);
    211  1.1  christos 		}
    212  1.1  christos 	}
    213  1.1  christos }
    214  1.1  christos 
    215  1.1  christos static void
    216  1.9  christos destroy(isc_timer_t *timer) {
    217  1.9  christos 	isc_timermgr_t *manager = timer->manager;
    218  1.1  christos 
    219  1.1  christos 	/*
    220  1.1  christos 	 * The caller must ensure it is safe to destroy the timer.
    221  1.1  christos 	 */
    222  1.1  christos 
    223  1.1  christos 	LOCK(&manager->lock);
    224  1.1  christos 
    225  1.6  christos 	(void)isc_task_purgerange(timer->task, timer, ISC_TIMEREVENT_FIRSTEVENT,
    226  1.6  christos 				  ISC_TIMEREVENT_LASTEVENT, NULL);
    227  1.1  christos 	deschedule(timer);
    228  1.1  christos 	UNLINK(manager->timers, timer, link);
    229  1.1  christos 
    230  1.1  christos 	UNLOCK(&manager->lock);
    231  1.1  christos 
    232  1.1  christos 	isc_task_detach(&timer->task);
    233  1.3  christos 	isc_mutex_destroy(&timer->lock);
    234  1.9  christos 	timer->magic = 0;
    235  1.1  christos 	isc_mem_put(manager->mctx, timer, sizeof(*timer));
    236  1.1  christos }
    237  1.1  christos 
    238  1.1  christos isc_result_t
    239  1.9  christos isc_timer_create(isc_timermgr_t *manager, isc_timertype_t type,
    240  1.3  christos 		 const isc_time_t *expires, const isc_interval_t *interval,
    241  1.3  christos 		 isc_task_t *task, isc_taskaction_t action, void *arg,
    242  1.6  christos 		 isc_timer_t **timerp) {
    243  1.9  christos 	REQUIRE(VALID_MANAGER(manager));
    244  1.6  christos 	REQUIRE(task != NULL);
    245  1.6  christos 	REQUIRE(action != NULL);
    246  1.6  christos 
    247  1.9  christos 	isc_timer_t *timer;
    248  1.1  christos 	isc_result_t result;
    249  1.1  christos 	isc_time_t now;
    250  1.1  christos 
    251  1.1  christos 	/*
    252  1.1  christos 	 * Create a new 'type' timer managed by 'manager'.  The timers
    253  1.1  christos 	 * parameters are specified by 'expires' and 'interval'.  Events
    254  1.1  christos 	 * will be posted to 'task' and when dispatched 'action' will be
    255  1.1  christos 	 * called with 'arg' as the arg value.  The new timer is returned
    256  1.1  christos 	 * in 'timerp'.
    257  1.1  christos 	 */
    258  1.6  christos 	if (expires == NULL) {
    259  1.1  christos 		expires = isc_time_epoch;
    260  1.6  christos 	}
    261  1.6  christos 	if (interval == NULL) {
    262  1.1  christos 		interval = isc_interval_zero;
    263  1.6  christos 	}
    264  1.1  christos 	REQUIRE(type == isc_timertype_inactive ||
    265  1.1  christos 		!(isc_time_isepoch(expires) && isc_interval_iszero(interval)));
    266  1.1  christos 	REQUIRE(timerp != NULL && *timerp == NULL);
    267  1.1  christos 	REQUIRE(type != isc_timertype_limited ||
    268  1.1  christos 		!(isc_time_isepoch(expires) || isc_interval_iszero(interval)));
    269  1.1  christos 
    270  1.1  christos 	/*
    271  1.1  christos 	 * Get current time.
    272  1.1  christos 	 */
    273  1.1  christos 	if (type != isc_timertype_inactive) {
    274  1.1  christos 		TIME_NOW(&now);
    275  1.1  christos 	} else {
    276  1.1  christos 		/*
    277  1.1  christos 		 * We don't have to do this, but it keeps the compiler from
    278  1.1  christos 		 * complaining about "now" possibly being used without being
    279  1.1  christos 		 * set, even though it will never actually happen.
    280  1.1  christos 		 */
    281  1.1  christos 		isc_time_settoepoch(&now);
    282  1.1  christos 	}
    283  1.1  christos 
    284  1.1  christos 	timer = isc_mem_get(manager->mctx, sizeof(*timer));
    285  1.1  christos 
    286  1.1  christos 	timer->manager = manager;
    287  1.6  christos 	isc_refcount_init(&timer->references, 1);
    288  1.1  christos 
    289  1.1  christos 	if (type == isc_timertype_once && !isc_interval_iszero(interval)) {
    290  1.1  christos 		result = isc_time_add(&now, interval, &timer->idle);
    291  1.1  christos 		if (result != ISC_R_SUCCESS) {
    292  1.1  christos 			isc_mem_put(manager->mctx, timer, sizeof(*timer));
    293  1.1  christos 			return (result);
    294  1.1  christos 		}
    295  1.6  christos 	} else {
    296  1.1  christos 		isc_time_settoepoch(&timer->idle);
    297  1.6  christos 	}
    298  1.1  christos 
    299  1.1  christos 	timer->type = type;
    300  1.1  christos 	timer->expires = *expires;
    301  1.1  christos 	timer->interval = *interval;
    302  1.1  christos 	timer->task = NULL;
    303  1.1  christos 	isc_task_attach(task, &timer->task);
    304  1.1  christos 	timer->action = action;
    305  1.1  christos 	/*
    306  1.1  christos 	 * Removing the const attribute from "arg" is the best of two
    307  1.1  christos 	 * evils here.  If the timer->arg member is made const, then
    308  1.1  christos 	 * it affects a great many recipients of the timer event
    309  1.1  christos 	 * which did not pass in an "arg" that was truly const.
    310  1.1  christos 	 * Changing isc_timer_create() to not have "arg" prototyped as const,
    311  1.1  christos 	 * though, can cause compilers warnings for calls that *do*
    312  1.1  christos 	 * have a truly const arg.  The caller will have to carefully
    313  1.1  christos 	 * keep track of whether arg started as a true const.
    314  1.1  christos 	 */
    315  1.1  christos 	DE_CONST(arg, timer->arg);
    316  1.1  christos 	timer->index = 0;
    317  1.3  christos 	isc_mutex_init(&timer->lock);
    318  1.1  christos 	ISC_LINK_INIT(timer, link);
    319  1.9  christos 	timer->magic = TIMER_MAGIC;
    320  1.1  christos 
    321  1.1  christos 	LOCK(&manager->lock);
    322  1.1  christos 
    323  1.1  christos 	/*
    324  1.1  christos 	 * Note we don't have to lock the timer like we normally would because
    325  1.1  christos 	 * there are no external references to it yet.
    326  1.1  christos 	 */
    327  1.1  christos 
    328  1.6  christos 	if (type != isc_timertype_inactive) {
    329  1.3  christos 		result = schedule(timer, &now, true);
    330  1.6  christos 	} else {
    331  1.1  christos 		result = ISC_R_SUCCESS;
    332  1.6  christos 	}
    333  1.3  christos 	if (result == ISC_R_SUCCESS) {
    334  1.9  christos 		*timerp = timer;
    335  1.1  christos 		APPEND(manager->timers, timer, link);
    336  1.3  christos 	}
    337  1.1  christos 
    338  1.1  christos 	UNLOCK(&manager->lock);
    339  1.1  christos 
    340  1.1  christos 	if (result != ISC_R_SUCCESS) {
    341  1.9  christos 		timer->magic = 0;
    342  1.3  christos 		isc_mutex_destroy(&timer->lock);
    343  1.1  christos 		isc_task_detach(&timer->task);
    344  1.1  christos 		isc_mem_put(manager->mctx, timer, sizeof(*timer));
    345  1.1  christos 		return (result);
    346  1.1  christos 	}
    347  1.1  christos 
    348  1.1  christos 	return (ISC_R_SUCCESS);
    349  1.1  christos }
    350  1.1  christos 
    351  1.1  christos isc_result_t
    352  1.9  christos isc_timer_reset(isc_timer_t *timer, isc_timertype_t type,
    353  1.6  christos 		const isc_time_t *expires, const isc_interval_t *interval,
    354  1.6  christos 		bool purge) {
    355  1.1  christos 	isc_time_t now;
    356  1.9  christos 	isc_timermgr_t *manager;
    357  1.1  christos 	isc_result_t result;
    358  1.1  christos 
    359  1.1  christos 	/*
    360  1.1  christos 	 * Change the timer's type, expires, and interval values to the given
    361  1.3  christos 	 * values.  If 'purge' is true, any pending events from this timer
    362  1.1  christos 	 * are purged from its task's event queue.
    363  1.1  christos 	 */
    364  1.1  christos 
    365  1.9  christos 	REQUIRE(VALID_TIMER(timer));
    366  1.1  christos 	manager = timer->manager;
    367  1.1  christos 	REQUIRE(VALID_MANAGER(manager));
    368  1.1  christos 
    369  1.6  christos 	if (expires == NULL) {
    370  1.1  christos 		expires = isc_time_epoch;
    371  1.6  christos 	}
    372  1.6  christos 	if (interval == NULL) {
    373  1.1  christos 		interval = isc_interval_zero;
    374  1.6  christos 	}
    375  1.1  christos 	REQUIRE(type == isc_timertype_inactive ||
    376  1.1  christos 		!(isc_time_isepoch(expires) && isc_interval_iszero(interval)));
    377  1.1  christos 	REQUIRE(type != isc_timertype_limited ||
    378  1.1  christos 		!(isc_time_isepoch(expires) || isc_interval_iszero(interval)));
    379  1.1  christos 
    380  1.1  christos 	/*
    381  1.1  christos 	 * Get current time.
    382  1.1  christos 	 */
    383  1.1  christos 	if (type != isc_timertype_inactive) {
    384  1.1  christos 		TIME_NOW(&now);
    385  1.1  christos 	} else {
    386  1.1  christos 		/*
    387  1.1  christos 		 * We don't have to do this, but it keeps the compiler from
    388  1.1  christos 		 * complaining about "now" possibly being used without being
    389  1.1  christos 		 * set, even though it will never actually happen.
    390  1.1  christos 		 */
    391  1.1  christos 		isc_time_settoepoch(&now);
    392  1.1  christos 	}
    393  1.1  christos 
    394  1.1  christos 	LOCK(&manager->lock);
    395  1.1  christos 	LOCK(&timer->lock);
    396  1.1  christos 
    397  1.6  christos 	if (purge) {
    398  1.6  christos 		(void)isc_task_purgerange(timer->task, timer,
    399  1.1  christos 					  ISC_TIMEREVENT_FIRSTEVENT,
    400  1.6  christos 					  ISC_TIMEREVENT_LASTEVENT, NULL);
    401  1.6  christos 	}
    402  1.1  christos 	timer->type = type;
    403  1.1  christos 	timer->expires = *expires;
    404  1.1  christos 	timer->interval = *interval;
    405  1.1  christos 	if (type == isc_timertype_once && !isc_interval_iszero(interval)) {
    406  1.1  christos 		result = isc_time_add(&now, interval, &timer->idle);
    407  1.1  christos 	} else {
    408  1.1  christos 		isc_time_settoepoch(&timer->idle);
    409  1.1  christos 		result = ISC_R_SUCCESS;
    410  1.1  christos 	}
    411  1.1  christos 
    412  1.1  christos 	if (result == ISC_R_SUCCESS) {
    413  1.1  christos 		if (type == isc_timertype_inactive) {
    414  1.1  christos 			deschedule(timer);
    415  1.1  christos 			result = ISC_R_SUCCESS;
    416  1.6  christos 		} else {
    417  1.3  christos 			result = schedule(timer, &now, true);
    418  1.6  christos 		}
    419  1.1  christos 	}
    420  1.1  christos 
    421  1.1  christos 	UNLOCK(&timer->lock);
    422  1.1  christos 	UNLOCK(&manager->lock);
    423  1.1  christos 
    424  1.1  christos 	return (result);
    425  1.1  christos }
    426  1.1  christos 
    427  1.1  christos isc_timertype_t
    428  1.9  christos isc_timer_gettype(isc_timer_t *timer) {
    429  1.1  christos 	isc_timertype_t t;
    430  1.1  christos 
    431  1.9  christos 	REQUIRE(VALID_TIMER(timer));
    432  1.1  christos 
    433  1.1  christos 	LOCK(&timer->lock);
    434  1.1  christos 	t = timer->type;
    435  1.1  christos 	UNLOCK(&timer->lock);
    436  1.1  christos 
    437  1.1  christos 	return (t);
    438  1.1  christos }
    439  1.1  christos 
    440  1.1  christos isc_result_t
    441  1.9  christos isc_timer_touch(isc_timer_t *timer) {
    442  1.1  christos 	isc_result_t result;
    443  1.1  christos 	isc_time_t now;
    444  1.1  christos 
    445  1.1  christos 	/*
    446  1.1  christos 	 * Set the last-touched time of 'timer' to the current time.
    447  1.1  christos 	 */
    448  1.1  christos 
    449  1.9  christos 	REQUIRE(VALID_TIMER(timer));
    450  1.1  christos 
    451  1.1  christos 	LOCK(&timer->lock);
    452  1.1  christos 
    453  1.1  christos 	/*
    454  1.1  christos 	 * We'd like to
    455  1.1  christos 	 *
    456  1.1  christos 	 *	REQUIRE(timer->type == isc_timertype_once);
    457  1.1  christos 	 *
    458  1.1  christos 	 * but we cannot without locking the manager lock too, which we
    459  1.1  christos 	 * don't want to do.
    460  1.1  christos 	 */
    461  1.1  christos 
    462  1.1  christos 	TIME_NOW(&now);
    463  1.1  christos 	result = isc_time_add(&now, &timer->interval, &timer->idle);
    464  1.1  christos 
    465  1.1  christos 	UNLOCK(&timer->lock);
    466  1.1  christos 
    467  1.1  christos 	return (result);
    468  1.1  christos }
    469  1.1  christos 
    470  1.1  christos void
    471  1.9  christos isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) {
    472  1.1  christos 	/*
    473  1.1  christos 	 * Attach *timerp to timer.
    474  1.1  christos 	 */
    475  1.1  christos 
    476  1.9  christos 	REQUIRE(VALID_TIMER(timer));
    477  1.1  christos 	REQUIRE(timerp != NULL && *timerp == NULL);
    478  1.6  christos 	isc_refcount_increment(&timer->references);
    479  1.1  christos 
    480  1.9  christos 	*timerp = timer;
    481  1.1  christos }
    482  1.1  christos 
    483  1.1  christos void
    484  1.3  christos isc_timer_detach(isc_timer_t **timerp) {
    485  1.9  christos 	isc_timer_t *timer;
    486  1.1  christos 
    487  1.1  christos 	/*
    488  1.1  christos 	 * Detach *timerp from its timer.
    489  1.1  christos 	 */
    490  1.1  christos 
    491  1.1  christos 	REQUIRE(timerp != NULL);
    492  1.9  christos 	timer = *timerp;
    493  1.1  christos 	REQUIRE(VALID_TIMER(timer));
    494  1.1  christos 
    495  1.6  christos 	if (isc_refcount_decrement(&timer->references) == 1) {
    496  1.1  christos 		destroy(timer);
    497  1.6  christos 	}
    498  1.1  christos 
    499  1.1  christos 	*timerp = NULL;
    500  1.1  christos }
    501  1.1  christos 
    502  1.1  christos static void
    503  1.9  christos dispatch(isc_timermgr_t *manager, isc_time_t *now) {
    504  1.3  christos 	bool done = false, post_event, need_schedule;
    505  1.1  christos 	isc_timerevent_t *event;
    506  1.1  christos 	isc_eventtype_t type = 0;
    507  1.9  christos 	isc_timer_t *timer;
    508  1.1  christos 	isc_result_t result;
    509  1.3  christos 	bool idle;
    510  1.1  christos 
    511  1.1  christos 	/*!
    512  1.1  christos 	 * The caller must be holding the manager lock.
    513  1.1  christos 	 */
    514  1.1  christos 
    515  1.1  christos 	while (manager->nscheduled > 0 && !done) {
    516  1.1  christos 		timer = isc_heap_element(manager->heap, 1);
    517  1.1  christos 		INSIST(timer != NULL && timer->type != isc_timertype_inactive);
    518  1.1  christos 		if (isc_time_compare(now, &timer->due) >= 0) {
    519  1.1  christos 			if (timer->type == isc_timertype_ticker) {
    520  1.1  christos 				type = ISC_TIMEREVENT_TICK;
    521  1.3  christos 				post_event = true;
    522  1.3  christos 				need_schedule = true;
    523  1.1  christos 			} else if (timer->type == isc_timertype_limited) {
    524  1.1  christos 				int cmp;
    525  1.1  christos 				cmp = isc_time_compare(now, &timer->expires);
    526  1.1  christos 				if (cmp >= 0) {
    527  1.1  christos 					type = ISC_TIMEREVENT_LIFE;
    528  1.3  christos 					post_event = true;
    529  1.3  christos 					need_schedule = false;
    530  1.1  christos 				} else {
    531  1.1  christos 					type = ISC_TIMEREVENT_TICK;
    532  1.3  christos 					post_event = true;
    533  1.3  christos 					need_schedule = true;
    534  1.1  christos 				}
    535  1.1  christos 			} else if (!isc_time_isepoch(&timer->expires) &&
    536  1.6  christos 				   isc_time_compare(now, &timer->expires) >= 0)
    537  1.6  christos 			{
    538  1.1  christos 				type = ISC_TIMEREVENT_LIFE;
    539  1.3  christos 				post_event = true;
    540  1.3  christos 				need_schedule = false;
    541  1.1  christos 			} else {
    542  1.3  christos 				idle = false;
    543  1.1  christos 
    544  1.1  christos 				LOCK(&timer->lock);
    545  1.1  christos 				if (!isc_time_isepoch(&timer->idle) &&
    546  1.6  christos 				    isc_time_compare(now, &timer->idle) >= 0) {
    547  1.3  christos 					idle = true;
    548  1.1  christos 				}
    549  1.1  christos 				UNLOCK(&timer->lock);
    550  1.1  christos 				if (idle) {
    551  1.1  christos 					type = ISC_TIMEREVENT_IDLE;
    552  1.3  christos 					post_event = true;
    553  1.3  christos 					need_schedule = false;
    554  1.1  christos 				} else {
    555  1.1  christos 					/*
    556  1.1  christos 					 * Idle timer has been touched;
    557  1.1  christos 					 * reschedule.
    558  1.1  christos 					 */
    559  1.4  christos 					XTRACEID("idle reschedule", timer);
    560  1.3  christos 					post_event = false;
    561  1.3  christos 					need_schedule = true;
    562  1.1  christos 				}
    563  1.1  christos 			}
    564  1.1  christos 
    565  1.1  christos 			if (post_event) {
    566  1.4  christos 				XTRACEID("posting", timer);
    567  1.1  christos 				/*
    568  1.1  christos 				 * XXX We could preallocate this event.
    569  1.1  christos 				 */
    570  1.6  christos 				event = (isc_timerevent_t *)isc_event_allocate(
    571  1.6  christos 					manager->mctx, timer, type,
    572  1.6  christos 					timer->action, timer->arg,
    573  1.6  christos 					sizeof(*event));
    574  1.1  christos 
    575  1.1  christos 				if (event != NULL) {
    576  1.1  christos 					event->due = timer->due;
    577  1.1  christos 					isc_task_send(timer->task,
    578  1.1  christos 						      ISC_EVENT_PTR(&event));
    579  1.6  christos 				} else {
    580  1.6  christos 					UNEXPECTED_ERROR(__FILE__, __LINE__,
    581  1.6  christos 							 "%s",
    582  1.6  christos 							 "couldn't allocate "
    583  1.6  christos 							 "event");
    584  1.6  christos 				}
    585  1.1  christos 			}
    586  1.1  christos 
    587  1.1  christos 			timer->index = 0;
    588  1.1  christos 			isc_heap_delete(manager->heap, 1);
    589  1.1  christos 			manager->nscheduled--;
    590  1.1  christos 
    591  1.1  christos 			if (need_schedule) {
    592  1.3  christos 				result = schedule(timer, now, false);
    593  1.6  christos 				if (result != ISC_R_SUCCESS) {
    594  1.1  christos 					UNEXPECTED_ERROR(__FILE__, __LINE__,
    595  1.1  christos 							 "%s: %u",
    596  1.6  christos 							 "couldn't schedule "
    597  1.6  christos 							 "timer",
    598  1.1  christos 							 result);
    599  1.6  christos 				}
    600  1.1  christos 			}
    601  1.1  christos 		} else {
    602  1.1  christos 			manager->due = timer->due;
    603  1.3  christos 			done = true;
    604  1.1  christos 		}
    605  1.1  christos 	}
    606  1.1  christos }
    607  1.1  christos 
    608  1.1  christos static isc_threadresult_t
    609  1.6  christos #ifdef _WIN32 /* XXXDCL */
    610  1.6  christos 	WINAPI
    611  1.6  christos #endif /* ifdef _WIN32 */
    612  1.6  christos 	run(void *uap) {
    613  1.9  christos 	isc_timermgr_t *manager = uap;
    614  1.1  christos 	isc_time_t now;
    615  1.1  christos 	isc_result_t result;
    616  1.1  christos 
    617  1.1  christos 	LOCK(&manager->lock);
    618  1.1  christos 	while (!manager->done) {
    619  1.1  christos 		TIME_NOW(&now);
    620  1.1  christos 
    621  1.4  christos 		XTRACETIME("running", now);
    622  1.1  christos 
    623  1.1  christos 		dispatch(manager, &now);
    624  1.1  christos 
    625  1.1  christos 		if (manager->nscheduled > 0) {
    626  1.4  christos 			XTRACETIME2("waituntil", manager->due, now);
    627  1.6  christos 			result = WAITUNTIL(&manager->wakeup, &manager->lock,
    628  1.6  christos 					   &manager->due);
    629  1.1  christos 			INSIST(result == ISC_R_SUCCESS ||
    630  1.1  christos 			       result == ISC_R_TIMEDOUT);
    631  1.1  christos 		} else {
    632  1.4  christos 			XTRACETIME("wait", now);
    633  1.1  christos 			WAIT(&manager->wakeup, &manager->lock);
    634  1.1  christos 		}
    635  1.4  christos 		XTRACE("wakeup");
    636  1.1  christos 	}
    637  1.1  christos 	UNLOCK(&manager->lock);
    638  1.1  christos 
    639  1.1  christos #ifdef OPENSSL_LEAKS
    640  1.1  christos 	ERR_remove_state(0);
    641  1.6  christos #endif /* ifdef OPENSSL_LEAKS */
    642  1.1  christos 
    643  1.1  christos 	return ((isc_threadresult_t)0);
    644  1.1  christos }
    645  1.1  christos 
    646  1.3  christos static bool
    647  1.1  christos sooner(void *v1, void *v2) {
    648  1.9  christos 	isc_timer_t *t1, *t2;
    649  1.1  christos 
    650  1.1  christos 	t1 = v1;
    651  1.1  christos 	t2 = v2;
    652  1.1  christos 	REQUIRE(VALID_TIMER(t1));
    653  1.1  christos 	REQUIRE(VALID_TIMER(t2));
    654  1.1  christos 
    655  1.6  christos 	if (isc_time_compare(&t1->due, &t2->due) < 0) {
    656  1.3  christos 		return (true);
    657  1.6  christos 	}
    658  1.3  christos 	return (false);
    659  1.1  christos }
    660  1.1  christos 
    661  1.1  christos static void
    662  1.1  christos set_index(void *what, unsigned int index) {
    663  1.9  christos 	isc_timer_t *timer;
    664  1.1  christos 
    665  1.6  christos 	REQUIRE(VALID_TIMER(what));
    666  1.1  christos 	timer = what;
    667  1.1  christos 
    668  1.1  christos 	timer->index = index;
    669  1.1  christos }
    670  1.1  christos 
    671  1.1  christos isc_result_t
    672  1.3  christos isc_timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
    673  1.9  christos 	isc_timermgr_t *manager;
    674  1.1  christos 	isc_result_t result;
    675  1.1  christos 
    676  1.1  christos 	/*
    677  1.1  christos 	 * Create a timer manager.
    678  1.1  christos 	 */
    679  1.1  christos 
    680  1.1  christos 	REQUIRE(managerp != NULL && *managerp == NULL);
    681  1.1  christos 
    682  1.1  christos 	manager = isc_mem_get(mctx, sizeof(*manager));
    683  1.1  christos 
    684  1.9  christos 	manager->magic = TIMER_MANAGER_MAGIC;
    685  1.1  christos 	manager->mctx = NULL;
    686  1.3  christos 	manager->done = false;
    687  1.1  christos 	INIT_LIST(manager->timers);
    688  1.1  christos 	manager->nscheduled = 0;
    689  1.1  christos 	isc_time_settoepoch(&manager->due);
    690  1.1  christos 	manager->heap = NULL;
    691  1.1  christos 	result = isc_heap_create(mctx, sooner, set_index, 0, &manager->heap);
    692  1.1  christos 	if (result != ISC_R_SUCCESS) {
    693  1.1  christos 		INSIST(result == ISC_R_NOMEMORY);
    694  1.1  christos 		isc_mem_put(mctx, manager, sizeof(*manager));
    695  1.1  christos 		return (ISC_R_NOMEMORY);
    696  1.1  christos 	}
    697  1.3  christos 	isc_mutex_init(&manager->lock);
    698  1.1  christos 	isc_mem_attach(mctx, &manager->mctx);
    699  1.3  christos 	isc_condition_init(&manager->wakeup);
    700  1.6  christos 	isc_thread_create(run, manager, &manager->thread);
    701  1.8  christos 	isc_thread_setname(manager->thread, "timer");
    702  1.1  christos 
    703  1.9  christos 	*managerp = manager;
    704  1.1  christos 
    705  1.1  christos 	return (ISC_R_SUCCESS);
    706  1.1  christos }
    707  1.1  christos 
    708  1.1  christos void
    709  1.9  christos isc_timermgr_poke(isc_timermgr_t *manager) {
    710  1.9  christos 	REQUIRE(VALID_MANAGER(manager));
    711  1.1  christos 
    712  1.1  christos 	SIGNAL(&manager->wakeup);
    713  1.1  christos }
    714  1.1  christos 
    715  1.1  christos void
    716  1.3  christos isc_timermgr_destroy(isc_timermgr_t **managerp) {
    717  1.9  christos 	isc_timermgr_t *manager;
    718  1.1  christos 
    719  1.1  christos 	/*
    720  1.1  christos 	 * Destroy a timer manager.
    721  1.1  christos 	 */
    722  1.1  christos 
    723  1.1  christos 	REQUIRE(managerp != NULL);
    724  1.9  christos 	manager = *managerp;
    725  1.1  christos 	REQUIRE(VALID_MANAGER(manager));
    726  1.1  christos 
    727  1.1  christos 	LOCK(&manager->lock);
    728  1.1  christos 
    729  1.1  christos 	REQUIRE(EMPTY(manager->timers));
    730  1.3  christos 	manager->done = true;
    731  1.1  christos 
    732  1.4  christos 	XTRACE("signal (destroy)");
    733  1.1  christos 	SIGNAL(&manager->wakeup);
    734  1.1  christos 
    735  1.1  christos 	UNLOCK(&manager->lock);
    736  1.1  christos 
    737  1.1  christos 	/*
    738  1.1  christos 	 * Wait for thread to exit.
    739  1.1  christos 	 */
    740  1.6  christos 	isc_thread_join(manager->thread, NULL);
    741  1.1  christos 
    742  1.1  christos 	/*
    743  1.1  christos 	 * Clean up.
    744  1.1  christos 	 */
    745  1.1  christos 	(void)isc_condition_destroy(&manager->wakeup);
    746  1.3  christos 	isc_mutex_destroy(&manager->lock);
    747  1.1  christos 	isc_heap_destroy(&manager->heap);
    748  1.9  christos 	manager->magic = 0;
    749  1.6  christos 	isc_mem_putanddetach(&manager->mctx, manager, sizeof(*manager));
    750  1.1  christos 
    751  1.1  christos 	*managerp = NULL;
    752  1.1  christos }
    753