mirror of
https://github.com/status-im/nim-chronos.git
synced 2025-03-03 20:30:39 +00:00
Add peak to AsyncQueue.
This commit is contained in:
parent
c04576d829
commit
925e6ca9b4
@ -257,6 +257,12 @@ proc popLastImpl[T](aq: AsyncQueue[T]): T =
|
|||||||
aq.putters.wakeupNext()
|
aq.putters.wakeupNext()
|
||||||
res
|
res
|
||||||
|
|
||||||
|
proc peakFirstImpl[T](aq: AsyncQueue[T]): T =
|
||||||
|
aq.queue.peekFirst()
|
||||||
|
|
||||||
|
proc peakLastImpl[T](aq: AsyncQueue[T]): T =
|
||||||
|
aq.queue.peekLast()
|
||||||
|
|
||||||
proc addFirstNoWait*[T](aq: AsyncQueue[T], item: T) {.
|
proc addFirstNoWait*[T](aq: AsyncQueue[T], item: T) {.
|
||||||
raises: [AsyncQueueFullError].} =
|
raises: [AsyncQueueFullError].} =
|
||||||
## Put an item ``item`` to the beginning of the queue ``aq`` immediately.
|
## Put an item ``item`` to the beginning of the queue ``aq`` immediately.
|
||||||
@ -293,6 +299,26 @@ proc popLastNoWait*[T](aq: AsyncQueue[T]): T {.
|
|||||||
raise newException(AsyncQueueEmptyError, "AsyncQueue is empty!")
|
raise newException(AsyncQueueEmptyError, "AsyncQueue is empty!")
|
||||||
aq.popLastImpl()
|
aq.popLastImpl()
|
||||||
|
|
||||||
|
proc peakFirstNoWait*[T](aq: AsyncQueue[T]): T {.
|
||||||
|
raises: [AsyncQueueEmptyError].} =
|
||||||
|
## Get an item from the beginning of the queue ``aq`` immediately but without
|
||||||
|
## removing it.
|
||||||
|
##
|
||||||
|
## If queue ``aq`` is empty, then ``AsyncQueueEmptyError`` exception raised.
|
||||||
|
if aq.empty():
|
||||||
|
raise newException(AsyncQueueEmptyError, "AsyncQueue is empty!")
|
||||||
|
aq.peakFirstImpl()
|
||||||
|
|
||||||
|
proc peakLastNoWait*[T](aq: AsyncQueue[T]): T {.
|
||||||
|
raises: [AsyncQueueEmptyError].} =
|
||||||
|
## Get an item from the end of the queue ``aq`` immediately but without
|
||||||
|
## removing it.
|
||||||
|
##
|
||||||
|
## If queue ``aq`` is empty, then ``AsyncQueueEmptyError`` exception raised.
|
||||||
|
if aq.empty():
|
||||||
|
raise newException(AsyncQueueEmptyError, "AsyncQueue is empty!")
|
||||||
|
aq.peakLastImpl()
|
||||||
|
|
||||||
proc addFirst*[T](aq: AsyncQueue[T], item: T) {.
|
proc addFirst*[T](aq: AsyncQueue[T], item: T) {.
|
||||||
async: (raises: [CancelledError]).} =
|
async: (raises: [CancelledError]).} =
|
||||||
## Put an ``item`` to the beginning of the queue ``aq``. If the queue is full,
|
## Put an ``item`` to the beginning of the queue ``aq``. If the queue is full,
|
||||||
@ -357,6 +383,38 @@ proc popLast*[T](aq: AsyncQueue[T]): Future[T] {.
|
|||||||
raise exc
|
raise exc
|
||||||
aq.popLastImpl()
|
aq.popLastImpl()
|
||||||
|
|
||||||
|
proc peakFirst*[T](aq: AsyncQueue[T]): Future[T] {.
|
||||||
|
async: (raises: [CancelledError]).} =
|
||||||
|
## Return an ``item`` without removing it from the beginning of the queue
|
||||||
|
## ``aq``. If the queue is empty, wait until an item is available.
|
||||||
|
while aq.empty():
|
||||||
|
let getter =
|
||||||
|
Future[void].Raising([CancelledError]).init("AsyncQueue.peakFirst")
|
||||||
|
aq.getters.add(getter)
|
||||||
|
try:
|
||||||
|
await getter
|
||||||
|
except CancelledError as exc:
|
||||||
|
if not(aq.empty()) and not(getter.cancelled()):
|
||||||
|
aq.getters.wakeupNext()
|
||||||
|
raise exc
|
||||||
|
aq.peakFirstImpl()
|
||||||
|
|
||||||
|
proc peakLast*[T](aq: AsyncQueue[T]): Future[T] {.
|
||||||
|
async: (raises: [CancelledError]).} =
|
||||||
|
## Return an ``item`` without removing it from the end of the queue ``aq``.
|
||||||
|
## If the queue is empty, wait until an item is available.
|
||||||
|
while aq.empty():
|
||||||
|
let getter =
|
||||||
|
Future[void].Raising([CancelledError]).init("AsyncQueue.peakLast")
|
||||||
|
aq.getters.add(getter)
|
||||||
|
try:
|
||||||
|
await getter
|
||||||
|
except CancelledError as exc:
|
||||||
|
if not(aq.empty()) and not(getter.cancelled()):
|
||||||
|
aq.getters.wakeupNext()
|
||||||
|
raise exc
|
||||||
|
aq.peakLastImpl()
|
||||||
|
|
||||||
proc putNoWait*[T](aq: AsyncQueue[T], item: T) {.
|
proc putNoWait*[T](aq: AsyncQueue[T], item: T) {.
|
||||||
raises: [AsyncQueueFullError].} =
|
raises: [AsyncQueueFullError].} =
|
||||||
## Alias of ``addLastNoWait()``.
|
## Alias of ``addLastNoWait()``.
|
||||||
@ -367,6 +425,11 @@ proc getNoWait*[T](aq: AsyncQueue[T]): T {.
|
|||||||
## Alias of ``popFirstNoWait()``.
|
## Alias of ``popFirstNoWait()``.
|
||||||
aq.popFirstNoWait()
|
aq.popFirstNoWait()
|
||||||
|
|
||||||
|
proc peakNoWait*[T](aq: AsyncQueue[T]): T {.
|
||||||
|
raises: [AsyncQueueEmptyError].} =
|
||||||
|
## Alias of ``peakFirstNoWait()``.
|
||||||
|
aq.peakFirstNoWait()
|
||||||
|
|
||||||
proc put*[T](aq: AsyncQueue[T], item: T): Future[void] {.
|
proc put*[T](aq: AsyncQueue[T], item: T): Future[void] {.
|
||||||
async: (raw: true, raises: [CancelledError]).} =
|
async: (raw: true, raises: [CancelledError]).} =
|
||||||
## Alias of ``addLast()``.
|
## Alias of ``addLast()``.
|
||||||
@ -377,6 +440,11 @@ proc get*[T](aq: AsyncQueue[T]): Future[T] {.
|
|||||||
## Alias of ``popFirst()``.
|
## Alias of ``popFirst()``.
|
||||||
aq.popFirst()
|
aq.popFirst()
|
||||||
|
|
||||||
|
proc peak*[T](aq: AsyncQueue[T]): Future[T] {.
|
||||||
|
async: (raw: true, raises: [CancelledError]).} =
|
||||||
|
## Alias of ``peakFirst()``.
|
||||||
|
aq.peakFirst()
|
||||||
|
|
||||||
proc clear*[T](aq: AsyncQueue[T]) {.inline.} =
|
proc clear*[T](aq: AsyncQueue[T]) {.inline.} =
|
||||||
## Clears all elements of queue ``aq``.
|
## Clears all elements of queue ``aq``.
|
||||||
aq.queue.clear()
|
aq.queue.clear()
|
||||||
|
@ -353,6 +353,19 @@ suite "Asynchronous sync primitives test suite":
|
|||||||
test "AsyncQueue() contains test":
|
test "AsyncQueue() contains test":
|
||||||
check test9() == true
|
check test9() == true
|
||||||
|
|
||||||
|
test "AsyncQueue() peak test":
|
||||||
|
let q = newAsyncQueue[int]()
|
||||||
|
q.putNoWait(1)
|
||||||
|
q.putNoWait(2)
|
||||||
|
|
||||||
|
check:
|
||||||
|
q.peakNoWait() == 1
|
||||||
|
q.peakFirstNoWait() == 1
|
||||||
|
q.peakLastNoWait() == 2
|
||||||
|
(waitFor q.peak()) == 1
|
||||||
|
(waitFor q.peakFirst()) == 1
|
||||||
|
(waitFor q.peakLast()) == 2
|
||||||
|
|
||||||
test "AsyncEventQueue() behavior test":
|
test "AsyncEventQueue() behavior test":
|
||||||
let eventQueue = newAsyncEventQueue[int]()
|
let eventQueue = newAsyncEventQueue[int]()
|
||||||
let key = eventQueue.register()
|
let key = eventQueue.register()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user