Logo AND Algorithmique Numérique Distribuée

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