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
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
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>
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
#include <isc/async.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>
|
2022-07-26 13:03:40 +02:00
|
|
|
#include <isc/job.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>
|
2022-07-26 13:03:40 +02:00
|
|
|
#include <isc/uv.h>
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
#include "loop_p.h"
|
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 {
|
|
|
|
unsigned int magic;
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_refcount_t references;
|
|
|
|
isc_loop_t *loop;
|
|
|
|
uv_timer_t timer;
|
|
|
|
isc_job_cb cb;
|
|
|
|
void *cbarg;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-16 18:46:38 +00:00
|
|
|
/*
|
2022-07-26 13:03:40 +02:00
|
|
|
* We are locking the values here for now, but this needs to go away
|
|
|
|
* when the timers are pinned to the respective loops.
|
1998-10-16 18:46:38 +00:00
|
|
|
*/
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_mutex_t lock;
|
|
|
|
uint64_t timeout;
|
|
|
|
uint64_t repeat;
|
|
|
|
};
|
2022-03-31 21:59:57 +02:00
|
|
|
|
|
|
|
static void
|
2022-07-26 13:03:40 +02:00
|
|
|
isc__timer_detach(isc_timer_t **timerp);
|
2022-03-31 21:59:57 +02:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
void
|
|
|
|
isc_timer_create(isc_loop_t *loop, isc_job_cb cb, void *cbarg,
|
|
|
|
isc_timer_t **timerp) {
|
|
|
|
int r;
|
2022-07-26 13:03:45 +02:00
|
|
|
isc_timer_t *timer;
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_loopmgr_t *loopmgr = NULL;
|
2022-03-31 21:59:57 +02:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(cb != NULL);
|
|
|
|
REQUIRE(timerp != NULL && *timerp == NULL);
|
2022-03-31 21:59:57 +02:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(VALID_LOOP(loop));
|
2020-01-30 18:19:11 +11:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
loopmgr = loop->loopmgr;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(VALID_LOOPMGR(loopmgr));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(loop == isc_loop_current(loopmgr) ||
|
|
|
|
!atomic_load(&loopmgr->running) ||
|
|
|
|
atomic_load(&loopmgr->paused) > 0);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
timer = isc_mem_get(loop->mctx, sizeof(*timer));
|
2022-03-11 12:09:35 +01:00
|
|
|
*timer = (isc_timer_t){
|
2022-07-26 13:03:40 +02:00
|
|
|
.cb = cb,
|
|
|
|
.cbarg = cbarg,
|
2022-03-11 12:09:35 +01:00
|
|
|
};
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_loop_attach(loop, &timer->loop);
|
2000-05-18 17:15:11 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_refcount_init(&timer->references, 1);
|
2022-03-11 12:09:35 +01:00
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&timer->lock);
|
2022-03-31 21:59:57 +02:00
|
|
|
|
2022-03-11 12:09:35 +01:00
|
|
|
timer->magic = TIMER_MAGIC;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
r = uv_timer_init(&timer->loop->loop, &timer->timer);
|
|
|
|
UV_RUNTIME_CHECK(uv_timer_init, r);
|
|
|
|
uv_handle_set_data(&timer->timer, timer);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-03-11 12:09:35 +01:00
|
|
|
*timerp = timer;
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
static void
|
|
|
|
isc__timer_stop(void *arg) {
|
|
|
|
isc_timer_t *timer = (isc_timer_t *)arg;
|
|
|
|
uv_timer_stop(&timer->timer);
|
|
|
|
isc__timer_detach(&timer);
|
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
void
|
|
|
|
isc_timer_stop(isc_timer_t *timer) {
|
2021-04-19 11:12:58 +02:00
|
|
|
REQUIRE(VALID_TIMER(timer));
|
2011-03-11 06:11:27 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_refcount_increment(&timer->references);
|
|
|
|
if (timer->loop == isc_loop_current(timer->loop->loopmgr)) {
|
|
|
|
isc__timer_stop(timer);
|
2000-05-18 17:15:11 +00:00
|
|
|
} else {
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_async_run(timer->loop, isc__timer_stop, timer);
|
2000-05-18 17:15:11 +00:00
|
|
|
}
|
2022-07-26 13:03:40 +02:00
|
|
|
}
|
2000-05-18 17:15:11 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
static void
|
|
|
|
timer_cb(uv_timer_t *handle) {
|
|
|
|
isc_timer_t *timer = uv_handle_get_data(handle);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(VALID_TIMER(timer));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
timer->cb(timer->cbarg);
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
static void
|
|
|
|
isc__timer_start(void *arg) {
|
|
|
|
isc_timer_t *timer = (isc_timer_t *)arg;
|
2002-09-08 18:35:55 +00:00
|
|
|
|
|
|
|
LOCK(&timer->lock);
|
2022-07-26 13:03:40 +02:00
|
|
|
int r = uv_timer_start(&timer->timer, timer_cb, timer->timeout,
|
|
|
|
timer->repeat);
|
|
|
|
UV_RUNTIME_CHECK(uv_timer_start, r);
|
2002-09-08 18:35:55 +00:00
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc__timer_detach(&timer);
|
2002-09-08 18:35:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_timer_start(isc_timer_t *timer, isc_timertype_t type,
|
|
|
|
const isc_interval_t *interval) {
|
|
|
|
isc_loopmgr_t *loopmgr = NULL;
|
|
|
|
isc_loop_t *loop = NULL;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(VALID_TIMER(timer));
|
|
|
|
REQUIRE(type == isc_timertype_ticker || type == isc_timertype_once);
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
loop = timer->loop;
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(VALID_LOOP(loop));
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
loopmgr = loop->loopmgr;
|
|
|
|
|
|
|
|
REQUIRE(VALID_LOOPMGR(loopmgr));
|
2022-04-02 00:42:20 +02:00
|
|
|
|
|
|
|
LOCK(&timer->lock);
|
2022-07-26 13:03:40 +02:00
|
|
|
switch (type) {
|
|
|
|
case isc_timertype_once:
|
|
|
|
timer->timeout = isc_interval_ms(interval);
|
|
|
|
timer->repeat = 0;
|
|
|
|
break;
|
|
|
|
case isc_timertype_ticker:
|
|
|
|
timer->timeout = timer->repeat = isc_interval_ms(interval);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2022-04-02 00:42:20 +02:00
|
|
|
UNLOCK(&timer->lock);
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_refcount_increment(&timer->references);
|
|
|
|
if (timer->loop == isc_loop_current(timer->loop->loopmgr)) {
|
|
|
|
isc__timer_start(timer);
|
|
|
|
} else {
|
|
|
|
isc_async_run(timer->loop, isc__timer_start, timer);
|
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
1998-10-16 07:44:20 +00:00
|
|
|
static void
|
2022-07-26 13:03:40 +02:00
|
|
|
timer_destroy(uv_handle_t *handle) {
|
|
|
|
isc_timer_t *timer = uv_handle_get_data(handle);
|
|
|
|
isc_loop_t *loop;
|
2022-03-31 21:59:57 +02:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(VALID_TIMER(timer));
|
2022-03-11 23:00:55 +01:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
loop = timer->loop;
|
2022-03-31 21:59:57 +02:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_refcount_destroy(&timer->references);
|
|
|
|
isc_mutex_destroy(&timer->lock);
|
2022-03-31 22:14:14 +02:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_mem_put(loop->mctx, timer, sizeof(*timer));
|
2022-03-31 21:59:57 +02:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_loop_detach(&loop);
|
2022-03-11 23:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-07-26 13:03:40 +02:00
|
|
|
isc__timer_destroy(void *arg) {
|
|
|
|
isc_timer_t *timer = (isc_timer_t *)arg;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
uv_timer_stop(&timer->timer);
|
|
|
|
uv_close(&timer->timer, timer_destroy);
|
1998-10-16 07:44:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
static void
|
|
|
|
isc__timer_detach(isc_timer_t **timerp) {
|
|
|
|
isc_timer_t *timer = NULL;
|
1998-10-16 21:53:23 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(timerp != NULL && VALID_TIMER(*timerp));
|
1998-10-16 01:18:31 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
timer = *timerp;
|
|
|
|
*timerp = NULL;
|
1998-10-16 07:44:20 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
if (isc_refcount_decrement(&timer->references) == 1) {
|
|
|
|
if (timer->loop == isc_loop_current(timer->loop->loopmgr)) {
|
|
|
|
isc__timer_destroy(timer);
|
1998-10-16 19:56:56 +00:00
|
|
|
} else {
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_async_run(timer->loop, isc__timer_destroy, timer);
|
1998-10-16 19:56:56 +00:00
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-16 01:18:31 +00:00
|
|
|
}
|
1998-10-15 22:22:50 +00:00
|
|
|
|
2013-04-10 13:49:57 -07:00
|
|
|
void
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_timer_destroy(isc_timer_t **timerp) {
|
|
|
|
isc__timer_detach(timerp);
|
2009-09-01 00:22:28 +00:00
|
|
|
}
|
2022-09-12 16:35:55 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
isc_timer_async_destroy(isc_timer_t **timerp) {
|
|
|
|
isc_timer_t *timer = NULL;
|
|
|
|
|
|
|
|
REQUIRE(timerp != NULL && VALID_TIMER(*timerp));
|
|
|
|
|
|
|
|
timer = *timerp;
|
|
|
|
*timerp = NULL;
|
|
|
|
|
|
|
|
isc_timer_stop(timer);
|
|
|
|
isc_async_run(timer->loop, isc__timer_destroy, timer);
|
|
|
|
}
|