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);
ISC_LIST_APPEND(loop->queue_jobs, job, link);
UNLOCK(&loop->queue_lock);
* Schedule the job callback 'cb' to be run on the currently
* 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:
*
*\li 'loopmgr' is the active loop manager.
ISC_LIST_MOVE(list, loop->queue_jobs);
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) {
- 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__job_init(loop, job);
static void
async_cb(void *arg) {
- UNUSED(arg);
uint32_t tid = isc_tid();
+ UNUSED(arg);
+
atomic_fetch_add(&scheduled, 1);
if (tid > 0) {
static void
async_setup_cb(void *arg) {
- UNUSED(arg);
uint32_t tid = isc_loopmgr_nloops(loopmgr) - 1;
-
isc_loop_t *loop = isc_loop_get(loopmgr, tid);
+ UNUSED(arg);
+
isc_async_run(loop, async_cb, loopmgr);
}
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_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_MAIN
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_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_MAIN