Logo AND Algorithmique Numérique Distribuée

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