scoop:scoop.asyncio:0.1.0
src/loop/loop_timers.scoop
Selected production source · src/loop/loop_timers.scoop
1
package scoop.asyncio3
import scoop.platform.time.MonotonicTimeSource4
import scoop.time.Duration6
// The owner keeps logical timers separate from the one native wakeup shared by them.7
internal class LoopTimers(private val owner: LoopOwner) {8
private val pending = LoopTimerHeap()9
private var wakeup: LoopTimerWakeup? = None10
private var closed = false12
val isEmpty: Boolean get() = pending.size == 0L14
fun add(timer: LoopTimer) {15
if (closed) {16
timer.closeFromLoop()17
notify(timer, None)18
} else19
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
return39
val now = MonotonicTimeSource.markNow()40
while (pending.size != 0L) {41
val first = pending.first()42
if (!first.deadline.expired(now))43
break44
pending.remove(first)45
notify(first, None)46
}47
if (pending.size == 0L)48
return49
val next = pending.first().deadline50
when (wakeup) {51
case Some(val active) -> if (!next.before(active.deadline))52
return53
case None -> {}54
}55
val previous = wakeup56
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
return66
case None -> return67
}68
wakeup = None69
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
return84
closed = true85
while (pending.size != 0L) {86
val first = pending.first()87
pending.remove(first)88
first.closeFromLoop()89
notify(first, None)90
}91
val active = wakeup92
wakeup = None93
active?.operation?.requestStop()94
}95
}97
// Superseded wakeups retain only the queue and their deadline until native retirement.98
internal 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
@Unsafe109
{110
return@CallbackOperation nativeStatus(111
id,112
nativeTimerStart(113
native,114
id,115
remaining.floorSeconds,116
remaining.nanosecondsOfSecond117
)118
)119
}120
},121
{122
native,123
id -> @Unsafe124
{125
nativeTimerCancel(native, id)126
}127
},128
{129
event,130
failure -> timers.completed(this, completionFailure("timer", event.result, failure))131
}132
)133
}