Skip to content

Commit

Permalink
docs: add Sean's timer to event loop design
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Aug 3, 2021
1 parent 860d783 commit b29cf8a
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/runtime-prototype/event-loop.org
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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.
Expand Down Expand Up @@ -330,6 +343,9 @@ queue | __ __ __ | -.
// ...
case Timeout:
e.callback()
case TimeoutSean:
r := lookupReactor(e.timerEvent.receiver)
r.tick(e.timerEvent)
}
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b29cf8a

Please sign in to comment.