Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / src / s4u / s4u_Comm.cpp
index 670caa5..b27c782 100644 (file)
@@ -97,7 +97,7 @@ CommPtr Comm::set_dst_data(void** buff)
   return this;
 }
 
-size_t Comm::get_dst_data_size()
+size_t Comm::get_dst_data_size() const
 {
   xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__);
   return dst_buff_size_;
@@ -177,7 +177,7 @@ Comm* Comm::wait_for(double timeout)
       break;
 
     case State::STARTED:
-      simcall_comm_wait(pimpl_, timeout);
+      simcall_comm_wait(get_impl(), timeout);
       on_completion(*Actor::self());
       state_ = State::FINISHED;
       this->release_dependencies();
@@ -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;
@@ -233,7 +234,7 @@ bool Comm::test()
   if (state_ == State::INITED || state_ == State::STARTING)
     this->vetoable_start();
 
-  if (simcall_comm_test(pimpl_)) {
+  if (simcall_comm_test(get_impl())) {
     state_ = State::FINISHED;
     this->release_dependencies();
     return true;
@@ -241,12 +242,12 @@ bool Comm::test()
   return false;
 }
 
-Mailbox* Comm::get_mailbox()
+Mailbox* Comm::get_mailbox() const
 {
   return mailbox_;
 }
 
-Actor* Comm::get_sender()
+Actor* Comm::get_sender() const
 {
   return sender_ ? sender_->ciface() : nullptr;
 }
@@ -254,13 +255,81 @@ Actor* Comm::get_sender()
 } // namespace s4u
 } // namespace simgrid
 /* **************************** Public C interface *************************** */
-int sg_comm_wait_any_for(const xbt_dynar_t comms, double timeout)
+void sg_comm_detach(sg_comm_t comm, void (*clean_function)(void*))
+{
+  comm->detach(clean_function);
+  comm->unref();
+}
+void sg_comm_unref(sg_comm_t comm)
+{
+  comm->unref();
+}
+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 s4u_comm(comm, false);
+  try {
+    s4u_comm->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;
+
+  simgrid::s4u::CommPtr s4u_comm(comm, false);
+  try {
+    s4u_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;
+  }
+  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], false);
+
+  simgrid::s4u::Comm::wait_all(&s4u_comms);
+}
+
+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;
-  unsigned int i;
-  sg_comm_t comm;
-  xbt_dynar_foreach (comms, i, comm) {
-    s4u_comms.push_back(comm);
+  for (unsigned int i = 0; i < count; i++)
+    s4u_comms.emplace_back(comms[i], false);
+
+  int pos = simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
+  for (unsigned i = 0; i < count; i++) {
+    if (pos != -1 && static_cast<unsigned>(pos) != i)
+      s4u_comms[i]->add_ref();
   }
-  return simgrid::s4u::Comm::wait_any_for(&s4u_comms, timeout);
+  return pos;
 }