Logo AND Algorithmique Numérique Distribuée

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