De-async actors

This commit is contained in:
Emery Hemingway 2024-03-01 14:02:27 +00:00
parent 1ce96560f4
commit 0cee6670c9
3 changed files with 9 additions and 5 deletions

View File

@ -3,7 +3,7 @@
## This module implements the `Syndicate DSL <https://syndicate-lang.org/doc/syndicate/>`_. ## This module implements the `Syndicate DSL <https://syndicate-lang.org/doc/syndicate/>`_.
import std/[asyncdispatch, macros, tables, typetraits] import std/[macros, tables, typetraits]
import preserves import preserves
export fromPreserves, toPreserves export fromPreserves, toPreserves

View File

@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: ☭ Emery Hemingway # SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense # SPDX-License-Identifier: Unlicense
import std/[asyncfutures, hashes, monotimes, options, sets, tables, times] import std/[hashes, monotimes, options, sets, tables, times]
import preserves import preserves
import ../syndicate/protocols/[protocol, sturdy] import ../syndicate/protocols/[protocol, sturdy]
@ -492,13 +492,14 @@ proc terminate(actor; turn; reason: ref Exception) =
proc finish(turn: var Turn) = proc finish(turn: var Turn) =
actor.root.terminate(turn, reason.isNil) actor.root.terminate(turn, reason.isNil)
actor.exited = true actor.exited = true
callSoon do (): block:
run(actor.root, finish, true) run(actor.root, finish, true)
proc terminate*(facet; e: ref Exception) = proc terminate*(facet; e: ref Exception) =
run(facet.actor.root) do (turn: var Turn): run(facet.actor.root) do (turn: var Turn):
facet.actor.terminate(turn, e) facet.actor.terminate(turn, e)
#[
proc asyncCheck*(facet: Facet; fut: FutureBase) = proc asyncCheck*(facet: Facet; fut: FutureBase) =
## Sets a callback on `fut` which propagates exceptions to `facet`. ## Sets a callback on `fut` which propagates exceptions to `facet`.
addCallback(fut) do (): addCallback(fut) do ():
@ -507,13 +508,14 @@ proc asyncCheck*(facet: Facet; fut: FutureBase) =
proc asyncCheck*(turn; fut: FutureBase) = proc asyncCheck*(turn; fut: FutureBase) =
## Sets a callback on `fut` which propagates exceptions to the facet of `turn`. ## Sets a callback on `fut` which propagates exceptions to the facet of `turn`.
asyncCheck(turn.facet, fut) asyncCheck(turn.facet, fut)
]#
template tryFacet(facet; body: untyped) = template tryFacet(facet; body: untyped) =
try: body try: body
except CatchableError as err: terminate(facet, err) except CatchableError as err: terminate(facet, err)
proc run*(facet; action: TurnAction; zombieTurn = false) = proc run*(facet; action: TurnAction; zombieTurn = false) =
if zombieTurn or (facet.actor.exitReason.isNil and facet.isAlive): if true and zombieTurn or (facet.actor.exitReason.isNil and facet.isAlive):
tryFacet(facet): tryFacet(facet):
var queues = newTable[Facet, seq[TurnAction]]() var queues = newTable[Facet, seq[TurnAction]]()
block: block:
@ -530,6 +532,7 @@ proc run*(cap: Cap; action: TurnAction) =
## Convenience proc to run a `TurnAction` in the scope of a `Cap`. ## Convenience proc to run a `TurnAction` in the scope of a `Cap`.
run(cap.relay, action) run(cap.relay, action)
#[
proc addCallback*(fut: FutureBase; facet: Facet; act: TurnAction) = proc addCallback*(fut: FutureBase; facet: Facet; act: TurnAction) =
## Add a callback to a `Future` that will be called at a later `Turn` ## Add a callback to a `Future` that will be called at a later `Turn`
## within the context of `facet`. ## within the context of `facet`.
@ -562,6 +565,7 @@ proc addCallback*[T](fut: Future[T]; turn: var Turn; act: proc (t: var Turn, x:
turn.desc.cause = TurnCause(orKind: TurnCauseKind.external) turn.desc.cause = TurnCause(orKind: TurnCauseKind.external)
turn.desc.cause.external.description = "Future".toPreserves turn.desc.cause.external.description = "Future".toPreserves
act(turn, read fut) act(turn, read fut)
]#
proc stop*(turn: var Turn, facet: Facet) = proc stop*(turn: var Turn, facet: Facet) =
if facet.parent.isNil: if facet.parent.isNil:

View File

@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: ☭ Emery Hemingway # SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense # SPDX-License-Identifier: Unlicense
import std/[asyncdispatch, options, tables] import std/[options, tables]
from std/os import getEnv, `/` from std/os import getEnv, `/`
import preserves import preserves
import ../syndicate, /capabilities, ./durings, ./membranes, ./protocols/[gatekeeper, protocol, sturdy, transportAddress] import ../syndicate, /capabilities, ./durings, ./membranes, ./protocols/[gatekeeper, protocol, sturdy, transportAddress]