From 4207c83679f9e4e3b1b048c0448b390cfd73e390 Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sat, 6 Jun 2026 19:48:48 +0200 Subject: [PATCH] Make parallel_for_actually_parallel deterministic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test proved parallelism with a fixed 10000-iteration busy-wait, hoping workers would overlap — but on a loaded CI runner a worker could finish before the next thread started, leaving concurrent_max=1 (a flaky FAIL, seen on ubuntu-latest). Spin until at least two workers are concurrently active (bounded, with an early exit once the overlap is proven) so the parallelism invariant is demonstrated deterministically instead of by timing luck. --- tests/test_worker_pool.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/test_worker_pool.c b/tests/test_worker_pool.c index b1253f26c..612a159e3 100644 --- a/tests/test_worker_pool.c +++ b/tests/test_worker_pool.c @@ -174,11 +174,14 @@ static void concurrency_worker(int idx, void *ctx_ptr) { (void)idx; concurrency_ctx_t *cc = ctx_ptr; int cur = atomic_fetch_add(&cc->concurrent_now, 1) + 1; - /* Brief busy-wait to let others start */ - volatile int x = 0; - for (int j = 0; j < 10000; j++) - x++; - (void)x; + /* Spin until at least two workers are concurrently active, so overlap is + * demonstrated deterministically instead of depending on thread-spawn timing + * (a fixed busy-wait flaked on loaded runners when a worker finished before + * the next started). Bounded so it can't hang if real parallelism is absent. */ + for (long spins = 0; spins < 200000000L; spins++) { + if (atomic_load(&cc->concurrent_now) >= 2 || atomic_load(&cc->concurrent_max) >= 2) + break; + } /* Record max */ int prev_max = atomic_load(&cc->concurrent_max); while (cur > prev_max) {