Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleanup in log categories
[simgrid.git] / src / kernel / activity / MailboxImpl.cpp
1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/kernel/activity/MailboxImpl.hpp"
7 #include "src/kernel/activity/CommImpl.hpp"
8
9 #include <unordered_map>
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_mailbox, kernel, "Mailbox implementation");
12
13 /******************************************************************************/
14 /*                           Rendez-Vous Points                               */
15 /******************************************************************************/
16
17 namespace simgrid {
18 namespace kernel {
19 namespace activity {
20
21 /** @brief set the receiver of the mailbox to allow eager sends
22  *  @param actor The receiving dude
23  */
24 void MailboxImpl::set_receiver(s4u::ActorPtr actor)
25 {
26   if (actor != nullptr)
27     this->permanent_receiver_ = actor->get_impl();
28   else
29     this->permanent_receiver_ = nullptr;
30 }
31 /** @brief Pushes a communication activity into a mailbox
32  *  @param comm What to add
33  */
34 void MailboxImpl::push(CommImplPtr comm)
35 {
36   comm->set_mailbox(this);
37   this->comm_queue_.push_back(std::move(comm));
38 }
39
40 /** @brief Removes a communication activity from a mailbox
41  *  @param comm What to remove
42  */
43 void MailboxImpl::remove(const CommImplPtr& comm)
44 {
45   xbt_assert(comm->get_mailbox() == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
46              (comm->get_mailbox() ? comm->get_mailbox()->get_cname() : "(null)"), this->get_cname());
47
48   comm->set_mailbox(nullptr);
49   for (auto it = this->comm_queue_.begin(); it != this->comm_queue_.end(); it++)
50     if (*it == comm) {
51       this->comm_queue_.erase(it);
52       return;
53     }
54   xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());
55 }
56
57 CommImplPtr MailboxImpl::iprobe(int type, bool (*match_fun)(void*, void*, CommImpl*), void* data)
58 {
59   XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
60
61   CommImplPtr this_comm;
62   CommImpl::Type smx_type;
63   if (type == 1) {
64     this_comm = CommImplPtr(new CommImpl(CommImpl::Type::SEND));
65     smx_type  = CommImpl::Type::RECEIVE;
66   } else {
67     this_comm = CommImplPtr(new CommImpl(CommImpl::Type::RECEIVE));
68     smx_type  = CommImpl::Type::SEND;
69   }
70   CommImplPtr other_comm = nullptr;
71   if (permanent_receiver_ != nullptr && not done_comm_queue_.empty()) {
72     XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
73     other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ true, /*remove_matching*/ false);
74   }
75   if (not other_comm) {
76     XBT_DEBUG("check if we have more luck in the normal mailbox");
77     other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ false);
78   }
79
80   return other_comm;
81 }
82
83 /**
84  *  @brief Checks if there is a communication activity queued in comm_queue_ matching our needs
85  *  @param type The type of communication we are looking for (comm_send, comm_recv)
86  *  @param match_fun the function to apply
87  *  @param this_user_data additional parameter to the match_fun
88  *  @param my_synchro what to compare against
89  *  @param remove_matching whether or not to clean the found object from the queue
90  *  @return The communication activity if found, nullptr otherwise
91  */
92 CommImplPtr MailboxImpl::find_matching_comm(CommImpl::Type type, bool (*match_fun)(void*, void*, CommImpl*),
93                                             void* this_user_data, const CommImplPtr& my_synchro, bool done,
94                                             bool remove_matching)
95 {
96   auto& comm_queue      = done ? done_comm_queue_ : comm_queue_;
97
98   auto iter = std::find_if(
99       comm_queue.begin(), comm_queue.end(), [&type, &match_fun, &this_user_data, &my_synchro](const CommImplPtr& comm) {
100         void* other_user_data = (comm->type_ == CommImpl::Type::SEND ? comm->src_data_ : comm->dst_data_);
101         return (comm->type_ == type && (not match_fun || match_fun(this_user_data, other_user_data, comm.get())) &&
102                 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get())));
103       });
104   if (iter == comm_queue.end()) {
105     XBT_DEBUG("No matching communication synchro found");
106     return nullptr;
107   }
108
109   const CommImplPtr& comm = *iter;
110   XBT_DEBUG("Found a matching communication synchro %p", comm.get());
111 #if SIMGRID_HAVE_MC
112   comm->mbox_cpy = comm->get_mailbox();
113 #endif
114   comm->set_mailbox(nullptr);
115   CommImplPtr comm_cpy = comm;
116   if (remove_matching)
117     comm_queue.erase(iter);
118   return comm_cpy;
119 }
120 } // namespace activity
121 } // namespace kernel
122 } // namespace simgrid