Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Engine is in charge of platform creation, not SIMIX anymore
[simgrid.git] / src / s4u / s4u_Mailbox.cpp
1 /* Copyright (c) 2006-2019. 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 #include <simgrid/mailbox.h>
10
11 XBT_LOG_EXTERNAL_CATEGORY(s4u);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel, s4u, "S4U Communication Mailboxes");
13
14 namespace simgrid {
15 namespace s4u {
16
17 const simgrid::xbt::string& Mailbox::get_name() const
18 {
19   return pimpl_->get_name();
20 }
21
22 const char* Mailbox::get_cname() const
23 {
24   return pimpl_->get_cname();
25 }
26
27 MailboxPtr Mailbox::by_name(std::string name)
28 {
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); });
32   }
33   return MailboxPtr(&mbox->piface_, true);
34 }
35
36 bool Mailbox::empty()
37 {
38   return pimpl_->comm_queue_.empty();
39 }
40
41 bool Mailbox::listen()
42 {
43   return not this->empty() || (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty());
44 }
45
46 bool Mailbox::ready()
47 {
48   bool comm_ready = false;
49   if (not pimpl_->comm_queue_.empty()) {
50     comm_ready = pimpl_->comm_queue_.front()->state_ == SIMIX_DONE;
51     
52   } else if (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty()) {
53     comm_ready = pimpl_->done_comm_queue_.front()->state_ == SIMIX_DONE;
54   }
55   return comm_ready;
56 }
57
58 smx_activity_t Mailbox::front()
59 {
60   return pimpl_->comm_queue_.empty() ? nullptr : pimpl_->comm_queue_.front();
61 }
62
63 void Mailbox::set_receiver(ActorPtr actor)
64 {
65   simix::simcall([this, actor]() { this->pimpl_->set_receiver(actor); });
66 }
67
68 /** @brief get the receiver (process associated to the mailbox) */
69 ActorPtr Mailbox::get_receiver()
70 {
71   if (pimpl_->permanent_receiver_ == nullptr)
72     return ActorPtr();
73   return pimpl_->permanent_receiver_->iface();
74 }
75
76 CommPtr Mailbox::put_init()
77 {
78   CommPtr res   = CommPtr(new s4u::Comm());
79   res->sender_  = SIMIX_process_self();
80   res->mailbox_ = this;
81   return res;
82 }
83 s4u::CommPtr Mailbox::put_init(void* data, uint64_t simulated_size_in_bytes)
84 {
85   s4u::CommPtr res = put_init();
86   res->set_remaining(simulated_size_in_bytes);
87   res->src_buff_      = data;
88   res->src_buff_size_ = sizeof(void*);
89   return res;
90 }
91 s4u::CommPtr Mailbox::put_async(void* payload, uint64_t simulated_size_in_bytes)
92 {
93   xbt_assert(payload != nullptr, "You cannot send nullptr");
94
95   s4u::CommPtr res = put_init(payload, simulated_size_in_bytes);
96   res->start();
97   return res;
98 }
99 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes)
100 {
101   xbt_assert(payload != nullptr, "You cannot send nullptr");
102
103   CommPtr c = put_init();
104   c->set_remaining(simulated_size_in_bytes);
105   c->set_src_data(payload);
106   c->wait();
107 }
108 /** Blocking send with timeout */
109 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes, double timeout)
110 {
111   xbt_assert(payload != nullptr, "You cannot send nullptr");
112
113   CommPtr c = put_init();
114   c->set_remaining(simulated_size_in_bytes);
115   c->set_src_data(payload);
116   // c->start() is optional.
117   c->wait_for(timeout);
118 }
119
120 s4u::CommPtr Mailbox::get_init()
121 {
122   CommPtr res    = CommPtr(new s4u::Comm());
123   res->receiver_ = SIMIX_process_self();
124   res->mailbox_  = this;
125   return res;
126 }
127 s4u::CommPtr Mailbox::get_async(void** data)
128 {
129   s4u::CommPtr res = get_init();
130   res->set_dst_data(data, sizeof(*data));
131   res->start();
132   return res;
133 }
134
135 void* Mailbox::get()
136 {
137   void* res = nullptr;
138   CommPtr c = get_init();
139   c->set_dst_data(&res, sizeof(res));
140   c->wait();
141   return res;
142 }
143 void* Mailbox::get(double timeout)
144 {
145   void* res = nullptr;
146   CommPtr c = get_init();
147   c->set_dst_data(&res, sizeof(res));
148   c->wait_for(timeout);
149   return res;
150 }
151 } // namespace s4u
152 } // namespace simgrid
153
154 /* **************************** Public C interface *************************** */
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 }