Logo AND Algorithmique Numérique Distribuée

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