Make parallel_for_actually_parallel deterministic

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.
This commit is contained in:
Martin Vogel committed 2026-06-06 19:48:48 +02:00
1 parent a2c9804397
commit 4207c83679
1 file changed
+8 -5
+8 -5
View File
@@ -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) {