Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more clear paranoia message
[simgrid.git] / src / simix / smx_network.c
1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/log.h"
9 #include "mc/mc.h"
10 #include "xbt/dict.h"
11
12 /* Pimple to get an histogram of message sizes in the simulation */
13 xbt_dict_t msg_sizes = NULL;
14 #ifdef HAVE_LATENCY_BOUND_TRACKING
15 xbt_dict_t latency_limited_dict = NULL;
16 #endif
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
19                                 "Logging specific to SIMIX (network)");
20
21 /******************************************************************************/
22 /*                           Rendez-Vous Points                               */
23 /******************************************************************************/
24
25 /**
26  *  \brief Creates a new rendez-vous point
27  *  \param name The name of the rendez-vous point
28  *  \return The created rendez-vous point
29  */
30 smx_rdv_t SIMIX_rdv_create(const char *name)
31 {
32   smx_rdv_t rdv = xbt_new0(s_smx_rvpoint_t, 1);
33   rdv->name = name ? xbt_strdup(name) : NULL;
34   rdv->read = SIMIX_mutex_init();
35   rdv->write = SIMIX_mutex_init();
36   rdv->comm_fifo = xbt_fifo_new();
37
38   return rdv;
39 }
40
41 /**
42  *  \brief Destroy a rendez-vous point
43  *  \param name The rendez-vous point to destroy
44  */
45 void SIMIX_rdv_destroy(smx_rdv_t rdv)
46 {
47   if (rdv->name)
48     xbt_free(rdv->name);
49   SIMIX_mutex_destroy(rdv->read);
50   SIMIX_mutex_destroy(rdv->write);
51   xbt_fifo_free(rdv->comm_fifo);
52   xbt_free(rdv);
53 }
54
55 /**
56  *  \brief Push a communication request into a rendez-vous point
57  *  \param rdv The rendez-vous point
58  *  \param comm The communication request
59  */
60 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
61 {
62   xbt_fifo_push(rdv->comm_fifo, comm);
63   comm->rdv = rdv;
64 }
65
66 /**
67  *  \brief Remove a communication request from a rendez-vous point
68  *  \param rdv The rendez-vous point
69  *  \param comm The communication request
70  */
71 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
72 {
73   xbt_fifo_remove(rdv->comm_fifo, comm);
74   comm->rdv = NULL;
75 }
76
77 /**
78  *  \brief Checks if there is a communication request queued in a rendez-vous matching our needs
79  *  \param type The type of communication we are looking for (comm_send, comm_recv)
80  *  \return The communication request if found, NULL otherwise.
81  */
82 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type)
83 {
84   smx_comm_t comm = (smx_comm_t)
85       xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
86
87   if (comm && comm->type == type) {
88     DEBUG0("Communication request found!");
89     xbt_fifo_shift(rdv->comm_fifo);
90     SIMIX_communication_use(comm);
91     comm->rdv = NULL;
92     return comm;
93   }
94
95   DEBUG0("Communication request not found");
96   return NULL;
97 }
98
99 /**
100  *  \brief counts the number of communication requests of a given host pending
101  *         on a rendez-vous point
102  *  \param rdv The rendez-vous point
103  *  \param host The host to be counted
104  *  \return The number of comm request pending in the rdv
105  */
106 int SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
107 {
108   smx_comm_t comm = NULL;
109   xbt_fifo_item_t item = NULL;
110   int count = 0;
111
112   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
113     if (comm->src_proc->smx_host == host)
114       count++;
115   }
116
117   return count;
118 }
119
120 /**
121  *  \brief returns the communication at the head of the rendez-vous
122  *  \param rdv The rendez-vous point
123  *  \return The communication or NULL if empty
124  */
125 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
126 {
127   return (smx_comm_t)
128       xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
129 }
130
131 /** @brief adds some API-related data to the rendez-vous point */
132 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv, void *data)
133 {
134   rdv->data = data;
135 }
136
137 /** @brief gets API-related data from the rendez-vous point */
138 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv)
139 {
140   return rdv->data;
141 }
142
143 /******************************************************************************/
144 /*                           Communication Requests                           */
145 /******************************************************************************/
146
147 /**
148  *  \brief Creates a new communication request
149  *  \param type The type of communication (comm_send, comm_recv)
150  *  \return The new communication request
151  */
152 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
153 {
154   /* alloc structures */
155   smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
156   comm->type = type;
157   comm->sem = SIMIX_sem_init(0);
158   comm->refcount = 1;
159   VERB2("Create communication %p; refcount initially %d", comm,
160          comm->refcount);
161
162   return comm;
163 }
164
165 /**
166  *  \brief Destroy a communication request
167  *  \param comm The request to be destroyed
168  */
169 void SIMIX_communication_destroy(smx_comm_t comm)
170 {
171   VERB2("Destroy communication %p; refcount initially %d", comm,
172         comm->refcount);
173
174   if(!(comm->refcount>0))
175           xbt_die(bprintf("the refcount of comm %p is already 0 before decreasing it. That's a bug!",comm));
176
177 #ifdef HAVE_LATENCY_BOUND_TRACKING
178   //save is latency limited flag to use afterwards
179   if (latency_limited_dict == NULL) {
180     latency_limited_dict = xbt_dict_new();
181   }
182   if (comm->act) {
183     DEBUG2("adding key %p with latency limited value %d to the dict", comm,
184            SIMIX_action_is_latency_bounded(comm->act));
185     xbt_dicti_set(latency_limited_dict, (uintptr_t) comm,
186                   SIMIX_action_is_latency_bounded(comm->act));
187   }
188 #endif
189
190   comm->refcount--;
191   if (comm->refcount > 0)
192     return;
193   VERB2("Really free communication %p; refcount is now %d", comm,
194         comm->refcount);
195
196   if (comm->sem) {
197     SIMIX_sem_destroy(comm->sem);
198     comm->sem = NULL;
199   }
200
201   if (comm->act) {
202     SIMIX_action_destroy(comm->act);
203     comm->act = NULL;
204   }
205
206   if (comm->src_timeout) {
207     SIMIX_action_destroy(comm->src_timeout);
208     comm->src_timeout = NULL;
209   }
210
211   if (comm->dst_timeout) {
212     SIMIX_action_destroy(comm->dst_timeout);
213     comm->dst_timeout = NULL;
214   }
215
216
217
218   xbt_free(comm);
219 }
220
221 /**
222  *  \brief Increase the number of users of the communication.
223  *  \param comm The communication request
224  *  Each communication request can be used by more than one process, so it is
225  *  necessary to know number of them at destroy time, to avoid freeing stuff that
226  *  maybe is in use by others.
227  *  \
228  */
229 static XBT_INLINE void SIMIX_communication_use(smx_comm_t comm)
230 {
231   comm->refcount++;
232
233   VERB2("Use communication %p; refcount is now %d", comm,
234            comm->refcount);
235 }
236
237 /**
238  *  \brief Start the simulation of a communication request
239  *  \param comm The communication request
240  */
241 static XBT_INLINE void SIMIX_communication_start(smx_comm_t comm)
242 {
243   /* If both the sender and the receiver are already there, start the communication */
244   if (comm->src_proc && comm->dst_proc) {
245     DEBUG1("Starting communication %p", comm);
246     comm->act = SIMIX_action_communicate(comm->src_proc->smx_host,
247                                          comm->dst_proc->smx_host, NULL,
248                                          comm->task_size, comm->rate);
249 #ifdef HAVE_TRACING
250     TRACE_smx_action_communicate(comm->act, comm->src_proc);
251     TRACE_surf_action(comm->act->surf_action, comm->act->category);
252 #endif
253
254     /* If any of the process is suspend, create the action but stop its execution,
255        it will be restarted when the sender process resume */
256     if (SIMIX_process_is_suspended(comm->src_proc) ||
257         SIMIX_process_is_suspended(comm->dst_proc)) {
258       SIMIX_action_suspend(comm->act);
259     }
260
261     /* Add the communication as user data of the action */
262     comm->act->data = comm;
263
264     /* The semaphore will only get signaled once, but since the first unlocked guy will
265      * release_forever() the semaphore, that will unlock the second (and any other)
266      * communication partner */
267     SIMIX_register_action_to_semaphore(comm->act, comm->sem);
268   }
269 }
270
271 /**
272  * \brief Performs error checking and cleanup
273  * \param comm The communication
274  */
275 static XBT_INLINE void SIMIX_communication_cleanup(smx_comm_t comm)
276 {
277   DEBUG1("Checking errors and cleaning communication %p", comm);
278
279   /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
280   SIMIX_sem_release_forever(comm->sem);
281
282   /* Check for errors other than timeouts */
283   if (!SIMIX_host_get_state(SIMIX_host_self())) {
284     if (comm->rdv)
285       SIMIX_rdv_remove(comm->rdv, comm);
286     SIMIX_communication_destroy(comm);
287     THROW0(host_error, 0, "Host failed");
288   } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED) {
289     SIMIX_communication_destroy(comm);
290     THROW0(network_error, 0, "Link failure");
291   } else if (!SIMIX_host_get_state(SIMIX_process_get_host(comm->dst_proc))
292              ||
293              !SIMIX_host_get_state(SIMIX_process_get_host(comm->src_proc)))
294   {
295     /* We test both src&dst because we dunno who we are today, and we already tested myself above.
296      *    So, at the end, we test the remote peer only
297      * Moreover, we have to test it because if the remote peer fails, the action comm->act is not done nor failed.
298      *    In that case, we got awaken by the little endless actions created in the SIMIX_sem_acquire(comm->sem)
299      *    at the beginning of this function. */
300     SIMIX_communication_destroy(comm);
301     THROW0(network_error, 0, "Remote peer failed");
302
303   }
304   /* Copy network data */
305   SIMIX_network_copy_data(comm);
306
307   SIMIX_communication_destroy(comm);
308 }
309
310 /**
311  *  \brief Waits for communication completion
312  *  \param comm The communication
313  *  \param timeout The max amount of time to wait for the communication to finish
314  *
315  *  Throws:
316  *   - host_error if local peer failed
317  *   - timeout_error if communication reached the timeout specified (either because of local peer or remote peer)
318  *   - network_error if network failed or remote peer failed
319  */
320 static XBT_INLINE void SIMIX_communication_wait_for_completion(smx_comm_t
321                                                                comm,
322                                                                double
323                                                                timeout)
324 {
325   smx_action_t act_sleep = NULL;
326   int src_timeout = 0;
327   int dst_timeout = 0;
328
329   DEBUG1("Waiting for the completion of communication %p", comm);
330
331   if (timeout >= 0) {
332     act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
333     if (SIMIX_process_self() == comm->src_proc)
334       comm->src_timeout = act_sleep;
335     else
336       comm->dst_timeout = act_sleep;
337     SIMIX_action_set_name(act_sleep,
338                           bprintf
339                           ("Timeout for comm %p and wait on semaphore %p (max_duration:%f)",
340                            comm, comm->sem, timeout));
341     SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
342     SIMIX_process_self()->waiting_action = act_sleep;
343     SIMIX_sem_block_onto(comm->sem);
344     SIMIX_process_self()->waiting_action = NULL;
345     SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
346   } else {
347     SIMIX_sem_acquire(comm->sem);
348   }
349
350   /* Check for timeouts */
351   if ((src_timeout = ((comm->src_timeout)
352                       && (SIMIX_action_get_state(comm->src_timeout) ==
353                           SURF_ACTION_DONE)))
354       || (dst_timeout = ((comm->dst_timeout)
355                          && (SIMIX_action_get_state(comm->dst_timeout) ==
356                              SURF_ACTION_DONE)))) {
357     /* Somebody did a timeout! */
358     if (src_timeout)
359       DEBUG1("Communication timeout from the src! %p", comm);
360     if (dst_timeout)
361       DEBUG1("Communication timeout from the dst! %p", comm);
362
363     if (comm->act
364         && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
365       SIMIX_communication_cancel(comm);
366     else if (comm->rdv)
367       SIMIX_rdv_remove(comm->rdv, comm);
368
369     /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
370     SIMIX_sem_release_forever(comm->sem);
371     SIMIX_communication_destroy(comm);
372
373     THROW1(timeout_error, 0, "Communication timeouted because of %s",
374            src_timeout ? "the source" : "the destination");
375   }
376
377   DEBUG1("Communication %p complete!", comm);
378   SIMIX_communication_cleanup(comm);
379 }
380
381 /**
382  *  \brief Cancels a communication
383  *  \brief comm The communication to cancel
384  */
385 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
386 {
387   if (comm->act)
388     SIMIX_action_cancel(comm->act);
389 }
390
391 /**
392  *  \brief get the amount remaining from the communication
393  *  \param comm The communication
394  */
395 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
396 {
397   DEBUG1("calling SIMIX_action_get_remains(%p)", comm->act);
398   return SIMIX_action_get_remains(comm->act);
399 }
400
401 #ifdef HAVE_LATENCY_BOUND_TRACKING
402 /**
403  *  \brief verify if communication is latency bounded
404  *  \param comm The communication
405  */
406 XBT_INLINE int SIMIX_communication_is_latency_bounded(smx_comm_t comm)
407 {
408   //try to find comm on the list of finished flows
409   uintptr_t key = 0;
410   uintptr_t data = 0;
411   xbt_dict_cursor_t cursor;
412   xbt_dict_foreach(latency_limited_dict, cursor, key, data) {
413     DEBUG2("comparing key=%p with comm=%p", (void *) key, (void *) comm);
414     if ((void *) comm == (void *) key) {
415       DEBUG2("key %p found, return value latency limited value %d",
416              (void *) key, (int) data);
417       return (int) data;
418     }
419   }
420
421   DEBUG1("calling SIMIX_action_is_latency_bounded(%p)", comm->act);
422   return SIMIX_action_is_latency_bounded(comm->act);
423 }
424 #endif
425
426 /******************************************************************************/
427 /*                    SIMIX_network_copy_data callbacks                       */
428 /******************************************************************************/
429 static void (*SIMIX_network_copy_data_callback) (smx_comm_t, size_t) =
430     &SIMIX_network_copy_pointer_callback;
431
432 void
433 SIMIX_network_set_copy_data_callback(void (*callback) (smx_comm_t, size_t))
434 {
435   SIMIX_network_copy_data_callback = callback;
436 }
437
438 void SIMIX_network_copy_pointer_callback(smx_comm_t comm, size_t buff_size)
439 {
440   xbt_assert1((buff_size == sizeof(void *)),
441               "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
442   *(void **) (comm->dst_buff) = comm->src_buff;
443 }
444
445 void SIMIX_network_copy_buffer_callback(smx_comm_t comm, size_t buff_size)
446 {
447   memcpy(comm->dst_buff, comm->src_buff, buff_size);
448 }
449
450 /**
451  *  \brief Copy the communication data from the sender's buffer to the receiver's one
452  *  \param comm The communication
453  */
454 void SIMIX_network_copy_data(smx_comm_t comm)
455 {
456   size_t buff_size = comm->src_buff_size;
457   uintptr_t casted_size = 0;
458   uintptr_t amount = 0;
459   /* If there is no data to be copy then return */
460   if (!comm->src_buff || !comm->dst_buff || comm->copied == 1)
461     return;
462
463   DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
464          comm,
465          comm->src_proc->smx_host->name, comm->src_buff,
466          comm->dst_proc->smx_host->name, comm->dst_buff, buff_size);
467
468   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
469   if (comm->dst_buff_size)
470     buff_size = MIN(buff_size, *(comm->dst_buff_size));
471
472   /* Update the receiver's buffer size to the copied amount */
473   if (comm->dst_buff_size)
474     *comm->dst_buff_size = buff_size;
475
476   if (buff_size == 0)
477     return;
478   (*SIMIX_network_copy_data_callback) (comm, buff_size);
479
480   /* Set the copied flag so we copy data only once */
481   /* (this function might be called from both communication ends) */
482   comm->copied = 1;
483
484   /* pimple to display the message sizes */
485   {
486     if (msg_sizes == NULL)
487       msg_sizes = xbt_dict_new();
488     casted_size = comm->task_size;
489     amount = xbt_dicti_get(msg_sizes, casted_size);
490     amount++;
491
492     xbt_dicti_set(msg_sizes, casted_size, amount);
493   }
494 }
495
496 #include "xbt.h"
497 /* pimple to display the message sizes */
498 void SIMIX_message_sizes_output(const char *filename)
499 {
500   uintptr_t key = 0;
501   uintptr_t data = 0;
502   xbt_dict_cursor_t cursor;
503   FILE *out = NULL;
504   out = fopen(filename, "w");
505   xbt_assert1(out, "Cannot open file %s", filename);
506
507   xbt_dict_foreach(msg_sizes, cursor, key, data) {
508     fprintf(out, "%zu %zu\n", key, data);
509   }
510   fclose(out);
511 }
512
513 /**
514  *  \brief Return the user data associated to the communication
515  *  \param comm The communication
516  *  \return the user data
517  */
518 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
519 {
520   return comm->data;
521 }
522
523 XBT_PUBLIC(void *) SIMIX_communication_get_src_buf(smx_comm_t comm)
524 {
525   return comm->src_buff;
526 }
527
528 XBT_PUBLIC(void *) SIMIX_communication_get_dst_buf(smx_comm_t comm)
529 {
530   return comm->dst_buff;
531 }
532
533 XBT_PUBLIC(size_t) SIMIX_communication_get_src_buf_size(smx_comm_t comm)
534 {
535   return comm->src_buff_size;
536 }
537
538 XBT_PUBLIC(size_t) SIMIX_communication_get_dst_buf_size(smx_comm_t comm)
539 {
540   return *(comm->dst_buff_size);
541 }
542
543 /******************************************************************************/
544 /*                        Synchronous Communication                           */
545 /******************************************************************************/
546 /**
547  *  \brief Put a send communication request in a rendez-vous point and waits for
548  *         its completion (blocking)
549  *  \param rdv The rendez-vous point
550  *  \param task_size The size of the communication action (for surf simulation)
551  *  \param rate The rate of the communication action (for surf)
552  *  \param timeout The timeout used for the waiting the completion 
553  *  \param src_buff The source buffer containing the message to be sent
554  *  \param src_buff_size The size of the source buffer
555  *  \param comm_ref The communication object used for the send  (useful if someone else wants to cancel this communication afterward)
556  *  \param data User data associated to the communication object
557  *  Throws:
558  *   - host_error if peer failed
559  *   - timeout_error if communication reached the timeout specified
560  *   - network_error if network failed or peer issued a timeout
561  */
562 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size,
563                                    double rate, double timeout,
564                                    void *src_buff, size_t src_buff_size,
565                                    smx_comm_t * comm_ref, void *data)
566 {
567   xbt_ex_t e;
568   smx_comm_t comm = *comm_ref =
569               SIMIX_network_isend(rdv, task_size, rate, src_buff, src_buff_size,
570                                   data);
571   TRY {
572     SIMIX_network_wait(comm, timeout);
573   }
574   TRY_CLEANUP {
575     SIMIX_communication_destroy(comm);
576   }
577   CATCH(e) {
578     RETHROW;
579   }
580 }
581
582 /**
583  *  \brief Put a receive communication request in a rendez-vous point and waits
584  *         for its completion (blocking)
585  *  \param rdv The rendez-vous point
586  *  \param timeout The timeout used for the waiting the completion 
587  *  \param dst_buff The destination buffer to copy the received message
588  *  \param src_buff_size The size of the destination buffer
589  *  \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
590  *  Throws:
591  *   - host_error if peer failed
592  *   - timeout_error if communication reached the timeout specified
593  *   - network_error if network failed or peer issued a timeout
594  */
595 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout,
596                                    void *dst_buff, size_t * dst_buff_size,
597                                    smx_comm_t * comm_ref)
598 {
599   xbt_ex_t e;
600   smx_comm_t comm = *comm_ref =
601       (smx_comm_t) SIMIX_network_irecv(rdv, dst_buff, dst_buff_size);
602   TRY {
603     SIMIX_network_wait(comm, timeout);
604   }
605   TRY_CLEANUP {
606     SIMIX_communication_destroy(comm);
607   }
608   CATCH(e) {
609     RETHROW;
610   }
611 }
612
613 /******************************************************************************/
614 /*                        Asynchronous Communication                          */
615 /******************************************************************************/
616 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size,
617                                double rate, void *src_buff,
618                                size_t src_buff_size, void *data)
619 {
620   smx_comm_t comm;
621
622   /*If running in model-checking mode then intercept the communication action */
623 #ifdef HAVE_MC
624   if (_surf_do_model_check)
625     MC_trans_intercept_isend(rdv);
626 #endif
627   /* Look for communication request matching our needs.
628      If it is not found then create it and push it into the rendez-vous point */
629   comm = SIMIX_rdv_get_request(rdv, comm_recv);
630
631   if (!comm) {
632     comm = SIMIX_communication_new(comm_send);
633     SIMIX_rdv_push(rdv, comm);
634   }
635
636   /* Setup the communication request */
637   comm->src_proc = SIMIX_process_self();
638   comm->task_size = task_size;
639   comm->rate = rate;
640   comm->src_buff = src_buff;
641   comm->src_buff_size = src_buff_size;
642   comm->data = data;
643
644   SIMIX_communication_start(comm);
645   return comm;
646 }
647
648 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff,
649                                size_t * dst_buff_size)
650 {
651   smx_comm_t comm;
652
653   /*If running in model-checking mode then intercept the communication action */
654 #ifdef HAVE_MC
655   if (_surf_do_model_check)
656     MC_trans_intercept_irecv(rdv);
657 #endif
658   /* Look for communication request matching our needs.
659    * If it is not found then create it and push it into the rendez-vous point
660    */
661   comm = SIMIX_rdv_get_request(rdv, comm_send);
662
663   if (!comm) {
664     comm = SIMIX_communication_new(comm_recv);
665     SIMIX_rdv_push(rdv, comm);
666   }
667
668   /* Setup communication request */
669   comm->dst_proc = SIMIX_process_self();
670   comm->dst_buff = dst_buff;
671   comm->dst_buff_size = dst_buff_size;
672
673   SIMIX_communication_start(comm);
674   return comm;
675 }
676
677 /** @brief blocks until the communication terminates or the timeout occurs */
678 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout)
679 {
680   /*If running in model-checking mode then intercept the communication action */
681 #ifdef HAVE_MC
682   if (_surf_do_model_check)
683     MC_trans_intercept_wait(comm);
684 #endif
685   SIMIX_communication_use(comm);
686   /* Wait for communication completion */
687   SIMIX_communication_wait_for_completion(comm, timeout);
688 }
689
690 /** @Returns whether the (asynchronous) communication is done yet or not */
691 XBT_INLINE int SIMIX_network_test(smx_comm_t comm)
692 {
693   /*If running in model-checking mode then intercept the communication action */
694 #ifdef HAVE_MC
695   if (_surf_do_model_check)
696     MC_trans_intercept_test(comm);
697 #endif
698
699   /* Copy data if the communication is done */
700   if (comm->sem && !SIMIX_sem_would_block(comm->sem)) {
701     /* Copy network data */
702     SIMIX_network_copy_data(comm);
703     return TRUE;
704   }
705   return FALSE;
706 }
707
708 /** @brief wait for the completion of any communication of a set
709  *
710  *  @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
711  */
712 unsigned int SIMIX_network_waitany(xbt_dynar_t comms)
713 {
714   xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t), NULL);
715   unsigned int cursor, found_comm = -1;
716   smx_comm_t comm, comm_finished = NULL;
717
718   /*If running in model-checking mode then intercept the communication action */
719 #ifdef HAVE_MC
720   if (_surf_do_model_check)
721     MC_trans_intercept_waitany(comms);
722 #endif
723   xbt_dynar_foreach(comms, cursor, comm)
724       xbt_dynar_push(sems, &(comm->sem));
725
726   DEBUG1("Waiting for the completion of communication set %p", comms);
727
728   found_comm = SIMIX_sem_acquire_any(sems);
729   xbt_dynar_free_container(&sems);
730   xbt_assert0(found_comm != -1,
731               "Cannot find which communication finished");
732   xbt_dynar_get_cpy(comms, found_comm, &comm_finished);
733
734   DEBUG2("Communication %p of communication set %p finished",
735          comm_finished, comms);
736
737   /* let the regular code deal with the communication end (errors checking and cleanup).
738    * A bit of useless work will be done, but that's good for source factorization */
739   SIMIX_sem_release_forever(comm_finished->sem);
740   SIMIX_communication_use(comm_finished);
741   SIMIX_communication_wait_for_completion(comm_finished, -1);
742   return found_comm;
743 }