1 /* Copyright (c) 2006-2018. 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/s4u/Comm.hpp"
7 #include "simgrid/s4u/Mailbox.hpp"
8 #include "src/kernel/activity/MailboxImpl.hpp"
9 #include <simgrid/mailbox.h>
11 XBT_LOG_EXTERNAL_CATEGORY(s4u);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel, s4u, "S4U Communication Mailboxes");
17 const simgrid::xbt::string& Mailbox::get_name() const
19 return pimpl_->get_name();
22 const char* Mailbox::get_cname() const
24 return pimpl_->get_cname();
27 MailboxPtr Mailbox::by_name(std::string name)
29 kernel::activity::MailboxImpl* mbox = kernel::activity::MailboxImpl::by_name_or_null(name);
30 if (mbox == nullptr) {
31 mbox = simix::simcall([name] { return kernel::activity::MailboxImpl::by_name_or_create(name); });
33 return MailboxPtr(&mbox->piface_, true);
38 return pimpl_->comm_queue_.empty();
41 bool Mailbox::listen()
43 return not this->empty() || (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty());
46 smx_activity_t Mailbox::front()
48 return pimpl_->comm_queue_.empty() ? nullptr : pimpl_->comm_queue_.front();
51 void Mailbox::set_receiver(ActorPtr actor)
53 simix::simcall([this, actor]() { this->pimpl_->set_receiver(actor); });
56 /** @brief get the receiver (process associated to the mailbox) */
57 ActorPtr Mailbox::get_receiver()
59 if (pimpl_->permanent_receiver_ == nullptr)
61 return pimpl_->permanent_receiver_->iface();
64 CommPtr Mailbox::put_init()
66 CommPtr res = CommPtr(new s4u::Comm());
67 res->sender_ = SIMIX_process_self();
71 s4u::CommPtr Mailbox::put_init(void* data, uint64_t simulated_size_in_bytes)
73 s4u::CommPtr res = put_init();
74 res->set_remaining(simulated_size_in_bytes);
75 res->src_buff_ = data;
76 res->src_buff_size_ = sizeof(void*);
79 s4u::CommPtr Mailbox::put_async(void* payload, uint64_t simulated_size_in_bytes)
81 xbt_assert(payload != nullptr, "You cannot send nullptr");
83 s4u::CommPtr res = put_init(payload, simulated_size_in_bytes);
87 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes)
89 xbt_assert(payload != nullptr, "You cannot send nullptr");
91 CommPtr c = put_init();
92 c->set_remaining(simulated_size_in_bytes);
93 c->set_src_data(payload);
96 /** Blocking send with timeout */
97 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes, double timeout)
99 xbt_assert(payload != nullptr, "You cannot send nullptr");
101 CommPtr c = put_init();
102 c->set_remaining(simulated_size_in_bytes);
103 c->set_src_data(payload);
104 // c->start() is optional.
108 s4u::CommPtr Mailbox::get_init()
110 CommPtr res = CommPtr(new s4u::Comm());
111 res->receiver_ = SIMIX_process_self();
112 res->mailbox_ = this;
115 s4u::CommPtr Mailbox::get_async(void** data)
117 s4u::CommPtr res = get_init();
118 res->set_dst_data(data, sizeof(*data));
126 CommPtr c = get_init();
127 c->set_dst_data(&res, sizeof(res));
131 void* Mailbox::get(double timeout)
134 CommPtr c = get_init();
135 c->set_dst_data(&res, sizeof(res));
140 } // namespace simgrid
142 /* **************************** Public C interface *************************** */
143 /** @brief Set the mailbox to receive in asynchronous mode
145 * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
146 * The receive call will still be necessary to use the received data.
147 * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
149 * @param alias The name of the mailbox
151 void sg_mailbox_set_receiver(const char* alias)
153 simgrid::s4u::Mailbox::by_name(alias)->set_receiver(simgrid::s4u::Actor::self());
154 XBT_VERB("%s mailbox set to receive eagerly for myself\n", alias);
157 /** @brief Check if there is a communication going on in a mailbox.
159 * @param alias the name of the mailbox to be considered
160 * @return Returns 1 if there is a communication, 0 otherwise
162 int sg_mailbox_listen(const char* alias)
164 return simgrid::s4u::Mailbox::by_name(alias)->listen() ? 1 : 0;