Logo AND Algorithmique Numérique Distribuée

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