Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
added doc for wifi ns3
[simgrid.git] / src / s4u / s4u_Comm.cpp
index 84e27cb071d01211fd7f855dc44e40447dd52a13..46515ea9347b795ed7c92f10b9f223ddff7d2d8f 100644 (file)
@@ -35,7 +35,7 @@ Comm::~Comm()
 
 int Comm::wait_any_for(const std::vector<CommPtr>* comms, double timeout)
 {
-  std::unique_ptr<kernel::activity::CommImpl* []> rcomms(new kernel::activity::CommImpl*[comms->size()]);
+  auto rcomms = std::make_unique<kernel::activity::CommImpl*[]>(comms->size());
   std::transform(begin(*comms), end(*comms), rcomms.get(),
                  [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
   int changed_pos = simcall_comm_waitany(rcomms.get(), comms->size(), timeout);
@@ -138,6 +138,10 @@ Comm* Comm::start()
   } else {
     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
   }
+
+  if (suspended_)
+    pimpl_->suspend();
+
   state_ = State::STARTED;
   return this;
 }
@@ -194,7 +198,7 @@ Comm* Comm::wait_for(double timeout)
 
 int Comm::test_any(const std::vector<CommPtr>* comms)
 {
-  std::unique_ptr<kernel::activity::CommImpl* []> rcomms(new kernel::activity::CommImpl*[comms->size()]);
+  auto rcomms = std::make_unique<kernel::activity::CommImpl*[]>(comms->size());
   std::transform(begin(*comms), end(*comms), rcomms.get(),
                  [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
   int changed_pos = simcall_comm_testany(rcomms.get(), comms->size());
@@ -249,12 +253,24 @@ Mailbox* Comm::get_mailbox() const
 
 Actor* Comm::get_sender() const
 {
-  return sender_ ? sender_->ciface() : nullptr;
+  kernel::actor::ActorImplPtr sender = nullptr;
+  if (pimpl_)
+    sender = boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->src_actor_;
+  return sender ? sender->ciface() : nullptr;
 }
 
 } // namespace s4u
 } // namespace simgrid
 /* **************************** Public C interface *************************** */
+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();
@@ -267,9 +283,9 @@ sg_error_t sg_comm_wait(sg_comm_t comm)
 {
   sg_error_t status = SG_OK;
 
-  simgrid::s4u::CommPtr comm_ptr(comm, false);
+  simgrid::s4u::CommPtr s4u_comm(comm, false);
   try {
-    comm_ptr->wait_for(-1);
+    s4u_comm->wait_for(-1);
   } catch (const simgrid::TimeoutException&) {
     status = SG_ERROR_TIMEOUT;
   } catch (const simgrid::CancelException&) {
@@ -284,8 +300,9 @@ 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 {
-    comm->wait_for(timeout);
+    s4u_comm->wait_for(timeout);
   } catch (const simgrid::TimeoutException&) {
     status = SG_ERROR_TIMEOUT;
   } catch (const simgrid::CancelException&) {
@@ -293,7 +310,6 @@ sg_error_t sg_comm_wait_for(sg_comm_t comm, double timeout)
   } catch (const simgrid::NetworkFailureException&) {
     status = SG_ERROR_NETWORK;
   }
-  comm->unref();
   return status;
 }
 
@@ -301,11 +317,9 @@ 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]);
+    s4u_comms.emplace_back(comms[i], false);
 
   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)
@@ -316,14 +330,13 @@ int sg_comm_wait_any(sg_comm_t* comms, size_t count)
 int sg_comm_wait_any_for(sg_comm_t* comms, size_t count, double timeout)
 {
   std::vector<simgrid::s4u::CommPtr> s4u_comms;
-  for (unsigned int i = 0; i < count; i++) {
-    s4u_comms.emplace_back(comms[i]);
-  }
+  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);
-  if (pos != -1)
-    s4u_comms[pos]->unref();
-  else
-    for (const auto& c : s4u_comms)
-      c->unref();
+  for (unsigned i = 0; i < count; i++) {
+    if (pos != -1 && static_cast<unsigned>(pos) != i)
+      s4u_comms[i]->add_ref();
+  }
   return pos;
 }