1 /* Copyright (c) 2007-2023. 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()
25 } catch (const std::bad_alloc& ba) {
26 XBT_ERROR("MailboxImpl::clear() failure: %s", ba.what());
28 set_receiver(nullptr);
31 /** @brief set the receiver of the mailbox to allow eager sends
32 * @param actor The receiving dude
34 void MailboxImpl::set_receiver(s4u::ActorPtr actor)
36 if (this->permanent_receiver_) {
37 std::vector<MailboxImpl*>& mboxes = this->permanent_receiver_->mailboxes_;
38 mboxes.erase(std::remove(mboxes.begin(), mboxes.end(), this), mboxes.end());
42 this->permanent_receiver_ = actor->get_impl();
44 this->permanent_receiver_ = nullptr;
46 /** @brief Pushes a communication activity into a mailbox
47 * @param comm What to add
49 void MailboxImpl::push(const CommImplPtr& comm)
51 comm->set_mailbox(this);
52 this->comm_queue_.push_back(std::move(comm));
55 /** @brief Removes a communication activity from a mailbox
56 * @param comm What to remove
58 void MailboxImpl::remove(const CommImplPtr& comm)
60 xbt_assert(comm->get_mailbox() == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
61 (comm->get_mailbox() ? comm->get_mailbox()->get_cname() : "(null)"), this->get_cname());
63 comm->set_mailbox(nullptr);
64 auto it = std::find(this->comm_queue_.begin(), this->comm_queue_.end(), comm);
65 if (it != this->comm_queue_.end())
66 this->comm_queue_.erase(it);
68 xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());
71 /** @brief Removes all communication activities from a mailbox
73 void MailboxImpl::clear(bool do_finish)
75 // CommImpl::cancel() will remove the comm from the mailbox..
76 for (const auto& comm : done_comm_queue_) {
78 comm->set_state(State::FAILED);
82 done_comm_queue_.clear();
84 while (not comm_queue_.empty()) {
85 auto comm = comm_queue_.back();
86 if (comm->get_state() == State::WAITING && not comm->is_detached()) {
88 comm->set_state(State::FAILED);
92 comm_queue_.pop_back();
94 xbt_assert(comm_queue_.empty() && done_comm_queue_.empty());
97 CommImplPtr MailboxImpl::iprobe(int type, const std::function<bool(void*, void*, CommImpl*)>& match_fun, void* data)
99 XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
101 CommImplPtr this_comm(new CommImpl);
102 CommImplType other_type;
104 this_comm->set_type(CommImplType::SEND);
105 other_type = CommImplType::RECEIVE;
107 this_comm->set_type(CommImplType::RECEIVE);
108 other_type = CommImplType::SEND;
110 CommImplPtr other_comm = nullptr;
111 if (permanent_receiver_ != nullptr && not done_comm_queue_.empty()) {
112 XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
113 other_comm = find_matching_comm(other_type, match_fun, data, this_comm, /*done*/ true, /*remove_matching*/ false);
115 if (not other_comm) {
116 XBT_DEBUG("check if we have more luck in the normal mailbox");
117 other_comm = find_matching_comm(other_type, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ false);
124 * @brief Checks if there is a communication activity queued in comm_queue_ matching our needs
125 * @param type The type of communication we are looking for (comm_send, comm_recv)
126 * @param match_fun the function to apply
127 * @param this_user_data additional parameter to the match_fun
128 * @param my_synchro what to compare against
129 * @param remove_matching whether or not to clean the found object from the queue
130 * @return The communication activity if found, nullptr otherwise
132 CommImplPtr MailboxImpl::find_matching_comm(CommImplType type,
133 const std::function<bool(void*, void*, CommImpl*)>& match_fun,
134 void* this_user_data, const CommImplPtr& my_synchro, bool done,
135 bool remove_matching)
137 auto& comm_queue = done ? done_comm_queue_ : comm_queue_;
139 auto iter = std::find_if(
140 comm_queue.begin(), comm_queue.end(), [&type, &match_fun, &this_user_data, &my_synchro](const CommImplPtr& comm) {
141 void* other_user_data = (comm->get_type() == CommImplType::SEND ? comm->src_data_ : comm->dst_data_);
142 return (comm->get_type() == type && (not match_fun || match_fun(this_user_data, other_user_data, comm.get())) &&
143 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get())));
145 if (iter == comm_queue.end()) {
146 XBT_DEBUG("No matching communication synchro found");
150 const CommImplPtr& comm = *iter;
151 XBT_DEBUG("Found a matching communication synchro %p", comm.get());
152 comm->set_mailbox(nullptr);
153 CommImplPtr comm_cpy = comm;
155 comm_queue.erase(iter);
158 } // namespace simgrid::kernel::activity