Logo AND Algorithmique Numérique Distribuée

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