Logo AND Algorithmique Numérique Distribuée

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