Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove an instance of namespace surf
[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->set_mailbox(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->get_mailbox() == this, "Comm %p is in mailbox %s, not mailbox %s", comm.get(),
77              (comm->get_mailbox() ? comm->get_mailbox()->get_cname() : "(null)"), this->get_cname());
78
79   comm->set_mailbox(nullptr);
80   for (auto it = this->comm_queue_.begin(); it != this->comm_queue_.end(); it++)
81     if (*it == comm) {
82       this->comm_queue_.erase(it);
83       return;
84     }
85   xbt_die("Comm %p not found in mailbox %s", comm.get(), this->get_cname());
86 }
87
88 CommImplPtr MailboxImpl::iprobe(int type, int (*match_fun)(void*, void*, CommImpl*), void* data)
89 {
90   XBT_DEBUG("iprobe from %p %p", this, &comm_queue_);
91
92   CommImplPtr this_comm;
93   CommImpl::Type smx_type;
94   if (type == 1) {
95     this_comm = CommImplPtr(new CommImpl());
96     this_comm->set_type(CommImpl::Type::SEND);
97     smx_type  = CommImpl::Type::RECEIVE;
98   } else {
99     this_comm = CommImplPtr(new CommImpl());
100     this_comm->set_type(CommImpl::Type::RECEIVE);
101     smx_type  = CommImpl::Type::SEND;
102   }
103   CommImplPtr other_comm = nullptr;
104   if (permanent_receiver_ != nullptr && not done_comm_queue_.empty()) {
105     XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
106     other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ true, /*remove_matching*/ false);
107   }
108   if (not other_comm) {
109     XBT_DEBUG("check if we have more luck in the normal mailbox");
110     other_comm = find_matching_comm(smx_type, match_fun, data, this_comm, /*done*/ false, /*remove_matching*/ false);
111   }
112
113   return other_comm;
114 }
115
116 /**
117  *  @brief Checks if there is a communication activity queued in comm_queue_ matching our needs
118  *  @param type The type of communication we are looking for (comm_send, comm_recv)
119  *  @param match_fun the function to apply
120  *  @param this_user_data additional parameter to the match_fun
121  *  @param my_synchro what to compare against
122  *  @param remove_matching whether or not to clean the found object from the queue
123  *  @return The communication activity if found, nullptr otherwise
124  */
125 CommImplPtr MailboxImpl::find_matching_comm(CommImpl::Type type, int (*match_fun)(void*, void*, CommImpl*),
126                                             void* this_user_data, const CommImplPtr& my_synchro, bool done,
127                                             bool remove_matching)
128 {
129   void* other_user_data = nullptr;
130   auto& comm_queue      = done ? done_comm_queue_ : comm_queue_;
131
132   for (auto it = comm_queue.begin(); it != comm_queue.end(); it++) {
133     CommImplPtr& comm = *it;
134
135     if (comm->type_ == CommImpl::Type::SEND) {
136       other_user_data = comm->src_data_;
137     } else if (comm->type_ == CommImpl::Type::RECEIVE) {
138       other_user_data = comm->dst_data_;
139     }
140     if (comm->type_ == type && (match_fun == nullptr || match_fun(this_user_data, other_user_data, comm.get())) &&
141         (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro.get()))) {
142       XBT_DEBUG("Found a matching communication synchro %p", comm.get());
143 #if SIMGRID_HAVE_MC
144       comm->mbox_cpy = comm->get_mailbox();
145 #endif
146       comm->set_mailbox(nullptr);
147       CommImplPtr comm_cpy = comm;
148       if (remove_matching)
149         comm_queue.erase(it);
150       return comm_cpy;
151     }
152     XBT_DEBUG("Sorry, communication synchro %p does not match our needs:"
153               " its type is %d but we are looking for a comm of type %d (or maybe the filtering didn't match)",
154               comm.get(), (int)comm->type_, (int)type);
155   }
156   XBT_DEBUG("No matching communication synchro found");
157   return nullptr;
158 }
159 } // namespace activity
160 } // namespace kernel
161 } // namespace simgrid