1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* 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()];
/* TODO: per-loop object cache */
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) {
int r = uv_timer_stop(&timer->timer);
UV_RUNTIME_CHECK(uv_timer_stop, r);
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);
}
|