Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Concatenate nested namespaces (sonar).
[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::kernel::activity {
18
19 unsigned MailboxImpl::next_id_ = 0;
20
21 MailboxImpl::~MailboxImpl()
22 {
23   clear();
24   set_receiver(nullptr);
25 }
26
27 /** @brief set the receiver of the mailbox to allow eager sends
28  *  @param actor The receiving dude
29  */
30 void MailboxImpl::set_receiver(s4u::ActorPtr actor)
31 {
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());
35   }
36
37   if (actor != nullptr)
38     this->permanent_receiver_ = actor->get_impl();
39   else
40     this->permanent_receiver_ = nullptr;
41 }
42 /** @brief Pushes a communication activity into a mailbox
43  *  @param comm What to add
44  */
45 void MailboxImpl::push(CommImplPtr comm)
46 {
47   comm->set_mailbox(this);
48   this->comm_queue_.push_back(std::move(comm));
49 }
50
51 /** @brief Removes a communication activity from a mailbox
52  *  @param comm What to remove
53  */
54 void MailboxImpl::remove(const CommImplPtr& comm)
55 {
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());
58
59   comm->set_mailbox(nullptr);
60   for (auto it = this->comm_queue_.begin(); it != this->comm_queue_.end(); it++)
61     if (*it == comm) {
62       this->comm_queue_.erase(it);
63       return;
64     }
65   xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());
66 }
67
68 /** @brief Removes all communication activities from a mailbox
69  */
70 void MailboxImpl::clear()
71 {
72   for (auto comm : done_comm_queue_) {
73     comm->cancel();
74     comm->set_state(State::DST_HOST_FAILURE);
75   }
76   done_comm_queue_.clear();
77
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()) {
82       comm->cancel();
83       comm->set_state(State::DST_HOST_FAILURE);
84     } else
85       comm_queue_.pop_back();
86   }
87 }
88
89 CommImplPtr MailboxImpl::iprobe(int type, const std::function<bool(void*, void*, CommImpl*)>& match_fun, void* data)
90 {
91   XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
92
93   CommImplPtr this_comm(new CommImpl);
94   CommImplType other_type;
95   if (type == 1) {
96     this_comm->set_type(CommImplType::SEND);
97     other_type = CommImplType::RECEIVE;
98   } else {
99     this_comm->set_type(CommImplType::RECEIVE);
100     other_type = CommImplType::SEND;
101   }
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);
106   }
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);
110   }
111
112   return other_comm;
113 }
114
115 /**
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
123  */
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)
128 {
129   auto& comm_queue      = done ? done_comm_queue_ : comm_queue_;
130
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())));
136       });
137   if (iter == comm_queue.end()) {
138     XBT_DEBUG("No matching communication synchro found");
139     return nullptr;
140   }
141
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;
146   if (remove_matching)
147     comm_queue.erase(iter);
148   return comm_cpy;
149 }
150 } // namespace simgrid::kernel::activity