Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
first attempt (ongoing WIP)
[simgrid.git] / src / s4u / s4u_comm.cpp
index da54cdd..8340bd3 100644 (file)
@@ -97,11 +97,6 @@ void Comm::start() {
   } else {
     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
   }
-  while (refcount_ > 1) { // Pass all the refcounts we had to the underlying pimpl since we are delegating the
-                          // refcounting to it afterward
-    refcount_--;
-    pimpl_->ref();
-  }
   state_ = started;
 }
 void Comm::wait() {
@@ -122,13 +117,17 @@ void Comm::wait() {
     }
   }
   state_ = finished;
+  if (pimpl_)
+    pimpl_->unref();
 }
+
 void Comm::wait(double timeout) {
   xbt_assert(state_ == started || state_ == inited);
 
   if (state_ == started) {
     simcall_comm_wait(pimpl_, timeout);
     state_ = finished;
+    pimpl_->unref();
     return;
   }
 
@@ -144,6 +143,8 @@ void Comm::wait(double timeout) {
         userData_, timeout, rate_);
   }
   state_ = finished;
+  if (pimpl_)
+    pimpl_->unref();
 }
 
 void Comm::send_detached(MailboxPtr dest, void* data, int simulatedSize)
@@ -155,6 +156,7 @@ void Comm::send_detached(MailboxPtr dest, void* data, int simulatedSize)
   res->detached_    = true;
   res->start();
 }
+
 s4u::CommPtr Comm::send_async(MailboxPtr dest, void* data, int simulatedSize)
 {
   s4u::CommPtr res = CommPtr(s4u::Comm::send_init(dest));
@@ -175,21 +177,24 @@ s4u::CommPtr Comm::recv_async(MailboxPtr dest, void** data)
 
 void Comm::cancel()
 {
-  simgrid::kernel::activity::Comm* commPimpl = static_cast<simgrid::kernel::activity::Comm*>(pimpl_);
+  simgrid::kernel::activity::CommImplPtr commPimpl =
+      boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(pimpl_);
   commPimpl->cancel();
 }
+
 bool Comm::test() {
   xbt_assert(state_ == inited || state_ == started || state_ == finished);
-  
-  if (state_ == finished) 
+
+  if (state_ == finished)
     xbt_die("Don't call test on a finished comm.");
-  
+
   if (state_ == inited) {
     this->start();
   }
-  
+
   if(simcall_comm_test(pimpl_)){
     state_ = finished;
+    pimpl_->unref();
     return true;
   }
   return false;
@@ -197,23 +202,14 @@ bool Comm::test() {
 
 void intrusive_ptr_release(simgrid::s4u::Comm* c)
 {
-  if (c->pimpl_ != nullptr) {
-    if (c->pimpl_->unref()) {
-      c->pimpl_ = nullptr;
-      delete c;
-    }
-  } else if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
+  if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
     std::atomic_thread_fence(std::memory_order_acquire);
     delete c;
   }
 }
 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
 {
-  if (c->pimpl_ != nullptr) {
-    c->pimpl_->ref();
-  } else {
-    c->refcount_.fetch_add(1, std::memory_order_relaxed);
-  }
+  c->refcount_.fetch_add(1, std::memory_order_relaxed);
 }
 }
 } // namespaces