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"
21 bool finished = false;
24 finished = s_comm->test();
25 if (finished && task_received != nullptr) {
26 /* I am the receiver */
27 (*task_received)->set_not_used();
29 } catch (const simgrid::TimeoutException&) {
30 status_ = MSG_TIMEOUT;
32 } catch (const simgrid::CancelException&) {
33 status_ = MSG_TASK_CANCELED;
35 } catch (const simgrid::NetworkFailureException&) {
36 status_ = MSG_TRANSFER_FAILURE;
42 msg_error_t Comm::wait_for(double timeout)
45 s_comm->wait_for(timeout);
47 if (task_received != nullptr) {
48 /* I am the receiver */
49 (*task_received)->set_not_used();
52 /* FIXME: these functions are not traceable */
53 } catch (const simgrid::TimeoutException&) {
54 status_ = MSG_TIMEOUT;
55 } catch (const simgrid::CancelException&) {
56 status_ = MSG_TASK_CANCELED;
57 } catch (const simgrid::NetworkFailureException&) {
58 status_ = MSG_TRANSFER_FAILURE;
64 } // namespace simgrid
67 * @brief Checks whether a communication is done, and if yes, finalizes it.
68 * @param comm the communication to test
69 * @return 'true' if the communication is finished
70 * (but it may have failed, use MSG_comm_get_status() to know its status)
71 * or 'false' if the communication is not finished yet
72 * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
74 int MSG_comm_test(msg_comm_t comm)
80 * @brief This function checks if a communication is finished.
81 * @param comms a vector of communications
82 * @return the position of the finished communication if any
83 * (but it may have failed, use MSG_comm_get_status() to know its status), or -1 if none is finished
85 int MSG_comm_testany(const_xbt_dynar_t comms)
87 ssize_t finished_index = -1;
89 /* Create the equivalent array with SIMIX objects: */
90 std::vector<simgrid::s4u::CommPtr> s_comms;
91 s_comms.reserve(xbt_dynar_length(comms));
94 xbt_dynar_foreach (comms, cursor, comm)
95 s_comms.push_back(comm->s_comm);
97 msg_error_t status = MSG_OK;
99 finished_index = simgrid::s4u::Comm::test_any(s_comms);
100 } catch (const simgrid::TimeoutException& e) {
101 finished_index = e.get_value();
102 status = MSG_TIMEOUT;
103 } catch (const simgrid::CancelException& e) {
104 finished_index = e.get_value();
105 status = MSG_TASK_CANCELED;
106 } catch (const simgrid::NetworkFailureException& e) {
107 finished_index = e.get_value();
108 status = MSG_TRANSFER_FAILURE;
111 if (finished_index != -1) {
112 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
113 /* the communication is finished */
114 comm->set_status(status);
116 if (status == MSG_OK && comm->task_received != nullptr) {
117 /* I am the receiver */
118 (*comm->task_received)->set_not_used();
122 return static_cast<int>(finished_index);
125 /** @brief Destroys the provided communication. */
126 void MSG_comm_destroy(const_msg_comm_t comm)
131 /** @brief Wait for the completion of a communication.
133 * It takes two parameters.
134 * @param comm the communication to wait.
135 * @param timeout Wait until the communication terminates or the timeout occurs.
136 * You can provide a -1 timeout to obtain an infinite timeout.
137 * @return msg_error_t
139 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
141 return comm->wait_for(timeout);
144 /** @brief This function is called by a sender and permits waiting for each communication
146 * @param comm a vector of communication
147 * @param nb_elem is the size of the comm vector
148 * @param timeout for each call of MSG_comm_wait
150 void MSG_comm_waitall(msg_comm_t* comm, int nb_elem, double timeout)
152 for (int i = 0; i < nb_elem; i++)
153 comm[i]->wait_for(timeout);
156 /** @brief This function waits for the first communication finished in a list.
157 * @param comms a vector of communications
158 * @return the position of the first finished communication
159 * (but it may have failed, use MSG_comm_get_status() to know its status)
161 int MSG_comm_waitany(const_xbt_dynar_t comms)
163 ssize_t finished_index = -1;
165 /* Create the equivalent array with SIMIX objects: */
166 std::vector<simgrid::s4u::CommPtr> s_comms;
167 s_comms.reserve(xbt_dynar_length(comms));
170 xbt_dynar_foreach (comms, cursor, comm) {
171 s_comms.push_back(comm->s_comm);
174 msg_error_t status = MSG_OK;
176 finished_index = simgrid::s4u::Comm::wait_any_for(s_comms, -1);
177 } catch (const simgrid::TimeoutException& e) {
178 finished_index = e.get_value();
179 status = MSG_TIMEOUT;
180 } catch (const simgrid::CancelException& e) {
181 finished_index = e.get_value();
182 status = MSG_TASK_CANCELED;
183 } catch (const simgrid::NetworkFailureException& e) {
184 finished_index = e.get_value();
185 status = MSG_TRANSFER_FAILURE;
188 xbt_assert(finished_index != -1, "WaitAny returned -1");
190 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
191 /* the communication is finished */
192 comm->set_status(status);
194 if (comm->task_received != nullptr) {
195 /* I am the receiver */
196 (*comm->task_received)->set_not_used();
199 return static_cast<int>(finished_index);
203 * @brief Returns the error (if any) that occurred during a finished communication.
204 * @param comm a finished communication
205 * @return the status of the communication, or #MSG_OK if no error occurred during the communication
207 msg_error_t MSG_comm_get_status(const_msg_comm_t comm)
209 return comm->get_status();
212 /** @brief Get a task (#msg_task_t) from a communication
214 * @param comm the communication where to get the task
215 * @return the task from the communication
217 msg_task_t MSG_comm_get_task(const_msg_comm_t comm)
219 xbt_assert(comm, "Invalid parameter");
221 return comm->task_received ? *comm->task_received : comm->task_sent;