From 1ed0e64dc405fbb627ff8a7d7b70ac4ff81cd489 Mon Sep 17 00:00:00 2001 From: Kevin Piotrkowski Date: Sat, 11 Aug 2018 19:16:21 -0300 Subject: [PATCH] Add method to check if a mailbox has a message ready to be consumed without having to wait --- include/simgrid/s4u/Mailbox.hpp | 3 +++ src/s4u/s4u_Mailbox.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/simgrid/s4u/Mailbox.hpp b/include/simgrid/s4u/Mailbox.hpp index 8b4267a86f..6dd039475b 100644 --- a/include/simgrid/s4u/Mailbox.hpp +++ b/include/simgrid/s4u/Mailbox.hpp @@ -131,6 +131,9 @@ public: /** Check if there is a communication going on in a mailbox. */ bool listen(); + /** Check if there is a communication ready to be consumed from a mailbox. */ + bool ready(); + /** Gets the first element in the queue (without dequeuing it), or nullptr if none is there */ smx_activity_t front(); diff --git a/src/s4u/s4u_Mailbox.cpp b/src/s4u/s4u_Mailbox.cpp index e33292eb30..b2b60de515 100644 --- a/src/s4u/s4u_Mailbox.cpp +++ b/src/s4u/s4u_Mailbox.cpp @@ -43,6 +43,18 @@ bool Mailbox::listen() return not this->empty() || (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty()); } +bool Mailbox::ready() +{ + bool comm_ready = false; + if (not pimpl_->comm_queue_.empty()) { + comm_ready = pimpl_->comm_queue_.front()->state_ == SIMIX_DONE; + } + if (!comm_ready && pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty()) { + comm_ready = pimpl_->done_comm_queue_.front()->state_ == SIMIX_DONE; + } + return comm_ready; +} + smx_activity_t Mailbox::front() { return pimpl_->comm_queue_.empty() ? nullptr : pimpl_->comm_queue_.front(); -- 2.20.1