1 /* Copyright (c) 2007-2021. 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(simix_mailbox, simix, "Mailbox implementation");
13 static std::unordered_map<std::string, smx_mailbox_t> mailboxes;
15 void SIMIX_mailbox_exit()
17 for (auto const& elm : mailboxes)
22 /******************************************************************************/
23 /* Rendez-Vous Points */
24 /******************************************************************************/
29 /** @brief Returns the mailbox of that name, or nullptr */
30 MailboxImpl* MailboxImpl::by_name_or_null(const std::string& name)
32 auto mbox = mailboxes.find(name);
33 if (mbox != mailboxes.end())
39 /** @brief Returns the mailbox of that name, newly created on need */
40 MailboxImpl* MailboxImpl::by_name_or_create(const std::string& name)
42 /* two processes may have pushed the same mbox_create simcall at the same time */
43 auto m = mailboxes.find(name);
44 if (m == mailboxes.end()) {
45 auto* mbox = new MailboxImpl(name);
46 XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name.c_str());
47 mailboxes[name] = mbox;
52 /** @brief set the receiver of the mailbox to allow eager sends
53 * @param actor The receiving dude
55 void MailboxImpl::set_receiver(s4u::ActorPtr actor)
58 this->permanent_receiver_ = actor->get_impl();
60 this->permanent_receiver_ = nullptr;
62 /** @brief Pushes a communication activity into a mailbox
63 * @param comm What to add
65 void MailboxImpl::push(CommImplPtr comm)
67 comm->set_mailbox(this);
68 this->comm_queue_.push_back(std::move(comm));
71 /** @brief Removes a communication activity from a mailbox
72 * @param comm What to remove
74 void MailboxImpl::remove(const CommImplPtr& comm)
76 xbt_assert(comm->get_mailbox() == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
77 (comm->get_mailbox() ? comm->get_mailbox()->get_cname() : "(null)"), this->get_cname());
79 comm->set_mailbox(nullptr);
80 for (auto it = this->comm_queue_.begin(); it != this->comm_queue_.end(); it++)
82 this->comm_queue_.erase(it);
85 xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());
88 CommImplPtr MailboxImpl::iprobe(int type, bool (*match_fun)(void*, void*, CommImpl*), void* data)
90 XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
92 CommImplPtr this_comm;
93 CommImpl::Type smx_type;
95 this_comm = CommImplPtr(new CommImpl(CommImpl::Type::SEND));
96 smx_type = CommImpl::Type::RECEIVE;
98 this_comm = CommImplPtr(new CommImpl(CommImpl::Type::RECEIVE));
99 smx_type = CommImpl::Type::SEND;
101 CommImplPtr other_comm = nullptr;
102 if (permanent_receiver_ != nullptr && not done_comm_queue_.empty()) {
103 XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
104 other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ true, /*remove_matching*/ false);
106 if (not other_comm) {
107 XBT_DEBUG("check if we have more luck in the normal mailbox");
108 other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ false);
115 * @brief Checks if there is a communication activity queued in comm_queue_ matching our needs
116 * @param type The type of communication we are looking for (comm_send, comm_recv)
117 * @param match_fun the function to apply
118 * @param this_user_data additional parameter to the match_fun
119 * @param my_synchro what to compare against
120 * @param remove_matching whether or not to clean the found object from the queue
121 * @return The communication activity if found, nullptr otherwise
123 CommImplPtr MailboxImpl::find_matching_comm(CommImpl::Type type, bool (*match_fun)(void*, void*, CommImpl*),
124 void* this_user_data, const CommImplPtr& my_synchro, bool done,
125 bool remove_matching)
127 void* other_user_data = nullptr;
128 auto& comm_queue = done ? done_comm_queue_ : comm_queue_;
130 for (auto it = comm_queue.begin(); it != comm_queue.end(); it++) {
131 const CommImplPtr& comm = *it;
133 if (comm->type_ == CommImpl::Type::SEND) {
134 other_user_data = comm->src_data_;
135 } else if (comm->type_ == CommImpl::Type::RECEIVE) {
136 other_user_data = comm->dst_data_;
138 if (comm->type_ == type && (match_fun == nullptr || match_fun(this_user_data, other_user_data, comm.get())) &&
139 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get()))) {
140 XBT_DEBUG("Found a matching communication synchro %p", comm.get());
142 comm->mbox_cpy = comm->get_mailbox();
144 comm->set_mailbox(nullptr);
145 CommImplPtr comm_cpy = comm;
147 comm_queue.erase(it);
150 XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
151 " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
152 comm.get(), (int)comm->type_, (int)type);
154 XBT_DEBUG("No matching communication synchro found");
157 } // namespace activity
158 } // namespace kernel
159 } // namespace simgrid