1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1998-12-12 20:48:14 +00:00
|
|
|
*/
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
#include <isc/app.h>
|
2000-12-26 21:45:08 +00:00
|
|
|
#include <isc/condition.h>
|
1998-10-16 01:18:31 +00:00
|
|
|
#include <isc/heap.h>
|
2002-09-09 20:04:10 +00:00
|
|
|
#include <isc/log.h>
|
2001-06-04 19:33:39 +00:00
|
|
|
#include <isc/magic.h>
|
2000-04-28 04:26:08 +00:00
|
|
|
#include <isc/mem.h>
|
2013-04-10 13:49:57 -07:00
|
|
|
#include <isc/once.h>
|
2015-05-23 14:21:51 +02:00
|
|
|
#include <isc/print.h>
|
2019-05-16 18:53:33 +02:00
|
|
|
#include <isc/refcount.h>
|
2000-04-25 19:32:29 +00:00
|
|
|
#include <isc/task.h>
|
2000-12-26 21:45:08 +00:00
|
|
|
#include <isc/thread.h>
|
2000-04-28 18:58:40 +00:00
|
|
|
#include <isc/time.h>
|
1998-10-15 22:22:50 +00:00
|
|
|
#include <isc/timer.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2021-04-27 00:07:43 +02:00
|
|
|
#include "timer_p.h"
|
2009-10-05 17:30:49 +00:00
|
|
|
|
1998-10-21 01:57:35 +00:00
|
|
|
#ifdef ISC_TIMER_TRACE
|
2020-02-13 14:44:37 -08:00
|
|
|
#define XTRACE(s) fprintf(stderr, "%s\n", (s))
|
2020-02-12 13:59:18 +01:00
|
|
|
#define XTRACEID(s, t) fprintf(stderr, "%s %p\n", (s), (t))
|
|
|
|
#define XTRACETIME(s, d) \
|
|
|
|
fprintf(stderr, "%s %u.%09u\n", (s), (d).seconds, (d).nanoseconds)
|
|
|
|
#define XTRACETIME2(s, d, n) \
|
|
|
|
fprintf(stderr, "%s %u.%09u %u.%09u\n", (s), (d).seconds, \
|
|
|
|
(d).nanoseconds, (n).seconds, (n).nanoseconds)
|
|
|
|
#define XTRACETIMER(s, t, d) \
|
|
|
|
fprintf(stderr, "%s %p %u.%09u\n", (s), (t), (d).seconds, \
|
|
|
|
(d).nanoseconds)
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef ISC_TIMER_TRACE */
|
1998-10-16 21:53:23 +00:00
|
|
|
#define XTRACE(s)
|
|
|
|
#define XTRACEID(s, t)
|
|
|
|
#define XTRACETIME(s, d)
|
2002-09-08 18:35:55 +00:00
|
|
|
#define XTRACETIME2(s, d, n)
|
1998-10-16 21:53:23 +00:00
|
|
|
#define XTRACETIMER(s, t, d)
|
1998-10-21 01:57:35 +00:00
|
|
|
#endif /* ISC_TIMER_TRACE */
|
1998-10-16 21:53:23 +00:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define TIMER_MAGIC ISC_MAGIC('T', 'I', 'M', 'R')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_TIMER(t) ISC_MAGIC_VALID(t, TIMER_MAGIC)
|
2001-06-04 19:33:39 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
struct isc_timer {
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! Not locked. */
|
2021-04-19 11:12:58 +02:00
|
|
|
unsigned int magic;
|
|
|
|
isc_timermgr_t *manager;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mutex_t lock;
|
|
|
|
isc_refcount_t references;
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! Locked by timer lock. */
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_time_t idle;
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! Locked by manager lock. */
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_timertype_t type;
|
|
|
|
isc_time_t expires;
|
|
|
|
isc_interval_t interval;
|
|
|
|
isc_task_t *task;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_taskaction_t action;
|
2020-02-13 14:44:37 -08:00
|
|
|
void *arg;
|
|
|
|
unsigned int index;
|
|
|
|
isc_time_t due;
|
2021-04-19 11:12:58 +02:00
|
|
|
LINK(isc_timer_t) link;
|
1998-10-15 22:22:50 +00:00
|
|
|
};
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define TIMER_MANAGER_MAGIC ISC_MAGIC('T', 'I', 'M', 'M')
|
2020-02-13 14:44:37 -08:00
|
|
|
#define VALID_MANAGER(m) ISC_MAGIC_VALID(m, TIMER_MANAGER_MAGIC)
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
struct isc_timermgr {
|
1998-10-15 22:22:50 +00:00
|
|
|
/* Not locked. */
|
2021-04-19 11:12:58 +02:00
|
|
|
unsigned int magic;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx;
|
|
|
|
isc_mutex_t lock;
|
1998-10-15 22:22:50 +00:00
|
|
|
/* Locked by manager lock. */
|
2020-02-12 13:59:18 +01:00
|
|
|
bool done;
|
2021-04-19 11:12:58 +02:00
|
|
|
LIST(isc_timer_t) timers;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int nscheduled;
|
|
|
|
isc_time_t due;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_condition_t wakeup;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_thread_t thread;
|
|
|
|
isc_heap_t *heap;
|
1998-10-15 22:22:50 +00:00
|
|
|
};
|
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
void
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
isc_timermgr_poke(isc_timermgr_t *manager);
|
2009-09-01 00:22:28 +00:00
|
|
|
|
1998-10-26 23:08:23 +00:00
|
|
|
static inline isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
schedule(isc_timer_t *timer, isc_time_t *now, bool signal_ok) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timermgr_t *manager;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_t due;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1998-10-16 18:46:38 +00:00
|
|
|
* Note: the caller must ensure locking.
|
|
|
|
*/
|
|
|
|
|
1999-05-10 22:50:31 +00:00
|
|
|
REQUIRE(timer->type != isc_timertype_inactive);
|
|
|
|
|
2002-09-09 21:15:58 +00:00
|
|
|
manager = timer->manager;
|
|
|
|
|
1998-10-16 18:46:38 +00:00
|
|
|
/*
|
|
|
|
* Compute the new due time.
|
1998-10-16 01:18:31 +00:00
|
|
|
*/
|
2001-10-18 06:06:13 +00:00
|
|
|
if (timer->type != isc_timertype_once) {
|
2000-05-18 17:15:11 +00:00
|
|
|
result = isc_time_add(now, &timer->interval, &due);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2000-05-18 17:15:11 +00:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-10-18 06:06:13 +00:00
|
|
|
if (timer->type == isc_timertype_limited &&
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_compare(&timer->expires, &due) < 0)
|
|
|
|
{
|
2001-10-18 06:06:13 +00:00
|
|
|
due = timer->expires;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-05-18 17:15:11 +00:00
|
|
|
} else {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (isc_time_isepoch(&timer->idle)) {
|
1998-10-16 19:56:56 +00:00
|
|
|
due = timer->expires;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (isc_time_isepoch(&timer->expires)) {
|
1998-10-16 19:56:56 +00:00
|
|
|
due = timer->idle;
|
2020-02-13 14:44:37 -08:00
|
|
|
} else if (isc_time_compare(&timer->idle, &timer->expires) < 0)
|
|
|
|
{
|
1998-10-16 18:46:38 +00:00
|
|
|
due = timer->idle;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
1998-10-16 18:46:38 +00:00
|
|
|
due = timer->expires;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-16 18:46:38 +00:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-16 18:46:38 +00:00
|
|
|
/*
|
|
|
|
* Schedule the timer.
|
|
|
|
*/
|
2002-09-09 21:15:58 +00:00
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
if (timer->index > 0) {
|
|
|
|
/*
|
|
|
|
* Already scheduled.
|
|
|
|
*/
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
int cmp = isc_time_compare(&due, &timer->due);
|
1998-10-16 19:56:56 +00:00
|
|
|
timer->due = due;
|
|
|
|
switch (cmp) {
|
1998-10-16 01:18:31 +00:00
|
|
|
case -1:
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_increased(manager->heap, timer->index);
|
1998-10-16 01:18:31 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_decreased(manager->heap, timer->index);
|
1998-10-16 01:18:31 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
/* Nothing to do. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
1998-10-16 19:56:56 +00:00
|
|
|
timer->due = due;
|
1998-10-21 01:13:50 +00:00
|
|
|
result = isc_heap_insert(manager->heap, timer);
|
1998-10-16 01:18:31 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
INSIST(result == ISC_R_NOMEMORY);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
manager->nscheduled++;
|
|
|
|
}
|
1998-10-16 19:56:56 +00:00
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
XTRACETIMER("schedule", timer, due);
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-29 21:30:03 +00:00
|
|
|
* 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.
|
1998-10-16 01:18:31 +00:00
|
|
|
*/
|
2002-09-09 20:04:10 +00:00
|
|
|
|
1998-10-22 19:23:26 +00:00
|
|
|
if (timer->index == 1 && signal_ok) {
|
2018-11-23 21:35:01 +01:00
|
|
|
XTRACE("signal (schedule)");
|
1998-10-22 19:23:26 +00:00
|
|
|
SIGNAL(&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
|
2021-04-19 11:12:58 +02:00
|
|
|
deschedule(isc_timer_t *timer) {
|
|
|
|
isc_timermgr_t *manager;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
2000-08-01 01:33:37 +00:00
|
|
|
/*
|
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) {
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
bool need_wakeup = false;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (timer->index == 1) {
|
2018-04-17 08:29:14 -07:00
|
|
|
need_wakeup = true;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_delete(manager->heap, timer->index);
|
1998-10-16 01:18:31 +00:00
|
|
|
timer->index = 0;
|
|
|
|
INSIST(manager->nscheduled > 0);
|
|
|
|
manager->nscheduled--;
|
1998-10-16 19:56:56 +00:00
|
|
|
if (need_wakeup) {
|
2018-11-23 21:35:01 +01:00
|
|
|
XTRACE("signal (deschedule)");
|
1998-10-22 19:23:26 +00:00
|
|
|
SIGNAL(&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
|
2021-04-19 11:12:58 +02:00
|
|
|
destroy(isc_timer_t *timer) {
|
|
|
|
isc_timermgr_t *manager = timer->manager;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
1999-05-10 22:50:31 +00:00
|
|
|
* The caller must ensure it is safe to destroy the timer.
|
1998-10-15 22:22:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
(void)isc_task_purgerange(timer->task, timer, ISC_TIMEREVENT_FIRSTEVENT,
|
|
|
|
ISC_TIMEREVENT_LASTEVENT, NULL);
|
1998-10-15 22:22:50 +00:00
|
|
|
deschedule(timer);
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
UNLINK(manager->timers, timer, link);
|
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_detach(&timer->task);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&timer->lock);
|
2021-04-19 11:12:58 +02:00
|
|
|
timer->magic = 0;
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_mem_put(manager->mctx, timer, sizeof(*timer));
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_create(isc_timermgr_t *manager, isc_timertype_t type,
|
2018-08-06 13:00:55 +02:00
|
|
|
const isc_time_t *expires, const isc_interval_t *interval,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, void *arg,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_timer_t **timerp) {
|
2021-04-19 11:12:58 +02:00
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
2020-01-30 18:19:11 +11:00
|
|
|
REQUIRE(task != NULL);
|
|
|
|
REQUIRE(action != NULL);
|
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_t *timer;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
|
|
|
isc_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
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (expires == NULL) {
|
1999-06-12 01:13:22 +00:00
|
|
|
expires = isc_time_epoch;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (interval == NULL) {
|
1999-06-12 01:13:22 +00:00
|
|
|
interval = isc_interval_zero;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-06-12 01:13:22 +00:00
|
|
|
REQUIRE(type == isc_timertype_inactive ||
|
1999-05-10 22:50:31 +00:00
|
|
|
!(isc_time_isepoch(expires) && isc_interval_iszero(interval)));
|
1998-10-15 22:22:50 +00:00
|
|
|
REQUIRE(timerp != NULL && *timerp == NULL);
|
2001-10-18 06:06:13 +00:00
|
|
|
REQUIRE(type != isc_timertype_limited ||
|
|
|
|
!(isc_time_isepoch(expires) || isc_interval_iszero(interval)));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get current time.
|
|
|
|
*/
|
1999-07-03 21:00:55 +00:00
|
|
|
if (type != isc_timertype_inactive) {
|
2001-11-30 01:59:49 +00:00
|
|
|
TIME_NOW(&now);
|
1999-07-03 21:00:55 +00:00
|
|
|
} 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.
|
|
|
|
*/
|
|
|
|
isc_time_settoepoch(&now);
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
timer = isc_mem_get(manager->mctx, sizeof(*timer));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
timer->manager = manager;
|
2019-05-16 18:53:33 +02:00
|
|
|
isc_refcount_init(&timer->references, 1);
|
2000-05-18 17:15:11 +00:00
|
|
|
|
|
|
|
if (type == isc_timertype_once && !isc_interval_iszero(interval)) {
|
|
|
|
result = isc_time_add(&now, interval, &timer->idle);
|
2005-11-30 03:33:49 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
isc_mem_put(manager->mctx, timer, sizeof(*timer));
|
2000-05-18 17:15:11 +00:00
|
|
|
return (result);
|
2005-11-30 03:33:49 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
1998-10-23 22:59:44 +00:00
|
|
|
isc_time_settoepoch(&timer->idle);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-05-18 17:15:11 +00:00
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->type = type;
|
1998-10-22 01:33:20 +00:00
|
|
|
timer->expires = *expires;
|
|
|
|
timer->interval = *interval;
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->task = NULL;
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_attach(task, &timer->task);
|
1998-10-16 01:18:31 +00:00
|
|
|
timer->action = action;
|
2000-06-01 17:20:56 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
DE_CONST(arg, timer->arg);
|
1998-10-16 01:18:31 +00:00
|
|
|
timer->index = 0;
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&timer->lock);
|
2000-10-20 02:21:58 +00:00
|
|
|
ISC_LINK_INIT(timer, link);
|
2021-04-19 11:12:58 +02:00
|
|
|
timer->magic = TIMER_MAGIC;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (type != isc_timertype_inactive) {
|
2018-04-17 08:29:14 -07:00
|
|
|
result = schedule(timer, &now, true);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
1999-05-10 22:50:31 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2018-09-16 19:57:08 -07:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
2021-04-19 11:12:58 +02:00
|
|
|
*timerp = timer;
|
1998-12-13 02:03:46 +00:00
|
|
|
APPEND(manager->timers, timer, link);
|
2018-09-16 19:57:08 -07:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-15 22:22:50 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-12-13 02:03:46 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2021-04-19 11:12:58 +02:00
|
|
|
timer->magic = 0;
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&timer->lock);
|
1998-12-13 02:03:46 +00:00
|
|
|
isc_task_detach(&timer->task);
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_mem_put(manager->mctx, timer, sizeof(*timer));
|
1998-12-13 02:03:46 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_reset(isc_timer_t *timer, isc_timertype_t type,
|
2020-02-12 13:59:18 +01:00
|
|
|
const isc_time_t *expires, const isc_interval_t *interval,
|
2020-02-13 14:44:37 -08:00
|
|
|
bool purge) {
|
|
|
|
isc_time_t now;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timermgr_t *manager;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
1998-10-16 23:57:51 +00:00
|
|
|
* Change the timer's type, expires, and interval values to the given
|
2018-04-17 08:29:14 -07:00
|
|
|
* values. If 'purge' is true, any pending events from this timer
|
1998-10-16 23:57:51 +00:00
|
|
|
* are purged from its task's event queue.
|
1998-10-15 22:22:50 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
REQUIRE(VALID_TIMER(timer));
|
1998-10-15 22:22:50 +00:00
|
|
|
manager = timer->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
2011-03-11 06:11:27 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (expires == NULL) {
|
1999-06-12 01:13:22 +00:00
|
|
|
expires = isc_time_epoch;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (interval == NULL) {
|
1999-06-12 01:13:22 +00:00
|
|
|
interval = isc_interval_zero;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-06-12 01:13:22 +00:00
|
|
|
REQUIRE(type == isc_timertype_inactive ||
|
1999-05-10 22:50:31 +00:00
|
|
|
!(isc_time_isepoch(expires) && isc_interval_iszero(interval)));
|
2001-10-18 06:06:13 +00:00
|
|
|
REQUIRE(type != isc_timertype_limited ||
|
|
|
|
!(isc_time_isepoch(expires) || isc_interval_iszero(interval)));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get current time.
|
|
|
|
*/
|
1999-07-03 21:00:55 +00:00
|
|
|
if (type != isc_timertype_inactive) {
|
2001-11-30 01:59:49 +00:00
|
|
|
TIME_NOW(&now);
|
1999-07-03 21:00:55 +00:00
|
|
|
} 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.
|
|
|
|
*/
|
|
|
|
isc_time_settoepoch(&now);
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (purge) {
|
2020-02-12 13:59:18 +01:00
|
|
|
(void)isc_task_purgerange(timer->task, timer,
|
2001-11-30 01:59:49 +00:00
|
|
|
ISC_TIMEREVENT_FIRSTEVENT,
|
2020-02-12 13:59:18 +01:00
|
|
|
ISC_TIMEREVENT_LASTEVENT, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
timer->type = type;
|
1998-10-22 01:33:20 +00:00
|
|
|
timer->expires = *expires;
|
|
|
|
timer->interval = *interval;
|
2000-05-18 17:15:11 +00:00
|
|
|
if (type == isc_timertype_once && !isc_interval_iszero(interval)) {
|
|
|
|
result = isc_time_add(&now, interval, &timer->idle);
|
|
|
|
} else {
|
1998-10-23 22:59:44 +00:00
|
|
|
isc_time_settoepoch(&timer->idle);
|
1999-05-10 22:50:31 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-05-18 17:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
if (type == isc_timertype_inactive) {
|
|
|
|
deschedule(timer);
|
|
|
|
result = ISC_R_SUCCESS;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2018-04-17 08:29:14 -07:00
|
|
|
result = schedule(timer, &now, true);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-05-18 17:15:11 +00:00
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_timertype_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_gettype(isc_timer_t *timer) {
|
2002-09-08 18:35:55 +00:00
|
|
|
isc_timertype_t t;
|
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
REQUIRE(VALID_TIMER(timer));
|
2002-09-08 18:35:55 +00:00
|
|
|
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
t = timer->type;
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
|
|
|
return (t);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_touch(isc_timer_t *timer) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
|
|
|
isc_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.
|
|
|
|
*/
|
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
REQUIRE(VALID_TIMER(timer));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
|
1999-05-10 22:50:31 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2001-11-30 01:59:49 +00:00
|
|
|
TIME_NOW(&now);
|
|
|
|
result = isc_time_add(&now, &timer->interval, &timer->idle);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
2000-05-18 17:15:11 +00:00
|
|
|
return (result);
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_attach(isc_timer_t *timer, isc_timer_t **timerp) {
|
|
|
|
REQUIRE(VALID_TIMER(timer));
|
1998-10-15 22:22:50 +00:00
|
|
|
REQUIRE(timerp != NULL && *timerp == NULL);
|
2019-05-16 18:53:33 +02:00
|
|
|
isc_refcount_increment(&timer->references);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
*timerp = timer;
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_timer_detach(isc_timer_t **timerp) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_t *timer;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Detach *timerp from its timer.
|
|
|
|
*/
|
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
REQUIRE(timerp != NULL);
|
2021-04-19 11:12:58 +02:00
|
|
|
timer = *timerp;
|
1998-10-15 22:22:50 +00:00
|
|
|
REQUIRE(VALID_TIMER(timer));
|
|
|
|
|
2019-05-16 18:53:33 +02:00
|
|
|
if (isc_refcount_decrement(&timer->references) == 1) {
|
1998-10-15 22:22:50 +00:00
|
|
|
destroy(timer);
|
2019-05-16 18:53:33 +02:00
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
|
|
|
|
*timerp = NULL;
|
|
|
|
}
|
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
static void
|
2021-04-19 11:12:58 +02:00
|
|
|
dispatch(isc_timermgr_t *manager, isc_time_t *now) {
|
2020-02-13 14:44:37 -08:00
|
|
|
bool done = false, post_event, need_schedule;
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_timerevent_t *event;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_eventtype_t type = 0;
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_t *timer;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
|
|
|
bool idle;
|
1998-10-16 07:44:20 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-05-10 22:50:31 +00:00
|
|
|
* The caller must be holding the manager lock.
|
|
|
|
*/
|
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
while (manager->nscheduled > 0 && !done) {
|
1998-10-21 01:13:50 +00:00
|
|
|
timer = isc_heap_element(manager->heap, 1);
|
2012-10-23 22:04:06 -07:00
|
|
|
INSIST(timer != NULL && timer->type != isc_timertype_inactive);
|
1998-10-22 01:33:20 +00:00
|
|
|
if (isc_time_compare(now, &timer->due) >= 0) {
|
1998-10-21 01:57:35 +00:00
|
|
|
if (timer->type == isc_timertype_ticker) {
|
|
|
|
type = ISC_TIMEREVENT_TICK;
|
2018-04-17 08:29:14 -07:00
|
|
|
post_event = true;
|
|
|
|
need_schedule = true;
|
2001-10-18 06:06:13 +00:00
|
|
|
} else if (timer->type == isc_timertype_limited) {
|
|
|
|
int cmp;
|
|
|
|
cmp = isc_time_compare(now, &timer->expires);
|
|
|
|
if (cmp >= 0) {
|
|
|
|
type = ISC_TIMEREVENT_LIFE;
|
2018-04-17 08:29:14 -07:00
|
|
|
post_event = true;
|
|
|
|
need_schedule = false;
|
2001-10-18 06:06:13 +00:00
|
|
|
} else {
|
|
|
|
type = ISC_TIMEREVENT_TICK;
|
2018-04-17 08:29:14 -07:00
|
|
|
post_event = true;
|
|
|
|
need_schedule = true;
|
2001-10-18 06:06:13 +00:00
|
|
|
}
|
1998-10-23 22:59:44 +00:00
|
|
|
} else if (!isc_time_isepoch(&timer->expires) &&
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_compare(now, &timer->expires) >= 0)
|
|
|
|
{
|
1998-10-21 01:57:35 +00:00
|
|
|
type = ISC_TIMEREVENT_LIFE;
|
2018-04-17 08:29:14 -07:00
|
|
|
post_event = true;
|
|
|
|
need_schedule = false;
|
1998-10-16 07:44:20 +00:00
|
|
|
} else {
|
2018-04-17 08:29:14 -07:00
|
|
|
idle = false;
|
2007-10-24 00:57:23 +00:00
|
|
|
|
|
|
|
LOCK(&timer->lock);
|
|
|
|
if (!isc_time_isepoch(&timer->idle) &&
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_time_compare(now, &timer->idle) >= 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
idle = true;
|
2007-10-24 00:57:23 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
if (idle) {
|
|
|
|
type = ISC_TIMEREVENT_IDLE;
|
2018-04-17 08:29:14 -07:00
|
|
|
post_event = true;
|
|
|
|
need_schedule = false;
|
2007-10-24 00:57:23 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Idle timer has been touched;
|
|
|
|
* reschedule.
|
|
|
|
*/
|
2018-11-23 21:35:01 +01:00
|
|
|
XTRACEID("idle reschedule", timer);
|
2018-04-17 08:29:14 -07:00
|
|
|
post_event = false;
|
|
|
|
need_schedule = true;
|
2007-10-24 00:57:23 +00:00
|
|
|
}
|
1998-10-16 07:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (post_event) {
|
2018-11-23 21:35:01 +01:00
|
|
|
XTRACEID("posting", timer);
|
1999-01-15 02:14:55 +00:00
|
|
|
/*
|
|
|
|
* XXX We could preallocate this event.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
event = (isc_timerevent_t *)isc_event_allocate(
|
|
|
|
manager->mctx, timer, type,
|
|
|
|
timer->action, timer->arg,
|
|
|
|
sizeof(*event));
|
1998-10-16 21:41:30 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
if (event != NULL) {
|
|
|
|
event->due = timer->due;
|
|
|
|
isc_task_send(timer->task,
|
2008-08-22 05:57:53 +00:00
|
|
|
ISC_EVENT_PTR(&event));
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2020-02-12 13:59:18 +01:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"%s",
|
|
|
|
"couldn't allocate "
|
|
|
|
"event");
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-16 07:44:20 +00:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
timer->index = 0;
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_delete(manager->heap, 1);
|
1998-10-16 07:44:20 +00:00
|
|
|
manager->nscheduled--;
|
|
|
|
|
|
|
|
if (need_schedule) {
|
2018-04-17 08:29:14 -07:00
|
|
|
result = schedule(timer, now, false);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2009-01-23 01:15:41 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2009-01-23 01:27:12 +00:00
|
|
|
"%s: %u",
|
2020-02-12 13:59:18 +01:00
|
|
|
"couldn't schedule "
|
|
|
|
"timer",
|
1998-10-16 07:44:20 +00:00
|
|
|
result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-16 07:44:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
1998-10-16 18:46:38 +00:00
|
|
|
manager->due = timer->due;
|
2018-04-17 08:29:14 -07:00
|
|
|
done = true;
|
1998-10-16 07:44:20 +00:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
}
|
1998-10-16 07:44:20 +00:00
|
|
|
}
|
|
|
|
|
1998-10-23 05:45:26 +00:00
|
|
|
static isc_threadresult_t
|
2021-05-20 15:53:50 +02:00
|
|
|
run(void *uap) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timermgr_t *manager = uap;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_t now;
|
|
|
|
isc_result_t result;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
while (!manager->done) {
|
2001-11-30 01:59:49 +00:00
|
|
|
TIME_NOW(&now);
|
1998-10-16 21:53:23 +00:00
|
|
|
|
2018-11-23 21:35:01 +01: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) {
|
2018-11-23 21:35:01 +01:00
|
|
|
XTRACETIME2("waituntil", manager->due, now);
|
2020-02-12 13:59:18 +01:00
|
|
|
result = WAITUNTIL(&manager->wakeup, &manager->lock,
|
|
|
|
&manager->due);
|
1998-10-22 01:33:20 +00:00
|
|
|
INSIST(result == ISC_R_SUCCESS ||
|
|
|
|
result == ISC_R_TIMEDOUT);
|
1998-10-16 19:56:56 +00:00
|
|
|
} else {
|
2018-11-23 21:35:01 +01:00
|
|
|
XTRACETIME("wait", now);
|
1998-10-16 01:18:31 +00:00
|
|
|
WAIT(&manager->wakeup, &manager->lock);
|
1998-10-16 19:56:56 +00:00
|
|
|
}
|
2018-11-23 21:35:01 +01:00
|
|
|
XTRACE("wakeup");
|
1998-10-16 01:18:31 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-10-23 05:45:26 +00:00
|
|
|
return ((isc_threadresult_t)0);
|
1998-10-16 01:18:31 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2020-02-13 14:44:37 -08:00
|
|
|
sooner(void *v1, void *v2) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_t *t1, *t2;
|
1998-10-16 21:54:53 +00:00
|
|
|
|
|
|
|
t1 = v1;
|
|
|
|
t2 = v2;
|
|
|
|
REQUIRE(VALID_TIMER(t1));
|
|
|
|
REQUIRE(VALID_TIMER(t2));
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (isc_time_compare(&t1->due, &t2->due) < 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
1998-10-16 21:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
set_index(void *what, unsigned int index) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timer_t *timer;
|
1998-10-16 21:54:53 +00:00
|
|
|
|
2020-01-30 18:19:11 +11:00
|
|
|
REQUIRE(VALID_TIMER(what));
|
1998-10-16 21:54:53 +00:00
|
|
|
timer = what;
|
|
|
|
|
|
|
|
timer->index = index;
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
isc_result_t
|
2021-04-27 00:07:43 +02:00
|
|
|
isc__timermgr_create(isc_mem_t *mctx, isc_timermgr_t **managerp) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timermgr_t *manager;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a timer manager.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(managerp != NULL && *managerp == NULL);
|
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
manager = isc_mem_get(mctx, sizeof(*manager));
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
manager->magic = TIMER_MANAGER_MAGIC;
|
2000-04-12 01:27:20 +00:00
|
|
|
manager->mctx = NULL;
|
2018-04-17 08:29:14 -07:00
|
|
|
manager->done = false;
|
1998-10-16 01:18:31 +00:00
|
|
|
INIT_LIST(manager->timers);
|
|
|
|
manager->nscheduled = 0;
|
1998-10-23 22:59:44 +00:00
|
|
|
isc_time_settoepoch(&manager->due);
|
1998-10-16 01:18:31 +00:00
|
|
|
manager->heap = NULL;
|
1998-10-21 01:13:50 +00:00
|
|
|
result = isc_heap_create(mctx, sooner, set_index, 0, &manager->heap);
|
1998-10-16 01:18:31 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
INSIST(result == ISC_R_NOMEMORY);
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_mem_put(mctx, manager, sizeof(*manager));
|
1998-10-16 01:18:31 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&manager->lock);
|
2000-08-29 21:30:03 +00:00
|
|
|
isc_mem_attach(mctx, &manager->mctx);
|
2018-11-15 17:20:36 +01:00
|
|
|
isc_condition_init(&manager->wakeup);
|
2019-07-18 17:24:05 +02:00
|
|
|
isc_thread_create(run, manager, &manager->thread);
|
2017-04-21 13:58:22 -07:00
|
|
|
isc_thread_setname(manager->thread, "isc-timer");
|
1998-10-16 01:18:31 +00:00
|
|
|
|
2021-04-19 11:12:58 +02:00
|
|
|
*managerp = manager;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timermgr_poke(isc_timermgr_t *manager) {
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
2002-09-09 06:01:06 +00:00
|
|
|
|
2002-09-08 18:35:55 +00:00
|
|
|
SIGNAL(&manager->wakeup);
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2021-04-27 00:07:43 +02:00
|
|
|
isc__timermgr_destroy(isc_timermgr_t **managerp) {
|
2021-04-19 11:12:58 +02:00
|
|
|
isc_timermgr_t *manager;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a timer manager.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(managerp != NULL);
|
2021-04-19 11:12:58 +02:00
|
|
|
manager = *managerp;
|
1998-10-16 01:18:31 +00:00
|
|
|
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));
|
2018-04-17 08:29:14 -07:00
|
|
|
manager->done = true;
|
1998-10-16 01:18:31 +00:00
|
|
|
|
2018-11-23 21:35:01 +01:00
|
|
|
XTRACE("signal (destroy)");
|
1998-10-22 19:23:26 +00:00
|
|
|
SIGNAL(&manager->wakeup);
|
1998-10-16 01:18:31 +00:00
|
|
|
|
1998-12-16 02:04:43 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-10-16 01:18:31 +00:00
|
|
|
/*
|
|
|
|
* Wait for thread to exit.
|
|
|
|
*/
|
2019-07-18 17:47:40 +02:00
|
|
|
isc_thread_join(manager->thread, NULL);
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up.
|
|
|
|
*/
|
1998-10-22 01:33:20 +00:00
|
|
|
(void)isc_condition_destroy(&manager->wakeup);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&manager->lock);
|
1998-10-21 01:13:50 +00:00
|
|
|
isc_heap_destroy(&manager->heap);
|
2021-04-19 11:12:58 +02:00
|
|
|
manager->magic = 0;
|
2019-07-23 17:16:57 -04:00
|
|
|
isc_mem_putanddetach(&manager->mctx, manager, sizeof(*manager));
|
1998-10-16 01:18:31 +00:00
|
|
|
|
|
|
|
*managerp = NULL;
|
2009-09-01 00:22:28 +00:00
|
|
|
}
|