1 /* Copyright (c) 2004-2022. 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 namespace simgrid::msg {
20 bool finished = false;
23 finished = s_comm->test();
24 if (finished && task_received != nullptr) {
25 /* I am the receiver */
26 (*task_received)->set_not_used();
28 } catch (const simgrid::TimeoutException&) {
29 status_ = MSG_TIMEOUT;
31 } catch (const simgrid::CancelException&) {
32 status_ = MSG_TASK_CANCELED;
34 } catch (const simgrid::NetworkFailureException&) {
35 status_ = MSG_TRANSFER_FAILURE;
41 msg_error_t Comm::wait_for(double timeout)
44 s_comm->wait_for(timeout);
46 if (task_received != nullptr) {
47 /* I am the receiver */
48 (*task_received)->set_not_used();
51 /* FIXME: these functions are not traceable */
52 } catch (const simgrid::TimeoutException&) {
53 status_ = MSG_TIMEOUT;
54 } catch (const simgrid::CancelException&) {
55 status_ = MSG_TASK_CANCELED;
56 } catch (const simgrid::NetworkFailureException&) {
57 status_ = MSG_TRANSFER_FAILURE;
62 } // namespace simgrid::msg
65 * @brief Checks whether a communication is done, and if yes, finalizes it.
66 * @param comm the communication to test
67 * @return 'true' if the communication is finished
68 * (but it may have failed, use MSG_comm_get_status() to know its status)
69 * or 'false' if the communication is not finished yet
70 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
72 int MSG_comm_test(msg_comm_t comm)
78 * @brief This function checks if a communication is finished.
79 * @param comms a vector of communications
80 * @return the position of the finished communication if any
81 * (but it may have failed, use MSG_comm_get_status() to know its status), or -1 if none is finished
83 int MSG_comm_testany(const_xbt_dynar_t comms)
85 ssize_t finished_index = -1;
87 /* Create the equivalent array with SIMIX objects: */
88 std::vector<simgrid::s4u::CommPtr> s_comms;
89 s_comms.reserve(xbt_dynar_length(comms));
92 xbt_dynar_foreach (comms, cursor, comm)
93 s_comms.push_back(comm->s_comm);
95 msg_error_t status = MSG_OK;
97 finished_index = simgrid::s4u::Comm::test_any(s_comms);
98 } catch (const simgrid::TimeoutException& e) {
99 finished_index = e.get_value();
100 status = MSG_TIMEOUT;
101 } catch (const simgrid::CancelException& e) {
102 finished_index = e.get_value();
103 status = MSG_TASK_CANCELED;
104 } catch (const simgrid::NetworkFailureException& e) {
105 finished_index = e.get_value();
106 status = MSG_TRANSFER_FAILURE;
109 if (finished_index != -1) {
110 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
111 /* the communication is finished */
112 comm->set_status(status);
114 if (status == MSG_OK && comm->task_received != nullptr) {
115 /* I am the receiver */
116 (*comm->task_received)->set_not_used();
120 return static_cast<int>(finished_index);
123 /** @brief Destroys the provided communication. */
124 void MSG_comm_destroy(const_msg_comm_t comm)
129 /** @brief Wait for the completion of a communication.
131 * It takes two parameters.
132 * @param comm the communication to wait.
133 * @param timeout Wait until the communication terminates or the timeout occurs.
134 * You can provide a -1 timeout to obtain an infinite timeout.
135 * @return msg_error_t
137 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
139 return comm->wait_for(timeout);
142 /** @brief This function is called by a sender and permits waiting for each communication
144 * @param comm a vector of communication
145 * @param nb_elem is the size of the comm vector
146 * @param timeout for each call of MSG_comm_wait
148 void MSG_comm_waitall(msg_comm_t* comm, int nb_elem, double timeout)
150 for (int i = 0; i < nb_elem; i++)
151 comm[i]->wait_for(timeout);
154 /** @brief This function waits for the first communication finished in a list.
155 * @param comms a vector of communications
156 * @return the position of the first finished communication
157 * (but it may have failed, use MSG_comm_get_status() to know its status)
159 int MSG_comm_waitany(const_xbt_dynar_t comms)
161 ssize_t finished_index = -1;
163 /* Create the equivalent array with SIMIX objects: */
164 std::vector<simgrid::s4u::CommPtr> s_comms;
165 s_comms.reserve(xbt_dynar_length(comms));
168 xbt_dynar_foreach (comms, cursor, comm) {
169 s_comms.push_back(comm->s_comm);
172 msg_error_t status = MSG_OK;
174 finished_index = simgrid::s4u::Comm::wait_any_for(s_comms, -1);
175 } catch (const simgrid::TimeoutException& e) {
176 finished_index = e.get_value();
177 status = MSG_TIMEOUT;
178 } catch (const simgrid::CancelException& e) {
179 finished_index = e.get_value();
180 status = MSG_TASK_CANCELED;
181 } catch (const simgrid::NetworkFailureException& e) {
182 finished_index = e.get_value();
183 status = MSG_TRANSFER_FAILURE;
186 xbt_assert(finished_index != -1, "WaitAny returned -1");
188 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
189 /* the communication is finished */
190 comm->set_status(status);
192 if (comm->task_received != nullptr) {
193 /* I am the receiver */
194 (*comm->task_received)->set_not_used();
197 return static_cast<int>(finished_index);
201 * @brief Returns the error (if any) that occurred during a finished communication.
202 * @param comm a finished communication
203 * @return the status of the communication, or #MSG_OK if no error occurred during the communication
205 msg_error_t MSG_comm_get_status(const_msg_comm_t comm)
207 return comm->get_status();
210 /** @brief Get a task (#msg_task_t) from a communication
212 * @param comm the communication where to get the task
213 * @return the task from the communication
215 msg_task_t MSG_comm_get_task(const_msg_comm_t comm)
217 xbt_assert(comm, "Invalid parameter");
219 return comm->task_received ? *comm->task_received : comm->task_sent;