From f8a4095d1824df095ea91253f914bc0512646684 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 16 Feb 2018 15:25:19 +0100 Subject: refactor(#18): Provide access to process context in nested calls This change provides a method in the standard library to access the process context in nested suspending function calls. --- .../src/main/kotlin/com/atlarge/opendc/omega/Messages.kt | 2 +- .../main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'opendc-kernel-omega/src/main') diff --git a/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/Messages.kt b/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/Messages.kt index 73c3676f..d63a53c8 100644 --- a/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/Messages.kt +++ b/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/Messages.kt @@ -27,4 +27,4 @@ object Timeout * * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl) */ -data class Launch(val process: Process<*, M>) +data class Launch(val process: Process<*, M>) diff --git a/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt b/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt index bd3f4529..22382ccd 100644 --- a/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt +++ b/opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt @@ -242,7 +242,8 @@ internal class OmegaSimulation(bootstrap: Bootstrap) : Simulation, Boot /** * This internal class provides the default implementation for the [Context] interface for this simulator. */ - private inner class OmegaContext(val process: Process) : Context, Continuation { + private inner class OmegaContext(val process: Process) : Context, Continuation, + AbstractCoroutineContextElement(Context) { /** * The model in which the process exists. */ @@ -255,6 +256,12 @@ internal class OmegaSimulation(bootstrap: Bootstrap) : Simulation, Boot override val time: Instant get() = this@OmegaSimulation.time + /** + * The [Entity] associated with this context. + */ + override val self: Entity + get() = process + /** * The duration between the current point in simulation time and the last point in simulation time where the * [Context] has executed some work. @@ -281,7 +288,7 @@ internal class OmegaSimulation(bootstrap: Bootstrap) : Simulation, Boot /** * The [CoroutineContext] for a [Context]. */ - override val context: CoroutineContext = EmptyCoroutineContext + override val context: CoroutineContext = this /** * The continuation to resume the execution of the process. @@ -321,7 +328,7 @@ internal class OmegaSimulation(bootstrap: Bootstrap) : Simulation, Boot override suspend fun Entity<*, *>.send(msg: Any, sender: Entity<*, *>, delay: Duration) = schedule(prepare(msg, this, sender, delay)) - override suspend fun Entity<*, *>.interrupt() = send(Interrupt) + override suspend fun Entity<*, *>.interrupt(interrupt: Interrupt) = send(interrupt) override suspend fun hold(duration: Duration) { require(duration >= 0) { "The amount of time to hold must be a positive number" } -- cgit v1.2.3