Home | History | Annotate | Line # | Download | only in isc
work.c revision 1.3
      1 /*	$NetBSD: work.c,v 1.3 2025/05/21 14:48:05 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 #include <stdlib.h>
     17 
     18 #include <isc/iterated_hash.h>
     19 #include <isc/job.h>
     20 #include <isc/loop.h>
     21 #include <isc/urcu.h>
     22 #include <isc/uv.h>
     23 #include <isc/work.h>
     24 
     25 #include "loop_p.h"
     26 
     27 static void
     28 isc__work_cb(uv_work_t *req) {
     29 	isc_work_t *work = uv_req_get_data((uv_req_t *)req);
     30 
     31 	isc__iterated_hash_initialize();
     32 
     33 	rcu_register_thread();
     34 
     35 	work->work_cb(work->cbarg);
     36 
     37 	rcu_unregister_thread();
     38 
     39 	isc__iterated_hash_shutdown();
     40 }
     41 
     42 static void
     43 isc__after_work_cb(uv_work_t *req, int status) {
     44 	isc_work_t *work = uv_req_get_data((uv_req_t *)req);
     45 	isc_loop_t *loop = work->loop;
     46 
     47 	UV_RUNTIME_CHECK(uv_after_work_cb, status);
     48 
     49 	work->after_work_cb(work->cbarg);
     50 
     51 	isc_mem_put(loop->mctx, work, sizeof(*work));
     52 
     53 	isc_loop_detach(&loop);
     54 }
     55 
     56 void
     57 isc_work_enqueue(isc_loop_t *loop, isc_work_cb work_cb,
     58 		 isc_after_work_cb after_work_cb, void *cbarg) {
     59 	isc_work_t *work = NULL;
     60 	int r;
     61 
     62 	REQUIRE(VALID_LOOP(loop));
     63 	REQUIRE(work_cb != NULL);
     64 	REQUIRE(after_work_cb != NULL);
     65 
     66 	work = isc_mem_get(loop->mctx, sizeof(*work));
     67 	*work = (isc_work_t){
     68 		.work_cb = work_cb,
     69 		.after_work_cb = after_work_cb,
     70 		.cbarg = cbarg,
     71 	};
     72 
     73 	isc_loop_attach(loop, &work->loop);
     74 
     75 	uv_req_set_data((uv_req_t *)&work->work, work);
     76 
     77 	r = uv_queue_work(&loop->loop, &work->work, isc__work_cb,
     78 			  isc__after_work_cb);
     79 	UV_RUNTIME_CHECK(uv_queue_work, r);
     80 }
     81