1 /* Copyright (c) 2004-2019. 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. */
8 #include "simgrid/Exception.hpp"
9 #include "simgrid/s4u/Actor.hpp"
10 #include "simgrid/s4u/Comm.hpp"
11 #include "simgrid/s4u/Exec.hpp"
12 #include "simgrid/s4u/Mailbox.hpp"
13 #include "src/instr/instr_private.hpp"
14 #include "src/msg/msg_private.hpp"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_comm, msg, "Logging specific to MSG (comm)");
23 bool finished = false;
26 finished = s_comm->test();
27 if (finished && task_received != nullptr) {
28 /* I am the receiver */
29 (*task_received)->set_not_used();
31 } catch (const simgrid::TimeoutError&) {
32 status_ = MSG_TIMEOUT;
34 } catch (const simgrid::CancelException&) {
35 status_ = MSG_TASK_CANCELED;
38 if (e.category == network_error) {
39 status_ = MSG_TRANSFER_FAILURE;
48 msg_error_t Comm::wait_for(double timeout)
51 s_comm->wait_for(timeout);
53 if (task_received != nullptr) {
54 /* I am the receiver */
55 (*task_received)->set_not_used();
58 /* FIXME: these functions are not traceable */
59 } catch (const simgrid::TimeoutError&) {
60 status_ = MSG_TIMEOUT;
61 } catch (const simgrid::CancelException&) {
62 status_ = MSG_TASK_CANCELED;
64 if (e.category == network_error)
65 status_ = MSG_TRANSFER_FAILURE;
73 } // namespace simgrid
76 * @brief Checks whether a communication is done, and if yes, finalizes it.
77 * @param comm the communication to test
78 * @return 'true' if the communication is finished
79 * (but it may have failed, use MSG_comm_get_status() to know its status)
80 * or 'false' if the communication is not finished yet
81 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
83 int MSG_comm_test(msg_comm_t comm)
89 * @brief This function checks if a communication is finished.
90 * @param comms a vector of communications
91 * @return the position of the finished communication if any
92 * (but it may have failed, use MSG_comm_get_status() to know its status), or -1 if none is finished
94 int MSG_comm_testany(xbt_dynar_t comms)
96 int finished_index = -1;
98 /* Create the equivalent array with SIMIX objects: */
99 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
100 s_comms.reserve(xbt_dynar_length(comms));
103 xbt_dynar_foreach (comms, cursor, comm) {
104 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl()));
107 msg_error_t status = MSG_OK;
109 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
110 } catch (simgrid::TimeoutError& e) {
111 finished_index = e.value;
112 status = MSG_TIMEOUT;
113 } catch (simgrid::CancelException& e) {
114 finished_index = e.value;
115 status = MSG_TASK_CANCELED;
116 } catch (xbt_ex& e) {
117 if (e.category != network_error)
119 finished_index = e.value;
120 status = MSG_TRANSFER_FAILURE;
123 if (finished_index != -1) {
124 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
125 /* the communication is finished */
126 comm->set_status(status);
128 if (status == MSG_OK && comm->task_received != nullptr) {
129 /* I am the receiver */
130 (*comm->task_received)->set_not_used();
134 return finished_index;
137 /** @brief Destroys the provided communication. */
138 void MSG_comm_destroy(msg_comm_t comm)
143 /** @brief Wait for the completion of a communication.
145 * It takes two parameters.
146 * @param comm the communication to wait.
147 * @param timeout Wait until the communication terminates or the timeout occurs.
148 * You can provide a -1 timeout to obtain an infinite timeout.
149 * @return msg_error_t
151 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
153 return comm->wait_for(timeout);
156 /** @brief This function is called by a sender and permit to wait for each communication
158 * @param comm a vector of communication
159 * @param nb_elem is the size of the comm vector
160 * @param timeout for each call of MSG_comm_wait
162 void MSG_comm_waitall(msg_comm_t* comm, int nb_elem, double timeout)
164 for (int i = 0; i < nb_elem; i++)
165 comm[i]->wait_for(timeout);
168 /** @brief This function waits for the first communication finished in a list.
169 * @param comms a vector of communications
170 * @return the position of the first finished communication
171 * (but it may have failed, use MSG_comm_get_status() to know its status)
173 int MSG_comm_waitany(xbt_dynar_t comms)
175 int finished_index = -1;
177 /* Create the equivalent array with SIMIX objects: */
178 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
179 s_comms.reserve(xbt_dynar_length(comms));
182 xbt_dynar_foreach (comms, cursor, comm) {
183 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl()));
186 msg_error_t status = MSG_OK;
188 finished_index = simcall_comm_waitany(s_comms.data(), s_comms.size(), -1);
189 } catch (simgrid::TimeoutError& e) {
190 finished_index = e.value;
191 status = MSG_TIMEOUT;
192 } catch (simgrid::CancelException& e) {
193 finished_index = e.value;
194 status = MSG_TASK_CANCELED;
195 } catch (xbt_ex& e) {
196 if (e.category == network_error) {
197 finished_index = e.value;
198 status = MSG_TRANSFER_FAILURE;
204 xbt_assert(finished_index != -1, "WaitAny returned -1");
206 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
207 /* the communication is finished */
208 comm->set_status(status);
210 if (comm->task_received != nullptr) {
211 /* I am the receiver */
212 (*comm->task_received)->set_not_used();
215 return finished_index;
219 * @brief Returns the error (if any) that occurred during a finished communication.
220 * @param comm a finished communication
221 * @return the status of the communication, or #MSG_OK if no error occurred during the communication
223 msg_error_t MSG_comm_get_status(msg_comm_t comm)
226 return comm->get_status();
229 /** @brief Get a task (#msg_task_t) from a communication
231 * @param comm the communication where to get the task
232 * @return the task from the communication
234 msg_task_t MSG_comm_get_task(msg_comm_t comm)
236 xbt_assert(comm, "Invalid parameter");
238 return comm->task_received ? *comm->task_received : comm->task_sent;
242 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
243 * @param comm the comm
244 * @param buff the data copied
245 * @param buff_size size of the buffer
247 // deprecated but used by MSG_set_copy_callback. Should be removed in v325
248 void MSG_comm_copy_data_from_SIMIX(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
250 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
252 // notify the user callback if any
253 if (msg_global->task_copy_callback) {
254 msg_task_t task = static_cast<msg_task_t>(buff);
255 msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());