Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix logic error and race condition #8

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions actors/pool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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 @@ -191,7 +191,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 @@ -388,6 +388,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
Loading