File: async.c

package info (click to toggle)
bind9 1%3A9.20.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,220 kB
  • sloc: ansic: 316,435; sh: 53,541; python: 19,591; perl: 3,951; makefile: 2,251
file content (122 lines) | stat: -rw-r--r-- 3,285 bytes parent folder | download | duplicates (5)
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
121
122
/*
 * 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 <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#include <isc/async.h>
#include <isc/atomic.h>
#include <isc/barrier.h>
#include <isc/condition.h>
#include <isc/job.h>
#include <isc/loop.h>
#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/mutex.h>
#include <isc/refcount.h>
#include <isc/result.h>
#include <isc/signal.h>
#include <isc/strerr.h>
#include <isc/thread.h>
#include <isc/util.h>
#include <isc/uv.h>
#include <isc/work.h>

#include "async_p.h"
#include "job_p.h"
#include "loop_p.h"

void
isc_async_run(isc_loop_t *loop, isc_job_cb cb, void *cbarg) {
	REQUIRE(VALID_LOOP(loop));
	REQUIRE(cb != NULL);

	isc_job_t *job = isc_mem_get(loop->mctx, sizeof(*job));
	*job = (isc_job_t){
		.cb = cb,
		.cbarg = cbarg,
	};

	cds_wfcq_node_init(&job->wfcq_node);

	/*
	 * cds_wfcq_enqueue() is non-blocking and enqueues the job to async
	 * queue.
	 *
	 * The function returns 'false' in case the queue was empty - in such
	 * case we need to trigger the async callback.
	 */
	if (!cds_wfcq_enqueue(&loop->async_jobs.head, &loop->async_jobs.tail,
			      &job->wfcq_node))
	{
		int r = uv_async_send(&loop->async_trigger);
		UV_RUNTIME_CHECK(uv_async_send, r);
	}
}

void
isc__async_cb(uv_async_t *handle) {
	isc_loop_t *loop = uv_handle_get_data(handle);
	isc_jobqueue_t jobs;

	REQUIRE(VALID_LOOP(loop));

	/* Initialize local wfcqueue */
	__cds_wfcq_init(&jobs.head, &jobs.tail);

	/*
	 * Move all the elements from loop->async_jobs to a local jobs queue.
	 *
	 * __cds_wfcq_splice_blocking() assumes that synchronization is
	 * done externally - there's no internal locking, unlike
	 * cds_wfcq_splice_blocking(), and we do not need to check whether
	 * it needs to block, unlike __cds_wfcq_splice_nonblocking().
	 *
	 * The reason we can use __cds_wfcq_splice_blocking() is that the
	 * only other function we use is cds_wfcq_enqueue() which doesn't
	 * require any synchronization (see the table in urcu/wfcqueue.h
	 * for more details).
	 */
	enum cds_wfcq_ret ret = __cds_wfcq_splice_blocking(
		&jobs.head, &jobs.tail, &loop->async_jobs.head,
		&loop->async_jobs.tail);
	INSIST(ret != CDS_WFCQ_RET_WOULDBLOCK);
	if (ret == CDS_WFCQ_RET_SRC_EMPTY) {
		/*
		 * Nothing to do, the source queue was empty - most
		 * probably we were called from isc__async_close() below.
		 */
		return;
	}

	/*
	 * Walk through the local queue which has now all the members copied
	 * locally, and call the callbacks and free all the isc_job_t(s).
	 */
	struct cds_wfcq_node *node, *next;
	__cds_wfcq_for_each_blocking_safe(&jobs.head, &jobs.tail, node, next) {
		isc_job_t *job = caa_container_of(node, isc_job_t, wfcq_node);

		job->cb(job->cbarg);

		isc_mem_put(loop->mctx, job, sizeof(*job));
	}
}

void
isc__async_close(uv_handle_t *handle) {
	isc_loop_t *loop = uv_handle_get_data(handle);

	isc__async_cb(&loop->async_trigger);
}