Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[project-description] Fix extraction of the ns-3 version.
[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 "simgrid/msg.h"
8 #include "src/kernel/activity/CommImpl.hpp"
9
10 #include <unordered_map>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_mailbox, kernel, "Mailbox implementation");
13
14 /******************************************************************************/
15 /*                           Rendez-Vous Points                               */
16 /******************************************************************************/
17
18 namespace simgrid::kernel::activity {
19
20 unsigned MailboxImpl::next_id_ = 0;
21
22 MailboxImpl::~MailboxImpl()
23 {
24   clear(false);
25   set_receiver(nullptr);
26 }
27
28 /** @brief set the receiver of the mailbox to allow eager sends
29  *  @param actor The receiving dude
30  */
31 void MailboxImpl::set_receiver(s4u::ActorPtr actor)
32 {
33   if (this->permanent_receiver_) {
34     std::vector<MailboxImpl*>& mboxes = this->permanent_receiver_->mailboxes_;
35     mboxes.erase(std::remove(mboxes.begin(), mboxes.end(), this), mboxes.end());
36   }
37
38   if (actor != nullptr)
39     this->permanent_receiver_ = actor->get_impl();
40   else
41     this->permanent_receiver_ = nullptr;
42 }
43 /** @brief Pushes a communication activity into a mailbox
44  *  @param comm What to add
45  */
46 void MailboxImpl::push(CommImplPtr comm)
47 {
48   comm->set_mailbox(this);
49   this->comm_queue_.push_back(std::move(comm));
50 }
51
52 /** @brief Removes a communication activity from a mailbox
53  *  @param comm What to remove
54  */
55 void MailboxImpl::remove(const CommImplPtr& comm)
56 {
57   xbt_assert(comm->get_mailbox() == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
58              (comm->get_mailbox() ? comm->get_mailbox()->get_cname() : "(null)"), this->get_cname());
59
60   comm->set_mailbox(nullptr);
61   for (auto it = this->comm_queue_.begin(); it != this->comm_queue_.end(); it++)
62     if (*it == comm) {
63       this->comm_queue_.erase(it);
64       return;
65     }
66   xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());
67 }
68
69 /** @brief Removes all communication activities from a mailbox
70  */
71 void MailboxImpl::clear( bool do_post )
72 {
73   // CommImpl::cancel() will remove the comm from the mailbox..
74   for (auto comm : done_comm_queue_) {
75     comm->cancel();
76     comm->set_state(State::FAILED);
77     if(do_post)
78       comm->post();
79   }
80   done_comm_queue_.clear();
81
82   while (not comm_queue_.empty()) {
83     auto comm = comm_queue_.back();
84     if (comm->get_state() == State::WAITING && not comm->is_detached()) {
85       comm->cancel();
86       comm->set_state(State::FAILED);
87       if(do_post)
88         comm->post();
89     } else
90       comm_queue_.pop_back();
91   }
92   xbt_assert(comm_queue_.empty() && done_comm_queue_.empty());
93 }
94
95 CommImplPtr MailboxImpl::iprobe(int type, const std::function<bool(void*, void*, CommImpl*)>& match_fun, void* data)
96 {
97   XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
98
99   CommImplPtr this_comm(new CommImpl);
100   CommImplType other_type;
101   if (type == 1) {
102     this_comm->set_type(CommImplType::SEND);
103     other_type = CommImplType::RECEIVE;
104   } else {
105     this_comm->set_type(CommImplType::RECEIVE);
106     other_type = CommImplType::SEND;
107   }
108   CommImplPtr other_comm = nullptr;
109   if (permanent_receiver_ != nullptr && not done_comm_queue_.empty()) {
110     XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
111     other_comm = find_matching_comm(other_type, match_fun, data, this_comm, /*done*/ true, /*remove_matching*/ false);
112   }
113   if (not other_comm) {
114     XBT_DEBUG("check if we have more luck in the normal mailbox");
115     other_comm = find_matching_comm(other_type, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ false);
116   }
117
118   return other_comm;
119 }
120
121 /**
122  *  @brief Checks if there is a communication activity queued in comm_queue_ matching our needs
123  *  @param type The type of communication we are looking for (comm_send, comm_recv)
124  *  @param match_fun the function to apply
125  *  @param this_user_data additional parameter to the match_fun
126  *  @param my_synchro what to compare against
127  *  @param remove_matching whether or not to clean the found object from the queue
128  *  @return The communication activity if found, nullptr otherwise
129  */
130 CommImplPtr MailboxImpl::find_matching_comm(CommImplType type,
131                                             const std::function<bool(void*, void*, CommImpl*)>& match_fun,
132                                             void* this_user_data, const CommImplPtr& my_synchro, bool done,
133                                             bool remove_matching)
134 {
135   auto& comm_queue      = done ? done_comm_queue_ : comm_queue_;
136
137   auto iter = std::find_if(
138       comm_queue.begin(), comm_queue.end(), [&type, &match_fun, &this_user_data, &my_synchro](const CommImplPtr& comm) {
139         void* other_user_data = (comm->get_type() == CommImplType::SEND ? comm->src_data_ : comm->dst_data_);
140         return (comm->get_type() == type && (not match_fun || match_fun(this_user_data, other_user_data, comm.get())) &&
141                 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get())));
142       });
143   if (iter == comm_queue.end()) {
144     XBT_DEBUG("No matching communication synchro found");
145     return nullptr;
146   }
147
148   const CommImplPtr& comm = *iter;
149   XBT_DEBUG("Found a matching communication synchro %p", comm.get());
150   comm->set_mailbox(nullptr);
151   CommImplPtr comm_cpy = comm;
152   if (remove_matching)
153     comm_queue.erase(iter);
154   return comm_cpy;
155 }
156 } // namespace simgrid::kernel::activity