Skip to content

Commit

Permalink
Merge pull request #8 from zerbina/fix-pool-bug
Browse files Browse the repository at this point in the history
fix logic error and race condition
  • Loading branch information
zevv authored Apr 14, 2024
2 parents 95191a5 + 2344e1b commit 94e9831
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 13 additions & 2 deletions actors/pool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type
pool: ptr Pool

State = enum
Suspended, Queued, Running, Jielding, Killed, Dead
Suspended, Queued, Running, Suspending, Jielding, Killed, Dead

ActorObject* = object
rc*: Atomic[int]
Expand Down Expand Up @@ -189,7 +189,7 @@ proc suspend*(actor: Actor, c: sink Continuation): ActorCont =
if actor[].sigQueue.len == 0:
actor[].c = move c
if actor[].state == Running:
actor[].state = Suspended
actor[].state = Suspending
return nil

actor.handleSignals()
Expand Down Expand Up @@ -386,6 +386,17 @@ proc workerThread(worker: ptr Worker) {.thread.} =
if c.finished:
{.emit: ["/* actor finished */"].}
pool.exit(actor, Normal)
elif state == Suspending:
pool.withLock:
actor.withLock:
# if there are already new signals in the queue, the actor needs
# to be queued again right away
if actor[].sigQueue.len > 0:
actor[].state = Queued
pool.workQueue.addLast(actor)
pool.cond.signal()
else:
actor[].state = Suspended
elif state == Jielding:
pool.withLock:
actor.withLock:
Expand Down
6 changes: 5 additions & 1 deletion doc/flow.dot
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ digraph dot {

Suspended -> Running [ label = "recvCps()" ];

Suspending -> Suspended;

Suspending -> Queued;

Queued -> Running;

Running -> Queued [ label = "jield()" ];

Running -> Suspended [ label = "recv() ||\nsendCps()" ];
Running -> Suspending [ label = "recv() ||\nsendCps()" ];

Running -> Killed [ label = "rx SigKill" ];

Expand Down

0 comments on commit 94e9831

Please sign in to comment.