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 /******************************************************************************/
21 unsigned MailboxImpl::next_id_ = 0;
23 /** @brief set the receiver of the mailbox to allow eager sends
24 * @param actor The receiving dude
26 void MailboxImpl::set_receiver(s4u::ActorPtr actor)
29 this->permanent_receiver_ = actor->get_impl();
31 this->permanent_receiver_ = nullptr;
33 /** @brief Pushes a communication activity into a mailbox
34 * @param comm What to add
36 void MailboxImpl::push(CommImplPtr comm)
38 comm->set_mailbox(this);
39 this->comm_queue_.push_back(std::move(comm));
42 /** @brief Removes a communication activity from a mailbox
43 * @param comm What to remove
45 void MailboxImpl::remove(const CommImplPtr& comm)
47 xbt_assert(comm->get_mailbox() == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
48 (comm->get_mailbox() ? comm->get_mailbox()->get_cname() : "(null)"), this->get_cname());
50 comm->set_mailbox(nullptr);
51 for (auto it = this->comm_queue_.begin(); it != this->comm_queue_.end(); it++)
53 this->comm_queue_.erase(it);
56 xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());
59 CommImplPtr MailboxImpl::iprobe(int type, bool (*match_fun)(void*, void*, CommImpl*), void* data)
61 XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
63 CommImplPtr this_comm;
64 CommImpl::Type smx_type;
66 this_comm = CommImplPtr(new CommImpl(CommImpl::Type::SEND));
67 smx_type = CommImpl::Type::RECEIVE;
69 this_comm = CommImplPtr(new CommImpl(CommImpl::Type::RECEIVE));
70 smx_type = CommImpl::Type::SEND;
72 CommImplPtr other_comm = nullptr;
73 if (permanent_receiver_ != nullptr && not done_comm_queue_.empty()) {
74 XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
75 other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ true, /*remove_matching*/ false);
78 XBT_DEBUG("check if we have more luck in the normal mailbox");
79 other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ false);
86 * @brief Checks if there is a communication activity queued in comm_queue_ matching our needs
87 * @param type The type of communication we are looking for (comm_send, comm_recv)
88 * @param match_fun the function to apply
89 * @param this_user_data additional parameter to the match_fun
90 * @param my_synchro what to compare against
91 * @param remove_matching whether or not to clean the found object from the queue
92 * @return The communication activity if found, nullptr otherwise
94 CommImplPtr MailboxImpl::find_matching_comm(CommImpl::Type type, bool (*match_fun)(void*, void*, CommImpl*),
95 void* this_user_data, const CommImplPtr& my_synchro, bool done,
98 auto& comm_queue = done ? done_comm_queue_ : comm_queue_;
100 auto iter = std::find_if(
101 comm_queue.begin(), comm_queue.end(), [&type, &match_fun, &this_user_data, &my_synchro](const CommImplPtr& comm) {
102 void* other_user_data = (comm->type_ == CommImpl::Type::SEND ? comm->src_data_ : comm->dst_data_);
103 return (comm->type_ == type && (not match_fun || match_fun(this_user_data, other_user_data, comm.get())) &&
104 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get())));
106 if (iter == comm_queue.end()) {
107 XBT_DEBUG("No matching communication synchro found");
111 const CommImplPtr& comm = *iter;
112 XBT_DEBUG("Found a matching communication synchro %p", comm.get());
114 comm->mbox_cpy = comm->get_mailbox();
116 comm->set_mailbox(nullptr);
117 CommImplPtr comm_cpy = comm;
119 comm_queue.erase(iter);
122 } // namespace activity
123 } // namespace kernel
124 } // namespace simgrid