Logo AND Algorithmique Numérique Distribuée

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