44/* *****************************************************************************
55Condition Variables - BYO Implementation (FIO_THREADS_COND_BYO)
66
7- On Windows: fio-stl.h's fio_thread_cond_wait uses SleepConditionVariableSRW
8- with INFINITE timeout, causing a deadlock during reactor startup. Worker threads
9- enter the infinite wait immediately (queue is empty at startup), and since
10- nothing signals them before the reactor's main loop starts, they sleep forever:
11-
12- - Main thread spin-waits for grp.stop == 0 (set only after manager creates
13- all workers)
14- - Manager thread blocks in fio_thread_join waiting for workers to exit
15- - Workers sleep forever in SleepConditionVariableSRW(INFINITE)
16- - Nothing signals workers because the IO loop hasn't started yet
17- - IO loop can't start because main thread is stuck in the spin-wait
18-
19- Fix: use a 500ms timed wait so workers periodically wake to check grp->stop
20- and exit cleanly when signalled.
21-
22- On POSIX: delegate to pthread_cond_* unchanged (no behavioral change).
7+ Provides Windows and POSIX implementations of condition variable primitives
8+ used by facil.io's worker thread pool.
9+
10+ On Windows: Uses SleepConditionVariableSRW with INFINITE timeout, matching
11+ POSIX pthread_cond_wait behavior. Workers are properly signaled when:
12+ - Tasks are added to the queue (fio_queue_push signals all consumers)
13+ - Shutdown is initiated (fio_queue_workers_stop signals all workers)
14+
15+ On POSIX: Delegates to pthread_cond_* unchanged.
2316***************************************************************************** */
2417
2518#ifdef _WIN32
@@ -31,11 +24,11 @@ FIO_IFUNC int fio_thread_cond_init(fio_thread_cond_t *c) {
3124 return 0 ;
3225}
3326
34- /* Use 500ms timeout instead of INFINITE — lets workers wake periodically
35- * to check the stop flag, preventing deadlock during reactor startup . */
27+ /* Wait indefinitely until signaled — matches POSIX pthread_cond_wait behavior.
28+ * Workers are signaled when tasks are added or during shutdown . */
3629FIO_IFUNC int fio_thread_cond_wait (fio_thread_cond_t * c ,
3730 fio_thread_mutex_t * m ) {
38- SleepConditionVariableSRW (c , m , 500 , 0 );
31+ SleepConditionVariableSRW (c , m , INFINITE , 0 );
3932 return 0 ;
4033}
4134
0 commit comments