From 491213dfa0d565f39eac361253f73d840766cf5e Mon Sep 17 00:00:00 2001 From: Eugene Kabanov Date: Tue, 19 Jan 2021 14:48:39 +0200 Subject: [PATCH] Add callIdle() primitive. (#148) * Add callIdle primitive. * Make single idle callback to be processed by single poll() step. Add idleAsync() primitive to allow wait for "idle" time. Refactor some `result` usage. --- chronos/asyncloop.nim | 107 +++++++++++++++++++++++++++++++++--------- 1 file changed, 85 insertions(+), 22 deletions(-) diff --git a/chronos/asyncloop.nim b/chronos/asyncloop.nim index ee8b9d9..d7a98ea 100644 --- a/chronos/asyncloop.nim +++ b/chronos/asyncloop.nim @@ -200,6 +200,7 @@ type PDispatcherBase = ref object of RootRef timers*: HeapQueue[TimerCallback] callbacks*: Deque[AsyncCallback] + idlers*: Deque[AsyncCallback] trackers*: Table[string, TrackerBase] proc `<`(a, b: TimerCallback): bool = @@ -235,17 +236,18 @@ template processTimersGetTimeout(loop, timeout: untyped) = break loop.callbacks.addLast(loop.timers.pop().function) + if loop.timers.len > 0: timeout = (lastFinish - curTime).getAsyncTimestamp() if timeout == 0: - if len(loop.callbacks) == 0: + if (len(loop.callbacks) == 0) and (len(loop.idlers) == 0): when defined(windows): timeout = INFINITE else: timeout = -1 else: - if len(loop.callbacks) != 0: + if (len(loop.callbacks) != 0) or (len(loop.idlers) != 0): timeout = 0 template processTimers(loop: untyped) = @@ -259,6 +261,10 @@ template processTimers(loop: untyped) = break loop.callbacks.addLast(loop.timers.pop().function) +template processIdlers(loop: untyped) = + if len(loop.idlers) > 0: + loop.callbacks.addLast(loop.idlers.popFirst()) + template processCallbacks(loop: untyped) = var count = len(loop.callbacks) for i in 0..