summaryrefslogtreecommitdiff
path: root/opendc-simulator/opendc-simulator-compute/src/main/kotlin
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2022-10-28 11:48:59 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2022-10-28 12:03:38 +0200
commit3528091684f610d80fcebb5b730d3a201e79a99a (patch)
treed39ddff0957924b1a9cfbcc5cd2a4fd5f3349798 /opendc-simulator/opendc-simulator-compute/src/main/kotlin
parentcdc7df3c3d398a7af15014b4c0f6cd495c05fcce (diff)
feat(sim/compute): Add completion parameter to startWorkload
This change updates the interface of `SimMachine#startWorkload` to introduce a parameter `completion` that is invoked when the workload completes either succesfully or due to failure. This functionality has often been implemented by wrapping a `SimWorkload` and catching its exceptions. However, since this functionality is used in all usages of `SimMachine#startWorkload` we instead embed it into `SimMachine` itself.
Diffstat (limited to 'opendc-simulator/opendc-simulator-compute/src/main/kotlin')
-rw-r--r--opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/Coroutines.kt29
1 files changed, 3 insertions, 26 deletions
diff --git a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/Coroutines.kt b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/Coroutines.kt
index c23f48dc..b354caff 100644
--- a/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/Coroutines.kt
+++ b/opendc-simulator/opendc-simulator-compute/src/main/kotlin/org/opendc/simulator/compute/Coroutines.kt
@@ -39,31 +39,8 @@ public suspend fun SimMachine.runWorkload(workload: SimWorkload, meta: Map<Strin
return suspendCancellableCoroutine { cont ->
cont.invokeOnCancellation { this@runWorkload.cancel() }
- startWorkload(
- object : SimWorkload {
- override fun onStart(ctx: SimMachineContext) {
- try {
- workload.onStart(ctx)
- } catch (cause: Throwable) {
- cont.resumeWithException(cause)
- throw cause
- }
- }
-
- override fun onStop(ctx: SimMachineContext) {
- try {
- workload.onStop(ctx)
-
- if (!cont.isCompleted) {
- cont.resume(Unit)
- }
- } catch (cause: Throwable) {
- cont.resumeWithException(cause)
- throw cause
- }
- }
- },
- meta
- )
+ startWorkload(workload, meta) { cause ->
+ if (cause != null) cont.resumeWithException(cause) else cont.resume(Unit)
+ }
}
}