Logo AND Algorithmique Numérique Distribuée

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