Logo AND Algorithmique Numérique Distribuée

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