Scoop API

scoop:scoop.asyncio:0.1.0

src/loop/scheduled_timer.scoop

Selected production source · src/loop/scheduled_timer.scoop

1package scoop.asyncio
3import scoop.platform.time.MonotonicTimeSource
4import scoop.time.Duration
6internal fun scheduleOnLoop(
7 loop: LoopOwner,
8 duration: Duration,
9 completion: (Throwable?) -> Unit
10): Disposable {
11 val job = loop.requireJob()
12 if (duration < Duration.ZERO)
13 throw IllegalArgumentException(Some("negative timer duration"))
14 val timer = LoopTimer(loop, duration, false, completion)
15 timer.start(job)
16 return timer
19private enum TimerChoice {
20 Pending, Stopped(Throwable?), Completed
23// The heap position belongs only to the owner; result selection can race with another thread.
24internal class LoopTimer(
25 private val loop: LoopOwner,
26 duration: Duration,
27 private val notifyCancellation: Boolean,
28 completion: (Throwable?) -> Unit
29): Disposable {
30 val deadline = LoopDeadline(MonotonicTimeSource.markNow(), duration)
31 var heapIndex = -1L
32 private val lock = IoLock()
33 private var choice = TimerChoice.Pending
34 private var callback: ((Throwable?) -> Unit)? = Some(completion)
35 private var subscription: Disposable? = None
37 private fun pending(): Boolean =
38 when (choice) {
39 case Pending -> true
40 else -> false
41 }
43 val isPending: Boolean get() = lock.withLock { pending() }
45 fun start(job: Job) {
46 val listener =
47 job.invokeOnCancellation {
48 val error: Throwable? =
49 if (notifyCancellation)
50 Some(CancellationException())
51 else
52 None
53 if (selectStop(error))
54 loop.cancelTimer(this)
55 }
56 val stopped =
57 lock.withLock {
58 if (pending()) {
59 subscription = Some(listener)
60 false
61 } else
62 true
63 }
64 if (stopped)
65 listener.dispose()
66 loop.scheduleTimer(this)
67 }
69 private fun selectStop(error: Throwable?): Boolean {
70 val result: (Boolean, Disposable?) =
71 lock.withLock {
72 if (!pending())
73 return@withLock (false, None)
74 choice = TimerChoice.Stopped(error)
75 if (!notifyCancellation)
76 callback = None
77 val saved = subscription
78 subscription = None;
79 (true, saved)
80 }
81 val(selected, listener) = result
82 listener?.dispose()
83 return selected
84 }
86 public override fun dispose() {
87 if (selectStop(None))
88 loop.cancelTimer(this)
89 }
91 fun closeFromLoop() {
92 selectStop(
93 if (notifyCancellation)
94 Some(IoClosedException())
95 else
96 None
97 )
98 }
100 // Selection happens before queuing the callback, so a later cancellation cannot replace it.
101 fun notification(error: Throwable?): (() -> Unit)? {
102 val result: (((Throwable?) -> Unit)?, Disposable?, Throwable?) =
103 lock.withLock {
104 val selected =
105 when (choice) {
106 case Completed -> return@withLock (None, None, error)
107 case Stopped(val stopped) -> stopped
108 case Pending -> error
109 }
110 choice = TimerChoice.Completed
111 val saved = (callback, subscription, selected)
112 callback = None
113 subscription = None
114 saved
115 }
116 val(handler, listener, failure) = result
117 listener?.dispose()
118 return when (handler) {
119 case Some(val notify) -> Some(
120 {
121 try {
122 notify(failure)
123 }
124 catch (error: Throwable) {
125 loop.cleanupFailed(error)
126 loop.cancelRoot()
127 }
128 }
129 )
130 case None -> None
131 }
132 }