Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Need to make this test for windows.
[simgrid.git] / src / simix / smx_network.c
index 29e1bcf..b84c935 100644 (file)
@@ -156,6 +156,8 @@ smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
   comm->type = type;
   comm->sem = SIMIX_sem_init(0);
   comm->refcount = 1;
+  VERB2("Create communication %p; refcount initially %d", comm,
+         comm->refcount);
 
   return comm;
 }
@@ -169,6 +171,11 @@ void SIMIX_communication_destroy(smx_comm_t comm)
   VERB2("Destroy communication %p; refcount initially %d", comm,
         comm->refcount);
 
+  if(!(comm->refcount>0)) {
+         INFO1("There is no more reference to this comm (%p). Cannot destroy!",comm);
+         xbt_die("Argh.!");
+  }
+
 #ifdef HAVE_LATENCY_BOUND_TRACKING
   //save is latency limited flag to use afterwards
   if (latency_limited_dict == NULL) {
@@ -185,6 +192,8 @@ void SIMIX_communication_destroy(smx_comm_t comm)
   comm->refcount--;
   if (comm->refcount > 0)
     return;
+  VERB2("Really free communication %p; refcount is now %d", comm,
+        comm->refcount);
 
   if (comm->sem) {
     SIMIX_sem_destroy(comm->sem);
@@ -222,6 +231,9 @@ void SIMIX_communication_destroy(smx_comm_t comm)
 static XBT_INLINE void SIMIX_communication_use(smx_comm_t comm)
 {
   comm->refcount++;
+
+  VERB2("Use communication %p; refcount is now %d", comm,
+           comm->refcount);
 }
 
 /**
@@ -554,10 +566,19 @@ XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size,
                                    void *src_buff, size_t src_buff_size,
                                    smx_comm_t * comm_ref, void *data)
 {
-  *comm_ref =
-      SIMIX_network_isend(rdv, task_size, rate, src_buff, src_buff_size,
-                          data);
-  SIMIX_network_wait(*comm_ref, timeout);
+  xbt_ex_t e;
+  smx_comm_t comm = *comm_ref =
+             SIMIX_network_isend(rdv, task_size, rate, src_buff, src_buff_size,
+                                 data);
+  TRY {
+    SIMIX_network_wait(comm, timeout);
+  }
+  TRY_CLEANUP {
+    SIMIX_communication_destroy(comm);
+  }
+  CATCH(e) {
+    RETHROW;
+  }
 }
 
 /**
@@ -577,9 +598,18 @@ XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout,
                                    void *dst_buff, size_t * dst_buff_size,
                                    smx_comm_t * comm_ref)
 {
-  *comm_ref =
+  xbt_ex_t e;
+  smx_comm_t comm = *comm_ref =
       (smx_comm_t) SIMIX_network_irecv(rdv, dst_buff, dst_buff_size);
-  SIMIX_network_wait(*comm_ref, timeout);
+  TRY {
+    SIMIX_network_wait(comm, timeout);
+  }
+  TRY_CLEANUP {
+    SIMIX_communication_destroy(comm);
+  }
+  CATCH(e) {
+    RETHROW;
+  }
 }
 
 /******************************************************************************/
@@ -654,6 +684,7 @@ XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout)
   if (_surf_do_model_check)
     MC_trans_intercept_wait(comm);
 #endif
+  SIMIX_communication_use(comm);
   /* Wait for communication completion */
   SIMIX_communication_wait_for_completion(comm, timeout);
 }
@@ -708,6 +739,7 @@ unsigned int SIMIX_network_waitany(xbt_dynar_t comms)
   /* let the regular code deal with the communication end (errors checking and cleanup).
    * A bit of useless work will be done, but that's good for source factorization */
   SIMIX_sem_release_forever(comm_finished->sem);
+  SIMIX_communication_use(comm_finished);
   SIMIX_communication_wait_for_completion(comm_finished, -1);
   return found_comm;
 }