Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4c4480e2eea8c004ca4400467747c031fab24459
[simgrid.git] / src / msg / gos.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/sysdep.h"
9 #include "mc/mc.h"
10 #include "xbt/log.h"
11 #include "mailbox.h"
12
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
15                                 "Logging specific to MSG (gos)");
16
17 /** \ingroup msg_gos_functions
18  *
19  * \brief Return the last value returned by a MSG function (except
20  * MSG_get_errno...).
21  */
22 MSG_error_t MSG_get_errno(void)
23 {
24   return PROCESS_GET_ERRNO();
25 }
26
27 /** \ingroup msg_gos_functions
28  * \brief Executes a task and waits for its termination.
29  *
30  * This function is used for describing the behavior of an agent. It
31  * takes only one parameter.
32  * \param task a #m_task_t to execute on the location on which the
33  agent is running.
34  * \return #MSG_FATAL if \a task is not properly initialized and
35  * #MSG_OK otherwise.
36  */
37 MSG_error_t MSG_task_execute(m_task_t task)
38 {
39   simdata_task_t simdata = NULL;
40   m_process_t self = MSG_process_self();
41   e_smx_state_t comp_state;
42   CHECK_HOST();
43
44   simdata = task->simdata;
45
46   xbt_assert0(simdata->host_nb == 0,
47               "This is a parallel task. Go to hell.");
48
49 #ifdef HAVE_TRACING
50   TRACE_msg_task_execute_start(task);
51 #endif
52
53   xbt_assert1((!simdata->compute) && (task->simdata->isused == 0),
54               "This task is executed somewhere else. Go fix your code! %d",
55               task->simdata->isused);
56
57   DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
58
59   if (simdata->computation_amount == 0) {
60 #ifdef HAVE_TRACING
61     TRACE_msg_task_execute_end(task);
62 #endif
63     return MSG_OK;
64   }
65   simdata->isused=1;
66   simdata->compute =
67       SIMIX_req_host_execute(task->name, SIMIX_host_self(),
68                            simdata->computation_amount,
69                            simdata->priority);
70 #ifdef HAVE_TRACING
71   SIMIX_req_set_category(simdata->compute, task->category);
72 #endif
73
74   self->simdata->waiting_action = simdata->compute;
75   comp_state = SIMIX_req_host_execution_wait(simdata->compute);
76   self->simdata->waiting_action = NULL;
77
78   simdata->isused=0;
79
80   DEBUG2("Execution task '%s' finished in state %d", task->name, comp_state);
81   if (comp_state == SIMIX_DONE) {
82     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
83     simdata->computation_amount = 0.0;
84     simdata->comm = NULL;
85     simdata->compute = NULL;
86 #ifdef HAVE_TRACING
87     TRACE_msg_task_execute_end(task);
88 #endif
89     MSG_RETURN(MSG_OK);
90   } else if (SIMIX_req_host_get_state(SIMIX_host_self()) == 0) {
91     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
92     simdata->comm = NULL;
93     simdata->compute = NULL;
94 #ifdef HAVE_TRACING
95     TRACE_msg_task_execute_end(task);
96 #endif
97     MSG_RETURN(MSG_HOST_FAILURE);
98   } else {
99     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
100     simdata->comm = NULL;
101     simdata->compute = NULL;
102 #ifdef HAVE_TRACING
103     TRACE_msg_task_execute_end(task);
104 #endif
105     MSG_RETURN(MSG_TASK_CANCELLED);
106   }
107 }
108
109 /** \ingroup m_task_management
110  * \brief Creates a new #m_task_t (a parallel one....).
111  *
112  * A constructor for #m_task_t taking six arguments and returning the
113  corresponding object.
114  * \param name a name for the object. It is for user-level information
115  and can be NULL.
116  * \param host_nb the number of hosts implied in the parallel task.
117  * \param host_list an array of \p host_nb m_host_t.
118  * \param computation_amount an array of \p host_nb
119  doubles. computation_amount[i] is the total number of operations
120  that have to be performed on host_list[i].
121  * \param communication_amount an array of \p host_nb* \p host_nb doubles.
122  * \param data a pointer to any data may want to attach to the new
123  object.  It is for user-level information and can be NULL. It can
124  be retrieved with the function \ref MSG_task_get_data.
125  * \see m_task_t
126  * \return The new corresponding object.
127  */
128 m_task_t
129 MSG_parallel_task_create(const char *name, int host_nb,
130                          const m_host_t * host_list,
131                          double *computation_amount,
132                          double *communication_amount, void *data)
133 {
134   int i;
135   simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
136   m_task_t task = xbt_new0(s_m_task_t, 1);
137   task->simdata = simdata;
138
139   /* Task structure */
140   task->name = xbt_strdup(name);
141   task->data = data;
142
143   /* Simulator Data */
144   simdata->computation_amount = 0;
145   simdata->message_size = 0;
146   simdata->compute = NULL;
147   simdata->comm = NULL;
148   simdata->rate = -1.0;
149   simdata->isused = 0;
150   simdata->sender = NULL;
151   simdata->receiver = NULL;
152   simdata->source = NULL;
153
154   simdata->host_nb = host_nb;
155   simdata->host_list = xbt_new0(smx_host_t, host_nb);
156   simdata->comp_amount = computation_amount;
157   simdata->comm_amount = communication_amount;
158
159   for (i = 0; i < host_nb; i++)
160     simdata->host_list[i] = host_list[i]->simdata->smx_host;
161
162   return task;
163 }
164
165 MSG_error_t MSG_parallel_task_execute(m_task_t task)
166 {
167   simdata_task_t simdata = NULL;
168   e_smx_state_t comp_state;
169   m_process_t self = MSG_process_self();
170   CHECK_HOST();
171
172   simdata = task->simdata;
173
174   xbt_assert0((!simdata->compute)
175               && (task->simdata->isused == 0),
176               "This task is executed somewhere else. Go fix your code!");
177
178   xbt_assert0(simdata->host_nb,
179               "This is not a parallel task. Go to hell.");
180
181   DEBUG1("Parallel computing on %s", MSG_process_self()->simdata->m_host->name);
182
183   simdata->isused=1;
184
185   simdata->compute =
186       SIMIX_req_host_parallel_execute(task->name, simdata->host_nb,
187                                   simdata->host_list,
188                                   simdata->comp_amount,
189                                   simdata->comm_amount, 1.0, -1.0);
190   DEBUG1("Parallel execution action created: %p", simdata->compute);
191
192   self->simdata->waiting_action = simdata->compute;
193   comp_state = SIMIX_req_host_execution_wait(simdata->compute);
194   self->simdata->waiting_action = NULL;
195
196   DEBUG2("Finished waiting for execution of action %p, state = %d", simdata->compute, comp_state);
197
198   simdata->isused=0;
199
200   if (comp_state == SIMIX_DONE) {
201     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
202     simdata->computation_amount = 0.0;
203     simdata->comm = NULL;
204     simdata->compute = NULL;
205     MSG_RETURN(MSG_OK);
206   } else if (SIMIX_req_host_get_state(SIMIX_host_self()) == 0) {
207     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
208     simdata->comm = NULL;
209     simdata->compute = NULL;
210     MSG_RETURN(MSG_HOST_FAILURE);
211   } else {
212     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
213     simdata->comm = NULL;
214     simdata->compute = NULL;
215     MSG_RETURN(MSG_TASK_CANCELLED);
216   }
217 }
218
219
220 /** \ingroup msg_gos_functions
221  * \brief Sleep for the specified number of seconds
222  *
223  * Makes the current process sleep until \a time seconds have elapsed.
224  *
225  * \param nb_sec a number of second
226  */
227 MSG_error_t MSG_process_sleep(double nb_sec)
228 {
229   e_smx_state_t state;
230   /*m_process_t proc = MSG_process_self();*/
231
232 #ifdef HAVE_TRACING
233   TRACE_msg_process_sleep_in(MSG_process_self());
234 #endif
235
236   /* create action to sleep */
237   state = SIMIX_req_process_sleep(nb_sec);
238
239   /*proc->simdata->waiting_action = act_sleep;
240
241   FIXME: check if not setting the waiting_action breaks something on msg
242   
243   proc->simdata->waiting_action = NULL;*/
244   
245   if (state == SIMIX_DONE) {
246 #ifdef HAVE_TRACING
247   TRACE_msg_process_sleep_out(MSG_process_self());
248 #endif
249     MSG_RETURN(MSG_OK);
250   } else {
251 #ifdef HAVE_TRACING
252     TRACE_msg_process_sleep_out(MSG_process_self());
253 #endif
254     MSG_RETURN(MSG_HOST_FAILURE);
255   }
256 }
257
258 /** \ingroup msg_gos_functions
259  * \brief Listen on \a channel and waits for receiving a task from \a host.
260  *
261  * It takes three parameters.
262  * \param task a memory location for storing a #m_task_t. It will
263  hold a task when this function will return. Thus \a task should not
264  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
265  those two condition does not hold, there will be a warning message.
266  * \param channel the channel on which the agent should be
267  listening. This value has to be >=0 and < than the maximal
268  number of channels fixed with MSG_set_channel_number().
269  * \param host the host that is to be watched.
270  * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
271  if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
272  */
273 MSG_error_t
274 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
275 {
276   return MSG_task_get_ext(task, channel, -1, host);
277 }
278
279 /** \ingroup msg_gos_functions
280  * \brief Listen on a channel and wait for receiving a task.
281  *
282  * It takes two parameters.
283  * \param task a memory location for storing a #m_task_t. It will
284  hold a task when this function will return. Thus \a task should not
285  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
286  those two condition does not hold, there will be a warning message.
287  * \param channel the channel on which the agent should be
288  listening. This value has to be >=0 and < than the maximal
289  number of channels fixed with MSG_set_channel_number().
290  * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
291  * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
292  */
293 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
294 {
295   return MSG_task_get_with_timeout(task, channel, -1);
296 }
297
298 /** \ingroup msg_gos_functions
299  * \brief Listen on a channel and wait for receiving a task with a timeout.
300  *
301  * It takes three parameters.
302  * \param task a memory location for storing a #m_task_t. It will
303  hold a task when this function will return. Thus \a task should not
304  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
305  those two condition does not hold, there will be a warning message.
306  * \param channel the channel on which the agent should be
307  listening. This value has to be >=0 and < than the maximal
308  number of channels fixed with MSG_set_channel_number().
309  * \param max_duration the maximum time to wait for a task before giving
310  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
311  will not be modified and will still be
312  equal to \c NULL when returning.
313  * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
314  if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
315  */
316 MSG_error_t
317 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
318                           double max_duration)
319 {
320   return MSG_task_get_ext(task, channel, max_duration, NULL);
321 }
322
323 /** \defgroup msg_gos_functions MSG Operating System Functions
324  *  \brief This section describes the functions that can be used
325  *  by an agent for handling some task.
326  */
327
328 MSG_error_t
329 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
330                  m_host_t host)
331 {
332   xbt_assert1((channel >= 0)
333               && (channel < msg_global->max_channel), "Invalid channel %d",
334               channel);
335
336   return
337       MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
338                                (MSG_host_self(), channel), task, host,
339                                timeout);
340 }
341
342 MSG_error_t
343 MSG_task_receive_from_host(m_task_t * task, const char *alias,
344                            m_host_t host)
345 {
346   return MSG_task_receive_ext(task, alias, -1, host);
347 }
348
349 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
350 {
351   return MSG_task_receive_with_timeout(task, alias, -1);
352 }
353
354 MSG_error_t
355 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
356                               double timeout)
357 {
358   return MSG_task_receive_ext(task, alias, timeout, NULL);
359 }
360
361 MSG_error_t
362 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
363                      m_host_t host)
364 {
365   DEBUG1
366       ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
367        alias);
368   return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
369                                   host, timeout);
370 }
371
372 /** \ingroup msg_gos_functions
373  * \brief Sends a task on a mailbox.
374  *
375  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
376  * to end the communication.
377  *
378  * \param task a #m_task_t to send on another location.
379  * \param alias name of the mailbox to sent the task to
380  * \return the msg_comm_t communication created
381  */
382 msg_comm_t MSG_task_isend(m_task_t task, const char *alias)
383 {
384   simdata_task_t t_simdata = NULL;
385   m_process_t process = MSG_process_self();
386   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
387
388   CHECK_HOST();
389
390   /* FIXME: these functions are not traceable */
391
392   /* Prepare the task to send */
393   t_simdata = task->simdata;
394   t_simdata->sender = process;
395   t_simdata->source = MSG_host_self();
396
397   xbt_assert0(t_simdata->isused == 0,
398               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
399
400   t_simdata->isused = 1;
401   msg_global->sent_msg++;
402
403   /* Send it by calling SIMIX network layer */
404   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
405   comm->task_sent = task;
406   comm->task_received = NULL;
407   comm->status = MSG_OK;
408   comm->s_comm =
409     SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
410                          t_simdata->rate, task, sizeof(void *), NULL, NULL, 0);
411   t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
412
413   return comm;
414 }
415
416 /** \ingroup msg_gos_functions
417  * \brief Sends a task on a mailbox.
418  *
419  * This is a non blocking detached send function.
420  * Think of it as a best effort send. The communication
421  * object will be destroyed by the receiver (if any).
422  *
423  * \param task a #m_task_t to send on another location.
424  * \param alias name of the mailbox to sent the task to
425  */
426 void MSG_task_dsend(m_task_t task, const char *alias, void_f_pvoid_t cleanup)
427 {
428   simdata_task_t t_simdata = NULL;
429   m_process_t process = MSG_process_self();
430   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
431
432   CHECK_HOST();
433
434   /* FIXME: these functions are not traceable */
435
436   /* Prepare the task to send */
437   t_simdata = task->simdata;
438   t_simdata->sender = process;
439   t_simdata->source = MSG_host_self();
440
441   xbt_assert0(t_simdata->isused == 0,
442               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
443
444   t_simdata->isused = 1;
445   msg_global->sent_msg++;
446
447   /* Send it by calling SIMIX network layer */
448   SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
449                        t_simdata->rate, task, sizeof(void *), NULL, cleanup, 1);
450 }
451
452 /** \ingroup msg_gos_functions
453  * \brief Starts listening for receiving a task from an asynchronous communication.
454  *
455  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
456  * to end the communication.
457  *
458  * \param task a memory location for storing a #m_task_t.
459  * \param name of the mailbox to receive the task on
460  * \return the msg_comm_t communication created
461  */
462 msg_comm_t MSG_task_irecv(m_task_t *task, const char *alias)
463 {
464   smx_rdv_t rdv = MSG_mailbox_get_by_alias(alias);
465
466   CHECK_HOST();
467
468   /* FIXME: these functions are not tracable */
469
470   /* Sanity check */
471   xbt_assert0(task, "Null pointer for the task storage");
472
473   if (*task)
474     CRITICAL0
475         ("MSG_task_get() was asked to write in a non empty task struct.");
476
477   /* Try to receive it by calling SIMIX network layer */
478   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
479   comm->task_sent = NULL;
480   comm->task_received = task;
481   comm->status = MSG_OK;
482   comm->s_comm = SIMIX_req_comm_irecv(rdv, task, NULL, NULL, NULL);
483
484   return comm;
485 }
486
487 /** \ingroup msg_gos_functions
488  * \brief Checks whether a communication is done, and if yes, finalizes it.
489  * \param comm the communication to test
490  * \return TRUE if the communication is finished
491  * (but it may have failed, use MSG_comm_get_status() to know its status)
492  * or FALSE if the communication is not finished yet
493  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
494  */
495 int MSG_comm_test(msg_comm_t comm)
496 {
497   xbt_ex_t e;
498   int finished = 0;
499   TRY {
500     finished = SIMIX_req_comm_test(comm->s_comm);
501   }
502   CATCH(e) {
503     switch (e.category) {
504
505       case host_error:
506         comm->status = MSG_HOST_FAILURE;
507         finished = 1;
508         break;
509
510       case network_error:
511         comm->status = MSG_TRANSFER_FAILURE;
512         finished = 1;
513         break;
514
515       case timeout_error:
516         comm->status = MSG_TIMEOUT;
517         finished = 1;
518         break;
519
520       default:
521         RETHROW;
522     }
523     xbt_ex_free(e);
524   }
525
526   return finished;
527 }
528
529 /** \ingroup msg_gos_functions
530  * \brief This function checks if a communication is finished.
531  * \param comms a vector of communications
532  * \return the position of the finished communication if any
533  * (but it may have failed, use MSG_comm_get_status() to know its status),
534  * or -1 if none is finished
535  */
536 int MSG_comm_testany(xbt_dynar_t comms)
537 {
538   xbt_ex_t e;
539   int finished_index = -1;
540
541   /* create the equivalent dynar with SIMIX objects */
542   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
543   msg_comm_t comm;
544   unsigned int cursor;
545   xbt_dynar_foreach(comms, cursor, comm) {
546     xbt_dynar_push(s_comms, &comm->s_comm);
547   }
548
549   MSG_error_t status = MSG_OK;
550   TRY {
551     finished_index = SIMIX_req_comm_testany(s_comms);
552   }
553   CATCH(e) {
554     switch (e.category) {
555
556       case host_error:
557         finished_index = e.value;
558         status = MSG_HOST_FAILURE;
559         break;
560
561       case network_error:
562         finished_index = e.value;
563         status = MSG_TRANSFER_FAILURE;
564         break;
565
566       case timeout_error:
567         finished_index = e.value;
568         status = MSG_TIMEOUT;
569         break;
570
571       default:
572         RETHROW;
573     }
574     xbt_ex_free(e);
575   }
576   xbt_dynar_free(&s_comms);
577
578   if (finished_index != -1) {
579     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
580     /* the communication is finished */
581     comm->status = status;
582   }
583
584   return finished_index;
585 }
586
587 /** \ingroup msg_gos_functions
588  * \brief Destroys a communication.
589  * \param comm the communication to destroy.
590  */
591 void MSG_comm_destroy(msg_comm_t comm)
592 {
593   if (comm->task_received != NULL
594       && *comm->task_received != NULL
595       && MSG_comm_get_status(comm) == MSG_OK) {
596     (*comm->task_received)->simdata->isused = 0;
597   }
598
599   /* FIXME auto-destroy comms from SIMIX to avoid this request */
600   SIMIX_req_comm_destroy(comm->s_comm);
601   free(comm);
602 }
603
604 /** \ingroup msg_gos_functions
605  * \brief Wait for the completion of a communication.
606  *
607  * It takes two parameters.
608  * \param comm the communication to wait.
609  * \param timeout Wait until the communication terminates or the timeout occurs
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     SIMIX_req_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_gos_functions
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_gos_functions
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 = SIMIX_req_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_assert0(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   return finished_index;
715 }
716
717 /**
718  * \ingroup msg_gos_functions
719  * \brief Returns the error (if any) that occured during a finished communication.
720  * \param comm a finished communication
721  * \return the status of the communication, or MSG_OK if no error occured
722  * during the communication
723  */
724 MSG_error_t MSG_comm_get_status(msg_comm_t comm) {
725
726   return comm->status;
727 }
728
729 m_task_t MSG_comm_get_task(msg_comm_t comm)
730 {
731   xbt_assert0(comm, "Invalid parameter");
732
733   return comm->task_received ? *comm->task_received : comm->task_sent;
734 }
735
736 /** \ingroup msg_gos_functions
737  * \brief Put a task on a channel of an host and waits for the end of the
738  * transmission.
739  *
740  * This function is used for describing the behavior of an agent. It
741  * takes three parameter.
742  * \param task a #m_task_t to send on another location. This task
743  will not be usable anymore when the function will return. There is
744  no automatic task duplication and you have to save your parameters
745  before calling this function. Tasks are unique and once it has been
746  sent to another location, you should not access it anymore. You do
747  not need to call MSG_task_destroy() but to avoid using, as an
748  effect of inattention, this task anymore, you definitely should
749  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
750  can be transfered iff it has been correctly created with
751  MSG_task_create().
752  * \param dest the destination of the message
753  * \param channel the channel on which the agent should put this
754  task. This value has to be >=0 and < than the maximal number of
755  channels fixed with MSG_set_channel_number().
756  * \return #MSG_FATAL if \a task is not properly initialized and
757  * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
758  * this function was called was shut down. Returns
759  * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
760  * (network failure, dest failure)
761  */
762 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
763 {
764   return MSG_task_put_with_timeout(task, dest, channel, -1.0);
765 }
766
767 /** \ingroup msg_gos_functions
768  * \brief Does exactly the same as MSG_task_put but with a bounded transmition
769  * rate.
770  *
771  * \sa MSG_task_put
772  */
773 MSG_error_t
774 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
775                      double maxrate)
776 {
777   task->simdata->rate = maxrate;
778   return MSG_task_put(task, dest, channel);
779 }
780
781 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
782  * host (with a timeout on the waiting of the destination host) and
783  * waits for the end of the transmission.
784  *
785  * This function is used for describing the behavior of an agent. It
786  * takes four parameter.
787  * \param task a #m_task_t to send on another location. This task
788  will not be usable anymore when the function will return. There is
789  no automatic task duplication and you have to save your parameters
790  before calling this function. Tasks are unique and once it has been
791  sent to another location, you should not access it anymore. You do
792  not need to call MSG_task_destroy() but to avoid using, as an
793  effect of inattention, this task anymore, you definitely should
794  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
795  can be transfered iff it has been correctly created with
796  MSG_task_create().
797  * \param dest the destination of the message
798  * \param channel the channel on which the agent should put this
799  task. This value has to be >=0 and < than the maximal number of
800  channels fixed with MSG_set_channel_number().
801  * \param timeout the maximum time to wait for a task before giving
802  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
803  will not be modified
804  * \return #MSG_FATAL if \a task is not properly initialized and
805 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
806 this function was called was shut down. Returns
807 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
808 (network failure, dest failure, timeout...)
809  */
810 MSG_error_t
811 MSG_task_put_with_timeout(m_task_t task, m_host_t dest,
812                           m_channel_t channel, double timeout)
813 {
814   xbt_assert1((channel >= 0)
815               && (channel < msg_global->max_channel), "Invalid channel %d",
816               channel);
817
818   DEBUG1("MSG_task_put_with_timout: Trying to send a task to '%s'", dest->name);
819   return
820       MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
821                                    (dest, channel), task, timeout);
822 }
823
824 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
825 {
826   DEBUG1("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
827   return MSG_task_send_with_timeout(task, alias, -1);
828 }
829
830
831 MSG_error_t
832 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
833 {
834   task->simdata->rate = maxrate;
835   return MSG_task_send(task, alias);
836 }
837
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 int MSG_task_listen(const char *alias)
848 {
849   CHECK_HOST();
850
851   return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
852 }
853
854 /** \ingroup msg_gos_functions
855  * \brief Test whether there is a pending communication on a channel.
856  *
857  * It takes one parameter.
858  * \param channel the channel on which the agent should be
859  listening. This value has to be >=0 and < than the maximal
860  number of channels fixed with MSG_set_channel_number().
861  * \return 1 if there is a pending communication and 0 otherwise
862  */
863 int MSG_task_Iprobe(m_channel_t channel)
864 {
865   xbt_assert1((channel >= 0)
866               && (channel < msg_global->max_channel), "Invalid channel %d",
867               channel);
868
869   CHECK_HOST();
870
871   return
872       !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
873                             (MSG_host_self(), channel));
874 }
875
876 /** \ingroup msg_gos_functions
877
878  * \brief Return the number of tasks waiting to be received on a \a
879  channel and sent by \a host.
880  *
881  * It takes two parameters.
882  * \param channel the channel on which the agent should be
883  listening. This value has to be >=0 and < than the maximal
884  number of channels fixed with MSG_set_channel_number().
885  * \param host the host that is to be watched.
886  * \return the number of tasks waiting to be received on \a channel
887  and sent by \a host.
888  */
889 int MSG_task_probe_from_host(int channel, m_host_t host)
890 {
891   xbt_assert1((channel >= 0)
892               && (channel < msg_global->max_channel), "Invalid channel %d",
893               channel);
894
895   CHECK_HOST();
896
897   return
898       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
899                                                (MSG_host_self(), channel),
900                                                host);
901
902 }
903
904 int MSG_task_listen_from_host(const char *alias, m_host_t host)
905 {
906   CHECK_HOST();
907
908   return
909       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
910                                                (alias), host);
911 }
912
913 /** \ingroup msg_gos_functions
914  * \brief Test whether there is a pending communication on a channel, and who sent it.
915  *
916  * It takes one parameter.
917  * \param channel the channel on which the agent should be
918  listening. This value has to be >=0 and < than the maximal
919  number of channels fixed with MSG_set_channel_number().
920  * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
921  */
922 int MSG_task_probe_from(m_channel_t channel)
923 {
924   m_task_t task;
925
926   CHECK_HOST();
927
928   xbt_assert1((channel >= 0)
929               && (channel < msg_global->max_channel), "Invalid channel %d",
930               channel);
931
932   if (NULL ==
933       (task =
934        MSG_mailbox_get_head(MSG_mailbox_get_by_channel
935                             (MSG_host_self(), channel))))
936     return -1;
937
938   return MSG_process_get_PID(task->simdata->sender);
939 }
940
941 int MSG_task_listen_from(const char *alias)
942 {
943   m_task_t task;
944
945   CHECK_HOST();
946
947   if (NULL ==
948       (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
949     return -1;
950
951   return MSG_process_get_PID(task->simdata->sender);
952 }