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