scoop:scoop.asyncio:0.1.0
src/loop/scheduled_timer.scoop
Selected production source · src/loop/scheduled_timer.scoop
1
package scoop.asyncio3
import scoop.platform.time.MonotonicTimeSource4
import scoop.time.Duration6
internal fun scheduleOnLoop(7
loop: LoopOwner,8
duration: Duration,9
completion: (Throwable?) -> Unit10
): 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 timer17
}19
private enum TimerChoice {20
Pending, Stopped(Throwable?), Completed21
}23
// The heap position belongs only to the owner; result selection can race with another thread.24
internal class LoopTimer(25
private val loop: LoopOwner,26
duration: Duration,27
private val notifyCancellation: Boolean,28
completion: (Throwable?) -> Unit29
): Disposable {30
val deadline = LoopDeadline(MonotonicTimeSource.markNow(), duration)31
var heapIndex = -1L32
private val lock = IoLock()33
private var choice = TimerChoice.Pending34
private var callback: ((Throwable?) -> Unit)? = Some(completion)35
private var subscription: Disposable? = None37
private fun pending(): Boolean =38
when (choice) {39
case Pending -> true40
else -> false41
}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
else52
None53
if (selectStop(error))54
loop.cancelTimer(this)55
}56
val stopped =57
lock.withLock {58
if (pending()) {59
subscription = Some(listener)60
false61
} else62
true63
}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 = None77
val saved = subscription78
subscription = None;79
(true, saved)80
}81
val(selected, listener) = result82
listener?.dispose()83
return selected84
}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
else96
None97
)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) -> stopped108
case Pending -> error109
}110
choice = TimerChoice.Completed111
val saved = (callback, subscription, selected)112
callback = None113
subscription = None114
saved115
}116
val(handler, listener, failure) = result117
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 -> None131
}132
}133
}