Skip to content

Commit efcd2a2

Browse files
committed
timing issues
1 parent 0f7aeef commit efcd2a2

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

ext/iodine/iodine_threads.h

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,15 @@
44
/* *****************************************************************************
55
Condition 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. */
3629
FIO_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

Comments
 (0)