Scoop API

scoop:scoop.window:0.1.0

src/owner/deadlines.scoop

Selected production source · src/owner/deadlines.scoop

1package scoop.window
3import scoop.collections.sortedBy
4import scoop.platform.time.MonotonicTimeSource
5import scoop.platform.time.MonotonicInstant
7internal class DeadlineDelivery(
8 val deadline: WindowDeadline,
9 val revision: ULong,
10 val time: MonotonicInstant
11) {
12 fun deliver() {
13 deadline.deliver(revision)
14 }
17internal fun ApplicationOwner.takeDeadlines(now: MonotonicInstant): Array<DeadlineDelivery> {
18 val ready = ArrayList<DeadlineDelivery>()
19 for (deadline in deadlines) {
20 when (deadline.scheduled) {
21 case Some(val time) ->
22 if (time <= now) {
23 ready.add(DeadlineDelivery(deadline, deadline.revision, time))
24 deadline.scheduled = None
25 }
26 case None -> {}
27 }
28 }
29 return ready.sortedBy<DeadlineDelivery, MonotonicInstant> { item -> item.time }.toArray()
32internal fun ApplicationOwner.refreshWindowDeadlines(policy: MonotonicInstant?, force: Boolean) {
33 val pointer = native ?: return
34 var next: MonotonicInstant? = if (exiting) None else policy
35 if (!exiting) {
36 for (deadline in deadlines) {
37 when (deadline.scheduled) {
38 case Some(val time) -> if (next == None || time < next!!) next = Some(time)
39 case None -> {}
40 }
41 }
42 }
43 if (!force && next == armedDeadline) return
44 val seconds = when (next) {
45 case None -> -1.0
47 case Some(val time) -> {
48 val now = MonotonicTimeSource.now()
49 if (time <= now)
50 0.0
51 else {
52 val remaining = time - now
53 remaining.floorSeconds.toDouble() +
54 remaining.nanosecondsOfSecond.toDouble() /
55 1000000000.0
56 }
57 }
58 }
59 armedDeadline = next
60 @Unsafe
61 {
62 nativeApplicationDeadline(pointer, seconds)
63 }
66internal fun ApplicationOwner.retireDeadlines(window: WindowOwner) {
67 var index = 0L
68 while (index < deadlines.size) {
69 val deadline = deadlines[index]
70 if (deadline.window === window) {
71 deadline.retire()
72 deadlines.removeAt(index)
73 } else
74 index += 1L
75 }
76 refreshDeadline()