async.c revision 1.2.4.2 1 1.2.4.2 perseant /* $NetBSD: async.c,v 1.2.4.2 2025/08/02 05:53:51 perseant Exp $ */
2 1.2.4.2 perseant
3 1.2.4.2 perseant /*
4 1.2.4.2 perseant * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.2.4.2 perseant *
6 1.2.4.2 perseant * SPDX-License-Identifier: MPL-2.0
7 1.2.4.2 perseant *
8 1.2.4.2 perseant * This Source Code Form is subject to the terms of the Mozilla Public
9 1.2.4.2 perseant * License, v. 2.0. If a copy of the MPL was not distributed with this
10 1.2.4.2 perseant * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 1.2.4.2 perseant *
12 1.2.4.2 perseant * See the COPYRIGHT file distributed with this work for additional
13 1.2.4.2 perseant * information regarding copyright ownership.
14 1.2.4.2 perseant */
15 1.2.4.2 perseant
16 1.2.4.2 perseant #include <stdlib.h>
17 1.2.4.2 perseant #include <sys/types.h>
18 1.2.4.2 perseant #include <unistd.h>
19 1.2.4.2 perseant
20 1.2.4.2 perseant #include <isc/async.h>
21 1.2.4.2 perseant #include <isc/atomic.h>
22 1.2.4.2 perseant #include <isc/barrier.h>
23 1.2.4.2 perseant #include <isc/condition.h>
24 1.2.4.2 perseant #include <isc/job.h>
25 1.2.4.2 perseant #include <isc/loop.h>
26 1.2.4.2 perseant #include <isc/magic.h>
27 1.2.4.2 perseant #include <isc/mem.h>
28 1.2.4.2 perseant #include <isc/mutex.h>
29 1.2.4.2 perseant #include <isc/refcount.h>
30 1.2.4.2 perseant #include <isc/result.h>
31 1.2.4.2 perseant #include <isc/signal.h>
32 1.2.4.2 perseant #include <isc/strerr.h>
33 1.2.4.2 perseant #include <isc/thread.h>
34 1.2.4.2 perseant #include <isc/util.h>
35 1.2.4.2 perseant #include <isc/uv.h>
36 1.2.4.2 perseant #include <isc/work.h>
37 1.2.4.2 perseant
38 1.2.4.2 perseant #include "async_p.h"
39 1.2.4.2 perseant #include "job_p.h"
40 1.2.4.2 perseant #include "loop_p.h"
41 1.2.4.2 perseant
42 1.2.4.2 perseant void
43 1.2.4.2 perseant isc_async_run(isc_loop_t *loop, isc_job_cb cb, void *cbarg) {
44 1.2.4.2 perseant REQUIRE(VALID_LOOP(loop));
45 1.2.4.2 perseant REQUIRE(cb != NULL);
46 1.2.4.2 perseant
47 1.2.4.2 perseant isc_job_t *job = isc_mem_get(loop->mctx, sizeof(*job));
48 1.2.4.2 perseant *job = (isc_job_t){
49 1.2.4.2 perseant .cb = cb,
50 1.2.4.2 perseant .cbarg = cbarg,
51 1.2.4.2 perseant };
52 1.2.4.2 perseant
53 1.2.4.2 perseant cds_wfcq_node_init(&job->wfcq_node);
54 1.2.4.2 perseant
55 1.2.4.2 perseant /*
56 1.2.4.2 perseant * cds_wfcq_enqueue() is non-blocking and enqueues the job to async
57 1.2.4.2 perseant * queue.
58 1.2.4.2 perseant *
59 1.2.4.2 perseant * The function returns 'false' in case the queue was empty - in such
60 1.2.4.2 perseant * case we need to trigger the async callback.
61 1.2.4.2 perseant */
62 1.2.4.2 perseant if (!cds_wfcq_enqueue(&loop->async_jobs.head, &loop->async_jobs.tail,
63 1.2.4.2 perseant &job->wfcq_node))
64 1.2.4.2 perseant {
65 1.2.4.2 perseant int r = uv_async_send(&loop->async_trigger);
66 1.2.4.2 perseant UV_RUNTIME_CHECK(uv_async_send, r);
67 1.2.4.2 perseant }
68 1.2.4.2 perseant }
69 1.2.4.2 perseant
70 1.2.4.2 perseant void
71 1.2.4.2 perseant isc__async_cb(uv_async_t *handle) {
72 1.2.4.2 perseant isc_loop_t *loop = uv_handle_get_data(handle);
73 1.2.4.2 perseant isc_jobqueue_t jobs;
74 1.2.4.2 perseant
75 1.2.4.2 perseant REQUIRE(VALID_LOOP(loop));
76 1.2.4.2 perseant
77 1.2.4.2 perseant /* Initialize local wfcqueue */
78 1.2.4.2 perseant __cds_wfcq_init(&jobs.head, &jobs.tail);
79 1.2.4.2 perseant
80 1.2.4.2 perseant /*
81 1.2.4.2 perseant * Move all the elements from loop->async_jobs to a local jobs queue.
82 1.2.4.2 perseant *
83 1.2.4.2 perseant * __cds_wfcq_splice_blocking() assumes that synchronization is
84 1.2.4.2 perseant * done externally - there's no internal locking, unlike
85 1.2.4.2 perseant * cds_wfcq_splice_blocking(), and we do not need to check whether
86 1.2.4.2 perseant * it needs to block, unlike __cds_wfcq_splice_nonblocking().
87 1.2.4.2 perseant *
88 1.2.4.2 perseant * The reason we can use __cds_wfcq_splice_blocking() is that the
89 1.2.4.2 perseant * only other function we use is cds_wfcq_enqueue() which doesn't
90 1.2.4.2 perseant * require any synchronization (see the table in urcu/wfcqueue.h
91 1.2.4.2 perseant * for more details).
92 1.2.4.2 perseant */
93 1.2.4.2 perseant enum cds_wfcq_ret ret = __cds_wfcq_splice_blocking(
94 1.2.4.2 perseant &jobs.head, &jobs.tail, &loop->async_jobs.head,
95 1.2.4.2 perseant &loop->async_jobs.tail);
96 1.2.4.2 perseant INSIST(ret != CDS_WFCQ_RET_WOULDBLOCK);
97 1.2.4.2 perseant if (ret == CDS_WFCQ_RET_SRC_EMPTY) {
98 1.2.4.2 perseant /*
99 1.2.4.2 perseant * Nothing to do, the source queue was empty - most
100 1.2.4.2 perseant * probably we were called from isc__async_close() below.
101 1.2.4.2 perseant */
102 1.2.4.2 perseant return;
103 1.2.4.2 perseant }
104 1.2.4.2 perseant
105 1.2.4.2 perseant /*
106 1.2.4.2 perseant * Walk through the local queue which has now all the members copied
107 1.2.4.2 perseant * locally, and call the callbacks and free all the isc_job_t(s).
108 1.2.4.2 perseant */
109 1.2.4.2 perseant struct cds_wfcq_node *node, *next;
110 1.2.4.2 perseant __cds_wfcq_for_each_blocking_safe(&jobs.head, &jobs.tail, node, next) {
111 1.2.4.2 perseant isc_job_t *job = caa_container_of(node, isc_job_t, wfcq_node);
112 1.2.4.2 perseant
113 1.2.4.2 perseant job->cb(job->cbarg);
114 1.2.4.2 perseant
115 1.2.4.2 perseant isc_mem_put(loop->mctx, job, sizeof(*job));
116 1.2.4.2 perseant }
117 1.2.4.2 perseant }
118 1.2.4.2 perseant
119 1.2.4.2 perseant void
120 1.2.4.2 perseant isc__async_close(uv_handle_t *handle) {
121 1.2.4.2 perseant isc_loop_t *loop = uv_handle_get_data(handle);
122 1.2.4.2 perseant
123 1.2.4.2 perseant isc__async_cb(&loop->async_trigger);
124 1.2.4.2 perseant }
125