From ece94d69369cd19ed0c501defcec84c679194c93 Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Wed, 30 Mar 2022 11:18:23 -0700 Subject: [PATCH] fixup! fixup! WIP: Add loopmgr that replaces isc_app, and parts of isc_nm related to the uv_loops --- lib/isc/include/isc/loop.h | 24 ++++++++++++++++++++---- lib/isc/loop.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/lib/isc/include/isc/loop.h b/lib/isc/include/isc/loop.h index 852c43ff86..91e451c014 100644 --- a/lib/isc/include/isc/loop.h +++ b/lib/isc/include/isc/loop.h @@ -81,25 +81,41 @@ isc_loopmgr_run(isc_loopmgr_t *loopmgr); * Requires: *\li 'loopmgr' is a valid loop manager. */ +void +isc_loop_runjob(isc_loop_t *loop, isc_job_cb cb, void *cbarg); +/*%< + * Schedule a single job to run in 'loop' in the next iteration + * of uv_run(): 'cb' will be called with argument 'cbarg'. + * + * Requires: + *\li 'loop' is a valid loop. + *\li 'loop' is the currently running loop, or the loop manager + * associated with 'loop' is paused or has not yet been started. + */ void isc_loopmgr_pause(isc_loopmgr_t *loopmgr); /*%< - * Send pause events to all running loops in 'loopmgr' (except the - * current one if called from one of the loops). All paused loops will - * wait utnil isc_loopmgr_resume() is called before continuing. + * Send pause events to all running loops in 'loopmgr' except the + * current one. This can only be called from a running loop. + * All the paused loops will wait until isc_loopmgr_resume() is + * run in the calling loop before continuing. * * Requires: *\li 'loopmgr' is a valid loop manager. + *\li We are in a running loop. */ void isc_loopmgr_resume(isc_loopmgr_t *loopmgr); /*%< - * Send resume events to all paused loops in 'loopmgr'. + * Send resume events to all paused loops in 'loopmgr'. This can + * only be called by a running loop (which must therefore be the + * loop that called isc_loopmgr_pause()). * * Requires: *\li 'loopmgr' is a valid loop manager. + *\li We are in a running loop. */ void diff --git a/lib/isc/loop.c b/lib/isc/loop.c index ac10a913a7..c1310dbdc3 100644 --- a/lib/isc/loop.c +++ b/lib/isc/loop.c @@ -370,6 +370,36 @@ isc_loopmgr_teardown(isc_loopmgr_t *loopmgr, isc_job_cb cb, void *cbarg) { isc__loopmgr_schedule(loopmgr, isc_loop_dtor, cb, cbarg); } +void +isc_loop_runjob(isc_loop_t *loop, isc_job_cb cb, void *cbarg) { + isc_loopmgr_t *loopmgr = NULL; + isc_job_t *job = NULL; + int r; + + REQUIRE(VALID_LOOP(loop)); + + loopmgr = loop->loopmgr; + + REQUIRE(loop->tid == isc__loopmgr_tid_v || + !atomic_load(&loopmgr->running) || + atomic_load(&loopmgr->paused)); + + job = isc_mem_get(loop->mctx, sizeof(*job)); + *job = (isc_job_t){ + .cb = cb, + .cbarg = cbarg, + }; + + isc_mem_attach(loop->mctx, &job->mctx); + + r = uv_idle_init(&loop->loop, &job->idle); + UV_RUNTIME_CHECK(uv_idle_init, r); + uv_handle_set_data((uv_handle_t *)&job->idle, job); + + r = uv_idle_start(&job->idle, isc__job_cb); + UV_RUNTIME_CHECK(uv_idle_start, r); +} + void isc_loopmgr_run(isc_loopmgr_t *loopmgr) { REQUIRE(VALID_LOOPMGR(loopmgr)); @@ -396,6 +426,7 @@ isc_loopmgr_run(isc_loopmgr_t *loopmgr) { void isc_loopmgr_pause(isc_loopmgr_t *loopmgr) { REQUIRE(VALID_LOOPMGR(loopmgr)); + REQUIRE(isc__loopmgr_tid_v != ISC_LOOPMGR_TID_UNKNOWN); for (size_t i = 0; i < loopmgr->nloops; i++) { isc_loop_t *loop = &loopmgr->loops[i]; @@ -415,6 +446,7 @@ isc_loopmgr_pause(isc_loopmgr_t *loopmgr) { void isc_loopmgr_resume(isc_loopmgr_t *loopmgr) { REQUIRE(VALID_LOOPMGR(loopmgr)); + REQUIRE(isc__loopmgr_tid_v != ISC_LOOPMGR_TID_UNKNOWN); RUNTIME_CHECK(atomic_compare_exchange_strong(&loopmgr->paused, &(bool){ true }, false));