Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more than 100 codacy treats, not bad
[simgrid.git] / src / simix / smx_network.cpp
index 852db10..6233044 100644 (file)
@@ -45,21 +45,20 @@ _find_matching_comm(boost::circular_buffer_space_optimized<smx_activity_t>* dequ
 
   for(auto it = deque->begin(); it != deque->end(); it++){
     smx_activity_t synchro = *it;
-    simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
+    simgrid::kernel::activity::Commcomm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
 
     if (comm->type == SIMIX_COMM_SEND) {
       other_user_data = comm->src_data;
     } else if (comm->type == SIMIX_COMM_RECEIVE) {
       other_user_data = comm->dst_data;
     }
-    if (comm->type == type &&
-        (!      match_fun ||       match_fun(this_user_data,  other_user_data, synchro)) &&
-        (!comm->match_fun || comm->match_fun(other_user_data, this_user_data,  my_synchro))) {
+    if (comm->type == type && (match_fun == nullptr || match_fun(this_user_data, other_user_data, synchro)) &&
+        (not comm->match_fun || comm->match_fun(other_user_data, this_user_data, my_synchro))) {
       XBT_DEBUG("Found a matching communication synchro %p", comm);
       if (remove_matching)
         deque->erase(it);
-      comm->ref();
-#if HAVE_MC
+      comm = static_cast<simgrid::kernel::activity::Comm*>(SIMIX_comm_ref(comm));
+#if SIMGRID_HAVE_MC
       comm->mbox_cpy = comm->mbox;
 #endif
       comm->mbox = nullptr;
@@ -108,14 +107,14 @@ XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(smx_simcall_t simcall, smx
   simgrid::kernel::activity::Comm* other_comm =
       _find_matching_comm(&mbox->comm_queue, SIMIX_COMM_RECEIVE, match_fun, data, this_comm, /*remove_matching*/ true);
 
-  if (!other_comm) {
+  if (not other_comm) {
     other_comm = this_comm;
 
     if (mbox->permanent_receiver!=nullptr){
       //this mailbox is for small messages, which have to be sent right now
       other_comm->state   = SIMIX_READY;
       other_comm->dst_proc=mbox->permanent_receiver.get();
-      other_comm->ref();
+      other_comm          = static_cast<simgrid::kernel::activity::Comm*>(SIMIX_comm_ref(other_comm));
       mbox->done_comm_queue.push_back(other_comm);
       XBT_DEBUG("pushing a message into the permanent receive list %p, comm %p", mbox, &(other_comm));
 
@@ -124,7 +123,7 @@ XBT_PRIVATE smx_activity_t simcall_HANDLER_comm_isend(smx_simcall_t simcall, smx
     }
   } else {
     XBT_DEBUG("Receive already pushed");
-    this_comm->unref();
+    SIMIX_comm_unref(this_comm);
 
     other_comm->state = SIMIX_READY;
     other_comm->type = SIMIX_COMM_READY;
@@ -190,14 +189,14 @@ smx_activity_t SIMIX_comm_irecv(smx_actor_t dst_proc, smx_mailbox_t mbox, void *
 
   simgrid::kernel::activity::Comm* other_comm;
   //communication already done, get it inside the list of completed comms
-  if (mbox->permanent_receiver != nullptr && ! mbox->done_comm_queue.empty()) {
+  if (mbox->permanent_receiver != nullptr && not mbox->done_comm_queue.empty()) {
 
     XBT_DEBUG("We have a comm that has probably already been received, trying to match it, to skip the communication");
     //find a match in the list of already received comms
     other_comm = _find_matching_comm(&mbox->done_comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,
                                      /*remove_matching*/ true);
     //if not found, assume the receiver came first, register it to the mailbox in the classical way
-    if (!other_comm) {
+    if (not other_comm) {
       XBT_DEBUG("We have messages in the permanent receive list, but not the one we are looking for, pushing request into list");
       other_comm = this_synchro;
       mbox->push(this_synchro);
@@ -208,8 +207,8 @@ smx_activity_t SIMIX_comm_irecv(smx_actor_t dst_proc, smx_mailbox_t mbox, void *
         other_comm->type = SIMIX_COMM_DONE;
         other_comm->mbox = nullptr;
       }
-      other_comm->unref();
-      static_cast<simgrid::kernel::activity::Comm*>(this_synchro)->unref();
+      SIMIX_comm_unref(other_comm);
+      SIMIX_comm_unref(this_synchro);
     }
   } else {
     /* Prepare a comm describing us, so that it gets passed to the user-provided filter of other side */
@@ -221,12 +220,13 @@ smx_activity_t SIMIX_comm_irecv(smx_actor_t dst_proc, smx_mailbox_t mbox, void *
     other_comm = _find_matching_comm(&mbox->comm_queue, SIMIX_COMM_SEND, match_fun, data, this_synchro,
                                      /*remove_matching*/ true);
 
-    if (!other_comm) {
+    if (not other_comm) {
       XBT_DEBUG("Receive pushed first %zu", mbox->comm_queue.size());
       other_comm = this_synchro;
       mbox->push(this_synchro);
     } else {
-      this_synchro->unref();
+      SIMIX_comm_unref(this_synchro);
+      other_comm = static_cast<simgrid::kernel::activity::Comm*>(other_comm);
 
       other_comm->state = SIMIX_READY;
       other_comm->type = SIMIX_COMM_READY;
@@ -276,21 +276,21 @@ smx_activity_t SIMIX_comm_iprobe(smx_actor_t dst_proc, smx_mailbox_t mbox, int t
     smx_type = SIMIX_COMM_SEND;
   } 
   smx_activity_t other_synchro=nullptr;
-  if (mbox->permanent_receiver != nullptr && !mbox->done_comm_queue.empty()) {
+  if (mbox->permanent_receiver != nullptr && not mbox->done_comm_queue.empty()) {
     XBT_DEBUG("first check in the permanent recv mailbox, to see if we already got something");
     other_synchro = _find_matching_comm(&mbox->done_comm_queue,
       (e_smx_comm_type_t) smx_type, match_fun, data, this_comm,/*remove_matching*/false);
   }
-  if (!other_synchro){
+  if (not other_synchro) {
     XBT_DEBUG("check if we have more luck in the normal mailbox");
     other_synchro = _find_matching_comm(&mbox->comm_queue,
       (e_smx_comm_type_t) smx_type, match_fun, data, this_comm,/*remove_matching*/false);
   }
 
   if(other_synchro)
-    other_synchro->unref();
+    SIMIX_comm_unref(other_synchro);
 
-  this_comm->unref();
+  SIMIX_comm_unref(this_comm);
   return other_synchro;
 }
 
@@ -502,16 +502,14 @@ static inline void SIMIX_comm_start(smx_activity_t synchro)
 void SIMIX_comm_finish(smx_activity_t synchro)
 {
   simgrid::kernel::activity::Comm *comm = static_cast<simgrid::kernel::activity::Comm*>(synchro);
-  unsigned int destroy_count = 0;
 
-  while (!synchro->simcalls.empty()) {
+  while (not synchro->simcalls.empty()) {
     smx_simcall_t simcall = synchro->simcalls.front();
     synchro->simcalls.pop_front();
 
-    /* If a waitany simcall is waiting for this synchro to finish, then remove
-       it from the other synchros in the waitany list. Afterwards, get the
-       position of the actual synchro in the waitany dynar and
-       return it as the result of the simcall */
+    /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
+     * list. Afterwards, get the position of the actual synchro in the waitany dynar and return it as the result of the
+     * simcall */
 
     if (simcall->call == SIMCALL_NONE) //FIXME: maybe a better way to handle this case
       continue; // if process handling comm is killed
@@ -521,8 +519,9 @@ void SIMIX_comm_finish(smx_activity_t synchro)
         SIMIX_timer_remove(simcall->timer);
         simcall->timer = nullptr;
       }
-      if (!MC_is_active() && !MC_record_replay_is_active())
-        simcall_comm_waitany__set__result(simcall, xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
+      if (not MC_is_active() && not MC_record_replay_is_active())
+        simcall_comm_waitany__set__result(simcall,
+                                          xbt_dynar_search(simcall_comm_waitany__get__comms(simcall), &synchro));
     }
 
     /* If the synchro is still in a rendez-vous point then remove from it */
@@ -643,16 +642,10 @@ void SIMIX_comm_finish(smx_activity_t synchro)
         comm->dst_proc->comms.remove(synchro);
         comm->src_proc->comms.remove(synchro);
       }
-      //in case of a detached comm we have an extra ref to remove, as the sender won't do it
-      destroy_count++;
     }
 
     SIMIX_simcall_answer(simcall);
-    destroy_count++;
   }
-
-  while (destroy_count-- > 0)
-    static_cast<simgrid::kernel::activity::Comm*>(synchro)->unref();
 }
 
 /******************************************************************************/
@@ -685,7 +678,6 @@ void SIMIX_comm_copy_buffer_callback(smx_activity_t synchro, void* buff, size_t
   }
 }
 
-
 /**
  *  @brief Copy the communication data from the sender's buffer to the receiver's one
  *  @param synchro The communication
@@ -696,7 +688,7 @@ void SIMIX_comm_copy_data(smx_activity_t synchro)
 
   size_t buff_size = comm->src_buff_size;
   /* If there is no data to copy then return */
-  if (!comm->src_buff || !comm->dst_buff || comm->copied)
+  if (not comm->src_buff || not comm->dst_buff || comm->copied)
     return;
 
   XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)", comm,
@@ -718,8 +710,22 @@ void SIMIX_comm_copy_data(smx_activity_t synchro)
         SIMIX_comm_copy_data_callback (comm, comm->src_buff, buff_size);
   }
 
-
   /* Set the copied flag so we copy data only once */
   /* (this function might be called from both communication ends) */
   comm->copied = 1;
 }
+
+/** Increase the refcount for this comm */
+smx_activity_t SIMIX_comm_ref(smx_activity_t comm)
+{
+  if (comm != nullptr)
+    intrusive_ptr_add_ref(comm);
+  return comm;
+}
+
+/** Decrease the refcount for this comm */
+void SIMIX_comm_unref(smx_activity_t comm)
+{
+  if (comm != nullptr)
+    intrusive_ptr_release(comm);
+}