rename queue

This commit is contained in:
Jaremy Creechley 2024-02-09 21:49:43 -07:00
parent a7465c753f
commit e4242132f7
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
2 changed files with 11 additions and 8 deletions

View File

@ -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()

View File

@ -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