work.c revision 1.3 1 1.1 christos /* $NetBSD: work.c,v 1.3 2025/05/21 14:48:05 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
18 1.3 christos #include <isc/iterated_hash.h>
19 1.1 christos #include <isc/job.h>
20 1.1 christos #include <isc/loop.h>
21 1.1 christos #include <isc/urcu.h>
22 1.1 christos #include <isc/uv.h>
23 1.1 christos #include <isc/work.h>
24 1.1 christos
25 1.1 christos #include "loop_p.h"
26 1.1 christos
27 1.1 christos static void
28 1.1 christos isc__work_cb(uv_work_t *req) {
29 1.1 christos isc_work_t *work = uv_req_get_data((uv_req_t *)req);
30 1.1 christos
31 1.3 christos isc__iterated_hash_initialize();
32 1.3 christos
33 1.1 christos rcu_register_thread();
34 1.1 christos
35 1.1 christos work->work_cb(work->cbarg);
36 1.1 christos
37 1.1 christos rcu_unregister_thread();
38 1.3 christos
39 1.3 christos isc__iterated_hash_shutdown();
40 1.1 christos }
41 1.1 christos
42 1.1 christos static void
43 1.1 christos isc__after_work_cb(uv_work_t *req, int status) {
44 1.1 christos isc_work_t *work = uv_req_get_data((uv_req_t *)req);
45 1.1 christos isc_loop_t *loop = work->loop;
46 1.1 christos
47 1.1 christos UV_RUNTIME_CHECK(uv_after_work_cb, status);
48 1.1 christos
49 1.1 christos work->after_work_cb(work->cbarg);
50 1.1 christos
51 1.1 christos isc_mem_put(loop->mctx, work, sizeof(*work));
52 1.1 christos
53 1.1 christos isc_loop_detach(&loop);
54 1.1 christos }
55 1.1 christos
56 1.1 christos void
57 1.1 christos isc_work_enqueue(isc_loop_t *loop, isc_work_cb work_cb,
58 1.1 christos isc_after_work_cb after_work_cb, void *cbarg) {
59 1.1 christos isc_work_t *work = NULL;
60 1.1 christos int r;
61 1.1 christos
62 1.1 christos REQUIRE(VALID_LOOP(loop));
63 1.1 christos REQUIRE(work_cb != NULL);
64 1.1 christos REQUIRE(after_work_cb != NULL);
65 1.1 christos
66 1.1 christos work = isc_mem_get(loop->mctx, sizeof(*work));
67 1.1 christos *work = (isc_work_t){
68 1.1 christos .work_cb = work_cb,
69 1.1 christos .after_work_cb = after_work_cb,
70 1.1 christos .cbarg = cbarg,
71 1.1 christos };
72 1.1 christos
73 1.1 christos isc_loop_attach(loop, &work->loop);
74 1.1 christos
75 1.1 christos uv_req_set_data((uv_req_t *)&work->work, work);
76 1.1 christos
77 1.1 christos r = uv_queue_work(&loop->loop, &work->work, isc__work_cb,
78 1.1 christos isc__after_work_cb);
79 1.1 christos UV_RUNTIME_CHECK(uv_queue_work, r);
80 1.1 christos }
81