Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / s4u / s4u_Mailbox.cpp
1 /* Copyright (c) 2006-2022. 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/mailbox.h>
7 #include <simgrid/s4u/Engine.hpp>
8 #include <simgrid/s4u/Mailbox.hpp>
9
10 #include "src/kernel/activity/MailboxImpl.hpp"
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   return Engine::get_instance()->mailbox_by_name_or_create(name);
31 }
32
33 bool Mailbox::empty() const
34 {
35   return pimpl_->empty();
36 }
37
38 size_t Mailbox::size() const
39 {
40   return pimpl_->size();
41 }
42
43 bool Mailbox::listen() const
44 {
45   return not pimpl_->empty() || (pimpl_->is_permanent() && pimpl_->has_some_done_comm());
46 }
47
48 aid_t Mailbox::listen_from() const
49 {
50   kernel::activity::CommImplPtr comm = front();
51   if (comm && comm->src_actor_)
52     return comm->src_actor_->get_pid();
53   else
54     return -1;
55 }
56
57 bool Mailbox::ready() const
58 {
59   bool comm_ready = false;
60   if (not pimpl_->empty()) {
61     comm_ready = pimpl_->front()->state_ == kernel::activity::State::DONE;
62
63   } else if (pimpl_->is_permanent() && pimpl_->has_some_done_comm()) {
64     comm_ready = pimpl_->done_front()->state_ == kernel::activity::State::DONE;
65   }
66   return comm_ready;
67 }
68
69 kernel::activity::CommImplPtr Mailbox::front() const
70 {
71   return pimpl_->empty() ? nullptr : pimpl_->front();
72 }
73
74 void Mailbox::set_receiver(ActorPtr actor)
75 {
76   kernel::actor::simcall([this, actor]() { this->pimpl_->set_receiver(actor); });
77 }
78
79 /** @brief get the receiver (process associated to the mailbox) */
80 ActorPtr Mailbox::get_receiver() const
81 {
82   if (pimpl_->is_permanent())
83     return ActorPtr();
84   return pimpl_->get_permanent_receiver()->get_iface();
85 }
86
87 CommPtr Mailbox::put_init()
88 {
89   CommPtr res(new Comm());
90   res->sender_  = kernel::actor::ActorImpl::self();
91   res->mailbox_ = this;
92   return res;
93 }
94
95 CommPtr Mailbox::put_init(void* payload, uint64_t simulated_size_in_bytes)
96 {
97   return put_init()->set_payload_size(simulated_size_in_bytes)->set_src_data(payload)->set_src_data_size(sizeof(void*));
98 }
99
100 CommPtr Mailbox::put_async(void* payload, uint64_t simulated_size_in_bytes)
101 {
102   xbt_assert(payload != nullptr, "You cannot send nullptr");
103
104   CommPtr res = put_init(payload, simulated_size_in_bytes);
105   res->vetoable_start();
106   return res;
107 }
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   put_init()->set_payload_size(simulated_size_in_bytes)->set_src_data(payload)->vetoable_start()->wait();
114 }
115
116 /** Blocking send with timeout */
117 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes, double timeout)
118 {
119   xbt_assert(payload != nullptr, "You cannot send nullptr");
120
121   put_init()->set_payload_size(simulated_size_in_bytes)->set_src_data(payload)->vetoable_start()->wait_for(timeout);
122 }
123
124 CommPtr Mailbox::get_init()
125 {
126   CommPtr res(new Comm());
127   res->receiver_ = kernel::actor::ActorImpl::self();
128   res->mailbox_  = this;
129   return res;
130 }
131
132 kernel::activity::ActivityImplPtr
133 Mailbox::iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data)
134 {
135   return kernel::actor::simcall([this, type, match_fun, data] { return pimpl_->iprobe(type, match_fun, data); });
136 }
137 } // namespace s4u
138 } // namespace simgrid
139
140 /* **************************** Public C interface *************************** */
141 sg_mailbox_t sg_mailbox_by_name(const char* alias)
142 {
143   return simgrid::s4u::Mailbox::by_name(alias);
144 }
145
146 const char* sg_mailbox_get_name(const_sg_mailbox_t mailbox)
147 {
148   return mailbox->get_cname();
149 }
150
151 /** @brief Set the mailbox to receive in asynchronous mode
152  *
153  * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
154  * The receive call will still be necessary to use the received data.
155  * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
156  *
157  * @param alias The name of the mailbox
158  */
159 void sg_mailbox_set_receiver(const char* alias)
160 {
161   simgrid::s4u::Mailbox::by_name(alias)->set_receiver(simgrid::s4u::Actor::self());
162   XBT_VERB("%s mailbox set to receive eagerly for myself\n", alias);
163 }
164
165 /** @brief Check if there is a communication going on in a mailbox.
166  *
167  * @param alias the name of the mailbox to be considered
168  * @return Returns 1 if there is a communication, 0 otherwise
169  */
170 int sg_mailbox_listen(const char* alias)
171 {
172   return simgrid::s4u::Mailbox::by_name(alias)->listen() ? 1 : 0;
173 }
174
175 void* sg_mailbox_get(sg_mailbox_t mailbox)
176 {
177   return mailbox->get<void>();
178 }
179
180 sg_comm_t sg_mailbox_get_async(sg_mailbox_t mailbox, void** data)
181 {
182   auto comm = mailbox->get_async<void>(data);
183   comm->add_ref();
184   return comm.get();
185 }
186
187 void sg_mailbox_put(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
188 {
189   mailbox->put(payload, simulated_size_in_bytes);
190 }
191
192 sg_comm_t sg_mailbox_put_async(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
193 {
194   auto comm = mailbox->put_async(payload, simulated_size_in_bytes);
195   comm->add_ref();
196   return comm.get();
197 }
198
199 sg_comm_t sg_mailbox_put_init(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
200 {
201   auto comm = mailbox->put_init(payload, simulated_size_in_bytes);
202   comm->add_ref();
203   return comm.get();
204 }