Skip to content

Instantly share code, notes, and snippets.

@michaelsbradleyjr
Created June 9, 2021 16:22
Show Gist options
  • Save michaelsbradleyjr/89870e55fa46745bb0e3302555360420 to your computer and use it in GitHub Desktop.
Save michaelsbradleyjr/89870e55fa46745bb0e3302555360420 to your computer and use it in GitHub Desktop.
type
HelloTaskArg = ref object of TaskArg
taskStopped*: ByteAddress
someName*: string
otherStuff*: int
const
helloTask: Task = proc (taskArgEncoded: string) {.async, gcsafe, nimcall.} =
let
taskArg = decode[HelloTaskArg](taskArgEncoded)
chanSendToHost = cast[WorkerChannel](taskArg.chanSendToHost)
var taskStopped = cast[ptr Atomic[bool]](taskArg.taskStopped)
var workerRunning = cast[ptr Atomic[bool]](taskArg.workerRunning)
let someName = taskArg.someName
let otherStuff = taskArg.otherStuff
echo "Hello, " & someName & $otherStuff & "!"
proc hello[T, U](taskRunner: TaskRunner; workerName: string;
stopped: var Atomic[bool]; someName: string; otherStuff: int) {.async.} =
let
worker = taskRunner.workers[workerName].worker
chanSendToHost = worker.chanRecvFromWorker
chanSendToWorker = worker.chanSendToWorker
taskArg = HelloTaskArg(chanSendToHost: cast[ByteAddress](chanSendToHost),
task: cast[ByteAddress](helloTask),
taskName: "hello",
workerRunning: cast[ByteAddress](addr taskRunner.running))
asyncSpawn chanSendToWorker.send(taskArg.encode.safe)
proc helloSync[T, U](taskRunner: TaskRunner; workerName: string;
stopped: var Atomic[bool]; someName: string;
otherStuff: int) =
let
worker = taskRunner.workers[workerName].worker
chanSendToHost = worker.chanRecvFromWorker
chanSendToWorker = worker.chanSendToWorker
taskArg = HelloTaskArg(chanSendToHost: cast[ByteAddress](chanSendToHost),
task: cast[ByteAddress](helloTask),
taskName: "hello",
workerRunning: cast[ByteAddress](addr taskRunner.running))
chanSendToWorker.sendSync(taskArg.encode.safe)
export HelloTaskArg, hello, helloSync, helloTask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment