loop.c revision 1.2 1 1.1 christos /* $NetBSD: loop.c,v 1.2 2025/01/26 16:25:37 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos *
6 1.1 christos * SPDX-License-Identifier: MPL-2.0
7 1.1 christos *
8 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
9 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
10 1.1 christos * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 1.1 christos *
12 1.1 christos * See the COPYRIGHT file distributed with this work for additional
13 1.1 christos * information regarding copyright ownership.
14 1.1 christos */
15 1.1 christos
16 1.1 christos #include <stdlib.h>
17 1.1 christos #include <sys/types.h>
18 1.1 christos #include <unistd.h>
19 1.1 christos
20 1.1 christos #include <isc/async.h>
21 1.1 christos #include <isc/atomic.h>
22 1.1 christos #include <isc/barrier.h>
23 1.1 christos #include <isc/condition.h>
24 1.1 christos #include <isc/job.h>
25 1.1 christos #include <isc/list.h>
26 1.1 christos #include <isc/log.h>
27 1.1 christos #include <isc/loop.h>
28 1.1 christos #include <isc/magic.h>
29 1.1 christos #include <isc/mem.h>
30 1.1 christos #include <isc/mutex.h>
31 1.1 christos #include <isc/refcount.h>
32 1.1 christos #include <isc/result.h>
33 1.1 christos #include <isc/signal.h>
34 1.1 christos #include <isc/strerr.h>
35 1.1 christos #include <isc/thread.h>
36 1.1 christos #include <isc/tid.h>
37 1.1 christos #include <isc/time.h>
38 1.1 christos #include <isc/urcu.h>
39 1.1 christos #include <isc/util.h>
40 1.1 christos #include <isc/uv.h>
41 1.1 christos #include <isc/work.h>
42 1.1 christos
43 1.1 christos #include "async_p.h"
44 1.1 christos #include "job_p.h"
45 1.1 christos #include "loop_p.h"
46 1.1 christos
47 1.1 christos /**
48 1.1 christos * Private
49 1.1 christos */
50 1.1 christos
51 1.1 christos thread_local isc_loop_t *isc__loop_local = NULL;
52 1.1 christos
53 1.1 christos static void
54 1.1 christos ignore_signal(int sig, void (*handler)(int)) {
55 1.1 christos struct sigaction sa = { .sa_handler = handler };
56 1.1 christos
57 1.1 christos if (sigfillset(&sa.sa_mask) != 0 || sigaction(sig, &sa, NULL) < 0) {
58 1.1 christos FATAL_SYSERROR(errno, "ignore_signal(%d)", sig);
59 1.1 christos }
60 1.1 christos }
61 1.1 christos
62 1.1 christos void
63 1.1 christos isc_loopmgr_shutdown(isc_loopmgr_t *loopmgr) {
64 1.1 christos if (!atomic_compare_exchange_strong(&loopmgr->shuttingdown,
65 1.1 christos &(bool){ false }, true))
66 1.1 christos {
67 1.1 christos return;
68 1.1 christos }
69 1.1 christos
70 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
71 1.1 christos isc_loop_t *loop = &loopmgr->loops[i];
72 1.1 christos int r;
73 1.1 christos
74 1.1 christos r = uv_async_send(&loop->shutdown_trigger);
75 1.1 christos UV_RUNTIME_CHECK(uv_async_send, r);
76 1.1 christos }
77 1.1 christos }
78 1.1 christos
79 1.1 christos static void
80 1.1 christos isc__loopmgr_signal(void *arg, int signum) {
81 1.1 christos isc_loopmgr_t *loopmgr = (isc_loopmgr_t *)arg;
82 1.1 christos
83 1.1 christos switch (signum) {
84 1.1 christos case SIGINT:
85 1.1 christos case SIGTERM:
86 1.1 christos isc_loopmgr_shutdown(loopmgr);
87 1.1 christos break;
88 1.1 christos default:
89 1.1 christos UNREACHABLE();
90 1.1 christos }
91 1.1 christos }
92 1.1 christos
93 1.1 christos static void
94 1.1 christos pause_loop(isc_loop_t *loop) {
95 1.1 christos isc_loopmgr_t *loopmgr = loop->loopmgr;
96 1.1 christos
97 1.1 christos rcu_thread_offline();
98 1.1 christos
99 1.1 christos loop->paused = true;
100 1.1 christos (void)isc_barrier_wait(&loopmgr->pausing);
101 1.1 christos }
102 1.1 christos
103 1.1 christos static void
104 1.1 christos resume_loop(isc_loop_t *loop) {
105 1.1 christos isc_loopmgr_t *loopmgr = loop->loopmgr;
106 1.1 christos
107 1.1 christos (void)isc_barrier_wait(&loopmgr->resuming);
108 1.1 christos loop->paused = false;
109 1.1 christos
110 1.1 christos rcu_thread_online();
111 1.1 christos }
112 1.1 christos
113 1.1 christos static void
114 1.1 christos pauseresume_cb(uv_async_t *handle) {
115 1.1 christos isc_loop_t *loop = uv_handle_get_data(handle);
116 1.1 christos
117 1.1 christos pause_loop(loop);
118 1.1 christos resume_loop(loop);
119 1.1 christos }
120 1.1 christos
121 1.1 christos #define XX(uc, lc) \
122 1.1 christos case UV_##uc: \
123 1.1 christos fprintf(stderr, "%s, %s: dangling %p: %p.type = %s\n", \
124 1.1 christos __func__, (char *)arg, handle->loop, handle, #lc); \
125 1.1 christos break;
126 1.1 christos
127 1.1 christos static void
128 1.1 christos loop_walk_cb(uv_handle_t *handle, void *arg) {
129 1.1 christos if (uv_is_closing(handle)) {
130 1.1 christos return;
131 1.1 christos }
132 1.1 christos
133 1.1 christos switch (handle->type) {
134 1.1 christos UV_HANDLE_TYPE_MAP(XX)
135 1.1 christos default:
136 1.1 christos fprintf(stderr, "%s, %s: dangling %p: %p.type = %s\n", __func__,
137 1.1 christos (char *)arg, &handle->loop, handle, "unknown");
138 1.1 christos }
139 1.1 christos }
140 1.1 christos
141 1.1 christos static void
142 1.1 christos shutdown_trigger_close_cb(uv_handle_t *handle) {
143 1.1 christos isc_loop_t *loop = uv_handle_get_data(handle);
144 1.1 christos
145 1.1 christos loop->shuttingdown = true;
146 1.1 christos
147 1.1 christos isc_loop_detach(&loop);
148 1.1 christos }
149 1.1 christos
150 1.1 christos static void
151 1.1 christos destroy_cb(uv_async_t *handle) {
152 1.1 christos isc_loop_t *loop = uv_handle_get_data(handle);
153 1.1 christos
154 1.1 christos /* Again, the first close callback here is called last */
155 1.1 christos uv_close(&loop->async_trigger, isc__async_close);
156 1.1 christos uv_close(&loop->run_trigger, isc__job_close);
157 1.1 christos uv_close(&loop->destroy_trigger, NULL);
158 1.1 christos uv_close(&loop->pause_trigger, NULL);
159 1.1 christos uv_close(&loop->quiescent, NULL);
160 1.1 christos
161 1.1 christos uv_walk(&loop->loop, loop_walk_cb, (char *)"destroy_cb");
162 1.1 christos }
163 1.1 christos
164 1.1 christos static void
165 1.1 christos shutdown_cb(uv_async_t *handle) {
166 1.1 christos isc_loop_t *loop = uv_handle_get_data(handle);
167 1.1 christos isc_loopmgr_t *loopmgr = loop->loopmgr;
168 1.1 christos
169 1.1 christos /* Make sure, we can't be called again */
170 1.1 christos uv_close(&loop->shutdown_trigger, shutdown_trigger_close_cb);
171 1.1 christos
172 1.1 christos if (DEFAULT_LOOP(loopmgr) == CURRENT_LOOP(loopmgr)) {
173 1.1 christos /* Stop the signal handlers */
174 1.1 christos isc_signal_stop(loopmgr->sigterm);
175 1.1 christos isc_signal_stop(loopmgr->sigint);
176 1.1 christos
177 1.1 christos /* Free the signal handlers */
178 1.1 christos isc_signal_destroy(&loopmgr->sigterm);
179 1.1 christos isc_signal_destroy(&loopmgr->sigint);
180 1.1 christos }
181 1.1 christos
182 1.1 christos enum cds_wfcq_ret ret = __cds_wfcq_splice_blocking(
183 1.1 christos &loop->async_jobs.head, &loop->async_jobs.tail,
184 1.1 christos &loop->teardown_jobs.head, &loop->teardown_jobs.tail);
185 1.1 christos INSIST(ret != CDS_WFCQ_RET_WOULDBLOCK);
186 1.1 christos int r = uv_async_send(&loop->async_trigger);
187 1.1 christos UV_RUNTIME_CHECK(uv_async_send, r);
188 1.1 christos }
189 1.1 christos
190 1.1 christos static void
191 1.1 christos loop_init(isc_loop_t *loop, isc_loopmgr_t *loopmgr, uint32_t tid,
192 1.1 christos const char *kind) {
193 1.1 christos *loop = (isc_loop_t){
194 1.1 christos .tid = tid,
195 1.1 christos .loopmgr = loopmgr,
196 1.1 christos .run_jobs = ISC_LIST_INITIALIZER,
197 1.1 christos };
198 1.1 christos
199 1.1 christos __cds_wfcq_init(&loop->async_jobs.head, &loop->async_jobs.tail);
200 1.1 christos __cds_wfcq_init(&loop->setup_jobs.head, &loop->setup_jobs.tail);
201 1.1 christos __cds_wfcq_init(&loop->teardown_jobs.head, &loop->teardown_jobs.tail);
202 1.1 christos
203 1.1 christos int r = uv_loop_init(&loop->loop);
204 1.1 christos UV_RUNTIME_CHECK(uv_loop_init, r);
205 1.1 christos
206 1.1 christos r = uv_async_init(&loop->loop, &loop->pause_trigger, pauseresume_cb);
207 1.1 christos UV_RUNTIME_CHECK(uv_async_init, r);
208 1.1 christos uv_handle_set_data(&loop->pause_trigger, loop);
209 1.1 christos
210 1.1 christos r = uv_async_init(&loop->loop, &loop->shutdown_trigger, shutdown_cb);
211 1.1 christos UV_RUNTIME_CHECK(uv_async_init, r);
212 1.1 christos uv_handle_set_data(&loop->shutdown_trigger, loop);
213 1.1 christos
214 1.1 christos r = uv_async_init(&loop->loop, &loop->async_trigger, isc__async_cb);
215 1.1 christos UV_RUNTIME_CHECK(uv_async_init, r);
216 1.1 christos uv_handle_set_data(&loop->async_trigger, loop);
217 1.1 christos
218 1.1 christos r = uv_idle_init(&loop->loop, &loop->run_trigger);
219 1.1 christos UV_RUNTIME_CHECK(uv_idle_init, r);
220 1.1 christos uv_handle_set_data(&loop->run_trigger, loop);
221 1.1 christos
222 1.1 christos r = uv_async_init(&loop->loop, &loop->destroy_trigger, destroy_cb);
223 1.1 christos UV_RUNTIME_CHECK(uv_async_init, r);
224 1.1 christos uv_handle_set_data(&loop->destroy_trigger, loop);
225 1.1 christos
226 1.1 christos r = uv_prepare_init(&loop->loop, &loop->quiescent);
227 1.1 christos UV_RUNTIME_CHECK(uv_prepare_init, r);
228 1.1 christos uv_handle_set_data(&loop->quiescent, loop);
229 1.1 christos
230 1.1 christos char name[16];
231 1.1 christos snprintf(name, sizeof(name), "%s-%08" PRIx32, kind, tid);
232 1.1 christos isc_mem_create(&loop->mctx);
233 1.1 christos isc_mem_setname(loop->mctx, name);
234 1.1 christos
235 1.1 christos isc_refcount_init(&loop->references, 1);
236 1.1 christos
237 1.1 christos loop->magic = LOOP_MAGIC;
238 1.1 christos }
239 1.1 christos
240 1.1 christos static void
241 1.1 christos quiescent_cb(uv_prepare_t *handle) {
242 1.1 christos UNUSED(handle);
243 1.1 christos
244 1.1 christos #if defined(RCU_QSBR)
245 1.1 christos /* safe memory reclamation */
246 1.1 christos rcu_quiescent_state();
247 1.1 christos
248 1.1 christos /* mark the thread offline when polling */
249 1.1 christos rcu_thread_offline();
250 1.1 christos #else
251 1.1 christos INSIST(!rcu_read_ongoing());
252 1.1 christos #endif
253 1.1 christos }
254 1.1 christos
255 1.1 christos static void
256 1.1 christos helper_close(isc_loop_t *loop) {
257 1.1 christos int r = uv_loop_close(&loop->loop);
258 1.1 christos UV_RUNTIME_CHECK(uv_loop_close, r);
259 1.1 christos
260 1.1 christos INSIST(cds_wfcq_empty(&loop->async_jobs.head, &loop->async_jobs.tail));
261 1.1 christos
262 1.1 christos isc_mem_detach(&loop->mctx);
263 1.1 christos }
264 1.1 christos
265 1.1 christos static void
266 1.1 christos loop_close(isc_loop_t *loop) {
267 1.1 christos int r = uv_loop_close(&loop->loop);
268 1.1 christos UV_RUNTIME_CHECK(uv_loop_close, r);
269 1.1 christos
270 1.1 christos INSIST(cds_wfcq_empty(&loop->async_jobs.head, &loop->async_jobs.tail));
271 1.1 christos INSIST(ISC_LIST_EMPTY(loop->run_jobs));
272 1.1 christos
273 1.1 christos loop->magic = 0;
274 1.1 christos
275 1.1 christos isc_mem_detach(&loop->mctx);
276 1.1 christos }
277 1.1 christos
278 1.1 christos static void *
279 1.1 christos helper_thread(void *arg) {
280 1.1 christos isc_loop_t *helper = (isc_loop_t *)arg;
281 1.1 christos
282 1.1 christos int r = uv_prepare_start(&helper->quiescent, quiescent_cb);
283 1.1 christos UV_RUNTIME_CHECK(uv_prepare_start, r);
284 1.1 christos
285 1.1 christos isc_barrier_wait(&helper->loopmgr->starting);
286 1.1 christos
287 1.1 christos r = uv_run(&helper->loop, UV_RUN_DEFAULT);
288 1.1 christos UV_RUNTIME_CHECK(uv_run, r);
289 1.1 christos
290 1.1 christos /* Invalidate the helper early */
291 1.1 christos helper->magic = 0;
292 1.1 christos
293 1.1 christos isc_barrier_wait(&helper->loopmgr->stopping);
294 1.1 christos
295 1.1 christos return NULL;
296 1.1 christos }
297 1.1 christos
298 1.1 christos static void *
299 1.1 christos loop_thread(void *arg) {
300 1.1 christos isc_loop_t *loop = (isc_loop_t *)arg;
301 1.1 christos isc_loopmgr_t *loopmgr = loop->loopmgr;
302 1.1 christos isc_loop_t *helper = &loopmgr->helpers[loop->tid];
303 1.1 christos char name[32];
304 1.1 christos /* Initialize the thread_local variables*/
305 1.1 christos
306 1.1 christos REQUIRE(isc__loop_local == NULL || isc__loop_local == loop);
307 1.1 christos isc__loop_local = loop;
308 1.1 christos
309 1.1 christos isc__tid_init(loop->tid);
310 1.1 christos
311 1.1 christos /* Start the helper thread */
312 1.1 christos isc_thread_create(helper_thread, helper, &helper->thread);
313 1.1 christos snprintf(name, sizeof(name), "isc-helper-%04" PRIu32, loop->tid);
314 1.1 christos isc_thread_setname(helper->thread, name);
315 1.1 christos
316 1.1 christos int r = uv_prepare_start(&loop->quiescent, quiescent_cb);
317 1.1 christos UV_RUNTIME_CHECK(uv_prepare_start, r);
318 1.1 christos
319 1.1 christos isc_barrier_wait(&loopmgr->starting);
320 1.1 christos
321 1.1 christos enum cds_wfcq_ret ret = __cds_wfcq_splice_blocking(
322 1.1 christos &loop->async_jobs.head, &loop->async_jobs.tail,
323 1.1 christos &loop->setup_jobs.head, &loop->setup_jobs.tail);
324 1.1 christos INSIST(ret != CDS_WFCQ_RET_WOULDBLOCK);
325 1.1 christos
326 1.1 christos r = uv_async_send(&loop->async_trigger);
327 1.1 christos UV_RUNTIME_CHECK(uv_async_send, r);
328 1.1 christos
329 1.1 christos r = uv_run(&loop->loop, UV_RUN_DEFAULT);
330 1.1 christos UV_RUNTIME_CHECK(uv_run, r);
331 1.1 christos
332 1.1 christos isc__loop_local = NULL;
333 1.1 christos
334 1.1 christos /* Invalidate the loop early */
335 1.1 christos loop->magic = 0;
336 1.1 christos
337 1.1 christos /* Shutdown the helper thread */
338 1.1 christos r = uv_async_send(&helper->shutdown_trigger);
339 1.1 christos UV_RUNTIME_CHECK(uv_async_send, r);
340 1.1 christos
341 1.1 christos isc_barrier_wait(&loopmgr->stopping);
342 1.1 christos
343 1.1 christos return NULL;
344 1.1 christos }
345 1.1 christos
346 1.1 christos /**
347 1.1 christos * Public
348 1.1 christos */
349 1.1 christos
350 1.1 christos static void
351 1.1 christos threadpool_initialize(uint32_t workers) {
352 1.1 christos char buf[11];
353 1.1 christos int r = uv_os_getenv("UV_THREADPOOL_SIZE", buf,
354 1.1 christos &(size_t){ sizeof(buf) });
355 1.1 christos if (r == UV_ENOENT) {
356 1.1 christos snprintf(buf, sizeof(buf), "%" PRIu32, workers);
357 1.1 christos uv_os_setenv("UV_THREADPOOL_SIZE", buf);
358 1.1 christos }
359 1.1 christos }
360 1.1 christos
361 1.1 christos static void
362 1.1 christos loop_destroy(isc_loop_t *loop) {
363 1.1 christos int r = uv_async_send(&loop->destroy_trigger);
364 1.1 christos UV_RUNTIME_CHECK(uv_async_send, r);
365 1.1 christos }
366 1.1 christos
367 1.1 christos #if ISC_LOOP_TRACE
368 1.1 christos ISC_REFCOUNT_TRACE_IMPL(isc_loop, loop_destroy)
369 1.1 christos #else
370 1.1 christos ISC_REFCOUNT_IMPL(isc_loop, loop_destroy);
371 1.1 christos #endif
372 1.1 christos
373 1.1 christos void
374 1.1 christos isc_loopmgr_create(isc_mem_t *mctx, uint32_t nloops, isc_loopmgr_t **loopmgrp) {
375 1.1 christos isc_loopmgr_t *loopmgr = NULL;
376 1.1 christos
377 1.1 christos REQUIRE(loopmgrp != NULL && *loopmgrp == NULL);
378 1.1 christos REQUIRE(nloops > 0);
379 1.1 christos
380 1.1 christos threadpool_initialize(nloops);
381 1.1 christos isc__tid_initcount(nloops);
382 1.1 christos
383 1.1 christos loopmgr = isc_mem_get(mctx, sizeof(*loopmgr));
384 1.1 christos *loopmgr = (isc_loopmgr_t){
385 1.1 christos .nloops = nloops,
386 1.1 christos };
387 1.1 christos
388 1.1 christos isc_mem_attach(mctx, &loopmgr->mctx);
389 1.1 christos
390 1.1 christos /* We need to double the number for loops and helpers */
391 1.1 christos isc_barrier_init(&loopmgr->pausing, loopmgr->nloops * 2);
392 1.1 christos isc_barrier_init(&loopmgr->resuming, loopmgr->nloops * 2);
393 1.1 christos isc_barrier_init(&loopmgr->starting, loopmgr->nloops * 2);
394 1.1 christos isc_barrier_init(&loopmgr->stopping, loopmgr->nloops * 2);
395 1.1 christos
396 1.1 christos loopmgr->loops = isc_mem_cget(loopmgr->mctx, loopmgr->nloops,
397 1.1 christos sizeof(loopmgr->loops[0]));
398 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
399 1.1 christos isc_loop_t *loop = &loopmgr->loops[i];
400 1.1 christos loop_init(loop, loopmgr, i, "loop");
401 1.1 christos }
402 1.1 christos
403 1.1 christos loopmgr->helpers = isc_mem_cget(loopmgr->mctx, loopmgr->nloops,
404 1.1 christos sizeof(loopmgr->helpers[0]));
405 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
406 1.1 christos isc_loop_t *loop = &loopmgr->helpers[i];
407 1.1 christos loop_init(loop, loopmgr, i, "helper");
408 1.1 christos }
409 1.1 christos
410 1.1 christos loopmgr->sigint = isc_signal_new(loopmgr, isc__loopmgr_signal, loopmgr,
411 1.1 christos SIGINT);
412 1.1 christos loopmgr->sigterm = isc_signal_new(loopmgr, isc__loopmgr_signal, loopmgr,
413 1.1 christos SIGTERM);
414 1.1 christos
415 1.1 christos isc_signal_start(loopmgr->sigint);
416 1.1 christos isc_signal_start(loopmgr->sigterm);
417 1.1 christos
418 1.1 christos loopmgr->magic = LOOPMGR_MAGIC;
419 1.1 christos
420 1.1 christos *loopmgrp = loopmgr;
421 1.1 christos }
422 1.1 christos
423 1.1 christos isc_job_t *
424 1.1 christos isc_loop_setup(isc_loop_t *loop, isc_job_cb cb, void *cbarg) {
425 1.1 christos REQUIRE(VALID_LOOP(loop));
426 1.1 christos REQUIRE(cb != NULL);
427 1.1 christos
428 1.1 christos isc_loopmgr_t *loopmgr = loop->loopmgr;
429 1.1 christos isc_job_t *job = isc_mem_get(loop->mctx, sizeof(*job));
430 1.1 christos *job = (isc_job_t){
431 1.1 christos .cb = cb,
432 1.1 christos .cbarg = cbarg,
433 1.1 christos };
434 1.1 christos
435 1.1 christos cds_wfcq_node_init(&job->wfcq_node);
436 1.1 christos
437 1.1 christos REQUIRE(loop->tid == isc_tid() || !atomic_load(&loopmgr->running) ||
438 1.1 christos atomic_load(&loopmgr->paused));
439 1.1 christos
440 1.1 christos cds_wfcq_enqueue(&loop->setup_jobs.head, &loop->setup_jobs.tail,
441 1.1 christos &job->wfcq_node);
442 1.1 christos
443 1.1 christos return job;
444 1.1 christos }
445 1.1 christos
446 1.1 christos isc_job_t *
447 1.1 christos isc_loop_teardown(isc_loop_t *loop, isc_job_cb cb, void *cbarg) {
448 1.1 christos REQUIRE(VALID_LOOP(loop));
449 1.1 christos
450 1.1 christos isc_loopmgr_t *loopmgr = loop->loopmgr;
451 1.1 christos isc_job_t *job = isc_mem_get(loop->mctx, sizeof(*job));
452 1.1 christos *job = (isc_job_t){
453 1.1 christos .cb = cb,
454 1.1 christos .cbarg = cbarg,
455 1.1 christos };
456 1.1 christos cds_wfcq_node_init(&job->wfcq_node);
457 1.1 christos
458 1.1 christos REQUIRE(loop->tid == isc_tid() || !atomic_load(&loopmgr->running) ||
459 1.1 christos atomic_load(&loopmgr->paused));
460 1.1 christos
461 1.1 christos cds_wfcq_enqueue(&loop->teardown_jobs.head, &loop->teardown_jobs.tail,
462 1.1 christos &job->wfcq_node);
463 1.1 christos
464 1.1 christos return job;
465 1.1 christos }
466 1.1 christos
467 1.1 christos void
468 1.1 christos isc_loopmgr_setup(isc_loopmgr_t *loopmgr, isc_job_cb cb, void *cbarg) {
469 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
470 1.1 christos REQUIRE(!atomic_load(&loopmgr->running) ||
471 1.1 christos atomic_load(&loopmgr->paused));
472 1.1 christos
473 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
474 1.1 christos isc_loop_t *loop = &loopmgr->loops[i];
475 1.1 christos (void)isc_loop_setup(loop, cb, cbarg);
476 1.1 christos }
477 1.1 christos }
478 1.1 christos
479 1.1 christos void
480 1.1 christos isc_loopmgr_teardown(isc_loopmgr_t *loopmgr, isc_job_cb cb, void *cbarg) {
481 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
482 1.1 christos REQUIRE(!atomic_load(&loopmgr->running) ||
483 1.1 christos atomic_load(&loopmgr->paused));
484 1.1 christos
485 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
486 1.1 christos isc_loop_t *loop = &loopmgr->loops[i];
487 1.1 christos (void)isc_loop_teardown(loop, cb, cbarg);
488 1.1 christos }
489 1.1 christos }
490 1.1 christos
491 1.1 christos void
492 1.1 christos isc_loopmgr_run(isc_loopmgr_t *loopmgr) {
493 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
494 1.1 christos RUNTIME_CHECK(atomic_compare_exchange_strong(&loopmgr->running,
495 1.1 christos &(bool){ false }, true));
496 1.1 christos
497 1.1 christos /*
498 1.1 christos * Always ignore SIGPIPE.
499 1.1 christos */
500 1.1 christos ignore_signal(SIGPIPE, SIG_IGN);
501 1.1 christos
502 1.1 christos /*
503 1.1 christos * The thread 0 is this one.
504 1.1 christos */
505 1.1 christos for (size_t i = 1; i < loopmgr->nloops; i++) {
506 1.1 christos char name[32];
507 1.1 christos isc_loop_t *loop = &loopmgr->loops[i];
508 1.1 christos
509 1.1 christos isc_thread_create(loop_thread, loop, &loop->thread);
510 1.1 christos
511 1.1 christos snprintf(name, sizeof(name), "isc-loop-%04zu", i);
512 1.1 christos isc_thread_setname(loop->thread, name);
513 1.1 christos }
514 1.1 christos
515 1.1 christos isc_thread_main(loop_thread, &loopmgr->loops[0]);
516 1.1 christos }
517 1.1 christos
518 1.1 christos void
519 1.1 christos isc_loopmgr_pause(isc_loopmgr_t *loopmgr) {
520 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
521 1.1 christos REQUIRE(isc_tid() != ISC_TID_UNKNOWN);
522 1.1 christos
523 1.1 christos if (isc_log_wouldlog(isc_lctx, ISC_LOG_DEBUG(1))) {
524 1.1 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
525 1.1 christos ISC_LOGMODULE_OTHER, ISC_LOG_DEBUG(1),
526 1.1 christos "loop exclusive mode: starting");
527 1.1 christos }
528 1.1 christos
529 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
530 1.1 christos isc_loop_t *helper = &loopmgr->helpers[i];
531 1.1 christos
532 1.1 christos int r = uv_async_send(&helper->pause_trigger);
533 1.1 christos UV_RUNTIME_CHECK(uv_async_send, r);
534 1.1 christos }
535 1.1 christos
536 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
537 1.1 christos isc_loop_t *loop = &loopmgr->loops[i];
538 1.1 christos
539 1.1 christos /* Skip current loop */
540 1.1 christos if (i == isc_tid()) {
541 1.1 christos continue;
542 1.1 christos }
543 1.1 christos
544 1.1 christos int r = uv_async_send(&loop->pause_trigger);
545 1.1 christos UV_RUNTIME_CHECK(uv_async_send, r);
546 1.1 christos }
547 1.1 christos
548 1.1 christos RUNTIME_CHECK(atomic_compare_exchange_strong(&loopmgr->paused,
549 1.1 christos &(bool){ false }, true));
550 1.1 christos pause_loop(CURRENT_LOOP(loopmgr));
551 1.1 christos
552 1.1 christos if (isc_log_wouldlog(isc_lctx, ISC_LOG_DEBUG(1))) {
553 1.1 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
554 1.1 christos ISC_LOGMODULE_OTHER, ISC_LOG_DEBUG(1),
555 1.1 christos "loop exclusive mode: started");
556 1.1 christos }
557 1.1 christos }
558 1.1 christos
559 1.1 christos void
560 1.1 christos isc_loopmgr_resume(isc_loopmgr_t *loopmgr) {
561 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
562 1.1 christos
563 1.1 christos if (isc_log_wouldlog(isc_lctx, ISC_LOG_DEBUG(1))) {
564 1.1 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
565 1.1 christos ISC_LOGMODULE_OTHER, ISC_LOG_DEBUG(1),
566 1.1 christos "loop exclusive mode: ending");
567 1.1 christos }
568 1.1 christos
569 1.1 christos RUNTIME_CHECK(atomic_compare_exchange_strong(&loopmgr->paused,
570 1.1 christos &(bool){ true }, false));
571 1.1 christos resume_loop(CURRENT_LOOP(loopmgr));
572 1.1 christos
573 1.1 christos if (isc_log_wouldlog(isc_lctx, ISC_LOG_DEBUG(1))) {
574 1.1 christos isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
575 1.1 christos ISC_LOGMODULE_OTHER, ISC_LOG_DEBUG(1),
576 1.1 christos "loop exclusive mode: ended");
577 1.1 christos }
578 1.1 christos }
579 1.1 christos
580 1.1 christos void
581 1.1 christos isc_loopmgr_destroy(isc_loopmgr_t **loopmgrp) {
582 1.1 christos isc_loopmgr_t *loopmgr = NULL;
583 1.1 christos
584 1.1 christos REQUIRE(loopmgrp != NULL);
585 1.1 christos REQUIRE(VALID_LOOPMGR(*loopmgrp));
586 1.1 christos
587 1.1 christos loopmgr = *loopmgrp;
588 1.1 christos *loopmgrp = NULL;
589 1.1 christos
590 1.1 christos RUNTIME_CHECK(atomic_compare_exchange_strong(&loopmgr->running,
591 1.1 christos &(bool){ true }, false));
592 1.1 christos
593 1.1 christos /* Wait for all helpers to finish */
594 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
595 1.1 christos isc_loop_t *helper = &loopmgr->helpers[i];
596 1.1 christos isc_thread_join(helper->thread, NULL);
597 1.1 christos }
598 1.1 christos
599 1.1 christos /* First wait for all loops to finish */
600 1.1 christos for (size_t i = 1; i < loopmgr->nloops; i++) {
601 1.1 christos isc_loop_t *loop = &loopmgr->loops[i];
602 1.1 christos isc_thread_join(loop->thread, NULL);
603 1.1 christos }
604 1.1 christos
605 1.1 christos loopmgr->magic = 0;
606 1.1 christos
607 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
608 1.1 christos isc_loop_t *helper = &loopmgr->helpers[i];
609 1.1 christos helper_close(helper);
610 1.1 christos }
611 1.1 christos isc_mem_cput(loopmgr->mctx, loopmgr->helpers, loopmgr->nloops,
612 1.1 christos sizeof(loopmgr->helpers[0]));
613 1.1 christos
614 1.1 christos for (size_t i = 0; i < loopmgr->nloops; i++) {
615 1.1 christos isc_loop_t *loop = &loopmgr->loops[i];
616 1.1 christos loop_close(loop);
617 1.1 christos }
618 1.1 christos isc_mem_cput(loopmgr->mctx, loopmgr->loops, loopmgr->nloops,
619 1.1 christos sizeof(loopmgr->loops[0]));
620 1.1 christos
621 1.1 christos isc_barrier_destroy(&loopmgr->starting);
622 1.1 christos isc_barrier_destroy(&loopmgr->stopping);
623 1.1 christos isc_barrier_destroy(&loopmgr->resuming);
624 1.1 christos isc_barrier_destroy(&loopmgr->pausing);
625 1.1 christos
626 1.1 christos isc_mem_putanddetach(&loopmgr->mctx, loopmgr, sizeof(*loopmgr));
627 1.1 christos }
628 1.1 christos
629 1.1 christos uint32_t
630 1.1 christos isc_loopmgr_nloops(isc_loopmgr_t *loopmgr) {
631 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
632 1.1 christos
633 1.1 christos return loopmgr->nloops;
634 1.1 christos }
635 1.1 christos
636 1.1 christos isc_mem_t *
637 1.1 christos isc_loop_getmctx(isc_loop_t *loop) {
638 1.1 christos REQUIRE(VALID_LOOP(loop));
639 1.1 christos
640 1.1 christos return loop->mctx;
641 1.1 christos }
642 1.1 christos
643 1.1 christos isc_loop_t *
644 1.1 christos isc_loop_main(isc_loopmgr_t *loopmgr) {
645 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
646 1.1 christos
647 1.1 christos return DEFAULT_LOOP(loopmgr);
648 1.1 christos }
649 1.1 christos
650 1.1 christos isc_loop_t *
651 1.1 christos isc_loop_get(isc_loopmgr_t *loopmgr, uint32_t tid) {
652 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
653 1.1 christos REQUIRE(tid < loopmgr->nloops);
654 1.1 christos
655 1.1 christos return LOOP(loopmgr, tid);
656 1.1 christos }
657 1.1 christos
658 1.1 christos void
659 1.1 christos isc_loopmgr_blocking(isc_loopmgr_t *loopmgr) {
660 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
661 1.1 christos
662 1.1 christos isc_signal_stop(loopmgr->sigterm);
663 1.1 christos isc_signal_stop(loopmgr->sigint);
664 1.1 christos }
665 1.1 christos
666 1.1 christos void
667 1.1 christos isc_loopmgr_nonblocking(isc_loopmgr_t *loopmgr) {
668 1.1 christos REQUIRE(VALID_LOOPMGR(loopmgr));
669 1.1 christos
670 1.1 christos isc_signal_start(loopmgr->sigint);
671 1.1 christos isc_signal_start(loopmgr->sigterm);
672 1.1 christos }
673 1.1 christos
674 1.1 christos isc_loopmgr_t *
675 1.1 christos isc_loop_getloopmgr(isc_loop_t *loop) {
676 1.1 christos REQUIRE(VALID_LOOP(loop));
677 1.1 christos
678 1.1 christos return loop->loopmgr;
679 1.1 christos }
680 1.1 christos
681 1.1 christos isc_time_t
682 1.1 christos isc_loop_now(isc_loop_t *loop) {
683 1.1 christos REQUIRE(VALID_LOOP(loop));
684 1.1 christos
685 1.1 christos uint64_t msec = uv_now(&loop->loop);
686 1.1 christos isc_time_t t = {
687 1.1 christos .seconds = msec / MS_PER_SEC,
688 1.1 christos .nanoseconds = (msec % MS_PER_SEC) * NS_PER_MS,
689 1.1 christos };
690 1.1 christos
691 1.1 christos return t;
692 1.1 christos }
693 1.1 christos
694 1.1 christos bool
695 1.1 christos isc_loop_shuttingdown(isc_loop_t *loop) {
696 1.1 christos REQUIRE(VALID_LOOP(loop));
697 1.1 christos REQUIRE(loop->tid == isc_tid());
698 1.1 christos
699 1.1 christos return loop->shuttingdown;
700 1.1 christos }
701