1 /* Copyright (c) 2006-2022. The SimGrid Team. All rights reserved. */
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. */
6 #include <simgrid/mailbox.h>
7 #include <simgrid/s4u/Engine.hpp>
8 #include <simgrid/s4u/Mailbox.hpp>
10 #include "src/kernel/activity/MailboxImpl.hpp"
12 XBT_LOG_EXTERNAL_CATEGORY(s4u);
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel, s4u, "S4U Communication Mailboxes");
15 namespace simgrid::s4u {
17 const xbt::string& Mailbox::get_name() const
19 return pimpl_->get_name();
22 const char* Mailbox::get_cname() const
24 return pimpl_->get_cname();
27 Mailbox* Mailbox::by_name(const std::string& name)
29 return Engine::get_instance()->mailbox_by_name_or_create(name);
32 bool Mailbox::empty() const
34 return pimpl_->empty();
37 size_t Mailbox::size() const
39 return pimpl_->size();
42 bool Mailbox::listen() const
44 return not pimpl_->empty() || (pimpl_->is_permanent() && pimpl_->has_some_done_comm());
47 aid_t Mailbox::listen_from() const
49 kernel::activity::CommImplPtr comm = front();
50 if (comm && comm->src_actor_)
51 return comm->src_actor_->get_pid();
56 bool Mailbox::ready() const
58 bool comm_ready = false;
59 if (not pimpl_->empty()) {
60 comm_ready = pimpl_->front()->get_state() == kernel::activity::State::DONE;
62 } else if (pimpl_->is_permanent() && pimpl_->has_some_done_comm()) {
63 comm_ready = pimpl_->done_front()->get_state() == kernel::activity::State::DONE;
68 kernel::activity::CommImplPtr Mailbox::front() const
70 return pimpl_->empty() ? nullptr : pimpl_->front();
73 void Mailbox::set_receiver(ActorPtr actor)
75 kernel::actor::simcall_answered([this, actor]() { this->pimpl_->set_receiver(actor); });
78 /** @brief get the receiver (process associated to the mailbox) */
79 ActorPtr Mailbox::get_receiver() const
81 if (pimpl_->is_permanent())
83 return pimpl_->get_permanent_receiver()->get_iface();
86 CommPtr Mailbox::put_init()
88 CommPtr res(new Comm());
89 res->sender_ = kernel::actor::ActorImpl::self();
90 res->set_mailbox(this);
94 CommPtr Mailbox::put_init(void* payload, uint64_t simulated_size_in_bytes)
96 return put_init()->set_payload_size(simulated_size_in_bytes)->set_src_data(payload)->set_src_data_size(sizeof(void*));
99 CommPtr Mailbox::put_async(void* payload, uint64_t simulated_size_in_bytes)
101 xbt_assert(payload != nullptr, "You cannot send nullptr");
103 CommPtr res = put_init(payload, simulated_size_in_bytes);
104 res->vetoable_start();
108 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes)
110 xbt_assert(payload != nullptr, "You cannot send nullptr");
112 put_init()->set_payload_size(simulated_size_in_bytes)->set_src_data(payload)->vetoable_start()->wait();
115 /** Blocking send with timeout */
116 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes, double timeout)
118 xbt_assert(payload != nullptr, "You cannot send nullptr");
120 put_init()->set_payload_size(simulated_size_in_bytes)->set_src_data(payload)->vetoable_start()->wait_for(timeout);
123 CommPtr Mailbox::get_init()
125 auto res = CommPtr(new Comm())->set_mailbox(this);
126 res->receiver_ = kernel::actor::ActorImpl::self();
130 kernel::activity::ActivityImplPtr
131 Mailbox::iprobe(int type, const std::function<bool(void*, void*, kernel::activity::CommImpl*)>& match_fun, void* data)
133 return kernel::actor::simcall_answered(
134 [this, type, &match_fun, data] { return pimpl_->iprobe(type, match_fun, data); });
137 void Mailbox::clear()
139 kernel::actor::simcall_answered([this]() { this->pimpl_->clear(); });
142 } // namespace simgrid::s4u
144 /* **************************** Public C interface *************************** */
145 sg_mailbox_t sg_mailbox_by_name(const char* alias)
147 return simgrid::s4u::Mailbox::by_name(alias);
150 const char* sg_mailbox_get_name(const_sg_mailbox_t mailbox)
152 return mailbox->get_cname();
155 /** @brief Set the mailbox to receive in asynchronous mode
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.
161 * @param alias The name of the mailbox
163 void sg_mailbox_set_receiver(const char* alias)
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);
169 /** @brief Check if there is a communication going on in a mailbox.
171 * @param alias the name of the mailbox to be considered
172 * @return Returns 1 if there is a communication, 0 otherwise
174 int sg_mailbox_listen(const char* alias)
176 return simgrid::s4u::Mailbox::by_name(alias)->listen() ? 1 : 0;
179 void* sg_mailbox_get(sg_mailbox_t mailbox)
181 return mailbox->get<void>();
184 sg_comm_t sg_mailbox_get_async(sg_mailbox_t mailbox, void** data)
186 auto comm = mailbox->get_async<void>(data);
191 void sg_mailbox_put(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
193 mailbox->put(payload, simulated_size_in_bytes);
196 sg_comm_t sg_mailbox_put_async(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
198 auto comm = mailbox->put_async(payload, simulated_size_in_bytes);
203 sg_comm_t sg_mailbox_put_init(sg_mailbox_t mailbox, void* payload, long simulated_size_in_bytes)
205 auto comm = mailbox->put_init(payload, simulated_size_in_bytes);