bind9/lib/isc/job.c
Evan Hunt 522ca7bb54 switch to ISC_LIST_FOREACH everywhere
the pattern `for (x = ISC_LIST_HEAD(...); x != NULL; ISC_LIST_NEXT(...)`
has been changed to `ISC_LIST_FOREACH` throughout BIND, except in a few
cases where the change would be excessively complex.

in most cases this was a straightforward change. in some places,
however, the list element variable was referenced after the loop
ended, and the code was refactored to avoid this necessity.

also, because `ISC_LIST_FOREACH` uses typeof(list.head) to declare
the list elements, compilation failures can occur if the list object
has a `const` qualifier.  some `const` qualifiers have been removed
from function parameters to avoid this problem, and where that was not
possible, `UNCONST` was used.
2025-03-31 13:45:10 -07:00

86 lines
1.9 KiB
C

/*
* 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/atomic.h>
#include <isc/barrier.h>
#include <isc/job.h>
#include <isc/list.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 "job_p.h"
#include "loop_p.h"
#include "probes.h"
/*
* Public: #include <isc/job.h>
*/
void
isc_job_run(isc_loop_t *loop, isc_job_t *job, isc_job_cb cb, void *cbarg) {
if (ISC_LIST_EMPTY(loop->run_jobs)) {
uv_idle_start(&loop->run_trigger, isc__job_cb);
}
job->cb = cb;
job->cbarg = cbarg;
ISC_LINK_INIT(job, link);
ISC_LIST_APPEND(loop->run_jobs, job, link);
}
/*
* Protected: #include <job_p.h>
*/
void
isc__job_cb(uv_idle_t *handle) {
isc_loop_t *loop = uv_handle_get_data(handle);
ISC_LIST(isc_job_t) jobs = ISC_LIST_INITIALIZER;
ISC_LIST_MOVE(jobs, loop->run_jobs);
ISC_LIST_FOREACH_SAFE (jobs, job, link) {
isc_job_cb cb = job->cb;
void *cbarg = job->cbarg;
ISC_LIST_UNLINK(jobs, job, link);
LIBISC_JOB_CB_BEFORE(job, cb, cbarg);
cb(cbarg);
LIBISC_JOB_CB_AFTER(job, cb, cbarg);
}
if (ISC_LIST_EMPTY(loop->run_jobs)) {
uv_idle_stop(&loop->run_trigger);
}
}
void
isc__job_close(uv_handle_t *handle) {
isc_loop_t *loop = uv_handle_get_data(handle);
isc__job_cb(&loop->run_trigger);
}