mirror of
https://github.com/logos-storage/apatheia.git
synced 2026-02-10 08:03:07 +00:00
rename queue
This commit is contained in:
parent
a7465c753f
commit
e4242132f7
@ -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()
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user