Skip to content

Commit

Permalink
fix(threadpool): fix task garbage collection synchronization on weak …
Browse files Browse the repository at this point in the history
…memory models, closes #502 (#503)
  • Loading branch information
mratsim authored Dec 28, 2024
1 parent b79c0d0 commit 6b65b0e
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions constantine/threadpool/crossthread/tasks_flowvars.nim
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,16 @@ proc isCompleted*(task: ptr Task): bool {.inline.} =
proc setCompleted*(task: ptr Task) {.inline.} =
## Set a task to `complete`
## Wake a waiter thread if there is one
task.state.completed.store(1, moRelaxed)
fence(moSequentiallyConsistent)
let waiter = task.state.synchro.load(moRelaxed)
task.state.completed.store(1, moRelease)
let waiter = task.state.synchro.load(moAcquire)
if (waiter and kWaiterMask) != SentinelWaiter:
task.state.completed.wake()

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

# Leapfrogging synchronization
Expand Down

0 comments on commit 6b65b0e

Please sign in to comment.