/src/bind9/lib/isc/async.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 <stdlib.h> |
15 | | #include <sys/types.h> |
16 | | #include <unistd.h> |
17 | | |
18 | | #include <isc/async.h> |
19 | | #include <isc/atomic.h> |
20 | | #include <isc/barrier.h> |
21 | | #include <isc/job.h> |
22 | | #include <isc/loop.h> |
23 | | #include <isc/magic.h> |
24 | | #include <isc/mem.h> |
25 | | #include <isc/mutex.h> |
26 | | #include <isc/refcount.h> |
27 | | #include <isc/result.h> |
28 | | #include <isc/signal.h> |
29 | | #include <isc/strerr.h> |
30 | | #include <isc/thread.h> |
31 | | #include <isc/util.h> |
32 | | #include <isc/uv.h> |
33 | | #include <isc/work.h> |
34 | | |
35 | | #include "async_p.h" |
36 | | #include "job_p.h" |
37 | | #include "loop_p.h" |
38 | | |
39 | | void |
40 | 0 | isc_async_run(isc_loop_t *loop, isc_job_cb cb, void *cbarg) { |
41 | 0 | REQUIRE(VALID_LOOP(loop)); |
42 | 0 | REQUIRE(cb != NULL); |
43 | |
|
44 | 0 | isc_job_t *job = isc_mem_get(loop->mctx, sizeof(*job)); |
45 | 0 | *job = (isc_job_t){ |
46 | 0 | .cb = cb, |
47 | 0 | .cbarg = cbarg, |
48 | 0 | }; |
49 | |
|
50 | 0 | cds_wfcq_node_init(&job->wfcq_node); |
51 | | |
52 | | /* |
53 | | * cds_wfcq_enqueue() is non-blocking and enqueues the job to async |
54 | | * queue. |
55 | | * |
56 | | * The function returns 'false' in case the queue was empty - in such |
57 | | * case we need to trigger the async callback. |
58 | | */ |
59 | 0 | if (!cds_wfcq_enqueue(&loop->async_jobs.head, &loop->async_jobs.tail, |
60 | 0 | &job->wfcq_node)) |
61 | 0 | { |
62 | 0 | int r = uv_async_send(&loop->async_trigger); |
63 | 0 | UV_RUNTIME_CHECK(uv_async_send, r); |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | | void |
68 | 0 | isc__async_cb(uv_async_t *handle) { |
69 | 0 | isc_loop_t *loop = uv_handle_get_data(handle); |
70 | 0 | isc_jobqueue_t jobs; |
71 | |
|
72 | 0 | REQUIRE(VALID_LOOP(loop)); |
73 | | |
74 | | /* Initialize local wfcqueue */ |
75 | 0 | __cds_wfcq_init(&jobs.head, &jobs.tail); |
76 | | |
77 | | /* |
78 | | * Move all the elements from loop->async_jobs to a local jobs queue. |
79 | | * |
80 | | * __cds_wfcq_splice_blocking() assumes that synchronization is |
81 | | * done externally - there's no internal locking, unlike |
82 | | * cds_wfcq_splice_blocking(), and we do not need to check whether |
83 | | * it needs to block, unlike __cds_wfcq_splice_nonblocking(). |
84 | | * |
85 | | * The reason we can use __cds_wfcq_splice_blocking() is that the |
86 | | * only other function we use is cds_wfcq_enqueue() which doesn't |
87 | | * require any synchronization (see the table in urcu/wfcqueue.h |
88 | | * for more details). |
89 | | */ |
90 | 0 | enum cds_wfcq_ret ret = __cds_wfcq_splice_blocking( |
91 | 0 | &jobs.head, &jobs.tail, &loop->async_jobs.head, |
92 | 0 | &loop->async_jobs.tail); |
93 | 0 | INSIST(ret != CDS_WFCQ_RET_WOULDBLOCK); |
94 | 0 | if (ret == CDS_WFCQ_RET_SRC_EMPTY) { |
95 | | /* |
96 | | * Nothing to do, the source queue was empty - most |
97 | | * probably we were called from isc__async_close() below. |
98 | | */ |
99 | 0 | return; |
100 | 0 | } |
101 | | |
102 | | /* |
103 | | * Walk through the local queue which has now all the members copied |
104 | | * locally, and call the callbacks and free all the isc_job_t(s). |
105 | | */ |
106 | 0 | struct cds_wfcq_node *node, *next; |
107 | 0 | __cds_wfcq_for_each_blocking_safe(&jobs.head, &jobs.tail, node, next) { |
108 | 0 | isc_job_t *job = caa_container_of(node, isc_job_t, wfcq_node); |
109 | |
|
110 | 0 | job->cb(job->cbarg); |
111 | |
|
112 | 0 | isc_mem_put(loop->mctx, job, sizeof(*job)); |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | void |
117 | 0 | isc__async_close(uv_handle_t *handle) { |
118 | 0 | isc_loop_t *loop = uv_handle_get_data(handle); |
119 | |
|
120 | 0 | isc__async_cb(&loop->async_trigger); |
121 | 0 | } |