/src/bind9/lib/isc/netmgr/timer.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | #include <isc/netmgr.h> |
15 | | #include <isc/util.h> |
16 | | #include <isc/uv.h> |
17 | | |
18 | | #include "netmgr-int.h" |
19 | | |
20 | | struct isc_nm_timer { |
21 | | isc_refcount_t references; |
22 | | uv_timer_t timer; |
23 | | isc_nmhandle_t *handle; |
24 | | isc_nm_timer_cb cb; |
25 | | void *cbarg; |
26 | | }; |
27 | | |
28 | | void |
29 | | isc_nm_timer_create(isc_nmhandle_t *handle, isc_nm_timer_cb cb, void *cbarg, |
30 | 0 | isc_nm_timer_t **timerp) { |
31 | 0 | isc__networker_t *worker = NULL; |
32 | 0 | isc_nmsocket_t *sock = NULL; |
33 | 0 | isc_nm_timer_t *timer = NULL; |
34 | 0 | int r; |
35 | |
|
36 | 0 | REQUIRE(VALID_NMHANDLE(handle)); |
37 | 0 | REQUIRE(VALID_NMSOCK(handle->sock)); |
38 | |
|
39 | 0 | sock = handle->sock; |
40 | 0 | worker = sock->worker; |
41 | | |
42 | | /* TODO: per-loop object cache */ |
43 | 0 | timer = isc_mem_get(worker->mctx, sizeof(*timer)); |
44 | 0 | *timer = (isc_nm_timer_t){ .cb = cb, .cbarg = cbarg }; |
45 | 0 | isc_refcount_init(&timer->references, 1); |
46 | 0 | isc_nmhandle_attach(handle, &timer->handle); |
47 | |
|
48 | 0 | r = uv_timer_init(&worker->loop->loop, &timer->timer); |
49 | 0 | UV_RUNTIME_CHECK(uv_timer_init, r); |
50 | 0 | uv_handle_set_data((uv_handle_t *)&timer->timer, timer); |
51 | |
|
52 | 0 | *timerp = timer; |
53 | 0 | } |
54 | | |
55 | | void |
56 | 0 | isc_nm_timer_attach(isc_nm_timer_t *timer, isc_nm_timer_t **timerp) { |
57 | 0 | REQUIRE(timer != NULL); |
58 | 0 | REQUIRE(timerp != NULL && *timerp == NULL); |
59 | |
|
60 | 0 | isc_refcount_increment(&timer->references); |
61 | 0 | *timerp = timer; |
62 | 0 | } |
63 | | |
64 | | static void |
65 | 0 | timer_destroy(uv_handle_t *uvhandle) { |
66 | 0 | isc_nm_timer_t *timer = uv_handle_get_data(uvhandle); |
67 | 0 | isc_nmhandle_t *handle = timer->handle; |
68 | 0 | isc_mem_t *mctx = timer->handle->sock->worker->mctx; |
69 | |
|
70 | 0 | isc_mem_put(mctx, timer, sizeof(*timer)); |
71 | |
|
72 | 0 | isc_nmhandle_detach(&handle); |
73 | 0 | } |
74 | | |
75 | | void |
76 | 0 | isc_nm_timer_detach(isc_nm_timer_t **timerp) { |
77 | 0 | isc_nm_timer_t *timer = NULL; |
78 | 0 | isc_nmhandle_t *handle = NULL; |
79 | |
|
80 | 0 | REQUIRE(timerp != NULL && *timerp != NULL); |
81 | |
|
82 | 0 | timer = *timerp; |
83 | 0 | *timerp = NULL; |
84 | |
|
85 | 0 | handle = timer->handle; |
86 | |
|
87 | 0 | REQUIRE(VALID_NMHANDLE(handle)); |
88 | 0 | REQUIRE(VALID_NMSOCK(handle->sock)); |
89 | |
|
90 | 0 | if (isc_refcount_decrement(&timer->references) == 1) { |
91 | 0 | int r = uv_timer_stop(&timer->timer); |
92 | 0 | UV_RUNTIME_CHECK(uv_timer_stop, r); |
93 | 0 | uv_close((uv_handle_t *)&timer->timer, timer_destroy); |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | static void |
98 | 0 | timer_cb(uv_timer_t *uvtimer) { |
99 | 0 | isc_nm_timer_t *timer = uv_handle_get_data((uv_handle_t *)uvtimer); |
100 | |
|
101 | 0 | REQUIRE(timer->cb != NULL); |
102 | |
|
103 | 0 | timer->cb(timer->cbarg, ISC_R_TIMEDOUT); |
104 | 0 | } |
105 | | |
106 | | void |
107 | 0 | isc_nm_timer_start(isc_nm_timer_t *timer, uint64_t timeout) { |
108 | 0 | int r = uv_timer_start(&timer->timer, timer_cb, timeout, 0); |
109 | 0 | UV_RUNTIME_CHECK(uv_timer_start, r); |
110 | 0 | } |
111 | | |
112 | | void |
113 | 0 | isc_nm_timer_stop(isc_nm_timer_t *timer) { |
114 | 0 | int r = uv_timer_stop(&timer->timer); |
115 | 0 | UV_RUNTIME_CHECK(uv_timer_stop, r); |
116 | 0 | } |