Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[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(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(bool do_finish)
75 {
76   // CommImpl::cancel() will remove the comm from the mailbox..
77   for (auto comm : done_comm_queue_) {
78     comm->cancel();
79     comm->set_state(State::FAILED);
80     if (do_finish)
81       comm->finish();
82   }
83   done_comm_queue_.clear();
84
85   while (not comm_queue_.empty()) {
86     auto comm = comm_queue_.back();
87     if (comm->get_state() == State::WAITING && not comm->is_detached()) {
88       comm->cancel();
89       comm->set_state(State::FAILED);
90       if (do_finish)
91         comm->finish();
92     } else
93       comm_queue_.pop_back();
94   }
95   xbt_assert(comm_queue_.empty() && done_comm_queue_.empty());
96 }
97
98 CommImplPtr MailboxImpl::iprobe(int type, const std::function<bool(void*, void*, CommImpl*)>& match_fun, void* data)
99 {
100   XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
101
102   CommImplPtr this_comm(new CommImpl);
103   CommImplType other_type;
104   if (type == 1) {
105     this_comm->set_type(CommImplType::SEND);
106     other_type = CommImplType::RECEIVE;
107   } else {
108     this_comm->set_type(CommImplType::RECEIVE);
109     other_type = CommImplType::SEND;
110   }
111   CommImplPtr other_comm = nullptr;
112   if (permanent_receiver_ != nullptr && not done_comm_queue_.empty()) {
113     XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
114     other_comm = find_matching_comm(other_type, match_fun, data, this_comm, /*done*/ true, /*remove_matching*/ false);
115   }
116   if (not other_comm) {
117     XBT_DEBUG("check if we have more luck in the normal mailbox");
118     other_comm = find_matching_comm(other_type, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ false);
119   }
120
121   return other_comm;
122 }
123
124 /**
125  *  @brief Checks if there is a communication activity queued in comm_queue_ matching our needs
126  *  @param type The type of communication we are looking for (comm_send, comm_recv)
127  *  @param match_fun the function to apply
128  *  @param this_user_data additional parameter to the match_fun
129  *  @param my_synchro what to compare against
130  *  @param remove_matching whether or not to clean the found object from the queue
131  *  @return The communication activity if found, nullptr otherwise
132  */
133 CommImplPtr MailboxImpl::find_matching_comm(CommImplType type,
134                                             const std::function<bool(void*, void*, CommImpl*)>& match_fun,
135                                             void* this_user_data, const CommImplPtr& my_synchro, bool done,
136                                             bool remove_matching)
137 {
138   auto& comm_queue      = done ? done_comm_queue_ : comm_queue_;
139
140   auto iter = std::find_if(
141       comm_queue.begin(), comm_queue.end(), [&type, &match_fun, &this_user_data, &my_synchro](const CommImplPtr& comm) {
142         void* other_user_data = (comm->get_type() == CommImplType::SEND ? comm->src_data_ : comm->dst_data_);
143         return (comm->get_type() == type && (not match_fun || match_fun(this_user_data, other_user_data, comm.get())) &&
144                 (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get())));
145       });
146   if (iter == comm_queue.end()) {
147     XBT_DEBUG("No matching communication synchro found");
148     return nullptr;
149   }
150
151   const CommImplPtr& comm = *iter;
152   XBT_DEBUG("Found a matching communication synchro %p", comm.get());
153   comm->set_mailbox(nullptr);
154   CommImplPtr comm_cpy = comm;
155   if (remove_matching)
156     comm_queue.erase(iter);
157   return comm_cpy;
158 }
159 } // namespace simgrid::kernel::activity