Logo AND Algorithmique Numérique Distribuée

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