Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Replace redundant type with "auto" (include/ and src/).
[simgrid.git] / src / s4u / s4u_Mailbox.cpp
1 /* Copyright (c) 2006-2020. 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 "simgrid/s4u/Comm.hpp"
7 #include "simgrid/s4u/Mailbox.hpp"
8 #include "src/kernel/activity/MailboxImpl.hpp"
9
10 #include <simgrid/mailbox.h>
11
12 XBT_LOG_EXTERNAL_CATEGORY(s4u);
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel, s4u, "S4U Communication Mailboxes");
14
15 namespace simgrid {
16 namespace s4u {
17
18 const xbt::string& Mailbox::get_name() const
19 {
20   return pimpl_->get_name();
21 }
22
23 const char* Mailbox::get_cname() const
24 {
25   return pimpl_->get_cname();
26 }
27
28 Mailbox* Mailbox::by_name(const std::string& name)
29 {
30   kernel::activity::MailboxImpl* mbox = kernel::activity::MailboxImpl::by_name_or_null(name);
31   if (mbox == nullptr) {
32     mbox = kernel::actor::simcall([&name] { return kernel::activity::MailboxImpl::by_name_or_create(name); });
33   }
34   return &mbox->piface_;
35 }
36
37 bool Mailbox::empty() const
38 {
39   return pimpl_->comm_queue_.empty();
40 }
41
42 bool Mailbox::listen() const
43 {
44   return not this->empty() || (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty());
45 }
46
47 aid_t Mailbox::listen_from() const
48 {
49   kernel::activity::CommImplPtr comm = front();
50   if (comm && comm->src_actor_)
51     return comm->src_actor_->get_pid();
52   else
53     return -1;
54 }
55
56 bool Mailbox::ready() const
57 {
58   bool comm_ready = false;
59   if (not pimpl_->comm_queue_.empty()) {
60     comm_ready = pimpl_->comm_queue_.front()->state_ == kernel::activity::State::DONE;
61
62   } else if (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty()) {
63     comm_ready = pimpl_->done_comm_queue_.front()->state_ == kernel::activity::State::DONE;
64   }
65   return comm_ready;
66 }
67
68 kernel::activity::CommImplPtr Mailbox::front() const
69 {
70   return pimpl_->comm_queue_.empty() ? nullptr : pimpl_->comm_queue_.front();
71 }
72
73 void Mailbox::set_receiver(ActorPtr actor)
74 {
75   kernel::actor::simcall([this, actor]() { this->pimpl_->set_receiver(actor); });
76 }
77
78 /** @brief get the receiver (process associated to the mailbox) */
79 ActorPtr Mailbox::get_receiver() const
80 {
81   if (pimpl_->permanent_receiver_ == nullptr)
82     return ActorPtr();
83   return pimpl_->permanent_receiver_->iface();
84 }
85
86 CommPtr Mailbox::put_init()
87 {
88   CommPtr res(new Comm());
89   res->sender_  = kernel::actor::ActorImpl::self();
90   res->mailbox_ = this;
91   return res;
92 }
93
94 CommPtr Mailbox::put_init(void* data, uint64_t simulated_size_in_bytes)
95 {
96   CommPtr res = put_init();
97   res->set_remaining(simulated_size_in_bytes);
98   res->src_buff_      = data;
99   res->src_buff_size_ = sizeof(void*);
100   return res;
101 }
102
103 CommPtr Mailbox::put_async(void* payload, uint64_t simulated_size_in_bytes)
104 {
105   xbt_assert(payload != nullptr, "You cannot send nullptr");
106
107   CommPtr res = put_init(payload, simulated_size_in_bytes);
108   res->vetoable_start();
109   return res;
110 }
111 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes)
112 {
113   xbt_assert(payload != nullptr, "You cannot send nullptr");
114
115   CommPtr c = put_init();
116   c->set_remaining(simulated_size_in_bytes);
117   c->set_src_data(payload);
118   c->vetoable_start();
119   c->wait();
120 }
121 /** Blocking send with timeout */
122 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes, double timeout)
123 {
124   xbt_assert(payload != nullptr, "You cannot send nullptr");
125
126   CommPtr c = put_init();
127   c->set_remaining(simulated_size_in_bytes);
128   c->set_src_data(payload);
129   c->vetoable_start();
130   c->wait_for(timeout);
131 }
132
133 CommPtr Mailbox::get_init()
134 {
135   CommPtr res(new Comm());
136   res->receiver_ = kernel::actor::ActorImpl::self();
137   res->mailbox_  = this;
138   return res;
139 }
140
141 CommPtr Mailbox::get_async(void** data)
142 {
143   CommPtr res = get_init();
144   res->set_dst_data(data, sizeof(*data));
145   res->vetoable_start();
146   return res;
147 }
148
149 void* Mailbox::get()
150 {
151   void* res = nullptr;
152   CommPtr c = get_init();
153   c->set_dst_data(&res, sizeof(res));
154   c->vetoable_start();
155   c->wait();
156   return res;
157 }
158 void* Mailbox::get(double timeout)
159 {
160   void* res = nullptr;
161   CommPtr c = get_init();
162   c->set_dst_data(&res, sizeof(res));
163   c->vetoable_start();
164   c->wait_for(timeout);
165   return res;
166 }
167
168 kernel::activity::ActivityImplPtr
169 Mailbox::iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data)
170 {
171   return kernel::actor::simcall([this, type, match_fun, data] { return pimpl_->iprobe(type, match_fun, data); });
172 }
173 } // namespace s4u
174 } // namespace simgrid
175
176 /* **************************** Public C interface *************************** */
177 sg_mailbox_t sg_mailbox_by_name(const char* alias)
178 {
179   return simgrid::s4u::Mailbox::by_name(alias);
180 }
181
182 const char* sg_mailbox_get_name(const_sg_mailbox_t mailbox)
183 {
184   return mailbox->get_cname();
185 }
186
187 /** @brief Set the mailbox to receive in asynchronous mode
188  *
189  * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
190  * The receive call will still be necessary to use the received data.
191  * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
192  *
193  * @param alias The name of the mailbox
194  */
195 void sg_mailbox_set_receiver(const char* alias)
196 {
197   simgrid::s4u::Mailbox::by_name(alias)->set_receiver(simgrid::s4u::Actor::self());
198   XBT_VERB("%s mailbox set to receive eagerly for myself\n", alias);
199 }
200
201 /** @brief Check if there is a communication going on in a mailbox.
202  *
203  * @param alias the name of the mailbox to be considered
204  * @return Returns 1 if there is a communication, 0 otherwise
205  */
206 int sg_mailbox_listen(const char* alias)
207 {
208   return simgrid::s4u::Mailbox::by_name(alias)->listen() ? 1 : 0;
209 }
210
211 void* sg_mailbox_get(sg_mailbox_t mailbox)
212 {
213   return mailbox->get();
214 }
215
216 sg_comm_t sg_mailbox_get_async(sg_mailbox_t mailbox, void** data)
217 {
218   auto comm = mailbox->get_async(data);
219   comm->add_ref();
220   return comm.get();
221 }
222
223 void sg_mailbox_put(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
224 {
225   mailbox->put(payload, simulated_size_in_bytes);
226 }
227
228 sg_comm_t sg_mailbox_put_async(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
229 {
230   auto comm = mailbox->put_async(payload, simulated_size_in_bytes);
231   comm->add_ref();
232   return comm.get();
233 }
234
235 sg_comm_t sg_mailbox_put_init(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
236 {
237   auto comm = mailbox->put_init(payload, simulated_size_in_bytes);
238   comm->add_ref();
239   return comm.get();
240 }