Skip to content

Commit ea20f62

Browse files
authored
fix(threadpool): deadlock on Windows on fibonacci (#509)
1 parent 787e776 commit ea20f62

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

constantine/threadpool/crossthread/tasks_flowvars.nim

+6-4
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,18 @@ proc isCompleted*(task: ptr Task): bool {.inline.} =
142142
proc setCompleted*(task: ptr Task) {.inline.} =
143143
## Set a task to `complete`
144144
## Wake a waiter thread if there is one
145-
task.state.completed.store(1, moRelease)
146-
let waiter = task.state.synchro.load(moAcquire)
145+
task.state.completed.store(1, moRelease) # Correctness on weak memory models like ARM: tests/t_ethereum_eip4844_deneb_kzg_parallel.nim
146+
fence(moSequentiallyConsistent) # Avoid deadlock on Windows: benchmarks-threadpool/fibonacci/threadpool_fib.nim
147+
let waiter = task.state.synchro.load(moRelaxed)
147148
if (waiter and kWaiterMask) != SentinelWaiter:
148149
task.state.completed.wake()
149150

150151
proc sleepUntilComplete*(task: ptr Task, waiterID: int32) {.inline.} =
151152
## Sleep while waiting for task completion
152153
let waiter = (cast[uint32](waiterID) shl kWaiterShift) - SentinelWaiter
153-
discard task.state.synchro.fetchAdd(waiter, moRelease)
154-
while task.state.completed.load(moAcquire) == 0:
154+
discard task.state.synchro.fetchAdd(waiter, moRelaxed)
155+
fence(moAcquire)
156+
while task.state.completed.load(moRelaxed) == 0:
155157
task.state.completed.wait(0)
156158

157159
# Leapfrogging synchronization

0 commit comments

Comments
 (0)