Logo AND Algorithmique Numérique Distribuée

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