Home | History | Annotate | Line # | Download | only in isc
      1 /*	$NetBSD: job.c,v 1.2 2025/01/26 16:25:37 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 #include <sys/types.h>
     18 #include <unistd.h>
     19 
     20 #include <isc/atomic.h>
     21 #include <isc/barrier.h>
     22 #include <isc/condition.h>
     23 #include <isc/job.h>
     24 #include <isc/list.h>
     25 #include <isc/loop.h>
     26 #include <isc/magic.h>
     27 #include <isc/mem.h>
     28 #include <isc/mutex.h>
     29 #include <isc/refcount.h>
     30 #include <isc/result.h>
     31 #include <isc/signal.h>
     32 #include <isc/strerr.h>
     33 #include <isc/thread.h>
     34 #include <isc/util.h>
     35 #include <isc/uv.h>
     36 #include <isc/work.h>
     37 
     38 #include "job_p.h"
     39 #include "loop_p.h"
     40 #include "probes.h"
     41 
     42 /*
     43  * Public: #include <isc/job.h>
     44  */
     45 
     46 void
     47 isc_job_run(isc_loop_t *loop, isc_job_t *job, isc_job_cb cb, void *cbarg) {
     48 	if (ISC_LIST_EMPTY(loop->run_jobs)) {
     49 		uv_idle_start(&loop->run_trigger, isc__job_cb);
     50 	}
     51 
     52 	job->cb = cb;
     53 	job->cbarg = cbarg;
     54 	ISC_LINK_INIT(job, link);
     55 
     56 	ISC_LIST_APPEND(loop->run_jobs, job, link);
     57 }
     58 
     59 /*
     60  * Protected: #include <job_p.h>
     61  */
     62 
     63 void
     64 isc__job_cb(uv_idle_t *handle) {
     65 	isc_loop_t *loop = uv_handle_get_data(handle);
     66 	ISC_LIST(isc_job_t) jobs = ISC_LIST_INITIALIZER;
     67 
     68 	ISC_LIST_MOVE(jobs, loop->run_jobs);
     69 
     70 	isc_job_t *job, *next;
     71 	for (job = ISC_LIST_HEAD(jobs),
     72 	    next = (job != NULL) ? ISC_LIST_NEXT(job, link) : NULL;
     73 	     job != NULL;
     74 	     job = next, next = job ? ISC_LIST_NEXT(job, link) : NULL)
     75 	{
     76 		isc_job_cb cb = job->cb;
     77 		void *cbarg = job->cbarg;
     78 		ISC_LIST_UNLINK(jobs, job, link);
     79 		LIBISC_JOB_CB_BEFORE(job, cb, cbarg);
     80 		cb(cbarg);
     81 		LIBISC_JOB_CB_AFTER(job, cb, cbarg);
     82 	}
     83 
     84 	if (ISC_LIST_EMPTY(loop->run_jobs)) {
     85 		uv_idle_stop(&loop->run_trigger);
     86 	}
     87 }
     88 
     89 void
     90 isc__job_close(uv_handle_t *handle) {
     91 	isc_loop_t *loop = uv_handle_get_data(handle);
     92 
     93 	isc__job_cb(&loop->run_trigger);
     94 }
     95