Logo AND Algorithmique Numérique Distribuée

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