Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into CRTP
[simgrid.git] / src / kernel / activity / MailboxImpl.cpp
1 /* Copyright (c) 2007-2019. 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(simix_mailbox, simix, "Mailbox implementation");
12
13 static std::unordered_map<std::string, smx_mailbox_t> mailboxes;
14
15 void SIMIX_mailbox_exit()
16 {
17   for (auto const& elm : mailboxes)
18     delete elm.second;
19   mailboxes.clear();
20 }
21
22 /******************************************************************************/
23 /*                           Rendez-Vous Points                               */
24 /******************************************************************************/
25
26 namespace simgrid {
27 namespace kernel {
28 namespace activity {
29 /** @brief Returns the mailbox of that name, or nullptr */
30 MailboxImpl* MailboxImpl::by_name_or_null(const std::string& name)
31 {
32   auto mbox = mailboxes.find(name);
33   if (mbox != mailboxes.end())
34     return mbox->second;
35   else
36     return nullptr;
37 }
38
39 /** @brief Returns the mailbox of that name, newly created on need */
40 MailboxImpl* MailboxImpl::by_name_or_create(const std::string& name)
41 {
42   /* two processes may have pushed the same mbox_create simcall at the same time */
43   auto m = mailboxes.find(name);
44   if (m == mailboxes.end()) {
45     MailboxImpl* mbox = new MailboxImpl(name);
46     XBT_DEBUG("Creating a mailbox at %p with name %s", mbox, name.c_str());
47     mailboxes[name] = mbox;
48     return mbox;
49   } else
50     return m->second;
51 }
52 /** @brief set the receiver of the mailbox to allow eager sends
53  *  @param actor The receiving dude
54  */
55 void MailboxImpl::set_receiver(s4u::ActorPtr actor)
56 {
57   if (actor != nullptr)
58     this->permanent_receiver_ = actor->get_impl();
59   else
60     this->permanent_receiver_ = nullptr;
61 }
62 /** @brief Pushes a communication activity into a mailbox
63  *  @param comm What to add
64  */
65 void MailboxImpl::push(CommImplPtr comm)
66 {
67   comm->mbox = this;
68   this->comm_queue_.push_back(std::move(comm));
69 }
70
71 /** @brief Removes a communication activity from a mailbox
72  *  @param comm What to remove
73  */
74 void MailboxImpl::remove(const CommImplPtr& comm)
75 {
76   xbt_assert(comm->mbox == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
77              (comm->mbox ? comm->mbox->get_cname() : "(null)"), this->get_cname());
78   comm->mbox = nullptr;
79   for (auto it = this->comm_queue_.begin(); it != this->comm_queue_.end(); it++)
80     if (*it == comm) {
81       this->comm_queue_.erase(it);
82       return;
83     }
84   xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());
85 }
86
87 CommImplPtr MailboxImpl::iprobe(int type, int (*match_fun)(void*, void*, CommImpl*), void* data)
88 {
89   XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
90
91   CommImplPtr this_comm;
92   CommImpl::Type smx_type;
93   if (type == 1) {
94     this_comm = CommImplPtr(new CommImpl());
95     this_comm->set_type(CommImpl::Type::SEND);
96     smx_type  = CommImpl::Type::RECEIVE;
97   } else {
98     this_comm = CommImplPtr(new CommImpl());
99     this_comm->set_type(CommImpl::Type::RECEIVE);
100     smx_type  = CommImpl::Type::SEND;
101   }
102   CommImplPtr other_comm = nullptr;
103   if (permanent_receiver_ != nullptr && not done_comm_queue_.empty()) {
104     XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
105     other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ true, /*remove_matching*/ false);
106   }
107   if (not other_comm) {
108     XBT_DEBUG("check if we have more luck in the normal mailbox");
109     other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ false);
110   }
111
112   return other_comm;
113 }
114
115 /**
116  *  @brief Checks if there is a communication activity queued in comm_queue_ matching our needs
117  *  @param type The type of communication we are looking for (comm_send, comm_recv)
118  *  @param match_fun the function to apply
119  *  @param this_user_data additional parameter to the match_fun
120  *  @param my_synchro what to compare against
121  *  @param remove_matching whether or not to clean the found object from the queue
122  *  @return The communication activity if found, nullptr otherwise
123  */
124 CommImplPtr MailboxImpl::find_matching_comm(CommImpl::Type type, int (*match_fun)(void*, void*, CommImpl*),
125                                             void* this_user_data, const CommImplPtr& my_synchro, bool done,
126                                             bool remove_matching)
127 {
128   void* other_user_data = nullptr;
129   auto& comm_queue      = done ? done_comm_queue_ : comm_queue_;
130
131   for (auto it = comm_queue.begin(); it != comm_queue.end(); it++) {
132     CommImplPtr& comm = *it;
133
134     if (comm->type_ == CommImpl::Type::SEND) {
135       other_user_data = comm->src_data_;
136     } else if (comm->type_ == CommImpl::Type::RECEIVE) {
137       other_user_data = comm->dst_data_;
138     }
139     if (comm->type_ == type && (match_fun == nullptr || match_fun(this_user_data, other_user_data, comm.get())) &&
140         (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get()))) {
141       XBT_DEBUG("Found a matching communication synchro %p", comm.get());
142 #if SIMGRID_HAVE_MC
143       comm->mbox_cpy = comm->mbox;
144 #endif
145       comm->mbox = nullptr;
146       CommImplPtr comm_cpy = comm;
147       if (remove_matching)
148         comm_queue.erase(it);
149       return comm_cpy;
150     }
151     XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
152               " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
153               comm.get(), (int)comm->type_, (int)type);
154   }
155   XBT_DEBUG("No matching communication synchro found");
156   return nullptr;
157 }
158 } // namespace activity
159 } // namespace kernel
160 } // namespace simgrid