]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add network manager based timer API
authorOndřej Surý <ondrej@isc.org>
Wed, 2 Feb 2022 09:50:27 +0000 (10:50 +0100)
committerOndřej Surý <ondrej@isc.org>
Thu, 17 Feb 2022 20:38:17 +0000 (21:38 +0100)
This commits adds API that allows to create arbitrary timers associated
with the network manager handles.

lib/isc/Makefile.am
lib/isc/include/isc/netmgr.h
lib/isc/netmgr/timer.c [new file with mode: 0644]

index c6f800d5c683f3ddfe5ad9655921362960b9d95f..3d37127fe9691d0eb78f9c61b24d65a8ea49800e 100644 (file)
@@ -109,6 +109,7 @@ libisc_la_SOURCES =         \
        netmgr/netmgr.c         \
        netmgr/tcp.c            \
        netmgr/tcpdns.c         \
+       netmgr/timer.c          \
        netmgr/tlsdns.c         \
        netmgr/udp.c            \
        netmgr/uv-compat.c      \
index 671880137ddf6ae05fa373488ca850f985ee122d..16eedb708fbbf2e5dbf0ad1d3b95d53563eb6df7 100644 (file)
@@ -700,3 +700,27 @@ isc__nm_force_tid(int tid);
 
 void
 isc_nmhandle_setwritetimeout(isc_nmhandle_t *handle, uint64_t write_timeout);
+
+/*
+ * Timer related functions
+ */
+
+typedef struct isc_nm_timer isc_nm_timer_t;
+
+typedef void (*isc_nm_timer_cb)(void *, isc_result_t);
+
+void
+isc_nm_timer_create(isc_nmhandle_t *, isc_nm_timer_cb, void *,
+                   isc_nm_timer_t **);
+
+void
+isc_nm_timer_attach(isc_nm_timer_t *, isc_nm_timer_t **);
+
+void
+isc_nm_timer_detach(isc_nm_timer_t **);
+
+void
+isc_nm_timer_start(isc_nm_timer_t *, uint64_t);
+
+void
+isc_nm_timer_stop(isc_nm_timer_t *);
diff --git a/lib/isc/netmgr/timer.c b/lib/isc/netmgr/timer.c
new file mode 100644 (file)
index 0000000..3016387
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ *
+ * 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
+ * file, you can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * See the COPYRIGHT file distributed with this work for additional
+ * information regarding copyright ownership.
+ */
+
+#include <uv.h>
+
+#include <isc/netmgr.h>
+#include <isc/util.h>
+
+#include "netmgr-int.h"
+
+struct isc_nm_timer {
+       isc_refcount_t references;
+       uv_timer_t timer;
+       isc_nmhandle_t *handle;
+       isc_nm_timer_cb cb;
+       void *cbarg;
+};
+
+void
+isc_nm_timer_create(isc_nmhandle_t *handle, isc_nm_timer_cb cb, void *cbarg,
+                   isc_nm_timer_t **timerp) {
+       isc__networker_t *worker = NULL;
+       isc_nmsocket_t *sock = NULL;
+       isc_nm_timer_t *timer = NULL;
+       int r;
+
+       REQUIRE(isc__nm_in_netthread());
+       REQUIRE(VALID_NMHANDLE(handle));
+       REQUIRE(VALID_NMSOCK(handle->sock));
+
+       sock = handle->sock;
+       worker = &sock->mgr->workers[isc_nm_tid()];
+
+       timer = isc_mem_get(sock->mgr->mctx, sizeof(*timer));
+       *timer = (isc_nm_timer_t){ .cb = cb, .cbarg = cbarg };
+       isc_refcount_init(&timer->references, 1);
+       isc_nmhandle_attach(handle, &timer->handle);
+
+       r = uv_timer_init(&worker->loop, &timer->timer);
+       UV_RUNTIME_CHECK(uv_timer_init, r);
+
+       uv_handle_set_data((uv_handle_t *)&timer->timer, timer);
+
+       *timerp = timer;
+}
+
+void
+isc_nm_timer_attach(isc_nm_timer_t *timer, isc_nm_timer_t **timerp) {
+       REQUIRE(timer != NULL);
+       REQUIRE(timerp != NULL && *timerp == NULL);
+
+       isc_refcount_increment(&timer->references);
+       *timerp = timer;
+}
+
+static void
+timer_destroy(uv_handle_t *uvhandle) {
+       isc_nm_timer_t *timer = uv_handle_get_data(uvhandle);
+       isc_nmhandle_t *handle = timer->handle;
+       isc_mem_t *mctx = timer->handle->sock->mgr->mctx;
+
+       isc_mem_put(mctx, timer, sizeof(*timer));
+
+       isc_nmhandle_detach(&handle);
+}
+
+void
+isc_nm_timer_detach(isc_nm_timer_t **timerp) {
+       isc_nm_timer_t *timer = NULL;
+       isc_nmhandle_t *handle = NULL;
+
+       REQUIRE(timerp != NULL && *timerp != NULL);
+
+       timer = *timerp;
+       *timerp = NULL;
+
+       handle = timer->handle;
+
+       REQUIRE(isc__nm_in_netthread());
+       REQUIRE(VALID_NMHANDLE(handle));
+       REQUIRE(VALID_NMSOCK(handle->sock));
+
+       if (isc_refcount_decrement(&timer->references) == 1) {
+               uv_timer_stop(&timer->timer);
+               uv_close((uv_handle_t *)&timer->timer, timer_destroy);
+       }
+}
+
+static void
+timer_cb(uv_timer_t *uvtimer) {
+       isc_nm_timer_t *timer = uv_handle_get_data((uv_handle_t *)uvtimer);
+
+       REQUIRE(timer->cb != NULL);
+
+       timer->cb(timer->cbarg, ISC_R_TIMEDOUT);
+}
+
+void
+isc_nm_timer_start(isc_nm_timer_t *timer, uint64_t timeout) {
+       int r = uv_timer_start(&timer->timer, timer_cb, timeout, 0);
+       UV_RUNTIME_CHECK(uv_timer_start, r);
+}
+
+void
+isc_nm_timer_stop(isc_nm_timer_t *timer) {
+       int r = uv_timer_stop(&timer->timer);
+       UV_RUNTIME_CHECK(uv_timer_stop, r);
+}