Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove declarations for never used signal slots.
[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
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 = simix::simcall([&name] { return kernel::activity::MailboxImpl::by_name_or_create(name); });
33   }
34   return &mbox->piface_;
35 }
36
37 bool Mailbox::empty()
38 {
39   return pimpl_->comm_queue_.empty();
40 }
41
42 bool Mailbox::listen()
43 {
44   return not this->empty() || (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty());
45 }
46
47 bool Mailbox::ready()
48 {
49   bool comm_ready = false;
50   if (not pimpl_->comm_queue_.empty()) {
51     comm_ready = pimpl_->comm_queue_.front()->state_ == SIMIX_DONE;
52     
53   } else if (pimpl_->permanent_receiver_ && not pimpl_->done_comm_queue_.empty()) {
54     comm_ready = pimpl_->done_comm_queue_.front()->state_ == SIMIX_DONE;
55   }
56   return comm_ready;
57 }
58
59 kernel::activity::CommImplPtr Mailbox::front()
60 {
61   return pimpl_->comm_queue_.empty() ? nullptr : pimpl_->comm_queue_.front();
62 }
63
64 void Mailbox::set_receiver(ActorPtr actor)
65 {
66   simix::simcall([this, actor]() { this->pimpl_->set_receiver(actor); });
67 }
68
69 /** @brief get the receiver (process associated to the mailbox) */
70 ActorPtr Mailbox::get_receiver()
71 {
72   if (pimpl_->permanent_receiver_ == nullptr)
73     return ActorPtr();
74   return pimpl_->permanent_receiver_->iface();
75 }
76
77 CommPtr Mailbox::put_init()
78 {
79   CommPtr res   = CommPtr(new s4u::Comm());
80   res->sender_  = SIMIX_process_self();
81   res->mailbox_ = this;
82   return res;
83 }
84 s4u::CommPtr Mailbox::put_init(void* data, uint64_t simulated_size_in_bytes)
85 {
86   s4u::CommPtr res = put_init();
87   res->set_remaining(simulated_size_in_bytes);
88   res->src_buff_      = data;
89   res->src_buff_size_ = sizeof(void*);
90   return res;
91 }
92 s4u::CommPtr Mailbox::put_async(void* payload, uint64_t simulated_size_in_bytes)
93 {
94   xbt_assert(payload != nullptr, "You cannot send nullptr");
95
96   s4u::CommPtr res = put_init(payload, simulated_size_in_bytes);
97   res->start();
98   return res;
99 }
100 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes)
101 {
102   xbt_assert(payload != nullptr, "You cannot send nullptr");
103
104   CommPtr c = put_init();
105   c->set_remaining(simulated_size_in_bytes);
106   c->set_src_data(payload);
107   c->wait();
108 }
109 /** Blocking send with timeout */
110 void Mailbox::put(void* payload, uint64_t simulated_size_in_bytes, double timeout)
111 {
112   xbt_assert(payload != nullptr, "You cannot send nullptr");
113
114   CommPtr c = put_init();
115   c->set_remaining(simulated_size_in_bytes);
116   c->set_src_data(payload);
117   // c->start() is optional.
118   c->wait_for(timeout);
119 }
120
121 s4u::CommPtr Mailbox::get_init()
122 {
123   CommPtr res    = CommPtr(new s4u::Comm());
124   res->receiver_ = SIMIX_process_self();
125   res->mailbox_  = this;
126   return res;
127 }
128 s4u::CommPtr Mailbox::get_async(void** data)
129 {
130   s4u::CommPtr res = get_init();
131   res->set_dst_data(data, sizeof(*data));
132   res->start();
133   return res;
134 }
135
136 void* Mailbox::get()
137 {
138   void* res = nullptr;
139   CommPtr c = get_init();
140   c->set_dst_data(&res, sizeof(res));
141   c->wait();
142   return res;
143 }
144 void* Mailbox::get(double timeout)
145 {
146   void* res = nullptr;
147   CommPtr c = get_init();
148   c->set_dst_data(&res, sizeof(res));
149   c->wait_for(timeout);
150   return res;
151 }
152
153 smx_activity_t Mailbox::iprobe(int type, int (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data)
154 {
155   return simix::simcall([this, type, match_fun, data] { return pimpl_->iprobe(type, match_fun, data); });
156 }
157 } // namespace s4u
158 } // namespace simgrid
159
160 /* **************************** Public C interface *************************** */
161 /** @brief Set the mailbox to receive in asynchronous mode
162  *
163  * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
164  * The receive call will still be necessary to use the received data.
165  * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
166  *
167  * @param alias The name of the mailbox
168  */
169 void sg_mailbox_set_receiver(const char* alias)
170 {
171   simgrid::s4u::Mailbox::by_name(alias)->set_receiver(simgrid::s4u::Actor::self());
172   XBT_VERB("%s mailbox set to receive eagerly for myself\n", alias);
173 }
174
175 /** @brief Check if there is a communication going on in a mailbox.
176  *
177  * @param alias the name of the mailbox to be considered
178  * @return Returns 1 if there is a communication, 0 otherwise
179  */
180 int sg_mailbox_listen(const char* alias)
181 {
182   return simgrid::s4u::Mailbox::by_name(alias)->listen() ? 1 : 0;
183 }