summaryrefslogtreecommitdiff
path: root/opendc-omega/src/main
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-21 23:25:44 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-21 23:25:44 +0200
commita031db367c71ec1604b34f3765198c2196bfe551 (patch)
tree793c4af1a9b0c4f8bbc0d901b2a8ae90ac22d4ee /opendc-omega/src/main
parent735d5543ed72f0c6cf632b35b3f23323cebcf81b (diff)
Create simple datacenter experiment
Diffstat (limited to 'opendc-omega/src/main')
-rw-r--r--opendc-omega/src/main/kotlin/nl/atlarge/opendc/kernel/omega/OmegaSimulation.kt34
1 files changed, 32 insertions, 2 deletions
diff --git a/opendc-omega/src/main/kotlin/nl/atlarge/opendc/kernel/omega/OmegaSimulation.kt b/opendc-omega/src/main/kotlin/nl/atlarge/opendc/kernel/omega/OmegaSimulation.kt
index a29f2b08..67b192fb 100644
--- a/opendc-omega/src/main/kotlin/nl/atlarge/opendc/kernel/omega/OmegaSimulation.kt
+++ b/opendc-omega/src/main/kotlin/nl/atlarge/opendc/kernel/omega/OmegaSimulation.kt
@@ -202,7 +202,7 @@ internal class OmegaSimulation(override val kernel: OmegaKernel, override val to
/**
* The last point in time the process has done some work.
*/
- var last: Instant = 0
+ var last: Instant = -1
/**
* The state of the entity.
@@ -332,7 +332,11 @@ internal class OmegaSimulation(override val kernel: OmegaKernel, override val to
* @param duration The duration of simulation time to wait before resuming execution.
*/
suspend override fun wait(duration: Duration) {
- require(duration > 0) { "The amount of time to suspend must be a non-zero positive number" }
+ require(duration >= 0) { "The amount of time to suspend must be a positive number" }
+
+ if (duration == 0.toLong())
+ return
+
schedule(Resume, entity, entity, duration)
while (true) {
@@ -342,6 +346,32 @@ internal class OmegaSimulation(override val kernel: OmegaKernel, override val to
}
/**
+ * Suspend the [Process] of the [Entity] in simulation for the given duration of simulation time before resuming
+ * execution and push all messages that are received during this period to the given queue.
+ *
+ * A call to this method will not make the [Process] sleep for the actual duration of time, but instead suspend
+ * the process until the no more messages at an earlier point in time have to be processed.
+ *
+ * @param duration The duration of simulation time to wait before resuming execution.
+ * @param queue The mutable queue to push the messages to.
+ */
+ suspend override fun wait(duration: Duration, queue: Queue<Any>) {
+ require(duration >= 0) { "The amount of time to suspend must be a positive number" }
+
+ if (duration == 0.toLong())
+ return
+
+ schedule(Resume, entity, entity, duration)
+
+ while (true) {
+ val msg = receive()
+ if (msg is Resume)
+ return
+ queue.add(msg)
+ }
+ }
+
+ /**
* Update the state of the entity being simulated.
*
* <p>Instead of directly mutating the entity, we create a new instance of the entity to prevent other objects