+ActivityImplPtr CommImpl::isend(actor::CommIsendSimcall* observer)
+{
+ auto* mbox = observer->get_mailbox();
+ XBT_DEBUG("send from mailbox %p", mbox);
+
+ /* Prepare a synchro describing us, so that it gets passed to the user-provided filter of other side */
+ CommImplPtr this_comm(new CommImpl());
+ this_comm->set_type(CommImplType::SEND);
+
+ /* Look for communication synchro matching our needs. We also provide a description of
+ * ourself so that the other side also gets a chance of choosing if it wants to match with us.
+ *
+ * If it is not found then push our communication into the rendez-vous point */
+ CommImplPtr other_comm =
+ mbox->find_matching_comm(CommImplType::RECEIVE, observer->get_match_fun(), observer->get_payload(), this_comm,
+ /*done*/ false, /*remove_matching*/ true);
+
+ if (not other_comm) {
+ other_comm = std::move(this_comm);
+
+ if (mbox->is_permanent()) {
+ // this mailbox is for small messages, which have to be sent right now
+ other_comm->set_state(State::READY);
+ other_comm->dst_actor_ = mbox->get_permanent_receiver().get();
+ mbox->push_done(other_comm);
+ XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, other_comm.get());
+
+ } else {
+ mbox->push(other_comm);
+ }
+ } else {
+ XBT_DEBUG("Receive already pushed");
+
+ other_comm->set_state(State::READY);
+ }
+ observer->set_comm(other_comm.get());
+
+ if (observer->is_detached()) {
+ other_comm->detach();
+ other_comm->clean_fun = observer->get_clean_fun();
+ EngineImpl::get_instance()->get_maestro()->activities_.emplace_back(other_comm);
+ } else {
+ other_comm->clean_fun = nullptr;
+ observer->get_issuer()->activities_.emplace_back(other_comm);
+ }
+
+ /* Setup the communication synchro */
+ other_comm->src_actor_ = observer->get_issuer();
+ other_comm->src_data_ = observer->get_payload();
+ (*other_comm)
+ .set_src_buff(observer->get_src_buff(), observer->get_src_buff_size())
+ .set_size(observer->get_payload_size())
+ .set_rate(observer->get_rate());
+
+ other_comm->match_fun = observer->get_match_fun();
+ other_comm->copy_data_fun = observer->get_copy_data_fun();
+
+ if (MC_is_active() || MC_record_replay_is_active())
+ other_comm->set_state(simgrid::kernel::activity::State::RUNNING);
+ else
+ other_comm->start();
+
+ return (observer->is_detached() ? nullptr : other_comm);
+}
+
+ActivityImplPtr CommImpl::irecv(actor::CommIrecvSimcall* observer)
+{
+ CommImplPtr this_synchro(new CommImpl());
+ this_synchro->set_type(CommImplType::RECEIVE);
+
+ auto* mbox = observer->get_mailbox();
+ XBT_DEBUG("recv from mbox %p. this_synchro=%p", mbox, this_synchro.get());
+
+ CommImplPtr other_comm;
+ // communication already done, get it inside the list of completed comms
+ if (mbox->is_permanent() && mbox->has_some_done_comm()) {
+ XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
+ // find a match in the list of already received comms
+ other_comm = mbox->find_matching_comm(CommImplType::SEND, observer->get_match_fun(), observer->get_payload(),
+ this_synchro, /*done*/ true, /*remove_matching*/ true);
+ if (other_comm && other_comm->surf_action_ && other_comm->get_remaining() < 1e-12) {
+ XBT_DEBUG("comm %p has been already sent, and is finished, destroy it", other_comm.get());
+ other_comm->set_state(State::DONE);
+ other_comm->set_mailbox(nullptr);
+ } else {
+ // if not found, assume the receiver came first, register it to the mailbox in the classical way
+ if (not other_comm) {
+ XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request "
+ "into list");
+ other_comm = std::move(this_synchro);
+ mbox->push(other_comm);
+ }
+ observer->get_issuer()->activities_.emplace_back(other_comm);
+ }
+ } else {
+ /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
+
+ /* Look for communication activity matching our needs. We also provide a description of
+ * ourself so that the other side also gets a chance of choosing if it wants to match with us.
+ *
+ * If it is not found then push our communication into the rendez-vous point */
+ other_comm = mbox->find_matching_comm(CommImplType::SEND, observer->get_match_fun(), observer->get_payload(),
+ this_synchro, /*done*/ false, /*remove_matching*/ true);
+
+ if (other_comm == nullptr) {
+ XBT_DEBUG("Receive pushed first (%zu comm enqueued so far)", mbox->size());
+ other_comm = std::move(this_synchro);
+ mbox->push(other_comm);
+ } else {
+ XBT_DEBUG("Match my %p with the existing %p", this_synchro.get(), other_comm.get());
+
+ other_comm->set_state(simgrid::kernel::activity::State::READY);
+ }
+ observer->get_issuer()->activities_.emplace_back(other_comm);
+ }
+ observer->set_comm(other_comm.get());
+
+ /* Setup communication synchro */
+ other_comm->dst_actor_ = observer->get_issuer();
+ other_comm->dst_data_ = observer->get_payload();
+ other_comm->set_dst_buff(observer->get_dst_buff(), observer->get_dst_buff_size());
+
+ if (observer->get_rate() > -1.0 && (other_comm->get_rate() < 0.0 || observer->get_rate() < other_comm->get_rate()))
+ other_comm->set_rate(observer->get_rate());
+
+ other_comm->match_fun = observer->get_match_fun();
+ other_comm->copy_data_fun = observer->get_copy_data_fun();
+
+ if (MC_is_active() || MC_record_replay_is_active()) {
+ other_comm->set_state(State::RUNNING);
+ return other_comm;
+ }
+ other_comm->start();
+
+ return other_comm;
+}
+
+bool CommImpl::test(actor::ActorImpl* issuer)
+{
+ if ((MC_is_active() || MC_record_replay_is_active()) && src_actor_ && dst_actor_)
+ set_state(State::DONE);
+ return ActivityImpl::test(issuer);
+}
+
+void CommImpl::wait_for(actor::ActorImpl* issuer, double timeout)
+{
+ XBT_DEBUG("CommImpl::wait_for(%g), %p, state %s", timeout, this, get_state_str());
+
+ /* Associate this simcall to the wait synchro */
+ register_simcall(&issuer->simcall_);
+ if (MC_is_active() || MC_record_replay_is_active()) {
+ // FIXME: what about timeouts?
+ set_state(State::DONE);
+ finish();
+ return;
+ }
+ ActivityImpl::wait_for(issuer, timeout);
+}
+
+void CommImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout)
+{
+ std::vector<ActivityImpl*> activities(comms.begin(), comms.end());
+ ActivityImpl::wait_any_for(issuer, activities, timeout);
+}
+