diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-03-17 16:23:48 +0100 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2021-03-17 16:23:48 +0100 |
| commit | bb3b8e207a08edff81b8c2fe30b476c94bfea086 (patch) | |
| tree | ee739cf4092a2b807e0043bed7cae72cff7b6bac /simulator/opendc-utils/src/main/kotlin/org | |
| parent | 9ab482d0afd773703f78d51a2ba8a160896f03c6 (diff) | |
simulator: Make hypervisors generic for the resource type
This change moves the hypervisor implementations to the
opendc-simulator-resources module and makes them generic to the resource
type that is being used (e.g., CPU, disk or networking).
Diffstat (limited to 'simulator/opendc-utils/src/main/kotlin/org')
| -rw-r--r-- | simulator/opendc-utils/src/main/kotlin/org/opendc/utils/TimerScheduler.kt | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/TimerScheduler.kt b/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/TimerScheduler.kt index 9f40f26a..49964938 100644 --- a/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/TimerScheduler.kt +++ b/simulator/opendc-utils/src/main/kotlin/org/opendc/utils/TimerScheduler.kt @@ -22,24 +22,28 @@ package org.opendc.utils -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.sendBlocking -import kotlinx.coroutines.launch import kotlinx.coroutines.selects.select import java.time.Clock import java.util.* +import kotlin.coroutines.CoroutineContext import kotlin.math.max /** * A TimerScheduler facilitates scheduled execution of future tasks. * - * @property coroutineScope The [CoroutineScope] to run the tasks in. + * @property context The [CoroutineContext] to run the tasks with. * @property clock The clock to keep track of the time. */ @OptIn(ExperimentalCoroutinesApi::class) -public class TimerScheduler<T>(private val coroutineScope: CoroutineScope, private val clock: Clock) : AutoCloseable { +public class TimerScheduler<T>(context: CoroutineContext, private val clock: Clock) : AutoCloseable { + /** + * The scope in which the scheduler runs. + */ + private val scope = CoroutineScope(context + Job()) + /** * A priority queue containing the tasks to be scheduled in the future. */ @@ -58,7 +62,7 @@ public class TimerScheduler<T>(private val coroutineScope: CoroutineScope, priva /** * The scheduling job. */ - private val job = coroutineScope.launch { + private val job = scope.launch { val timers = timers val queue = queue val clock = clock @@ -71,7 +75,7 @@ public class TimerScheduler<T>(private val coroutineScope: CoroutineScope, priva val delay = next?.let { max(0L, it - clock.millis()) } ?: return@select onTimeout(delay) { - while (queue.isNotEmpty()) { + while (queue.isNotEmpty() && isActive) { val timer = queue.peek() val timestamp = clock.millis() @@ -86,7 +90,11 @@ public class TimerScheduler<T>(private val coroutineScope: CoroutineScope, priva if (!timer.isCancelled) { timers.remove(timer.key) - timer() + try { + timer() + } catch (e: Throwable) { + Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e) + } } } @@ -101,7 +109,7 @@ public class TimerScheduler<T>(private val coroutineScope: CoroutineScope, priva */ override fun close() { cancelAll() - job.cancel() + scope.cancel() } /** |
