Logo AND Algorithmique Numérique Distribuée

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