summaryrefslogtreecommitdiff
path: root/opendc-kernel-omega
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2018-02-16 15:25:19 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2018-02-18 23:38:35 +0100
commitf8a4095d1824df095ea91253f914bc0512646684 (patch)
tree1966b6f341cbaf634ec7c55fcaf1f195a6d45a73 /opendc-kernel-omega
parentb84a995a05fecb9ef90c9184959f285f324e7411 (diff)
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.
Diffstat (limited to 'opendc-kernel-omega')
-rw-r--r--opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/Messages.kt2
-rw-r--r--opendc-kernel-omega/src/main/kotlin/com/atlarge/opendc/omega/OmegaSimulation.kt13
2 files changed, 11 insertions, 4 deletions
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<in M>(val process: Process<*, M>)
+data class Launch<M>(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<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot
/**
* This internal class provides the default implementation for the [Context] interface for this simulator.
*/
- private inner class OmegaContext<S>(val process: Process<S, M>) : Context<S, M>, Continuation<Unit> {
+ private inner class OmegaContext<S>(val process: Process<S, M>) : Context<S, M>, Continuation<Unit>,
+ AbstractCoroutineContextElement(Context) {
/**
* The model in which the process exists.
*/
@@ -256,6 +257,12 @@ internal class OmegaSimulation<M>(bootstrap: Bootstrap<M>) : Simulation<M>, Boot
get() = this@OmegaSimulation.time
/**
+ * The [Entity] associated with this context.
+ */
+ override val self: Entity<S, M>
+ 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<M>(bootstrap: Bootstrap<M>) : Simulation<M>, 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<M>(bootstrap: Bootstrap<M>) : Simulation<M>, 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" }