Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use a smart pointer for RAII.
[simgrid.git] / src / s4u / s4u_Comm.cpp
index b088052..84e27cb 100644 (file)
@@ -225,7 +225,8 @@ Comm* Comm::cancel()
 
 bool Comm::test()
 {
-  xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
+  xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::STARTING ||
+             state_ == State::FINISHED);
 
   if (state_ == State::FINISHED)
     return true;
@@ -254,10 +255,64 @@ Actor* Comm::get_sender() const
 } // namespace s4u
 } // namespace simgrid
 /* **************************** Public C interface *************************** */
+int sg_comm_test(sg_comm_t comm)
+{
+  bool finished = comm->test();
+  if (finished)
+    comm->unref();
+  return finished;
+}
+
+sg_error_t sg_comm_wait(sg_comm_t comm)
+{
+  sg_error_t status = SG_OK;
+
+  simgrid::s4u::CommPtr comm_ptr(comm, false);
+  try {
+    comm_ptr->wait_for(-1);
+  } catch (const simgrid::TimeoutException&) {
+    status = SG_ERROR_TIMEOUT;
+  } catch (const simgrid::CancelException&) {
+    status = SG_ERROR_CANCELED;
+  } catch (const simgrid::NetworkFailureException&) {
+    status = SG_ERROR_NETWORK;
+  }
+  return status;
+}
+
+sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
+{
+  sg_error_t status = SG_OK;
+
+  try {
+    comm->wait_for(timeout);
+  } catch (const simgrid::TimeoutException&) {
+    status = SG_ERROR_TIMEOUT;
+  } catch (const simgrid::CancelException&) {
+    status = SG_ERROR_CANCELED;
+  } catch (const simgrid::NetworkFailureException&) {
+    status = SG_ERROR_NETWORK;
+  }
+  comm->unref();
+  return status;
+}
+
+void sg_comm_wait_all(sg_comm_t* comms, size_t count)
+{
+  std::vector<simgrid::s4u::CommPtr> s4u_comms;
+  for (unsigned int i = 0; i < count; i++)
+    s4u_comms.emplace_back(comms[i]);
+
+  simgrid::s4u::Comm::wait_all(&s4u_comms);
+  for (unsigned int i = 0; i < count; i++)
+    s4u_comms[i]->unref();
+}
+
 int sg_comm_wait_any(sg_comm_t* comms, size_t count)
 {
   return sg_comm_wait_any_for(comms, count, -1);
 }
+
 int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
 {
   std::vector<simgrid::s4u::CommPtr> s4u_comms;
@@ -267,5 +322,8 @@ int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
   int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
   if (pos != -1)
     s4u_comms[pos]->unref();
+  else
+    for (const auto& c : s4u_comms)
+      c->unref();
   return pos;
 }