mirror of
https://github.com/logos-storage/apatheia.git
synced 2026-01-05 22:43:10 +00:00
rename queue
This commit is contained in:
parent
ac83bb60e6
commit
a7465c753f
@ -1,12 +1,15 @@
|
|||||||
import std/options
|
import std/options
|
||||||
import ./types
|
import ./types
|
||||||
|
|
||||||
|
import results
|
||||||
import chronos
|
import chronos
|
||||||
import results
|
import results
|
||||||
import chronos/threadsync
|
import chronos/threadsync
|
||||||
|
|
||||||
export types
|
export types
|
||||||
export options
|
export options
|
||||||
|
export threadsync
|
||||||
|
export chronos
|
||||||
|
|
||||||
type
|
type
|
||||||
ChanPtr[T] = ptr Channel[T]
|
ChanPtr[T] = ptr Channel[T]
|
||||||
@ -22,16 +25,24 @@ type
|
|||||||
proc newSignalQueue*[T](): SignalQueue[T] {.raises: [ApatheiaSignalErr].} =
|
proc newSignalQueue*[T](): SignalQueue[T] {.raises: [ApatheiaSignalErr].} =
|
||||||
let res = ThreadSignalPtr.new()
|
let res = ThreadSignalPtr.new()
|
||||||
if res.isErr():
|
if res.isErr():
|
||||||
raise newException(ApatheiaSignalErr, msg: res.err())
|
raise newException(ApatheiaSignalErr, res.error())
|
||||||
result.signal = res.get()
|
result.signal = res.get()
|
||||||
result.chan = allocSharedChannel()
|
result.chan = allocSharedChannel[T]()
|
||||||
|
|
||||||
proc send*[T](c: SignalQueue[T], msg: sink T) {.inline.} =
|
proc send*[T](c: SignalQueue[T], msg: sink T): Result[void, string] {.raises: [].} =
|
||||||
## Sends a message to a thread. `msg` is copied.
|
## Sends a message to a thread. `msg` is copied.
|
||||||
c.chan.send(msg)
|
try:
|
||||||
c.signal.fireSync()
|
c.chan[].send(msg)
|
||||||
|
except Exception as exc:
|
||||||
|
result = err exc.msg
|
||||||
|
|
||||||
proc trySend*[T](c: SignalQueue[T], msg: sink T): bool {.inline.} =
|
let res = c.signal.fireSync()
|
||||||
|
if res.isErr():
|
||||||
|
let msg: string = res.error()
|
||||||
|
result = err msg
|
||||||
|
result = ok()
|
||||||
|
|
||||||
|
proc trySend*[T](c: SignalQueue[T], msg: sink T): bool =
|
||||||
result = c.chan.trySend(msg)
|
result = c.chan.trySend(msg)
|
||||||
if result:
|
if result:
|
||||||
c.signal.fireSync()
|
c.signal.fireSync()
|
||||||
@ -44,3 +55,5 @@ proc tryRecv*[T](c: SignalQueue[T]): Option[T] =
|
|||||||
if res.dataAvailable:
|
if res.dataAvailable:
|
||||||
some res.msg
|
some res.msg
|
||||||
|
|
||||||
|
proc wait*[T](c: SignalQueue[T]) {.async.} =
|
||||||
|
await wait(c.signal)
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
|
|
||||||
type
|
type
|
||||||
ApatheiaException* = ref object of CatchableError
|
ApatheiaException* = object of CatchableError
|
||||||
ApatheiaSignalErr* = ref object of ApatheiaException
|
ApatheiaSignalErr* = object of ApatheiaException
|
||||||
|
|||||||
@ -15,27 +15,23 @@ type
|
|||||||
doneSig: ThreadSignalPtr
|
doneSig: ThreadSignalPtr
|
||||||
value: float
|
value: float
|
||||||
|
|
||||||
proc addNums(a, b: float, ret: AsyncQueue[float]) =
|
proc addNums(a, b: float, queue: SignalQueue[float]) =
|
||||||
ret.value = a + b
|
|
||||||
os.sleep(500)
|
os.sleep(500)
|
||||||
let res = ret.doneSig.fireSync().get()
|
discard queue.send(a + b)
|
||||||
if not res:
|
|
||||||
echo "ERROR FIRING!"
|
|
||||||
|
|
||||||
suite "async tests":
|
suite "async tests":
|
||||||
|
|
||||||
var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads.
|
var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads.
|
||||||
var queue = newAsyncQueue[float]()
|
var queue = newSignalQueue[float]()
|
||||||
|
|
||||||
asyncTest "test":
|
asyncTest "test":
|
||||||
var args = ThreadArg()
|
|
||||||
args.doneSig = ThreadSignalPtr.new().get()
|
|
||||||
|
|
||||||
tp.spawn addNums(1, 2, addr args)
|
tp.spawn addNums(1.0, 2.0, queue)
|
||||||
|
|
||||||
# await sleepAsync(100.milliseconds)
|
# await sleepAsync(100.milliseconds)
|
||||||
await wait(args.doneSig).wait(1500.milliseconds)
|
await wait(queue).wait(1500.milliseconds)
|
||||||
|
|
||||||
echo "\nRES: ", args.value
|
# echo "\nRES: ", args.value
|
||||||
|
|
||||||
check true
|
check true
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user