Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9f04b7a5298ddbd1a0d895b33137f4f3d31b914e
[simgrid.git] / src / msg / msg_gos.c
1 /* Copyright (c) 2004-2012. 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 "msg_private.h"
7 #include "msg_mailbox.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
19  * takes only one parameter.
20  * \param task a #m_task_t to execute on the location on which the process is running.
21  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
22  * or #MSG_HOST_FAILURE otherwise
23  */
24 MSG_error_t MSG_task_execute(m_task_t task)
25 {
26   xbt_ex_t e;
27   simdata_task_t simdata = NULL;
28   simdata_process_t p_simdata;
29   e_smx_state_t comp_state;
30
31   simdata = task->simdata;
32
33   xbt_assert(simdata->host_nb == 0,
34               "This is a parallel task. Go to hell.");
35
36 #ifdef HAVE_TRACING
37   TRACE_msg_task_execute_start(task);
38 #endif
39
40   xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
41               "This task is executed somewhere else. Go fix your code! %d",
42               task->simdata->isused);
43
44   XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
45
46   if (simdata->computation_amount == 0) {
47 #ifdef HAVE_TRACING
48     TRACE_msg_task_execute_end(task);
49 #endif
50     return MSG_OK;
51   }
52
53   m_process_t self = SIMIX_process_self();
54   p_simdata = SIMIX_process_self_get_data(self);
55   simdata->isused=1;
56   simdata->compute =
57       simcall_host_execute(task->name, p_simdata->m_host->smx_host,
58                            simdata->computation_amount,
59                            simdata->priority);
60 #ifdef HAVE_TRACING
61   simcall_set_category(simdata->compute, task->category);
62 #endif
63   
64   p_simdata->waiting_action = simdata->compute;
65   TRY {
66     comp_state = simcall_host_execution_wait(simdata->compute);
67     p_simdata->waiting_action = NULL;
68
69     simdata->isused=0;
70
71     XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
72
73     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
74     simdata->computation_amount = 0.0;
75     simdata->comm = NULL;
76     simdata->compute = NULL;
77 #ifdef HAVE_TRACING
78     TRACE_msg_task_execute_end(task);
79 #endif
80     MSG_RETURN(MSG_OK);
81   }  
82   CATCH(e) {
83     switch (e.category) {
84       case host_error:
85         /* action ended, set comm and compute = NULL, the actions is already  destroyed in the main function */
86         simdata->comm = NULL;
87         simdata->compute = NULL;
88         #ifdef HAVE_TRACING
89           TRACE_msg_task_execute_end(task);
90         #endif
91         MSG_RETURN(MSG_HOST_FAILURE);
92         break;  
93       case cancel_error:
94         /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
95         simdata->comm = NULL;
96         simdata->compute = NULL;
97     #ifdef HAVE_TRACING
98         TRACE_msg_task_execute_end(task);
99     #endif
100         MSG_RETURN(MSG_TASK_CANCELED);        
101       break;
102       default:
103         RETHROW;
104     }
105   }
106   MSG_RETURN(MSG_OK);
107 }
108
109 /** \ingroup m_task_management
110  * \brief Creates a new #m_task_t (a parallel one....).
111  *
112  * A constructor for #m_task_t taking six arguments and returning the
113  corresponding object.
114  * \param name a name for the object. It is for user-level information
115  and can be NULL.
116  * \param host_nb the number of hosts implied in the parallel task.
117  * \param host_list an array of \p host_nb m_host_t.
118  * \param computation_amount an array of \p host_nb
119  doubles. computation_amount[i] is the total number of operations
120  that have to be performed on host_list[i].
121  * \param communication_amount an array of \p host_nb* \p host_nb doubles.
122  * \param data a pointer to any data may want to attach to the new
123  object.  It is for user-level information and can be NULL. It can
124  be retrieved with the function \ref MSG_task_get_data.
125  * \see m_task_t
126  * \return The new corresponding object.
127  */
128 m_task_t
129 MSG_parallel_task_create(const char *name, int host_nb,
130                          const m_host_t * host_list,
131                          double *computation_amount,
132                          double *communication_amount, void *data)
133 {
134   int i;
135   simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
136   m_task_t task = xbt_new0(s_m_task_t, 1);
137   task->simdata = simdata;
138
139   /* Task structure */
140   task->name = xbt_strdup(name);
141   task->data = data;
142
143   /* Simulator Data */
144   simdata->computation_amount = 0;
145   simdata->message_size = 0;
146   simdata->compute = NULL;
147   simdata->comm = NULL;
148   simdata->rate = -1.0;
149   simdata->isused = 0;
150   simdata->sender = NULL;
151   simdata->receiver = NULL;
152   simdata->source = NULL;
153
154   simdata->host_nb = host_nb;
155   simdata->host_list = xbt_new0(smx_host_t, host_nb);
156   simdata->comp_amount = computation_amount;
157   simdata->comm_amount = communication_amount;
158
159   for (i = 0; i < host_nb; i++)
160     simdata->host_list[i] = host_list[i]->smx_host;
161
162   return task;
163 }
164
165 /** \ingroup msg_task_usage
166  * \brief Executes a parallel task and waits for its termination.
167  *
168  * \param task a #m_task_t to execute on the location on which the process is running.
169  *
170  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
171  * or #MSG_HOST_FAILURE otherwise
172  */
173 MSG_error_t MSG_parallel_task_execute(m_task_t task)
174 {
175   simdata_task_t simdata = NULL;
176   e_smx_state_t comp_state;
177   simdata_process_t p_simdata;
178
179   simdata = task->simdata;
180   p_simdata = SIMIX_process_self_get_data(SIMIX_process_self());
181
182   xbt_assert((!simdata->compute)
183               && (task->simdata->isused == 0),
184               "This task is executed somewhere else. Go fix your code!");
185
186   xbt_assert(simdata->host_nb,
187               "This is not a parallel task. Go to hell.");
188
189   XBT_DEBUG("Parallel computing on %s", SIMIX_host_get_name(p_simdata->m_host->smx_host));
190
191   simdata->isused=1;
192
193   simdata->compute =
194       simcall_host_parallel_execute(task->name, simdata->host_nb,
195                                   simdata->host_list,
196                                   simdata->comp_amount,
197                                   simdata->comm_amount, 1.0, -1.0);
198   XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
199
200   p_simdata->waiting_action = simdata->compute;
201   comp_state = simcall_host_execution_wait(simdata->compute);
202   p_simdata->waiting_action = NULL;
203
204   XBT_DEBUG("Finished waiting for execution of action %p, state = %d", simdata->compute, (int)comp_state);
205
206   simdata->isused=0;
207
208   if (comp_state == SIMIX_DONE) {
209     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
210     simdata->computation_amount = 0.0;
211     simdata->comm = NULL;
212     simdata->compute = NULL;
213     MSG_RETURN(MSG_OK);
214   } else if (simcall_host_get_state(SIMIX_host_self()) == 0) {
215     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
216     simdata->comm = NULL;
217     simdata->compute = NULL;
218     MSG_RETURN(MSG_HOST_FAILURE);
219   } else {
220     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
221     simdata->comm = NULL;
222     simdata->compute = NULL;
223     MSG_RETURN(MSG_TASK_CANCELED);
224   }
225 }
226
227
228 /** \ingroup msg_task_usage
229  * \brief Sleep for the specified number of seconds
230  *
231  * Makes the current process sleep until \a time seconds have elapsed.
232  *
233  * \param nb_sec a number of second
234  */
235 MSG_error_t MSG_process_sleep(double nb_sec)
236 {
237   xbt_ex_t e;
238   /*m_process_t proc = MSG_process_self();*/
239
240 #ifdef HAVE_TRACING
241   TRACE_msg_process_sleep_in(MSG_process_self());
242 #endif
243
244   /* create action to sleep */
245
246   /*proc->simdata->waiting_action = act_sleep;
247
248   FIXME: check if not setting the waiting_action breaks something on msg
249   
250   proc->simdata->waiting_action = NULL;*/
251
252   TRY {
253     simcall_process_sleep(nb_sec);  
254   }
255   CATCH(e) {
256     switch (e.category) {
257       case host_error:
258         #ifdef HAVE_TRACING
259           TRACE_msg_process_sleep_out(MSG_process_self());
260         #endif
261         MSG_RETURN(MSG_HOST_FAILURE);
262         break;    
263       default:
264         RETHROW;
265     }
266   }
267   #ifdef HAVE_TRACING
268     TRACE_msg_process_sleep_out(MSG_process_self());
269   #endif
270   MSG_RETURN(MSG_OK);
271 }
272
273 /** \ingroup msg_task_usage
274  * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
275  *
276  * Sorry, this function is not supported anymore. That wouldn't be
277  * impossible to reimplement it, but we are lacking the time to do so ourselves.
278  * If you need this functionality, you can either:
279  *
280  *  - implement the buffering mechanism on the user-level by queuing all messages
281  *    received in the mailbox that do not match your expectation
282  *  - change your application logic to leverage the mailboxes features. For example,
283  *    if you have A receiving messages from B and C, you could have A waiting on
284  *    mailbox "A" most of the time, but on "A#B" when it's waiting for specific
285  *    messages from B and "A#C" when waiting for messages from C. You could even get A
286  *    sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
287  *    an example of use of this function in the @ref MSG_examples section.
288  *  - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
289  *    very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
290  *    simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
291  *    messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
292  *    and that your filtering function will receive as first parameter, and then, the filter could
293  *    simply compare the host names, for example. After sufficient testing, provide an example that
294  *    we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
295  *
296  * \param task a memory location for storing a #m_task_t.
297  * \param alias name of the mailbox to receive the task from
298  * \param host a #m_host_t host from where the task was sent
299  *
300  * \return Returns
301  * #MSG_OK if the task was successfully received,
302  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
303  */
304 MSG_error_t
305 MSG_task_receive_from_host(m_task_t * task, const char *alias,
306                            m_host_t host)
307 {
308   return MSG_task_receive_ext(task, alias, -1, host);
309 }
310
311 /** \ingroup msg_task_usage
312  * \brief Receives a task from a mailbox.
313  *
314  * This is a blocking function, the execution flow will be blocked
315  * until the task is received. See #MSG_task_irecv
316  * for receiving tasks asynchronously.
317  *
318  * \param task a memory location for storing a #m_task_t.
319  * \param alias name of the mailbox to receive the task from
320  *
321  * \return Returns
322  * #MSG_OK if the task was successfully received,
323  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
324  */
325 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
326 {
327   return MSG_task_receive_with_timeout(task, alias, -1);
328 }
329
330 /** \ingroup msg_task_usage
331  * \brief Receives a task from a mailbox with a given timeout.
332  *
333  * This is a blocking function with a timeout, the execution flow will be blocked
334  * until the task is received or the timeout is achieved. See #MSG_task_irecv
335  * for receiving tasks asynchronously.  You can provide a -1 timeout
336  * to obtain an infinite timeout.
337  *
338  * \param task a memory location for storing a #m_task_t.
339  * \param alias name of the mailbox to receive the task from
340  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
341  *
342  * \return Returns
343  * #MSG_OK if the task was successfully received,
344  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
345  */
346 MSG_error_t
347 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
348                               double timeout)
349 {
350   return MSG_task_receive_ext(task, alias, timeout, NULL);
351 }
352
353 /** \ingroup msg_task_usage
354  * \brief Receives a task from a mailbox from a specific host with a given timeout.
355  *
356  * This is a blocking function with a timeout, the execution flow will be blocked
357  * until the task is received or the timeout is achieved. See #MSG_task_irecv
358  * for receiving tasks asynchronously. You can provide a -1 timeout
359  * to obtain an infinite timeout.
360  *
361  * \param task a memory location for storing a #m_task_t.
362  * \param alias name of the mailbox to receive the task from
363  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
364  * \param host a #m_host_t host from where the task was sent
365  *
366  * \return Returns
367  * #MSG_OK if the task was successfully received,
368 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
369  */
370 MSG_error_t
371 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
372                      m_host_t host)
373 {
374   XBT_DEBUG
375       ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
376        alias);
377   return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
378                                   host, timeout);
379 }
380
381 /** \ingroup msg_task_usage
382  * \brief Sends a task on a mailbox.
383  *
384  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
385  * to end the communication.
386  *
387  * \param task a #m_task_t to send on another location.
388  * \param alias name of the mailbox to sent the task to
389  * \return the msg_comm_t communication created
390  */
391 msg_comm_t MSG_task_isend(m_task_t task, const char *alias)
392 {
393   return MSG_task_isend_with_matching(task,alias,NULL,NULL);
394 }
395
396 /** \ingroup msg_task_usage
397  * \brief Sends a task on a mailbox, with support for matching requests
398  *
399  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
400  * to end the communication.
401  *
402  * \param task a #m_task_t to send on another location.
403  * \param alias name of the mailbox to sent the task to
404  * \param match_fun boolean function which parameters are:
405  *        - match_data_provided_here
406  *        - match_data_provided_by_other_side_if_any
407  *        - the_smx_action_describing_the_other_side
408  * \param match_data user provided data passed to match_fun
409  * \return the msg_comm_t communication created
410  */
411 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(m_task_t task, const char *alias,
412     int (*match_fun)(void*,void*, smx_action_t),
413     void *match_data)
414 {
415   simdata_task_t t_simdata = NULL;
416   m_process_t process = MSG_process_self();
417   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
418
419   /* FIXME: these functions are not traceable */
420
421   /* Prepare the task to send */
422   t_simdata = task->simdata;
423   t_simdata->sender = process;
424   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
425
426   xbt_assert(t_simdata->isused == 0,
427               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
428
429   t_simdata->isused = 1;
430   t_simdata->comm = NULL;
431   msg_global->sent_msg++;
432
433   /* Send it by calling SIMIX network layer */
434   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
435   comm->task_sent = task;
436   comm->task_received = NULL;
437   comm->status = MSG_OK;
438   comm->s_comm =
439     simcall_comm_isend(mailbox, t_simdata->message_size,
440                          t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
441   t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
442
443   return comm;
444 }
445
446 /** \ingroup msg_task_usage
447  * \brief Sends a task on a mailbox.
448  *
449  * This is a non blocking detached send function.
450  * Think of it as a best effort send. Keep in mind that the third parameter
451  * is only called if the communication fails. If the communication does work,
452  * it is responsibility of the receiver code to free anything related to
453  * the task, as usual. More details on this can be obtained on
454  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
455  * in the SimGrid-user mailing list archive.
456  *
457  * \param task a #m_task_t to send on another location.
458  * \param alias name of the mailbox to sent the task to
459  * \param cleanup a function to destroy the task if the
460  * communication fails, e.g. MSG_task_destroy
461  * (if NULL, no function will be called)
462  */
463 void MSG_task_dsend(m_task_t task, const char *alias, void_f_pvoid_t cleanup)
464 {
465   simdata_task_t t_simdata = NULL;
466   m_process_t process = MSG_process_self();
467   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
468
469   /* FIXME: these functions are not traceable */
470
471   /* Prepare the task to send */
472   t_simdata = task->simdata;
473   t_simdata->sender = process;
474   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
475
476   xbt_assert(t_simdata->isused == 0,
477               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
478
479   t_simdata->isused = 1;
480   t_simdata->comm = NULL;
481   msg_global->sent_msg++;
482
483   /* Send it by calling SIMIX network layer */
484   smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
485                        t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
486   t_simdata->comm = comm;
487 }
488
489 /** \ingroup msg_task_usage
490  * \brief Starts listening for receiving a task from an asynchronous communication.
491  *
492  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
493  * to end the communication.
494  * 
495  * \param task a memory location for storing a #m_task_t. has to be valid until the end of the communication.
496  * \param name of the mailbox to receive the task on
497  * \return the msg_comm_t communication created
498  */
499 msg_comm_t MSG_task_irecv(m_task_t *task, const char *name)
500 {
501   smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
502
503   /* FIXME: these functions are not traceable */
504
505   /* Sanity check */
506   xbt_assert(task, "Null pointer for the task storage");
507
508   if (*task)
509     XBT_CRITICAL
510         ("MSG_task_irecv() was asked to write in a non empty task struct.");
511
512   /* Try to receive it by calling SIMIX network layer */
513   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
514   comm->task_sent = NULL;
515   comm->task_received = task;
516   comm->status = MSG_OK;
517   comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
518
519   return comm;
520 }
521
522 /** \ingroup msg_task_usage
523  * \brief Checks whether a communication is done, and if yes, finalizes it.
524  * \param comm the communication to test
525  * \return TRUE if the communication is finished
526  * (but it may have failed, use MSG_comm_get_status() to know its status)
527  * or FALSE if the communication is not finished yet
528  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
529  */
530 int MSG_comm_test(msg_comm_t comm)
531 {
532   xbt_ex_t e;
533   int finished = 0;
534   TRY {
535     finished = simcall_comm_test(comm->s_comm);
536
537     if (finished && comm->task_received != NULL) {
538       /* I am the receiver */
539       (*comm->task_received)->simdata->isused = 0;
540     }
541   }
542   CATCH(e) {
543     switch (e.category) {
544
545       case host_error:
546         comm->status = MSG_HOST_FAILURE;
547         finished = 1;
548         break;
549
550       case network_error:
551         comm->status = MSG_TRANSFER_FAILURE;
552         finished = 1;
553         break;
554
555       case timeout_error:
556         comm->status = MSG_TIMEOUT;
557         finished = 1;
558         break;
559
560       default:
561         RETHROW;
562     }
563     xbt_ex_free(e);
564   }
565
566   return finished;
567 }
568
569 /** \ingroup msg_task_usage
570  * \brief This function checks if a communication is finished.
571  * \param comms a vector of communications
572  * \return the position of the finished communication if any
573  * (but it may have failed, use MSG_comm_get_status() to know its status),
574  * or -1 if none is finished
575  */
576 int MSG_comm_testany(xbt_dynar_t comms)
577 {
578   xbt_ex_t e;
579   int finished_index = -1;
580
581   /* create the equivalent dynar with SIMIX objects */
582   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
583   msg_comm_t comm;
584   unsigned int cursor;
585   xbt_dynar_foreach(comms, cursor, comm) {
586     xbt_dynar_push(s_comms, &comm->s_comm);
587   }
588
589   MSG_error_t status = MSG_OK;
590   TRY {
591     finished_index = simcall_comm_testany(s_comms);
592   }
593   CATCH(e) {
594     switch (e.category) {
595
596       case host_error:
597         finished_index = e.value;
598         status = MSG_HOST_FAILURE;
599         break;
600
601       case network_error:
602         finished_index = e.value;
603         status = MSG_TRANSFER_FAILURE;
604         break;
605
606       case timeout_error:
607         finished_index = e.value;
608         status = MSG_TIMEOUT;
609         break;
610
611       default:
612         RETHROW;
613     }
614     xbt_ex_free(e);
615   }
616   xbt_dynar_free(&s_comms);
617
618   if (finished_index != -1) {
619     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
620     /* the communication is finished */
621     comm->status = status;
622
623     if (status == MSG_OK && comm->task_received != NULL) {
624       /* I am the receiver */
625       (*comm->task_received)->simdata->isused = 0;
626     }
627   }
628
629   return finished_index;
630 }
631
632 /** \ingroup msg_task_usage
633  * \brief Destroys a communication.
634  * \param comm the communication to destroy.
635  */
636 void MSG_comm_destroy(msg_comm_t comm)
637 {
638   xbt_free(comm);
639 }
640
641 /** \ingroup msg_task_usage
642  * \brief Wait for the completion of a communication.
643  *
644  * It takes two parameters.
645  * \param comm the communication to wait.
646  * \param timeout Wait until the communication terminates or the timeout 
647  * occurs. You can provide a -1 timeout to obtain an infinite timeout.
648  * \return MSG_error_t
649  */
650 MSG_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
651 {
652   xbt_ex_t e;
653   TRY {
654     simcall_comm_wait(comm->s_comm, timeout);
655
656     if (comm->task_received != NULL) {
657       /* I am the receiver */
658       (*comm->task_received)->simdata->isused = 0;
659     }
660
661     /* FIXME: these functions are not traceable */
662   }
663   CATCH(e) {
664     switch (e.category) {
665     case host_error:
666       comm->status = MSG_HOST_FAILURE;
667       break;
668     case network_error:
669       comm->status = MSG_TRANSFER_FAILURE;
670       break;
671     case timeout_error:
672       comm->status = MSG_TIMEOUT;
673       break;
674     default:
675       RETHROW;
676     }
677     xbt_ex_free(e);
678   }
679
680   return comm->status;
681 }
682
683 /** \ingroup msg_task_usage
684 * \brief This function is called by a sender and permit to wait for each communication
685 *
686 * \param comm a vector of communication
687 * \param nb_elem is the size of the comm vector
688 * \param timeout for each call of MSG_comm_wait
689 */
690 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
691 {
692   int i = 0;
693   for (i = 0; i < nb_elem; i++) {
694     MSG_comm_wait(comm[i], timeout);
695   }
696 }
697
698 /** \ingroup msg_task_usage
699  * \brief This function waits for the first communication finished in a list.
700  * \param comms a vector of communications
701  * \return the position of the first finished communication
702  * (but it may have failed, use MSG_comm_get_status() to know its status)
703  */
704 int MSG_comm_waitany(xbt_dynar_t comms)
705 {
706   xbt_ex_t e;
707   int finished_index = -1;
708
709   /* create the equivalent dynar with SIMIX objects */
710   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
711   msg_comm_t comm;
712   unsigned int cursor;
713   xbt_dynar_foreach(comms, cursor, comm) {
714     xbt_dynar_push(s_comms, &comm->s_comm);
715   }
716
717   MSG_error_t status = MSG_OK;
718   TRY {
719     finished_index = simcall_comm_waitany(s_comms);
720   }
721   CATCH(e) {
722     switch (e.category) {
723
724       case host_error:
725         finished_index = e.value;
726         status = MSG_HOST_FAILURE;
727         break;
728
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     (*comm->task_received)->simdata->isused = 0;
755   }
756
757   return finished_index;
758 }
759
760 /**
761  * \ingroup msg_task_usage
762  * \brief Returns the error (if any) that occured during a finished communication.
763  * \param comm a finished communication
764  * \return the status of the communication, or #MSG_OK if no error occured
765  * during the communication
766  */
767 MSG_error_t MSG_comm_get_status(msg_comm_t comm) {
768
769   return comm->status;
770 }
771
772 /** \ingroup msg_task_usage
773  * \brief Get a task (#m_task_t) from a communication
774  *
775  * \param comm the communication where to get the task
776  * \return the task from the communication
777  */
778 m_task_t MSG_comm_get_task(msg_comm_t comm)
779 {
780   xbt_assert(comm, "Invalid parameter");
781
782   return comm->task_received ? *comm->task_received : comm->task_sent;
783 }
784
785 /**
786  * \brief This function is called by SIMIX to copy the data of a comm.
787  * \param comm the comm
788  * \param buff the data copied
789  * \param buff_size size of the buffer
790  */
791 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
792
793   // copy the task
794   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
795
796   // notify the user callback if any
797   if (msg_global->task_copy_callback) {
798     m_task_t task = buff;
799     msg_global->task_copy_callback(task,
800         simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
801   }
802 }
803
804 /** \ingroup msg_task_usage
805  * \brief Sends a task to a mailbox
806  *
807  * This is a blocking function, the execution flow will be blocked
808  * until the task is sent (and received in the other side if #MSG_task_receive is used).
809  * See #MSG_task_isend for sending tasks asynchronously.
810  *
811  * \param task the task to be sent
812  * \param alias the mailbox name to where the task is sent
813  *
814  * \return Returns #MSG_OK if the task was successfully sent,
815  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
816  */
817 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
818 {
819   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
820   return MSG_task_send_with_timeout(task, alias, -1);
821 }
822
823 /** \ingroup msg_task_usage
824  * \brief Sends a task to a mailbox with a maximum rate
825  *
826  * This is a blocking function, the execution flow will be blocked
827  * until the task is sent. The maxrate parameter allows the application
828  * to limit the bandwidth utilization of network links when sending the task.
829  *
830  * \param task the task to be sent
831  * \param alias the mailbox name to where the task is sent
832  * \param maxrate the maximum communication rate for sending this task
833  *
834  * \return Returns #MSG_OK if the task was successfully sent,
835  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
836  */
837 MSG_error_t
838 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
839 {
840   task->simdata->rate = maxrate;
841   return MSG_task_send(task, alias);
842 }
843
844 /** \ingroup msg_task_usage
845  * \brief Sends a task to a mailbox with a timeout
846  *
847  * This is a blocking function, the execution flow will be blocked
848  * until the task is sent or the timeout is achieved.
849  *
850  * \param task the task to be sent
851  * \param alias the mailbox name to where the task is sent
852  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
853  *
854  * \return Returns #MSG_OK if the task was successfully sent,
855  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
856  */
857 MSG_error_t
858 MSG_task_send_with_timeout(m_task_t task, const char *alias,
859                            double timeout)
860 {
861   return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
862                                       task, timeout);
863 }
864
865 /** \ingroup msg_task_usage
866  * \brief Check if there is a communication going on in a mailbox.
867  *
868  * \param alias the name of the mailbox to be considered
869  *
870  * \return Returns 1 if there is a communication, 0 otherwise
871  */
872 int MSG_task_listen(const char *alias)
873 {
874   return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
875 }
876
877 /** \ingroup msg_task_usage
878  * \brief Check the number of communication actions of a given host pending in a mailbox.
879  *
880  * \param alias the name of the mailbox to be considered
881  * \param host the host to check for communication
882  *
883  * \return Returns the number of pending communication actions of the host in the
884  * given mailbox, 0 if there is no pending communication actions.
885  *
886  */
887 int MSG_task_listen_from_host(const char *alias, m_host_t host)
888 {
889   return
890       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
891                                                (alias), host);
892 }
893
894 /** \ingroup msg_task_usage
895  * \brief Look if there is a communication on a mailbox and return the
896  * PID of the sender process.
897  *
898  * \param alias the name of the mailbox to be considered
899  *
900  * \return Returns the PID of sender process,
901  * -1 if there is no communication in the mailbox.
902  */
903 int MSG_task_listen_from(const char *alias)
904 {
905   m_task_t task;
906
907   if (NULL ==
908       (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
909     return -1;
910
911   return MSG_process_get_PID(task->simdata->sender);
912 }
913
914 /** \ingroup msg_task_usage
915  * \brief Sets the tracing category of a task.
916  *
917  * This function should be called after the creation of
918  * a MSG task, to define the category of that task. The
919  * first parameter task must contain a task that was
920  * created with the function #MSG_task_create. The second
921  * parameter category must contain a category that was
922  * previously declared with the function #TRACE_category
923  * (or with #TRACE_category_with_color).
924  *
925  * See \ref tracing_tracing for details on how to trace
926  * the (categorized) resource utilization.
927  *
928  * \param task the task that is going to be categorized
929  * \param category the name of the category to be associated to the task
930  *
931  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
932  */
933 void MSG_task_set_category (m_task_t task, const char *category)
934 {
935 #ifdef HAVE_TRACING
936   TRACE_msg_set_task_category (task, category);
937 #endif
938 }
939
940 /** \ingroup msg_task_usage
941  *
942  * \brief Gets the current tracing category of a task.
943  *
944  * \param task the task to be considered
945  *
946  * \see MSG_task_set_category
947  *
948  * \return Returns the name of the tracing category of the given task, NULL otherwise
949  */
950 const char *MSG_task_get_category (m_task_t task)
951 {
952 #ifdef HAVE_TRACING
953   return task->category;
954 #else
955   return NULL;
956 #endif
957 }
958
959 #ifdef MSG_USE_DEPRECATED
960 /** \ingroup msg_deprecated_functions
961  *
962  * \brief Return the last value returned by a MSG function (except
963  * MSG_get_errno...).
964  */
965 MSG_error_t MSG_get_errno(void)
966 {
967   return PROCESS_GET_ERRNO();
968 }
969
970 /** \ingroup msg_deprecated_functions
971  * \brief Put a task on a channel of an host and waits for the end of the
972  * transmission.
973  *
974  * This function is used for describing the behavior of a process. It
975  * takes three parameter.
976  * \param task a #m_task_t to send on another location. This task
977  will not be usable anymore when the function will return. There is
978  no automatic task duplication and you have to save your parameters
979  before calling this function. Tasks are unique and once it has been
980  sent to another location, you should not access it anymore. You do
981  not need to call MSG_task_destroy() but to avoid using, as an
982  effect of inattention, this task anymore, you definitely should
983  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
984  can be transfered iff it has been correctly created with
985  MSG_task_create().
986  * \param dest the destination of the message
987  * \param channel the channel on which the process should put this
988  task. This value has to be >=0 and < than the maximal number of
989  channels fixed with MSG_set_channel_number().
990  * \return #MSG_HOST_FAILURE if the host on which
991  * this function was called was shut down,
992  * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
993  * (network failure, dest failure) or #MSG_OK if it succeeded.
994  */
995 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
996 {
997   XBT_WARN("DEPRECATED! Now use MSG_task_send");
998   return MSG_task_put_with_timeout(task, dest, channel, -1.0);
999 }
1000
1001 /** \ingroup msg_deprecated_functions
1002  * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1003  * rate.
1004  *
1005  * \sa MSG_task_put
1006  */
1007 MSG_error_t
1008 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
1009                      double maxrate)
1010 {
1011   XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1012   task->simdata->rate = maxrate;
1013   return MSG_task_put(task, dest, channel);
1014 }
1015
1016 /** \ingroup msg_deprecated_functions
1017  *
1018  * \brief Put a task on a channel of an
1019  * host (with a timeout on the waiting of the destination host) and
1020  * waits for the end of the transmission.
1021  *
1022  * This function is used for describing the behavior of a process. It
1023  * takes four parameter.
1024  * \param task a #m_task_t to send on another location. This task
1025  will not be usable anymore when the function will return. There is
1026  no automatic task duplication and you have to save your parameters
1027  before calling this function. Tasks are unique and once it has been
1028  sent to another location, you should not access it anymore. You do
1029  not need to call MSG_task_destroy() but to avoid using, as an
1030  effect of inattention, this task anymore, you definitely should
1031  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1032  can be transfered iff it has been correctly created with
1033  MSG_task_create().
1034  * \param dest the destination of the message
1035  * \param channel the channel on which the process should put this
1036  task. This value has to be >=0 and < than the maximal number of
1037  channels fixed with MSG_set_channel_number().
1038  * \param timeout the maximum time to wait for a task before giving
1039  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1040  will not be modified
1041  * \return #MSG_HOST_FAILURE if the host on which
1042 this function was called was shut down,
1043 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1044 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1045  */
1046 MSG_error_t
1047 MSG_task_put_with_timeout(m_task_t task, m_host_t dest,
1048                           m_channel_t channel, double timeout)
1049 {
1050   XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1051   xbt_assert((channel >= 0)
1052               && (channel < msg_global->max_channel), "Invalid channel %d",
1053               channel);
1054
1055   XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
1056   return
1057       MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1058                                    (dest, channel), task, timeout);
1059 }
1060
1061 /** \ingroup msg_deprecated_functions
1062  * \brief Test whether there is a pending communication on a channel, and who sent it.
1063  *
1064  * It takes one parameter.
1065  * \param channel the channel on which the process should be
1066  listening. This value has to be >=0 and < than the maximal
1067  number of channels fixed with MSG_set_channel_number().
1068  * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1069  */
1070 int MSG_task_probe_from(m_channel_t channel)
1071 {
1072   XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1073   m_task_t task;
1074
1075   xbt_assert((channel >= 0)
1076               && (channel < msg_global->max_channel), "Invalid channel %d",
1077               channel);
1078
1079   if (NULL ==
1080       (task =
1081        MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1082                             (MSG_host_self(), channel))))
1083     return -1;
1084
1085   return MSG_process_get_PID(task->simdata->sender);
1086 }
1087
1088 /** \ingroup msg_deprecated_functions
1089  * \brief Test whether there is a pending communication on a channel.
1090  *
1091  * It takes one parameter.
1092  * \param channel the channel on which the process should be
1093  listening. This value has to be >=0 and < than the maximal
1094  number of channels fixed with MSG_set_channel_number().
1095  * \return 1 if there is a pending communication and 0 otherwise
1096  */
1097 int MSG_task_Iprobe(m_channel_t channel)
1098 {
1099   XBT_WARN("DEPRECATED!");
1100   xbt_assert((channel >= 0)
1101               && (channel < msg_global->max_channel), "Invalid channel %d",
1102               channel);
1103
1104   return
1105       !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1106                             (MSG_host_self(), channel));
1107 }
1108
1109 /** \ingroup msg_deprecated_functions
1110
1111  * \brief Return the number of tasks waiting to be received on a \a
1112  channel and sent by \a host.
1113  *
1114  * It takes two parameters.
1115  * \param channel the channel on which the process should be
1116  listening. This value has to be >=0 and < than the maximal
1117  number of channels fixed with MSG_set_channel_number().
1118  * \param host the host that is to be watched.
1119  * \return the number of tasks waiting to be received on \a channel
1120  and sent by \a host.
1121  */
1122 int MSG_task_probe_from_host(int channel, m_host_t host)
1123 {
1124   XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1125   xbt_assert((channel >= 0)
1126               && (channel < msg_global->max_channel), "Invalid channel %d",
1127               channel);
1128
1129   return
1130       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1131                                                (MSG_host_self(), channel),
1132                                                host);
1133
1134 }
1135
1136 /** \ingroup msg_deprecated_functions
1137  * \brief Listen on \a channel and waits for receiving a task from \a host.
1138  *
1139  * It takes three parameters.
1140  * \param task a memory location for storing a #m_task_t. It will
1141  hold a task when this function will return. Thus \a task should not
1142  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1143  those two condition does not hold, there will be a warning message.
1144  * \param channel the channel on which the process should be
1145  listening. This value has to be >=0 and < than the maximal
1146  number of channels fixed with MSG_set_channel_number().
1147  * \param host the host that is to be watched.
1148  * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1149  */
1150 MSG_error_t
1151 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
1152 {
1153   XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1154   return MSG_task_get_ext(task, channel, -1, host);
1155 }
1156
1157 /** \ingroup msg_deprecated_functions
1158  * \brief Listen on a channel and wait for receiving a task.
1159  *
1160  * It takes two parameters.
1161  * \param task a memory location for storing a #m_task_t. It will
1162  hold a task when this function will return. Thus \a task should not
1163  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1164  those two condition does not hold, there will be a warning message.
1165  * \param channel the channel on which the process should be
1166  listening. This value has to be >=0 and < than the maximal
1167  number of channels fixed with MSG_set_channel_number().
1168  * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1169  */
1170 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
1171 {
1172   XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1173   return MSG_task_get_with_timeout(task, channel, -1);
1174 }
1175
1176 /** \ingroup msg_deprecated_functions
1177  * \brief Listen on a channel and wait for receiving a task with a timeout.
1178  *
1179  * It takes three parameters.
1180  * \param task a memory location for storing a #m_task_t. It will
1181  hold a task when this function will return. Thus \a task should not
1182  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1183  those two condition does not hold, there will be a warning message.
1184  * \param channel the channel on which the process should be
1185  listening. This value has to be >=0 and < than the maximal
1186  number of channels fixed with MSG_set_channel_number().
1187  * \param max_duration the maximum time to wait for a task before giving
1188  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1189  will not be modified and will still be
1190  equal to \c NULL when returning.
1191  * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1192  */
1193 MSG_error_t
1194 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
1195                           double max_duration)
1196 {
1197   XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1198   return MSG_task_get_ext(task, channel, max_duration, NULL);
1199 }
1200
1201 MSG_error_t
1202 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
1203                  m_host_t host)
1204 {
1205   XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1206   xbt_assert((channel >= 0)
1207               && (channel < msg_global->max_channel), "Invalid channel %d",
1208               channel);
1209
1210   return
1211       MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1212                                (MSG_host_self(), channel), task, host,
1213                                timeout);
1214 }
1215
1216 #endif