mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-09-03 16:15:27 +00:00
Merge branch '3632-async-backwards' into 'main'
isc_async_run() runs events in reverse order Closes #3632 See merge request isc-projects/bind9!7000
This commit is contained in:
@@ -47,7 +47,9 @@ isc_async_run(isc_loop_t *loop, isc_job_cb cb, void *cbarg) {
|
|||||||
|
|
||||||
job = isc__job_new(loop, cb, cbarg);
|
job = isc__job_new(loop, cb, cbarg);
|
||||||
|
|
||||||
/* Now send the half initialized job to loop queue */
|
/*
|
||||||
|
* Now send the half-initialized job to the loop queue.
|
||||||
|
*/
|
||||||
LOCK(&loop->queue_lock);
|
LOCK(&loop->queue_lock);
|
||||||
ISC_LIST_APPEND(loop->queue_jobs, job, link);
|
ISC_LIST_APPEND(loop->queue_jobs, job, link);
|
||||||
UNLOCK(&loop->queue_lock);
|
UNLOCK(&loop->queue_lock);
|
||||||
|
@@ -39,6 +39,11 @@ isc_job_run(isc_loopmgr_t *loopmgr, isc_job_cb cb, void *cbarg);
|
|||||||
* Schedule the job callback 'cb' to be run on the currently
|
* Schedule the job callback 'cb' to be run on the currently
|
||||||
* running event loop.
|
* running event loop.
|
||||||
*
|
*
|
||||||
|
* Note: Because of the design of uv_idle_start(), if more than one
|
||||||
|
* job is posted at once, the jobs will be pushed onto a stack and
|
||||||
|
* executed in last-in-first-out order. To post events that are
|
||||||
|
* executed in order posted, use isc_async_run() instead.
|
||||||
|
*
|
||||||
* Requires:
|
* Requires:
|
||||||
*
|
*
|
||||||
*\li 'loopmgr' is the active loop manager.
|
*\li 'loopmgr' is the active loop manager.
|
||||||
|
@@ -191,9 +191,14 @@ queue_cb(uv_async_t *handle) {
|
|||||||
ISC_LIST_MOVE(list, loop->queue_jobs);
|
ISC_LIST_MOVE(list, loop->queue_jobs);
|
||||||
UNLOCK(&loop->queue_lock);
|
UNLOCK(&loop->queue_lock);
|
||||||
|
|
||||||
job = ISC_LIST_HEAD(list);
|
/*
|
||||||
|
* The ISC_LIST_TAIL is counterintuitive here, but uv_idle
|
||||||
|
* drains its queue backwards, so if there's more than one event to
|
||||||
|
* be processed then they need to be in reverse order.
|
||||||
|
*/
|
||||||
|
job = ISC_LIST_TAIL(list);
|
||||||
while (job != NULL) {
|
while (job != NULL) {
|
||||||
isc_job_t *next = ISC_LIST_NEXT(job, link);
|
isc_job_t *next = ISC_LIST_PREV(job, link);
|
||||||
ISC_LIST_UNLINK(list, job, link);
|
ISC_LIST_UNLINK(list, job, link);
|
||||||
|
|
||||||
isc__job_init(loop, job);
|
isc__job_init(loop, job);
|
||||||
|
@@ -39,9 +39,10 @@ static atomic_uint scheduled = 0;
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
async_cb(void *arg) {
|
async_cb(void *arg) {
|
||||||
UNUSED(arg);
|
|
||||||
uint32_t tid = isc_tid();
|
uint32_t tid = isc_tid();
|
||||||
|
|
||||||
|
UNUSED(arg);
|
||||||
|
|
||||||
atomic_fetch_add(&scheduled, 1);
|
atomic_fetch_add(&scheduled, 1);
|
||||||
|
|
||||||
if (tid > 0) {
|
if (tid > 0) {
|
||||||
@@ -54,11 +55,11 @@ async_cb(void *arg) {
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
async_setup_cb(void *arg) {
|
async_setup_cb(void *arg) {
|
||||||
UNUSED(arg);
|
|
||||||
uint32_t tid = isc_loopmgr_nloops(loopmgr) - 1;
|
uint32_t tid = isc_loopmgr_nloops(loopmgr) - 1;
|
||||||
|
|
||||||
isc_loop_t *loop = isc_loop_get(loopmgr, tid);
|
isc_loop_t *loop = isc_loop_get(loopmgr, tid);
|
||||||
|
|
||||||
|
UNUSED(arg);
|
||||||
|
|
||||||
isc_async_run(loop, async_cb, loopmgr);
|
isc_async_run(loop, async_cb, loopmgr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,8 +69,40 @@ ISC_RUN_TEST_IMPL(isc_async_run) {
|
|||||||
assert_int_equal(atomic_load(&scheduled), loopmgr->nloops);
|
assert_int_equal(atomic_load(&scheduled), loopmgr->nloops);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char string[32] = "";
|
||||||
|
int n1 = 1, n2 = 2, n3 = 3, n4 = 4, n5 = 5;
|
||||||
|
|
||||||
|
static void
|
||||||
|
append(void *arg) {
|
||||||
|
char value[32];
|
||||||
|
sprintf(value, "%d", *(int *)arg);
|
||||||
|
strlcat(string, value, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
async_multiple(void *arg) {
|
||||||
|
isc_loop_t *loop = isc_loop_current(loopmgr);
|
||||||
|
|
||||||
|
UNUSED(arg);
|
||||||
|
|
||||||
|
isc_async_run(loop, append, &n1);
|
||||||
|
isc_async_run(loop, append, &n2);
|
||||||
|
isc_async_run(loop, append, &n3);
|
||||||
|
isc_async_run(loop, append, &n4);
|
||||||
|
isc_async_run(loop, append, &n5);
|
||||||
|
isc_loopmgr_shutdown(loopmgr);
|
||||||
|
}
|
||||||
|
|
||||||
|
ISC_RUN_TEST_IMPL(isc_async_multiple) {
|
||||||
|
string[0] = '\0';
|
||||||
|
isc_loop_setup(isc_loop_main(loopmgr), async_multiple, loopmgr);
|
||||||
|
isc_loopmgr_run(loopmgr);
|
||||||
|
assert_string_equal(string, "12345");
|
||||||
|
}
|
||||||
|
|
||||||
ISC_TEST_LIST_START
|
ISC_TEST_LIST_START
|
||||||
ISC_TEST_ENTRY_CUSTOM(isc_async_run, setup_loopmgr, teardown_loopmgr)
|
ISC_TEST_ENTRY_CUSTOM(isc_async_run, setup_loopmgr, teardown_loopmgr)
|
||||||
|
ISC_TEST_ENTRY_CUSTOM(isc_async_multiple, setup_loopmgr, teardown_loopmgr)
|
||||||
ISC_TEST_LIST_END
|
ISC_TEST_LIST_END
|
||||||
|
|
||||||
ISC_TEST_MAIN
|
ISC_TEST_MAIN
|
||||||
|
@@ -76,8 +76,39 @@ ISC_RUN_TEST_IMPL(isc_job_run) {
|
|||||||
assert_int_equal(atomic_load(&scheduled), atomic_load(&executed));
|
assert_int_equal(atomic_load(&scheduled), atomic_load(&executed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char string[32] = "";
|
||||||
|
int n1 = 1, n2 = 2, n3 = 3, n4 = 4, n5 = 5;
|
||||||
|
|
||||||
|
static void
|
||||||
|
append(void *arg) {
|
||||||
|
char value[32];
|
||||||
|
sprintf(value, "%d", *(int *)arg);
|
||||||
|
strlcat(string, value, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
job_multiple(void *arg) {
|
||||||
|
UNUSED(arg);
|
||||||
|
|
||||||
|
/* These will be processed in reverse order */
|
||||||
|
isc_job_run(loopmgr, append, &n1);
|
||||||
|
isc_job_run(loopmgr, append, &n2);
|
||||||
|
isc_job_run(loopmgr, append, &n3);
|
||||||
|
isc_job_run(loopmgr, append, &n4);
|
||||||
|
isc_job_run(loopmgr, append, &n5);
|
||||||
|
isc_loopmgr_shutdown(loopmgr);
|
||||||
|
}
|
||||||
|
|
||||||
|
ISC_RUN_TEST_IMPL(isc_job_multiple) {
|
||||||
|
string[0] = '\0';
|
||||||
|
isc_loop_setup(isc_loop_main(loopmgr), job_multiple, loopmgr);
|
||||||
|
isc_loopmgr_run(loopmgr);
|
||||||
|
assert_string_equal(string, "54321");
|
||||||
|
}
|
||||||
|
|
||||||
ISC_TEST_LIST_START
|
ISC_TEST_LIST_START
|
||||||
ISC_TEST_ENTRY_CUSTOM(isc_job_run, setup_loopmgr, teardown_loopmgr)
|
ISC_TEST_ENTRY_CUSTOM(isc_job_run, setup_loopmgr, teardown_loopmgr)
|
||||||
|
ISC_TEST_ENTRY_CUSTOM(isc_job_multiple, setup_loopmgr, teardown_loopmgr)
|
||||||
ISC_TEST_LIST_END
|
ISC_TEST_LIST_END
|
||||||
|
|
||||||
ISC_TEST_MAIN
|
ISC_TEST_MAIN
|
||||||
|
Reference in New Issue
Block a user