Logo AND Algorithmique Numérique Distribuée

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