1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "src/kernel/activity/MailboxImpl.hpp"
7 #include "src/kernel/activity/CommImpl.hpp"
9 #include <unordered_map>
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_mailbox, kernel, "Mailbox implementation");
13 /******************************************************************************/
14 /* Rendez-Vous Points */
15 /******************************************************************************/
17 namespace simgrid::kernel::activity {
19 unsigned MailboxImpl::next_id_ = 0;
21 MailboxImpl::~MailboxImpl()
24 set_receiver(nullptr);
27 /** @brief set the receiver of the mailbox to allow eager sends
28 * @param actor The receiving dude
30 void MailboxImpl::set_receiver(s4u::ActorPtr actor)
32 if (this->permanent_receiver_) {
33 std::vector<MailboxImpl*>& mboxes = this->permanent_receiver_->mailboxes_;
34 mboxes.erase(std::remove(mboxes.begin(), mboxes.end(), this), mboxes.end());
38 this->permanent_receiver_ = actor->get_impl();
40 this->permanent_receiver_ = nullptr;
42 /** @brief Pushes a communication activity into a mailbox
43 * @param comm What to add
45 void MailboxImpl::push(CommImplPtr comm)
47 comm->set_mailbox(this);
48 this->comm_queue_.push_back(std::move(comm));
51 /** @brief Removes a communication activity from a mailbox
52 * @param comm What to remove
54 void MailboxImpl::remove(const CommImplPtr& comm)
56 xbt_assert(comm->get_mailbox() == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
57 (comm->get_mailbox() ? comm->get_mailbox()->get_cname() : "(null)"), this->get_cname());
59 comm->set_mailbox(nullptr);
60 for (auto it = this->comm_queue_.begin(); it != this->comm_queue_.end(); it++)
62 this->comm_queue_.erase(it);
65 xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());
68 /** @brief Removes all communication activities from a mailbox
70 void MailboxImpl::clear()
72 for (auto comm : done_comm_queue_) {
74 comm->set_state(State::DST_HOST_FAILURE);
76 done_comm_queue_.clear();
78 // CommImpl::cancel() will remove the comm from the mailbox..
79 while (not comm_queue_.empty()) {
80 auto comm = comm_queue_.back();
81 if (comm->get_state() == State::WAITING && not comm->is_detached()) {
83 comm->set_state(State::DST_HOST_FAILURE);
85 comm_queue_.pop_back();
89 CommImplPtr MailboxImpl::iprobe(int type, const std::function<bool(void*, void*, CommImpl*)>& match_fun, void* data)
91 XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
93 CommImplPtr this_comm(new CommImpl);
94 CommImplType other_type;
96 this_comm->set_type(CommImplType::SEND);
97 other_type = CommImplType::RECEIVE;
99 this_comm->set_type(CommImplType::RECEIVE);
100 other_type = CommImplType::SEND;
102 CommImplPtr other_comm = nullptr;
103 if (permanent_receiver_ != nullptr && not done_comm_queue_.empty()) {
104 XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
105 other_comm = find_matching_comm(other_type, match_fun, data, this_comm, /*done*/ true, /*remove_matching*/ false);
107 if (not other_comm) {
108 XBT_DEBUG("check if we have more luck in the normal mailbox");
109 other_comm = find_matching_comm(other_type, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ false);
116 * @brief Checks if there is a communication activity queued in comm_queue_ matching our needs
117 * @param type The type of communication we are looking for (comm_send, comm_recv)
118 * @param match_fun the function to apply
119 * @param this_user_data additional parameter to the match_fun
120 * @param my_synchro what to compare against
121 * @param remove_matching whether or not to clean the found object from the queue
122 * @return The communication activity if found, nullptr otherwise
124 CommImplPtr MailboxImpl::find_matching_comm(CommImplType type,
125 const std::function<bool(void*, void*, CommImpl*)>& match_fun,
126 void* this_user_data, const CommImplPtr& my_synchro, bool done,
127 bool remove_matching)
129 auto& comm_queue = done ? done_comm_queue_ : comm_queue_;
131 auto iter = std::find_if(
132 comm_queue.begin(), comm_queue.end(), [&type, &match_fun, &this_user_data, &my_synchro](const CommImplPtr& comm) {
133 void* other_user_data = (comm->get_type() == CommImplType::SEND ? comm->src_data_ : comm->dst_data_);
134 return (comm->get_type() == type && (not match_fun || match_fun(this_user_data, other_user_data, comm.get())) &&
135 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get())));
137 if (iter == comm_queue.end()) {
138 XBT_DEBUG("No matching communication synchro found");
142 const CommImplPtr& comm = *iter;
143 XBT_DEBUG("Found a matching communication synchro %p", comm.get());
144 comm->set_mailbox(nullptr);
145 CommImplPtr comm_cpy = comm;
147 comm_queue.erase(iter);
150 } // namespace simgrid::kernel::activity