From e4242132f7707661a4813f3e0f8425b2a6f5bf8f Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Fri, 9 Feb 2024 21:49:43 -0700 Subject: [PATCH] rename queue --- src/apatheia/queues.nim | 10 +++++++--- tests/tasyncsEx2.nim | 9 ++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/apatheia/queues.nim b/src/apatheia/queues.nim index a083897..6ae6fc9 100644 --- a/src/apatheia/queues.nim +++ b/src/apatheia/queues.nim @@ -22,12 +22,13 @@ type signal: ThreadSignalPtr chan*: ChanPtr[T] -proc newSignalQueue*[T](): SignalQueue[T] {.raises: [ApatheiaSignalErr].} = +proc newSignalQueue*[T](maxItems: int = 0): SignalQueue[T] {.raises: [ApatheiaSignalErr].} = let res = ThreadSignalPtr.new() if res.isErr(): raise newException(ApatheiaSignalErr, res.error()) result.signal = res.get() result.chan = allocSharedChannel[T]() + result.chan[].open(maxItems) proc send*[T](c: SignalQueue[T], msg: sink T): Result[void, string] {.raises: [].} = ## Sends a message to a thread. `msg` is copied. @@ -47,8 +48,11 @@ proc trySend*[T](c: SignalQueue[T], msg: sink T): bool = if result: c.signal.fireSync() -proc recv*[T](c: SignalQueue[T]): T = - c.chan.recv() +proc recv*[T](c: SignalQueue[T]): Result[T, string] = + try: + result = ok c.chan[].recv() + except Exception as exc: + result = err exc.msg proc tryRecv*[T](c: SignalQueue[T]): Option[T] = let res = c.chan.recv() diff --git a/tests/tasyncsEx2.nim b/tests/tasyncsEx2.nim index edf70c1..2a0e601 100644 --- a/tests/tasyncsEx2.nim +++ b/tests/tasyncsEx2.nim @@ -10,13 +10,9 @@ import apatheia/queues ## todo: setup basic async + threadsignal + taskpools example here ## -type - ThreadArg = object - doneSig: ThreadSignalPtr - value: float - proc addNums(a, b: float, queue: SignalQueue[float]) = os.sleep(500) + echo "adding: ", a, " + ", b discard queue.send(a + b) suite "async tests": @@ -26,10 +22,13 @@ suite "async tests": asyncTest "test": + echo "\nstart" tp.spawn addNums(1.0, 2.0, queue) # await sleepAsync(100.milliseconds) + echo "waiting on queue" await wait(queue).wait(1500.milliseconds) + echo "result: ", queue.recv() # echo "\nRES: ", args.value