2023-07-31 07:52:04 +00:00
|
|
|
|
|
|
|
{.pragma: exported, exportc, cdecl, raises: [].}
|
|
|
|
{.pragma: callback, cdecl, raises: [], gcsafe.}
|
|
|
|
{.passc: "-fPIC".}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[json,sequtils,times,strformat,options,atomics,strutils,os]
|
|
|
|
import
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
2023-09-22 14:36:31 +00:00
|
|
|
chronos/threadsync,
|
|
|
|
taskpools/channels_spsc_single,
|
2023-07-31 07:52:04 +00:00
|
|
|
stew/results,
|
2023-09-22 14:36:31 +00:00
|
|
|
stew/shims/net
|
2023-07-31 07:52:04 +00:00
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../../../waku/node/waku_node,
|
2023-07-31 07:52:04 +00:00
|
|
|
../events/[json_error_event,json_message_event,json_base_event],
|
2023-09-18 07:21:50 +00:00
|
|
|
./inter_thread_communication/waku_thread_request,
|
|
|
|
./inter_thread_communication/waku_thread_response
|
2023-07-31 07:52:04 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Context* = object
|
|
|
|
thread: Thread[(ptr Context)]
|
2023-09-18 07:21:50 +00:00
|
|
|
reqChannel: ChannelSPSCSingle[ptr InterThreadRequest]
|
2023-09-22 14:36:31 +00:00
|
|
|
reqSignal: ThreadSignalPtr
|
2023-09-18 07:21:50 +00:00
|
|
|
respChannel: ChannelSPSCSingle[ptr InterThreadResponse]
|
2023-09-22 14:36:31 +00:00
|
|
|
respSignal: ThreadSignalPtr
|
2023-07-31 07:52:04 +00:00
|
|
|
|
|
|
|
var ctx {.threadvar.}: ptr Context
|
|
|
|
|
|
|
|
# To control when the thread is running
|
|
|
|
var running: Atomic[bool]
|
|
|
|
|
|
|
|
# Every Nim library must have this function called - the name is derived from
|
|
|
|
# the `--nimMainPrefix` command line option
|
|
|
|
proc NimMain() {.importc.}
|
|
|
|
var initialized: Atomic[bool]
|
|
|
|
|
|
|
|
proc waku_init() =
|
|
|
|
if not initialized.exchange(true):
|
|
|
|
NimMain() # Every Nim library needs to call `NimMain` once exactly
|
|
|
|
when declared(setupForeignThreadGc): setupForeignThreadGc()
|
|
|
|
when declared(nimGC_setStackBottom):
|
|
|
|
var locals {.volatile, noinit.}: pointer
|
|
|
|
locals = addr(locals)
|
|
|
|
nimGC_setStackBottom(locals)
|
|
|
|
|
|
|
|
proc run(ctx: ptr Context) {.thread.} =
|
|
|
|
## This is the worker thread body. This thread runs the Waku node
|
|
|
|
## and attends library user requests (stop, connect_to, etc.)
|
|
|
|
|
2023-09-01 06:37:02 +00:00
|
|
|
var node: WakuNode
|
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
while running.load == true:
|
|
|
|
## Trying to get a request from the libwaku main thread
|
|
|
|
|
2023-09-18 07:21:50 +00:00
|
|
|
var request: ptr InterThreadRequest
|
2023-09-22 14:36:31 +00:00
|
|
|
waitFor ctx.reqSignal.wait()
|
2023-09-18 07:21:50 +00:00
|
|
|
let recvOk = ctx.reqChannel.tryRecv(request)
|
|
|
|
if recvOk == true:
|
|
|
|
let resultResponse =
|
|
|
|
waitFor InterThreadRequest.process(request, addr node)
|
|
|
|
|
|
|
|
## Converting a `Result` into a thread-safe transferable response type
|
|
|
|
let threadSafeResp = InterThreadResponse.createShared(resultResponse)
|
|
|
|
|
|
|
|
## The error-handling is performed in the main thread
|
2023-09-22 14:36:31 +00:00
|
|
|
discard ctx.respChannel.trySend(threadSafeResp)
|
|
|
|
discard ctx.respSignal.fireSync()
|
2023-07-31 07:52:04 +00:00
|
|
|
|
|
|
|
tearDownForeignThreadGc()
|
|
|
|
|
2023-09-01 06:37:02 +00:00
|
|
|
proc createWakuThread*(): Result[void, string] =
|
2023-07-31 07:52:04 +00:00
|
|
|
## This proc is called from the main thread and it creates
|
|
|
|
## the Waku working thread.
|
|
|
|
|
|
|
|
waku_init()
|
|
|
|
|
|
|
|
ctx = createShared(Context, 1)
|
2023-09-22 14:36:31 +00:00
|
|
|
ctx.reqSignal = ThreadSignalPtr.new().valueOr:
|
|
|
|
return err("couldn't create reqSignal ThreadSignalPtr")
|
|
|
|
ctx.respSignal = ThreadSignalPtr.new().valueOr:
|
|
|
|
return err("couldn't create respSignal ThreadSignalPtr")
|
2023-07-31 07:52:04 +00:00
|
|
|
|
|
|
|
running.store(true)
|
|
|
|
|
|
|
|
try:
|
|
|
|
createThread(ctx.thread, run, ctx)
|
2023-09-01 06:37:02 +00:00
|
|
|
except ValueError, ResourceExhaustedError:
|
2023-07-31 07:52:04 +00:00
|
|
|
# and freeShared for typed allocations!
|
|
|
|
freeShared(ctx)
|
|
|
|
|
2023-09-01 06:37:02 +00:00
|
|
|
return err("failed to create the Waku thread: " & getCurrentExceptionMsg())
|
2023-07-31 07:52:04 +00:00
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
proc stopWakuNodeThread*() =
|
|
|
|
running.store(false)
|
|
|
|
joinThread(ctx.thread)
|
2023-09-22 14:36:31 +00:00
|
|
|
discard ctx.reqSignal.close()
|
|
|
|
discard ctx.respSignal.close()
|
2023-07-31 07:52:04 +00:00
|
|
|
freeShared(ctx)
|
|
|
|
|
2023-09-18 07:21:50 +00:00
|
|
|
proc sendRequestToWakuThread*(reqType: RequestType,
|
|
|
|
reqContent: pointer): Result[string, string] =
|
2023-07-31 07:52:04 +00:00
|
|
|
|
2023-09-18 07:21:50 +00:00
|
|
|
let req = InterThreadRequest.createShared(reqType, reqContent)
|
2023-07-31 07:52:04 +00:00
|
|
|
|
2023-09-18 07:21:50 +00:00
|
|
|
## Sending the request
|
|
|
|
let sentOk = ctx.reqChannel.trySend(req)
|
|
|
|
if not sentOk:
|
|
|
|
return err("Couldn't send a request to the waku thread: " & $req[])
|
2023-07-31 07:52:04 +00:00
|
|
|
|
2023-09-22 14:36:31 +00:00
|
|
|
let fireSyncRes = ctx.reqSignal.fireSync()
|
|
|
|
if fireSyncRes.isErr():
|
|
|
|
return err("failed fireSync: " & $fireSyncRes.error)
|
|
|
|
|
|
|
|
if fireSyncRes.get() == false:
|
|
|
|
return err("Couldn't fireSync in time")
|
|
|
|
|
2023-09-18 07:21:50 +00:00
|
|
|
## Waiting for the response
|
2023-09-22 14:36:31 +00:00
|
|
|
waitFor ctx.respSignal.wait()
|
|
|
|
|
2023-09-18 07:21:50 +00:00
|
|
|
var response: ptr InterThreadResponse
|
|
|
|
var recvOk = ctx.respChannel.tryRecv(response)
|
2023-09-22 14:36:31 +00:00
|
|
|
if recvOk == false:
|
|
|
|
return err("Couldn't receive response from the waku thread: " & $req[])
|
2023-07-31 07:52:04 +00:00
|
|
|
|
2023-09-18 07:21:50 +00:00
|
|
|
## Converting the thread-safe response into a managed/CG'ed `Result`
|
|
|
|
return InterThreadResponse.process(response)
|