Scoop API

scoop:scoop.asyncio:0.1.0

src/loop/loop_timers.scoop

Selected production source · src/loop/loop_timers.scoop

1package scoop.asyncio
3import scoop.platform.time.MonotonicTimeSource
4import scoop.time.Duration
6// The owner keeps logical timers separate from the one native wakeup shared by them.
7internal class LoopTimers(private val owner: LoopOwner) {
8 private val pending = LoopTimerHeap()
9 private var wakeup: LoopTimerWakeup? = None
10 private var closed = false
12 val isEmpty: Boolean get() = pending.size == 0L
14 fun add(timer: LoopTimer) {
15 if (closed) {
16 timer.closeFromLoop()
17 notify(timer, None)
18 } else
19 if (timer.isPending) {
20 pending.add(timer)
21 }
22 }
24 fun cancel(timer: LoopTimer) {
25 pending.remove(timer)
26 notify(timer, None)
27 }
29 private fun notify(timer: LoopTimer, error: Throwable?) {
30 when (timer.notification(error)) {
31 case Some(val notification) -> owner.enqueueReady(notification)
32 case None -> {}
33 }
34 }
36 fun advance() {
37 if (closed || pending.size == 0L)
38 return
39 val now = MonotonicTimeSource.markNow()
40 while (pending.size != 0L) {
41 val first = pending.first()
42 if (!first.deadline.expired(now))
43 break
44 pending.remove(first)
45 notify(first, None)
46 }
47 if (pending.size == 0L)
48 return
49 val next = pending.first().deadline
50 when (wakeup) {
51 case Some(val active) -> if (!next.before(active.deadline))
52 return
53 case None -> {}
54 }
55 val previous = wakeup
56 val replacement = LoopTimerWakeup(owner, this, next)
57 wakeup = Some(replacement)
58 previous?.operation?.requestStop()
59 owner.submit(replacement.operation)
60 }
62 fun completed(wake: LoopTimerWakeup, error: Throwable?) {
63 when (wakeup) {
64 case Some(val active) -> if (active !== wake)
65 return
66 case None -> return
67 }
68 wakeup = None
69 when (error) {
70 case Some(_) -> {
71 while (pending.size != 0L) {
72 val first = pending.first()
73 pending.remove(first)
74 notify(first, error)
75 }
76 }
77 case None -> {}
78 }
79 }
81 fun close() {
82 if (closed)
83 return
84 closed = true
85 while (pending.size != 0L) {
86 val first = pending.first()
87 pending.remove(first)
88 first.closeFromLoop()
89 notify(first, None)
90 }
91 val active = wakeup
92 wakeup = None
93 active?.operation?.requestStop()
94 }
97// Superseded wakeups retain only the queue and their deadline until native retirement.
98internal class LoopTimerWakeup(owner: LoopOwner, timers: LoopTimers, val deadline: LoopDeadline) {
99 val operation =
100 CallbackOperation(
101 owner,
102 owner.nextOperation(),
103 {
104 native, id ->
105 val remaining = deadline.remaining(MonotonicTimeSource.markNow())
106 if (remaining <= Duration.ZERO)
107 return@CallbackOperation IoStart.Finished(NativeEvent(id, IoResult.Done))
108 @Unsafe
109 {
110 return@CallbackOperation nativeStatus(
111 id,
112 nativeTimerStart(
113 native,
114 id,
115 remaining.floorSeconds,
116 remaining.nanosecondsOfSecond
117 )
118 )
119 }
120 },
121 {
122 native,
123 id -> @Unsafe
124 {
125 nativeTimerCancel(native, id)
126 }
127 },
128 {
129 event,
130 failure -> timers.completed(this, completionFailure("timer", event.result, failure))
131 }
132 )