Logo AND Algorithmique Numérique Distribuée

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