Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
seriously, me
[simgrid.git] / src / msg / msg_gos.cpp
1 /* Copyright (c) 2004-2015. 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 "msg_private.h"
8 #include "msg_mailbox.h"
9 #include "mc/mc.h"
10 #include "xbt/log.h"
11 #include "xbt/sysdep.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
14                                 "Logging specific to MSG (gos)");
15
16 /** \ingroup msg_task_usage
17  * \brief Executes a task and waits for its termination.
18  *
19  * This function is used for describing the behavior of a process. It
20  * takes only one parameter.
21  * \param task a #msg_task_t to execute on the location on which the process is running.
22  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
23  * or #MSG_HOST_FAILURE otherwise
24  */
25 msg_error_t MSG_task_execute(msg_task_t task)
26 {
27   /* TODO: add this to other locations */
28   msg_host_t host = MSG_process_get_host(MSG_process_self());
29   MSG_host_add_task(host, task);
30
31   msg_error_t ret = MSG_parallel_task_execute(task);
32
33   MSG_host_del_task(host, task);
34
35   return ret;
36 }
37
38 /** \ingroup msg_task_usage
39  * \brief Executes a parallel task and waits for its termination.
40  *
41  * \param task a #msg_task_t to execute on the location on which the process is running.
42  *
43  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
44  * or #MSG_HOST_FAILURE otherwise
45  */
46 msg_error_t MSG_parallel_task_execute(msg_task_t task)
47 {
48   xbt_ex_t e;
49   simdata_task_t simdata = task->simdata;
50   msg_process_t self = SIMIX_process_self();
51   simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data(self);
52   e_smx_state_t comp_state;
53   msg_error_t status = MSG_OK;
54
55   TRACE_msg_task_execute_start(task);
56
57   xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
58              "This task is executed somewhere else. Go fix your code! %d",
59              task->simdata->isused!=NULL);
60
61   XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
62
63   if (simdata->flops_amount == 0 && !simdata->host_nb) {
64     TRACE_msg_task_execute_end(task);
65     return MSG_OK;
66   }
67
68
69   TRY {
70     if (msg_global->debug_multiple_use)
71       MSG_BT(simdata->isused, "Using Backtrace");
72     else
73       simdata->isused = (void*)1;
74
75     if (simdata->host_nb > 0) {
76       simdata->compute = simcall_execution_parallel_start(task->name,
77                                                        simdata->host_nb,
78                                                        simdata->host_list,
79                                                        simdata->flops_parallel_amount,
80                                                        simdata->bytes_parallel_amount,
81                                                        1.0, -1.0);
82       XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
83     } else {
84       unsigned long affinity_mask = (unsigned long)(uintptr_t) xbt_dict_get_or_null_ext(simdata->affinity_mask_db, (char *) p_simdata->m_host, sizeof(msg_host_t));
85       XBT_DEBUG("execute %s@%s with affinity(0x%04lx)", MSG_task_get_name(task), MSG_host_get_name(p_simdata->m_host), affinity_mask);
86
87       simdata->compute = simcall_execution_start(task->name,
88                                               simdata->flops_amount,
89                                               simdata->priority,
90                                               simdata->bound,
91                                               affinity_mask
92                                               );
93
94     }
95     simcall_set_category(simdata->compute, task->category);
96     p_simdata->waiting_action = simdata->compute;
97     comp_state = simcall_execution_wait(simdata->compute);
98
99     p_simdata->waiting_action = NULL;
100
101     if (msg_global->debug_multiple_use && simdata->isused!=0)
102       xbt_ex_free(*(xbt_ex_t*)simdata->isused);
103     simdata->isused = 0;
104
105     XBT_DEBUG("Execution task '%s' finished in state %d",
106               task->name, (int)comp_state);
107   }
108   CATCH(e) {
109     switch (e.category) {
110     case cancel_error:
111       status = MSG_TASK_CANCELED;
112       break;
113     case host_error:
114       status = MSG_HOST_FAILURE;
115       break;
116     default:
117       RETHROW;
118     }
119     xbt_ex_free(e);
120   }
121   /* action ended, set comm and compute = NULL, the actions is already destroyed
122    * in the main function */
123   simdata->flops_amount = 0.0;
124   simdata->comm = NULL;
125   simdata->compute = NULL;
126   TRACE_msg_task_execute_end(task);
127
128   MSG_RETURN(status);
129 }
130
131
132 /** \ingroup msg_task_usage
133  * \brief Sleep for the specified number of seconds
134  *
135  * Makes the current process sleep until \a time seconds have elapsed.
136  *
137  * \param nb_sec a number of second
138  */
139 msg_error_t MSG_process_sleep(double nb_sec)
140 {
141   xbt_ex_t e;
142   msg_error_t status = MSG_OK;
143   /*msg_process_t proc = MSG_process_self();*/
144
145   TRACE_msg_process_sleep_in(MSG_process_self());
146
147   TRY {
148     simcall_process_sleep(nb_sec);
149   }
150   CATCH(e) {
151     switch (e.category) {
152     case cancel_error:
153       XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, WTF here ?"); 
154       // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
155       // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
156       // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
157       // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
158       // and did not change anythings at the C level.
159       // See comment in the jmsg_process.c file, function JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos) 
160       status = MSG_TASK_CANCELED;
161       break;
162     default:
163       RETHROW;
164     }
165     xbt_ex_free(e);
166   }
167
168   TRACE_msg_process_sleep_out(MSG_process_self());
169   MSG_RETURN(status);
170 }
171
172 /** \ingroup msg_task_usage
173  * \brief Receives a task from a mailbox.
174  *
175  * This is a blocking function, the execution flow will be blocked
176  * until the task is received. See #MSG_task_irecv
177  * for receiving tasks asynchronously.
178  *
179  * \param task a memory location for storing a #msg_task_t.
180  * \param alias name of the mailbox to receive the task from
181  *
182  * \return Returns
183  * #MSG_OK if the task was successfully received,
184  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
185  */
186 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
187 {
188   return MSG_task_receive_with_timeout(task, alias, -1);
189 }
190
191 /** \ingroup msg_task_usage
192  * \brief Receives a task from a mailbox at a given rate.
193  *
194  * \param task a memory location for storing a #msg_task_t.
195  * \param alias name of the mailbox to receive the task from
196  *  \param rate limit the reception to rate bandwidth
197  *
198  * \return Returns
199  * #MSG_OK if the task was successfully received,
200  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
201  */
202 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias,
203                                      double rate)
204 {
205   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
206 }
207
208 /** \ingroup msg_task_usage
209  * \brief Receives a task from a mailbox with a given timeout.
210  *
211  * This is a blocking function with a timeout, the execution flow will be blocked
212  * until the task is received or the timeout is achieved. See #MSG_task_irecv
213  * for receiving tasks asynchronously.  You can provide a -1 timeout
214  * to obtain an infinite timeout.
215  *
216  * \param task a memory location for storing a #msg_task_t.
217  * \param alias name of the mailbox to receive the task from
218  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
219  *
220  * \return Returns
221  * #MSG_OK if the task was successfully received,
222  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
223  */
224 msg_error_t
225 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
226                               double timeout)
227 {
228   return MSG_task_receive_ext(task, alias, timeout, NULL);
229 }
230
231 /** \ingroup msg_task_usage
232  * \brief Receives a task from a mailbox with a given timeout and at a given rate.
233  *
234  * \param task a memory location for storing a #msg_task_t.
235  * \param alias name of the mailbox to receive the task from
236  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
237  *  \param rate limit the reception to rate bandwidth
238  *
239  * \return Returns
240  * #MSG_OK if the task was successfully received,
241  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
242  */
243 msg_error_t
244 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
245                                       double timeout,double rate)
246 {
247   return MSG_task_receive_ext_bounded(task, alias, timeout, NULL, rate);
248 }
249
250 /** \ingroup msg_task_usage
251  * \brief Receives a task from a mailbox from a specific host with a given timeout.
252  *
253  * This is a blocking function with a timeout, the execution flow will be blocked
254  * until the task is received or the timeout is achieved. See #MSG_task_irecv
255  * for receiving tasks asynchronously. You can provide a -1 timeout
256  * to obtain an infinite timeout.
257  *
258  * \param task a memory location for storing a #msg_task_t.
259  * \param alias name of the mailbox to receive the task from
260  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
261  * \param host a #msg_host_t host from where the task was sent
262  *
263  * \return Returns
264  * #MSG_OK if the task was successfully received,
265 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
266  */
267 msg_error_t
268 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
269                      msg_host_t host)
270 {
271   xbt_ex_t e;
272   msg_error_t ret = MSG_OK;
273   XBT_DEBUG
274       ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
275        alias);
276   TRY {
277     ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
278                                    host, timeout);
279   }
280   CATCH(e) {
281     switch (e.category) {
282     case cancel_error:          /* may be thrown by MSG_mailbox_get_by_alias */
283       ret = MSG_HOST_FAILURE;
284       break;
285     default:
286       RETHROW;
287     }
288     xbt_ex_free(e);
289   }
290   return ret;
291 }
292
293 /** \ingroup msg_task_usage
294  * \brief Receives a task from a mailbox from a specific host with a given timeout
295  *  and at a given rate.
296  *
297  * \param task a memory location for storing a #msg_task_t.
298  * \param alias name of the mailbox to receive the task from
299  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
300  * \param host a #msg_host_t host from where the task was sent
301  * \param rate limit the reception to rate bandwidth
302  *
303  * \return Returns
304  * #MSG_OK if the task was successfully received,
305 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
306  */
307 msg_error_t
308 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
309                              msg_host_t host, double rate)
310 {
311   XBT_DEBUG
312       ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
313        alias);
314   return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
315                                           host, timeout, rate);
316 }
317
318 /* Internal function used to factorize code between
319  * MSG_task_isend_with_matching() and MSG_task_dsend().
320  */
321 static XBT_INLINE
322 msg_comm_t MSG_task_isend_internal(msg_task_t task, const char *alias,
323                                    int (*match_fun)(void*,void*, smx_synchro_t),
324                                    void *match_data, void_f_pvoid_t cleanup,
325                                    int detached)
326 {
327   simdata_task_t t_simdata = NULL;
328   msg_process_t process = MSG_process_self();
329   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
330   int call_end = TRACE_msg_task_put_start(task);
331
332   /* Prepare the task to send */
333   t_simdata = task->simdata;
334   t_simdata->sender = process;
335   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
336
337   if (t_simdata->isused != 0) {
338     if (msg_global->debug_multiple_use){
339       XBT_ERROR("This task is already used in there:");
340       xbt_backtrace_display((xbt_ex_t*) t_simdata->isused);
341       XBT_ERROR("And you try to reuse it from here:");
342       xbt_backtrace_display_current();
343     } else {
344       xbt_assert(t_simdata->isused == 0,
345                  "This task is still being used somewhere else. You cannot send it now. Go fix your code! (use --cfg=msg/debug_multiple_use:on to get the backtrace of the other process)");
346     }
347   }
348
349   if (msg_global->debug_multiple_use)
350     MSG_BT(t_simdata->isused, "Using Backtrace");
351   else
352     t_simdata->isused = (void*)1;
353   t_simdata->comm = NULL;
354   msg_global->sent_msg++;
355
356   /* Send it by calling SIMIX network layer */
357   smx_synchro_t act = simcall_comm_isend(SIMIX_process_self(), mailbox, t_simdata->bytes_amount,
358                                         t_simdata->rate, task, sizeof(void *),
359                                         match_fun, cleanup, NULL, match_data,detached);
360   t_simdata->comm = act; /* FIXME: is the field t_simdata->comm still useful? */
361
362   msg_comm_t comm;
363   if (detached) {
364     comm = NULL;
365   } else {
366     comm = xbt_new0(s_msg_comm_t, 1);
367     comm->task_sent = task;
368     comm->task_received = NULL;
369     comm->status = MSG_OK;
370     comm->s_comm = act;
371   }
372
373   if (TRACE_is_enabled())
374     simcall_set_category(act, task->category);
375   if (call_end)
376     TRACE_msg_task_put_end();
377
378   return comm;
379 }
380
381
382 /** \ingroup msg_task_usage
383  * \brief Sends a task on a mailbox.
384  *
385  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
386  * to end the communication.
387  *
388  * \param task a #msg_task_t to send on another location.
389  * \param alias name of the mailbox to sent the task to
390  * \return the msg_comm_t communication created
391  */
392 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
393 {
394   return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
395 }
396
397 /** \ingroup msg_task_usage
398  * \brief Sends a task on a mailbox with a maximum rate
399  *
400  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
401  * to end the communication. The maxrate parameter allows the application
402  * to limit the bandwidth utilization of network links when sending the task.
403  *
404  * \param task a #msg_task_t to send on another location.
405  * \param alias name of the mailbox to sent the task to
406  * \param maxrate the maximum communication rate for sending this task .
407  * \return the msg_comm_t communication created
408  */
409 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias,
410                                   double maxrate)
411 {
412   task->simdata->rate = maxrate;
413   return MSG_task_isend_internal(task, alias, NULL, NULL, NULL, 0);
414 }
415
416
417 /** \ingroup msg_task_usage
418  * \brief Sends a task on a mailbox, with support for matching requests
419  *
420  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
421  * to end the communication.
422  *
423  * \param task a #msg_task_t to send on another location.
424  * \param alias name of the mailbox to sent the task to
425  * \param match_fun boolean function which parameters are:
426  *        - match_data_provided_here
427  *        - match_data_provided_by_other_side_if_any
428  *        - the_smx_synchro_describing_the_other_side
429  * \param match_data user provided data passed to match_fun
430  * \return the msg_comm_t communication created
431  */
432 msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
433                                         int (*match_fun)(void*, void*,
434                                                          smx_synchro_t),
435                                         void *match_data)
436 {
437   return MSG_task_isend_internal(task, alias, match_fun, match_data, NULL, 0);
438 }
439
440 /** \ingroup msg_task_usage
441  * \brief Sends a task on a mailbox.
442  *
443  * This is a non blocking detached send function.
444  * Think of it as a best effort send. Keep in mind that the third parameter
445  * is only called if the communication fails. If the communication does work,
446  * it is responsibility of the receiver code to free anything related to
447  * the task, as usual. More details on this can be obtained on
448  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
449  * in the SimGrid-user mailing list archive.
450  *
451  * \param task a #msg_task_t to send on another location.
452  * \param alias name of the mailbox to sent the task to
453  * \param cleanup a function to destroy the task if the
454  * communication fails, e.g. MSG_task_destroy
455  * (if NULL, no function will be called)
456  */
457 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
458 {
459   MSG_task_isend_internal(task, alias, NULL, NULL, cleanup, 1);
460 }
461
462 /** \ingroup msg_task_usage
463  * \brief Sends a task on a mailbox with a maximal rate.
464  *
465  * This is a non blocking detached send function.
466  * Think of it as a best effort send. Keep in mind that the third parameter
467  * is only called if the communication fails. If the communication does work,
468  * it is responsibility of the receiver code to free anything related to
469  * the task, as usual. More details on this can be obtained on
470  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
471  * in the SimGrid-user mailing list archive.
472  *
473  * \param task a #msg_task_t to send on another location.
474  * \param alias name of the mailbox to sent the task to
475  * \param cleanup a function to destroy the task if the
476  * communication fails, e.g. MSG_task_destroy
477  * (if NULL, no function will be called)
478  * \param maxrate the maximum communication rate for sending this task
479  *
480  */
481 void MSG_task_dsend_bounded(msg_task_t task, const char *alias,
482                             void_f_pvoid_t cleanup, double maxrate)
483 {
484   task->simdata->rate = maxrate;
485   MSG_task_dsend(task, alias, cleanup);
486 }
487
488 /** \ingroup msg_task_usage
489  * \brief Starts listening for receiving a task from an asynchronous communication.
490  *
491  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
492  * to end the communication.
493  *
494  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
495  * \param name of the mailbox to receive the task on
496  * \return the msg_comm_t communication created
497  */
498 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
499 {
500   return MSG_task_irecv_bounded(task, name, -1.0);
501 }
502
503 /** \ingroup msg_task_usage
504  * \brief Starts listening for receiving a task from an asynchronous communication
505  * at a given rate.
506  *
507  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
508  * \param name of the mailbox to receive the task on
509  * \param rate limit the bandwidth to the given rate
510  * \return the msg_comm_t communication created
511  */
512 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name,
513                                   double rate)
514 {
515   smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
516
517   /* FIXME: these functions are not traceable */
518
519   /* Sanity check */
520   xbt_assert(task, "Null pointer for the task storage");
521
522   if (*task)
523     XBT_CRITICAL
524         ("MSG_task_irecv() was asked to write in a non empty task struct.");
525
526   /* Try to receive it by calling SIMIX network layer */
527   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
528   comm->task_sent = NULL;
529   comm->task_received = task;
530   comm->status = MSG_OK;
531   comm->s_comm = simcall_comm_irecv(MSG_process_self(), rdv, task, NULL, NULL, NULL, NULL, rate);
532
533   return comm;
534 }
535
536 /** \ingroup msg_task_usage
537  * \brief Checks whether a communication is done, and if yes, finalizes it.
538  * \param comm the communication to test
539  * \return TRUE if the communication is finished
540  * (but it may have failed, use MSG_comm_get_status() to know its status)
541  * or FALSE if the communication is not finished yet
542  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
543  */
544 int MSG_comm_test(msg_comm_t comm)
545 {
546   xbt_ex_t e;
547   int finished = 0;
548
549   TRY {
550     finished = simcall_comm_test(comm->s_comm);
551
552     if (finished && comm->task_received != NULL) {
553       /* I am the receiver */
554       if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
555         xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
556       (*comm->task_received)->simdata->isused = 0;
557     }
558   }
559   CATCH(e) {
560     switch (e.category) {
561       case network_error:
562         comm->status = MSG_TRANSFER_FAILURE;
563         finished = 1;
564         break;
565
566       case timeout_error:
567         comm->status = MSG_TIMEOUT;
568         finished = 1;
569         break;
570
571       default:
572         RETHROW;
573     }
574     xbt_ex_free(e);
575   }
576
577   return finished;
578 }
579
580 /** \ingroup msg_task_usage
581  * \brief This function checks if a communication is finished.
582  * \param comms a vector of communications
583  * \return the position of the finished communication if any
584  * (but it may have failed, use MSG_comm_get_status() to know its status),
585  * or -1 if none is finished
586  */
587 int MSG_comm_testany(xbt_dynar_t comms)
588 {
589   xbt_ex_t e;
590   int finished_index = -1;
591
592   /* create the equivalent dynar with SIMIX objects */
593   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
594   msg_comm_t comm;
595   unsigned int cursor;
596   xbt_dynar_foreach(comms, cursor, comm) {
597     xbt_dynar_push(s_comms, &comm->s_comm);
598   }
599
600   msg_error_t status = MSG_OK;
601   TRY {
602     finished_index = simcall_comm_testany(s_comms);
603   }
604   CATCH(e) {
605     switch (e.category) {
606       case network_error:
607         finished_index = e.value;
608         status = MSG_TRANSFER_FAILURE;
609         break;
610
611       case timeout_error:
612         finished_index = e.value;
613         status = MSG_TIMEOUT;
614         break;
615
616       default:
617         RETHROW;
618     }
619     xbt_ex_free(e);
620   }
621   xbt_dynar_free(&s_comms);
622
623   if (finished_index != -1) {
624     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
625     /* the communication is finished */
626     comm->status = status;
627
628     if (status == MSG_OK && comm->task_received != NULL) {
629       /* I am the receiver */
630       if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
631         xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
632       (*comm->task_received)->simdata->isused = 0;
633     }
634   }
635
636   return finished_index;
637 }
638
639 /** \ingroup msg_task_usage
640  * \brief Destroys a communication.
641  * \param comm the communication to destroy.
642  */
643 void MSG_comm_destroy(msg_comm_t comm)
644 {
645   xbt_free(comm);
646 }
647
648 /** \ingroup msg_task_usage
649  * \brief Wait for the completion of a communication.
650  *
651  * It takes two parameters.
652  * \param comm the communication to wait.
653  * \param timeout Wait until the communication terminates or the timeout
654  * occurs. You can provide a -1 timeout to obtain an infinite timeout.
655  * \return msg_error_t
656  */
657 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
658 {
659   xbt_ex_t e;
660   TRY {
661     simcall_comm_wait(comm->s_comm, timeout);
662
663     if (comm->task_received != NULL) {
664       /* I am the receiver */
665       if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
666         xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
667       (*comm->task_received)->simdata->isused = 0;
668     }
669
670     /* FIXME: these functions are not traceable */
671   }
672   CATCH(e) {
673     switch (e.category) {
674     case network_error:
675       comm->status = MSG_TRANSFER_FAILURE;
676       break;
677     case timeout_error:
678       comm->status = MSG_TIMEOUT;
679       break;
680     default:
681       RETHROW;
682     }
683     xbt_ex_free(e);
684   }
685
686   return comm->status;
687 }
688
689 /** \ingroup msg_task_usage
690 * \brief This function is called by a sender and permit to wait for each communication
691 *
692 * \param comm a vector of communication
693 * \param nb_elem is the size of the comm vector
694 * \param timeout for each call of MSG_comm_wait
695 */
696 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
697 {
698   int i = 0;
699   for (i = 0; i < nb_elem; i++) {
700     MSG_comm_wait(comm[i], timeout);
701   }
702 }
703
704 /** \ingroup msg_task_usage
705  * \brief This function waits for the first communication finished in a list.
706  * \param comms a vector of communications
707  * \return the position of the first finished communication
708  * (but it may have failed, use MSG_comm_get_status() to know its status)
709  */
710 int MSG_comm_waitany(xbt_dynar_t comms)
711 {
712   xbt_ex_t e;
713   int finished_index = -1;
714
715   /* create the equivalent dynar with SIMIX objects */
716   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
717   msg_comm_t comm;
718   unsigned int cursor;
719   xbt_dynar_foreach(comms, cursor, comm) {
720     xbt_dynar_push(s_comms, &comm->s_comm);
721   }
722
723   msg_error_t status = MSG_OK;
724   TRY {
725     finished_index = simcall_comm_waitany(s_comms);
726   }
727   CATCH(e) {
728     switch (e.category) {
729       case network_error:
730         finished_index = e.value;
731         status = MSG_TRANSFER_FAILURE;
732         break;
733
734       case timeout_error:
735         finished_index = e.value;
736         status = MSG_TIMEOUT;
737         break;
738
739       default:
740         RETHROW;
741     }
742     xbt_ex_free(e);
743   }
744
745   xbt_assert(finished_index != -1, "WaitAny returned -1");
746   xbt_dynar_free(&s_comms);
747
748   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
749   /* the communication is finished */
750   comm->status = status;
751
752   if (comm->task_received != NULL) {
753     /* I am the receiver */
754     if (msg_global->debug_multiple_use && (*comm->task_received)->simdata->isused!=0)
755       xbt_ex_free(*(xbt_ex_t*)(*comm->task_received)->simdata->isused);
756     (*comm->task_received)->simdata->isused = 0;
757   }
758
759   return finished_index;
760 }
761
762 /**
763  * \ingroup msg_task_usage
764  * \brief Returns the error (if any) that occured during a finished communication.
765  * \param comm a finished communication
766  * \return the status of the communication, or #MSG_OK if no error occured
767  * during the communication
768  */
769 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
770
771   return comm->status;
772 }
773
774 /** \ingroup msg_task_usage
775  * \brief Get a task (#msg_task_t) from a communication
776  *
777  * \param comm the communication where to get the task
778  * \return the task from the communication
779  */
780 msg_task_t MSG_comm_get_task(msg_comm_t comm)
781 {
782   xbt_assert(comm, "Invalid parameter");
783
784   return comm->task_received ? *comm->task_received : comm->task_sent;
785 }
786
787 /**
788  * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
789  * \param comm the comm
790  * \param buff the data copied
791  * \param buff_size size of the buffer
792  */
793 void MSG_comm_copy_data_from_SIMIX(smx_synchro_t comm, void* buff, size_t buff_size) {
794
795   // copy the task
796   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
797
798   // notify the user callback if any
799   if (msg_global->task_copy_callback) {
800     msg_task_t task = (msg_task_t) buff;
801     msg_global->task_copy_callback(task,
802         simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
803   }
804 }
805
806 /** \ingroup msg_task_usage
807  * \brief Sends a task to a mailbox
808  *
809  * This is a blocking function, the execution flow will be blocked
810  * until the task is sent (and received in the other side if #MSG_task_receive is used).
811  * See #MSG_task_isend for sending tasks asynchronously.
812  *
813  * \param task the task to be sent
814  * \param alias the mailbox name to where the task is sent
815  *
816  * \return Returns #MSG_OK if the task was successfully sent,
817  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
818  */
819 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
820 {
821   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
822   return MSG_task_send_with_timeout(task, alias, -1);
823 }
824
825 /** \ingroup msg_task_usage
826  * \brief Sends a task to a mailbox with a maximum rate
827  *
828  * This is a blocking function, the execution flow will be blocked
829  * until the task is sent. The maxrate parameter allows the application
830  * to limit the bandwidth utilization of network links when sending the task.
831  *
832  * \param task the task to be sent
833  * \param alias the mailbox name to where the task is sent
834  * \param maxrate the maximum communication rate for sending this task
835  *
836  * \return Returns #MSG_OK if the task was successfully sent,
837  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
838  */
839 msg_error_t
840 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
841 {
842   task->simdata->rate = maxrate;
843   return MSG_task_send(task, alias);
844 }
845
846 /** \ingroup msg_task_usage
847  * \brief Sends a task to a mailbox with a timeout
848  *
849  * This is a blocking function, the execution flow will be blocked
850  * until the task is sent or the timeout is achieved.
851  *
852  * \param task the task to be sent
853  * \param alias the mailbox name to where the task is sent
854  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
855  *
856  * \return Returns #MSG_OK if the task was successfully sent,
857  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
858  */
859 msg_error_t
860 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
861                            double timeout)
862 {
863   return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
864                                       task, timeout);
865 }
866
867 /** \ingroup msg_task_usage
868  * \brief Sends a task to a mailbox with a timeout and with a maximum rate
869  *
870  * This is a blocking function, the execution flow will be blocked
871  * until the task is sent or the timeout is achieved.
872  *
873  * \param task the task to be sent
874  * \param alias the mailbox name to where the task is sent
875  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
876  * \param maxrate the maximum communication rate for sending this task
877  *
878  * \return Returns #MSG_OK if the task was successfully sent,
879  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
880  */
881 msg_error_t
882 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
883                            double timeout, double maxrate)
884 {
885   task->simdata->rate = maxrate;
886   return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
887                                       task, timeout);
888 }
889
890 /** \ingroup msg_task_usage
891  * \brief Check if there is a communication going on in a mailbox.
892  *
893  * \param alias the name of the mailbox to be considered
894  *
895  * \return Returns 1 if there is a communication, 0 otherwise
896  */
897 int MSG_task_listen(const char *alias)
898 {
899   return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
900 }
901
902 /** \ingroup msg_task_usage
903  * \brief Check the number of communication actions of a given host pending in a mailbox.
904  *
905  * \param alias the name of the mailbox to be considered
906  * \param host the host to check for communication
907  *
908  * \return Returns the number of pending communication actions of the host in the
909  * given mailbox, 0 if there is no pending communication actions.
910  *
911  */
912 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
913 {
914   return
915       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
916                                                (alias), host);
917 }
918
919 /** \ingroup msg_task_usage
920  * \brief Look if there is a communication on a mailbox and return the
921  * PID of the sender process.
922  *
923  * \param alias the name of the mailbox to be considered
924  *
925  * \return Returns the PID of sender process,
926  * -1 if there is no communication in the mailbox.
927  */
928 int MSG_task_listen_from(const char *alias)
929 {
930   msg_task_t task;
931
932   if (NULL ==
933       (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
934     return -1;
935
936   return MSG_process_get_PID(task->simdata->sender);
937 }
938
939 /** \ingroup msg_task_usage
940  * \brief Sets the tracing category of a task.
941  *
942  * This function should be called after the creation of
943  * a MSG task, to define the category of that task. The
944  * first parameter task must contain a task that was
945  * created with the function #MSG_task_create. The second
946  * parameter category must contain a category that was
947  * previously declared with the function #TRACE_category
948  * (or with #TRACE_category_with_color).
949  *
950  * See \ref tracing for details on how to trace
951  * the (categorized) resource utilization.
952  *
953  * \param task the task that is going to be categorized
954  * \param category the name of the category to be associated to the task
955  *
956  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
957  */
958 void MSG_task_set_category (msg_task_t task, const char *category)
959 {
960   TRACE_msg_set_task_category (task, category);
961 }
962
963 /** \ingroup msg_task_usage
964  *
965  * \brief Gets the current tracing category of a task.
966  *
967  * \param task the task to be considered
968  *
969  * \see MSG_task_set_category
970  *
971  * \return Returns the name of the tracing category of the given task, NULL otherwise
972  */
973 const char *MSG_task_get_category (msg_task_t task)
974 {
975   return task->category;
976 }
977
978 /**
979  * \brief Returns the value of a given AS or router property
980  *
981  * \param asr the name of a router or AS
982  * \param name a property name
983  * \return value of a property (or NULL if property not set)
984  */
985 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
986 {
987   return (char*) xbt_dict_get_or_null(
988     MSG_as_router_get_properties(asr), name);
989 }
990
991 /**
992  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
993  * a the AS or router
994  *
995  * \param asr the name of a router or AS
996  * \return a dict containing the properties
997  */
998 xbt_dict_t MSG_as_router_get_properties(const char* asr)
999 {
1000   return (simcall_asr_get_properties(asr));
1001 }
1002
1003 /**
1004  * \brief Change the value of a given AS or router
1005  *
1006  * \param asr the name of a router or AS
1007  * \param name a property name
1008  * \param value what to change the property to
1009  * \param free_ctn the freeing function to use to kill the value on need
1010  */
1011 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1012   xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1013 }