Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
moved a line for comprehension
[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()
38 {
39   return pimpl_->comm_queue_.empty();
40 }
41
42 bool Mailbox::listen()
43 {
44   return not this->empty() || (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty());
45 }
46
47 aid_t Mailbox::listen_from()
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()
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()
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()
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   = CommPtr(new s4u::Comm());
89   res->sender_  = kernel::actor::ActorImpl::self();
90   res->mailbox_ = this;
91   return res;
92 }
93 s4u::CommPtr Mailbox::put_init(void* data, uint64_t simulated_size_in_bytes)
94 {
95   s4u::CommPtr res = put_init();
96   res->set_remaining(simulated_size_in_bytes);
97   res->src_buff_      = data;
98   res->src_buff_size_ = sizeof(void*);
99   return res;
100 }
101 s4u::CommPtr Mailbox::put_async(void* payload, uint64_t simulated_size_in_bytes)
102 {
103   xbt_assert(payload != nullptr, "You cannot send nullptr");
104
105   s4u::CommPtr res = put_init(payload, simulated_size_in_bytes);
106   res->vetoable_start();
107   return res;
108 }
109 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes)
110 {
111   xbt_assert(payload != nullptr, "You cannot send nullptr");
112
113   CommPtr c = put_init();
114   c->set_remaining(simulated_size_in_bytes);
115   c->set_src_data(payload);
116   c->vetoable_start();
117   c->wait();
118 }
119 /** Blocking send with timeout */
120 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes, double timeout)
121 {
122   xbt_assert(payload != nullptr, "You cannot send nullptr");
123
124   CommPtr c = put_init();
125   c->set_remaining(simulated_size_in_bytes);
126   c->set_src_data(payload);
127   c->vetoable_start();
128   c->wait_for(timeout);
129 }
130
131 s4u::CommPtr Mailbox::get_init()
132 {
133   CommPtr res    = CommPtr(new s4u::Comm());
134   res->receiver_ = kernel::actor::ActorImpl::self();
135   res->mailbox_  = this;
136   return res;
137 }
138 s4u::CommPtr Mailbox::get_async(void** data)
139 {
140   s4u::CommPtr res = get_init();
141   res->set_dst_data(data, sizeof(*data));
142   res->vetoable_start();
143   return res;
144 }
145
146 void* Mailbox::get()
147 {
148   void* res = nullptr;
149   CommPtr c = get_init();
150   c->set_dst_data(&res, sizeof(res));
151   c->vetoable_start();
152   c->wait();
153   return res;
154 }
155 void* Mailbox::get(double timeout)
156 {
157   void* res = nullptr;
158   CommPtr c = get_init();
159   c->set_dst_data(&res, sizeof(res));
160   c->vetoable_start();
161   c->wait_for(timeout);
162   return res;
163 }
164
165 kernel::activity::ActivityImplPtr
166 Mailbox::iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data)
167 {
168   return kernel::actor::simcall([this, type, match_fun, data] { return pimpl_->iprobe(type, match_fun, data); });
169 }
170 } // namespace s4u
171 } // namespace simgrid
172
173 /* **************************** Public C interface *************************** */
174 sg_mailbox_t sg_mailbox_by_name(const char* alias)
175 {
176   return simgrid::s4u::Mailbox::by_name(alias);
177 }
178
179 const char* sg_mailbox_get_name(const_sg_mailbox_t mailbox)
180 {
181   return mailbox->get_cname();
182 }
183
184 /** @brief Set the mailbox to receive in asynchronous mode
185  *
186  * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
187  * The receive call will still be necessary to use the received data.
188  * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
189  *
190  * @param alias The name of the mailbox
191  */
192 void sg_mailbox_set_receiver(const char* alias)
193 {
194   simgrid::s4u::Mailbox::by_name(alias)->set_receiver(simgrid::s4u::Actor::self());
195   XBT_VERB("%s mailbox set to receive eagerly for myself\n", alias);
196 }
197
198 /** @brief Check if there is a communication going on in a mailbox.
199  *
200  * @param alias the name of the mailbox to be considered
201  * @return Returns 1 if there is a communication, 0 otherwise
202  */
203 int sg_mailbox_listen(const char* alias)
204 {
205   return simgrid::s4u::Mailbox::by_name(alias)->listen() ? 1 : 0;
206 }
207
208 void* sg_mailbox_get(sg_mailbox_t mailbox)
209 {
210   return mailbox->get();
211 }
212
213 sg_comm_t sg_mailbox_get_async(sg_mailbox_t mailbox, void** data)
214 {
215   auto comm = mailbox->get_async(data);
216   comm->add_ref();
217   return comm.get();
218 }
219
220 void sg_mailbox_put(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
221 {
222   mailbox->put(payload, simulated_size_in_bytes);
223 }
224
225 sg_comm_t sg_mailbox_put_async(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
226 {
227   auto comm = mailbox->put_async(payload, simulated_size_in_bytes);
228   comm->add_ref();
229   return comm.get();
230 }
231
232 sg_comm_t sg_mailbox_put_init(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
233 {
234   auto comm = mailbox->put_init(payload, simulated_size_in_bytes);
235   comm->add_ref();
236   return comm.get();
237 }