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::TimeoutException&) {
32 status_ = MSG_TIMEOUT;
34 } catch (const simgrid::CancelException&) {
35 status_ = MSG_TASK_CANCELED;
37 } catch (const simgrid::NetworkFailureException&) {
38 status_ = MSG_TRANSFER_FAILURE;
44 msg_error_t Comm::wait_for(double timeout)
47 s_comm->wait_for(timeout);
49 if (task_received != nullptr) {
50 /* I am the receiver */
51 (*task_received)->set_not_used();
54 /* FIXME: these functions are not traceable */
55 } catch (const simgrid::TimeoutException&) {
56 status_ = MSG_TIMEOUT;
57 } catch (const simgrid::CancelException&) {
58 status_ = MSG_TASK_CANCELED;
59 } catch (const simgrid::NetworkFailureException&) {
60 status_ = MSG_TRANSFER_FAILURE;
66 } // namespace simgrid
69 * @brief Checks whether a communication is done, and if yes, finalizes it.
70 * @param comm the communication to test
71 * @return 'true' if the communication is finished
72 * (but it may have failed, use MSG_comm_get_status() to know its status)
73 * or 'false' if the communication is not finished yet
74 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
76 int MSG_comm_test(msg_comm_t comm)
82 * @brief This function checks if a communication is finished.
83 * @param comms a vector of communications
84 * @return the position of the finished communication if any
85 * (but it may have failed, use MSG_comm_get_status() to know its status), or -1 if none is finished
87 int MSG_comm_testany(xbt_dynar_t comms)
89 int finished_index = -1;
91 /* Create the equivalent array with SIMIX objects: */
92 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
93 s_comms.reserve(xbt_dynar_length(comms));
96 xbt_dynar_foreach (comms, cursor, comm) {
97 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl()));
100 msg_error_t status = MSG_OK;
102 finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
103 } catch (const simgrid::TimeoutException& e) {
104 finished_index = e.value;
105 status = MSG_TIMEOUT;
106 } catch (const simgrid::CancelException& e) {
107 finished_index = e.value;
108 status = MSG_TASK_CANCELED;
109 } catch (const simgrid::NetworkFailureException& e) {
110 finished_index = e.value;
111 status = MSG_TRANSFER_FAILURE;
114 if (finished_index != -1) {
115 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
116 /* the communication is finished */
117 comm->set_status(status);
119 if (status == MSG_OK && comm->task_received != nullptr) {
120 /* I am the receiver */
121 (*comm->task_received)->set_not_used();
125 return finished_index;
128 /** @brief Destroys the provided communication. */
129 void MSG_comm_destroy(msg_comm_t comm)
134 /** @brief Wait for the completion of a communication.
136 * It takes two parameters.
137 * @param comm the communication to wait.
138 * @param timeout Wait until the communication terminates or the timeout occurs.
139 * You can provide a -1 timeout to obtain an infinite timeout.
140 * @return msg_error_t
142 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
144 return comm->wait_for(timeout);
147 /** @brief This function is called by a sender and permit to wait for each communication
149 * @param comm a vector of communication
150 * @param nb_elem is the size of the comm vector
151 * @param timeout for each call of MSG_comm_wait
153 void MSG_comm_waitall(msg_comm_t* comm, int nb_elem, double timeout)
155 for (int i = 0; i < nb_elem; i++)
156 comm[i]->wait_for(timeout);
159 /** @brief This function waits for the first communication finished in a list.
160 * @param comms a vector of communications
161 * @return the position of the first finished communication
162 * (but it may have failed, use MSG_comm_get_status() to know its status)
164 int MSG_comm_waitany(xbt_dynar_t comms)
166 int finished_index = -1;
168 /* Create the equivalent array with SIMIX objects: */
169 std::vector<simgrid::kernel::activity::CommImpl*> s_comms;
170 s_comms.reserve(xbt_dynar_length(comms));
173 xbt_dynar_foreach (comms, cursor, comm) {
174 s_comms.push_back(static_cast<simgrid::kernel::activity::CommImpl*>(comm->s_comm->get_impl()));
177 msg_error_t status = MSG_OK;
179 finished_index = simcall_comm_waitany(s_comms.data(), s_comms.size(), -1);
180 } catch (const simgrid::TimeoutException& e) {
181 finished_index = e.value;
182 status = MSG_TIMEOUT;
183 } catch (const simgrid::CancelException& e) {
184 finished_index = e.value;
185 status = MSG_TASK_CANCELED;
186 } catch (const simgrid::NetworkFailureException& e) {
187 finished_index = e.value;
188 status = MSG_TRANSFER_FAILURE;
191 xbt_assert(finished_index != -1, "WaitAny returned -1");
193 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
194 /* the communication is finished */
195 comm->set_status(status);
197 if (comm->task_received != nullptr) {
198 /* I am the receiver */
199 (*comm->task_received)->set_not_used();
202 return finished_index;
206 * @brief Returns the error (if any) that occurred during a finished communication.
207 * @param comm a finished communication
208 * @return the status of the communication, or #MSG_OK if no error occurred during the communication
210 msg_error_t MSG_comm_get_status(msg_comm_t comm)
213 return comm->get_status();
216 /** @brief Get a task (#msg_task_t) from a communication
218 * @param comm the communication where to get the task
219 * @return the task from the communication
221 msg_task_t MSG_comm_get_task(msg_comm_t comm)
223 xbt_assert(comm, "Invalid parameter");
225 return comm->task_received ? *comm->task_received : comm->task_sent;
229 * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
230 * @param comm the comm
231 * @param buff the data copied
232 * @param buff_size size of the buffer
234 // deprecated but used by MSG_set_copy_callback. Should be removed in v325
235 void MSG_comm_copy_data_from_SIMIX(simgrid::kernel::activity::CommImpl* comm, void* buff, size_t buff_size)
237 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
239 // notify the user callback if any
240 if (msg_global->task_copy_callback) {
241 msg_task_t task = static_cast<msg_task_t>(buff);
242 msg_global->task_copy_callback(task, comm->src_actor_->ciface(), comm->dst_actor_->ciface());