diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2017-09-20 00:59:54 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2017-09-20 00:59:54 +0200 |
| commit | 39cfa9724c71796f2c16aa1ed90fbd4425540eef (patch) | |
| tree | 920ef35be300be4681ffd63f016f4db51ddd543e /opendc-omega/src/main/kotlin/nl/atlarge/opendc/kernel/omega/OmegaSimulation.kt | |
| parent | 90e0d1d8c6ab94e020dd4cb4831a369b270a69b7 (diff) | |
Add support for timeouts on receive calls
This change allows processes to set a timeout when waiting for a message
to arrive.
Diffstat (limited to 'opendc-omega/src/main/kotlin/nl/atlarge/opendc/kernel/omega/OmegaSimulation.kt')
| -rw-r--r-- | opendc-omega/src/main/kotlin/nl/atlarge/opendc/kernel/omega/OmegaSimulation.kt | 33 |
1 files changed, 32 insertions, 1 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 ec7701e7..65d9dd60 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 @@ -101,8 +101,14 @@ internal class OmegaSimulation(override val kernel: OmegaKernel, override val to // Tick has already occurred logger.warn { "message processed out of order" } } + // Remove the message from the queue queue.poll() + // If the sender has canceled the message, we move on to the next message + if (envelope.canceled) { + continue + } + val context = registry[envelope.destination] ?: continue if (envelope.message !is Interrupt) { @@ -244,7 +250,9 @@ internal class OmegaSimulation(override val kernel: OmegaKernel, override val to } /** - * Retrieves and removes a single message from this channel suspending the caller while the channel is empty. + * Retrieve and removes a single message from the entity's mailbox, suspending the function if the mailbox is empty. + * The execution is resumed after the message has landed in the entity's mailbox after which the message [Envelope] + * is mapped through `block` to generate a processed message. * * @param block The block to process the message with. * @return The processed message. @@ -255,6 +263,29 @@ internal class OmegaSimulation(override val kernel: OmegaKernel, override val to } /** + * Retrieve and removes a single message from the entity's mailbox, suspending the function if the mailbox is empty. + * The execution is resumed after the message has landed in the entity's mailbox or the timeout was reached, + * + * If the message has been received, the message [Envelope] is mapped through `block` to generate a processed + * message. If the timeout was reached, `block` is not called and `null` is returned. + * + * @param timeout The duration to wait before resuming execution. + * @param block The block to process the message with. + * @return The processed message or `null` if the timeout was reached. + */ + suspend override fun <T> receive(timeout: Duration, block: Envelope<*>.(Any) -> T): T? { + val receipt = schedule(Timeout, entity, entity, timeout) + val envelope = receiveEnvelope() + + if (envelope.message !is Timeout) { + receipt.cancel() + return block(envelope, envelope.message) + } + + return null + } + + /** * Send the given message to the specified entity. * * @param msg The message to send. |
