From b29cf8a87b5d4505aa24bf8027247a660e216bb8 Mon Sep 17 00:00:00 2001 From: Stevan Andjelkovic Date: Tue, 3 Aug 2021 11:29:34 +0200 Subject: [PATCH] docs: add Sean's timer to event loop design --- src/runtime-prototype/event-loop.org | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/runtime-prototype/event-loop.org b/src/runtime-prototype/event-loop.org index b31cd657..c65df99b 100644 --- a/src/runtime-prototype/event-loop.org +++ b/src/runtime-prototype/event-loop.org @@ -255,7 +255,7 @@ queue | __ __ __ | -. #+begin_src go func timeoutProducer1(ls loopState) event { now := ls.time.Now() - time, cb := ls.priorityQueue.peek() // TODO: c.f. Sean's timers branch... + time, cb := ls.priorityQueue.peek() if now.After(time) { ls.priorityQueue.pop() return Timeout{callback: &cb} @@ -266,6 +266,19 @@ queue | __ __ __ | -. } } + func timeoutProducerSean(ls loopState) event { + now := ls.time.Now() + time, timerEvent := ls.priorityQueue.peek() + if now.After(time) { + ls.priorityQueue.pop() + return TimeoutSean{timerEvent: timerEvent} + } else { + // NOTE: we can't just `sleep(time - now)` here, because new timeouts + // might be registered in the meantime. + return nil + } + } + func main() { ... ls := ... // the event loop state, contains the event queue etc. @@ -330,6 +343,9 @@ queue | __ __ __ | -. // ... case Timeout: e.callback() + case TimeoutSean: + r := lookupReactor(e.timerEvent.receiver) + r.tick(e.timerEvent) } } } @@ -370,7 +386,7 @@ queue | __ __ __ | -. for { r := posix.select(fds, ...) // one of the fds is ready... figure out which and enqueue appropritate event to queue - // when do we run eventConsumer? + // TODO: when do we run eventConsumer? } } #+end_src go