Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stubs for comm_testany, implemented with comm_waitany for now. That's crude but tempo...
[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   CHECK_HOST();
42
43   simdata = task->simdata;
44
45   xbt_assert0(simdata->host_nb == 0,
46               "This is a parallel task. Go to hell.");
47
48 #ifdef HAVE_TRACING
49   TRACE_msg_task_execute_start(task);
50 #endif
51
52   xbt_assert1((!simdata->compute) && (task->simdata->refcount == 1),
53               "This task is executed somewhere else. Go fix your code! %d",
54               task->simdata->refcount);
55
56   DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
57
58   if (simdata->computation_amount == 0) {
59 #ifdef HAVE_TRACING
60     TRACE_msg_task_execute_end(task);
61 #endif
62     return MSG_OK;
63   }
64   simdata->refcount++;
65   simdata->compute =
66       SIMIX_req_host_execute(task->name, SIMIX_host_self(),
67                            simdata->computation_amount);
68   SIMIX_req_host_execution_set_priority(simdata->compute, simdata->priority);
69
70   self->simdata->waiting_action = simdata->compute;
71   SIMIX_req_host_execution_wait(simdata->compute);
72   self->simdata->waiting_action = NULL;
73
74   simdata->refcount--;
75
76   DEBUG2("Execution task '%s' finished in state %d", task->name, SIMIX_req_host_execution_get_state(task->simdata->compute));
77   if (SIMIX_req_host_execution_get_state(task->simdata->compute) == SIMIX_DONE) {
78     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
79     SIMIX_req_host_execution_destroy(task->simdata->compute);
80     simdata->computation_amount = 0.0;
81     simdata->comm = NULL;
82     simdata->compute = NULL;
83 #ifdef HAVE_TRACING
84     TRACE_msg_task_execute_end(task);
85 #endif
86     MSG_RETURN(MSG_OK);
87   } else if (SIMIX_req_host_get_state(SIMIX_host_self()) == 0) {
88     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
89     SIMIX_req_host_execution_destroy(task->simdata->compute);
90     simdata->comm = NULL;
91     simdata->compute = NULL;
92 #ifdef HAVE_TRACING
93     TRACE_msg_task_execute_end(task);
94 #endif
95     MSG_RETURN(MSG_HOST_FAILURE);
96   } else {
97     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
98     SIMIX_req_host_execution_destroy(task->simdata->compute);
99     simdata->comm = NULL;
100     simdata->compute = NULL;
101 #ifdef HAVE_TRACING
102     TRACE_msg_task_execute_end(task);
103 #endif
104     MSG_RETURN(MSG_TASK_CANCELLED);
105   }
106 }
107
108 /** \ingroup m_task_management
109  * \brief Creates a new #m_task_t (a parallel one....).
110  *
111  * A constructor for #m_task_t taking six arguments and returning the
112  corresponding object.
113  * \param name a name for the object. It is for user-level information
114  and can be NULL.
115  * \param host_nb the number of hosts implied in the parallel task.
116  * \param host_list an array of \p host_nb m_host_t.
117  * \param computation_amount an array of \p host_nb
118  doubles. computation_amount[i] is the total number of operations
119  that have to be performed on host_list[i].
120  * \param communication_amount an array of \p host_nb* \p host_nb doubles.
121  * \param data a pointer to any data may want to attach to the new
122  object.  It is for user-level information and can be NULL. It can
123  be retrieved with the function \ref MSG_task_get_data.
124  * \see m_task_t
125  * \return The new corresponding object.
126  */
127 m_task_t
128 MSG_parallel_task_create(const char *name, int host_nb,
129                          const m_host_t * host_list,
130                          double *computation_amount,
131                          double *communication_amount, void *data)
132 {
133   int i;
134   simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
135   m_task_t task = xbt_new0(s_m_task_t, 1);
136   task->simdata = simdata;
137
138   /* Task structure */
139   task->name = xbt_strdup(name);
140   task->data = data;
141
142   /* Simulator Data */
143   simdata->computation_amount = 0;
144   simdata->message_size = 0;
145   simdata->compute = NULL;
146   simdata->comm = NULL;
147   simdata->rate = -1.0;
148   simdata->refcount = 1;
149   simdata->sender = NULL;
150   simdata->receiver = NULL;
151   simdata->source = NULL;
152
153   simdata->host_nb = host_nb;
154   simdata->host_list = xbt_new0(smx_host_t, host_nb);
155   simdata->comp_amount = computation_amount;
156   simdata->comm_amount = communication_amount;
157
158   for (i = 0; i < host_nb; i++)
159     simdata->host_list[i] = host_list[i]->simdata->smx_host;
160
161   return task;
162 }
163
164 MSG_error_t MSG_parallel_task_execute(m_task_t task)
165 {
166   simdata_task_t simdata = NULL;
167   m_process_t self = MSG_process_self();
168   CHECK_HOST();
169
170   simdata = task->simdata;
171
172   xbt_assert0((!simdata->compute)
173               && (task->simdata->refcount == 1),
174               "This task is executed somewhere else. Go fix your code!");
175
176   xbt_assert0(simdata->host_nb,
177               "This is not a parallel task. Go to hell.");
178
179   DEBUG1("Parallel computing on %s", MSG_process_self()->simdata->m_host->name);
180
181   simdata->refcount++;
182
183   simdata->compute =
184       SIMIX_req_host_parallel_execute(task->name, simdata->host_nb,
185                                   simdata->host_list,
186                                   simdata->comp_amount,
187                                   simdata->comm_amount, 1.0, -1.0);
188   DEBUG1("Parallel execution action created: %p", simdata->compute);
189
190   self->simdata->waiting_action = simdata->compute;
191   SIMIX_req_host_execution_wait(simdata->compute);
192   self->simdata->waiting_action = NULL;
193
194   DEBUG2("Finished waiting for execution of action %p, state = %d", simdata->compute, SIMIX_req_host_execution_get_state(task->simdata->compute));
195
196   simdata->refcount--;
197
198   if (SIMIX_req_host_execution_get_state(task->simdata->compute) == SIMIX_DONE) {
199     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
200     SIMIX_req_host_execution_destroy(task->simdata->compute);
201     simdata->computation_amount = 0.0;
202     simdata->comm = NULL;
203     simdata->compute = NULL;
204     MSG_RETURN(MSG_OK);
205   } else if (SIMIX_req_host_get_state(SIMIX_host_self()) == 0) {
206     /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
207     SIMIX_req_host_execution_destroy(task->simdata->compute);
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     SIMIX_req_host_execution_destroy(task->simdata->compute);
214     simdata->comm = NULL;
215     simdata->compute = NULL;
216     MSG_RETURN(MSG_TASK_CANCELLED);
217   }
218 }
219
220
221 /** \ingroup msg_gos_functions
222  * \brief Sleep for the specified number of seconds
223  *
224  * Makes the current process sleep until \a time seconds have elapsed.
225  *
226  * \param nb_sec a number of second
227  */
228 MSG_error_t MSG_process_sleep(double nb_sec)
229 {
230   e_smx_state_t state;
231   /*m_process_t proc = MSG_process_self();*/
232
233 #ifdef HAVE_TRACING
234   TRACE_msg_process_sleep_in(MSG_process_self());
235 #endif
236
237   /* create action to sleep */
238   state = SIMIX_req_process_sleep(nb_sec);
239
240   /*proc->simdata->waiting_action = act_sleep;
241
242   FIXME: check if not setting the waiting_action breaks something on msg
243   
244   proc->simdata->waiting_action = NULL;*/
245   
246   if (state == SIMIX_DONE) {
247     if (SIMIX_req_host_get_state(SIMIX_host_self()) == SURF_RESOURCE_OFF) {
248 #ifdef HAVE_TRACING
249       TRACE_msg_process_sleep_out(MSG_process_self());
250 #endif
251       MSG_RETURN(MSG_HOST_FAILURE);
252     }
253   } else {
254 #ifdef HAVE_TRACING
255     TRACE_msg_process_sleep_out(MSG_process_self());
256 #endif
257     MSG_RETURN(MSG_HOST_FAILURE);
258   }
259 #ifdef HAVE_TRACING
260   TRACE_msg_process_sleep_out(MSG_process_self());
261 #endif
262   MSG_RETURN(MSG_OK);
263 }
264
265 /** \ingroup msg_gos_functions
266  * \brief Listen on \a channel and waits for receiving a task from \a host.
267  *
268  * It takes three parameters.
269  * \param task a memory location for storing a #m_task_t. It will
270  hold a task when this function will return. Thus \a task should not
271  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
272  those two condition does not hold, there will be a warning message.
273  * \param channel the channel on which the agent should be
274  listening. This value has to be >=0 and < than the maximal
275  number of channels fixed with MSG_set_channel_number().
276  * \param host the host that is to be watched.
277  * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
278  if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
279  */
280 MSG_error_t
281 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
282 {
283   return MSG_task_get_ext(task, channel, -1, host);
284 }
285
286 /** \ingroup msg_gos_functions
287  * \brief Listen on a channel and wait for receiving a task.
288  *
289  * It takes two parameters.
290  * \param task a memory location for storing a #m_task_t. It will
291  hold a task when this function will return. Thus \a task should not
292  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
293  those two condition does not hold, there will be a warning message.
294  * \param channel the channel on which the agent should be
295  listening. This value has to be >=0 and < than the maximal
296  number of channels fixed with MSG_set_channel_number().
297  * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
298  * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
299  */
300 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
301 {
302   return MSG_task_get_with_timeout(task, channel, -1);
303 }
304
305 /** \ingroup msg_gos_functions
306  * \brief Listen on a channel and wait for receiving a task with a timeout.
307  *
308  * It takes three parameters.
309  * \param task a memory location for storing a #m_task_t. It will
310  hold a task when this function will return. Thus \a task should not
311  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
312  those two condition does not hold, there will be a warning message.
313  * \param channel the channel on which the agent should be
314  listening. This value has to be >=0 and < than the maximal
315  number of channels fixed with MSG_set_channel_number().
316  * \param max_duration the maximum time to wait for a task before giving
317  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
318  will not be modified and will still be
319  equal to \c NULL when returning.
320  * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
321  if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
322  */
323 MSG_error_t
324 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
325                           double max_duration)
326 {
327   return MSG_task_get_ext(task, channel, max_duration, NULL);
328 }
329
330 /** \defgroup msg_gos_functions MSG Operating System Functions
331  *  \brief This section describes the functions that can be used
332  *  by an agent for handling some task.
333  */
334
335 MSG_error_t
336 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
337                  m_host_t host)
338 {
339   xbt_assert1((channel >= 0)
340               && (channel < msg_global->max_channel), "Invalid channel %d",
341               channel);
342
343   return
344       MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
345                                (MSG_host_self(), channel), task, host,
346                                timeout);
347 }
348
349 MSG_error_t
350 MSG_task_receive_from_host(m_task_t * task, const char *alias,
351                            m_host_t host)
352 {
353   return MSG_task_receive_ext(task, alias, -1, host);
354 }
355
356 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
357 {
358   return MSG_task_receive_with_timeout(task, alias, -1);
359 }
360
361 MSG_error_t
362 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
363                               double timeout)
364 {
365   return MSG_task_receive_ext(task, alias, timeout, NULL);
366 }
367
368 MSG_error_t
369 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
370                      m_host_t host)
371 {
372   DEBUG1
373       ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
374        alias);
375   return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
376                                   host, timeout);
377 }
378
379 /** \ingroup msg_gos_functions
380  * \brief Send a task on a channel.
381  *
382  * This function takes two parameter.
383  * \param task a #m_task_t to send on another location.
384  * \param alias the channel on which the agent should put this
385  task. This value has to be >=0 and < than the maximal number of
386  channels fixed with MSG_set_channel_number().
387  * \return the msg_comm_t communication.
388  */
389 msg_comm_t MSG_task_isend(m_task_t task, const char *alias)
390 {
391   simdata_task_t t_simdata = NULL;
392   m_process_t process = MSG_process_self();
393   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
394
395   CHECK_HOST();
396
397   /* FIXME: these functions are not tracable */
398
399   /* Prepare the task to send */
400   t_simdata = task->simdata;
401   t_simdata->sender = process;
402   t_simdata->source = MSG_host_self();
403
404   xbt_assert0(t_simdata->refcount == 1,
405               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
406
407   t_simdata->refcount++;
408   msg_global->sent_msg++;
409   process->simdata->waiting_task = task;
410
411   /* Send it by calling SIMIX network layer */
412
413   return SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
414                              t_simdata->rate, task, sizeof(void *),
415                              &t_simdata->comm);
416 }
417
418 /** \ingroup msg_gos_functions
419  * \brief Listen on a channel for receiving a task from an asynchronous communication.
420  *
421  * It takes two parameters.
422  * \param task a memory location for storing a #m_task_t.
423  * \param alias the channel on which the agent should be
424  listening. This value has to be >=0 and < than the maximal
425  number of channels fixed with MSG_set_channel_number().
426  * \return the msg_comm_t communication.
427  */
428 msg_comm_t MSG_task_irecv(m_task_t * task, const char *alias)
429 {
430   smx_rdv_t rdv = MSG_mailbox_get_by_alias(alias);
431
432   CHECK_HOST();
433
434   /* FIXME: these functions are not tracable */
435
436   /* Sanity check */
437   xbt_assert0(task, "Null pointer for the task storage");
438
439   if (*task)
440     CRITICAL0
441         ("MSG_task_get() was asked to write in a non empty task struct.");
442
443   /* Try to receive it by calling SIMIX network layer */
444   return SIMIX_req_comm_irecv(rdv, task, NULL);
445 }
446
447 /** \ingroup msg_gos_functions
448  * \brief Test the status of a communication.
449  *
450  * It takes one parameter.
451  * \param comm the communication to test.
452  * \return the status of the communication:
453  *              TRUE : the communication is completed
454  *              FALSE: the communication is incompleted
455  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
456  */
457 int MSG_comm_test(msg_comm_t comm)
458 {
459   return SIMIX_req_comm_test(comm);
460 }
461
462 /** \ingroup msg_gos_functions
463  * \brief After received TRUE to MSG_comm_test(), the communication must be destroyed.
464  *
465  * It takes one parameter.
466  * \param comm the communication to destroy.
467  */
468 void MSG_comm_destroy(msg_comm_t comm)
469 {
470   if (SIMIX_req_comm_get_src_proc(comm) != SIMIX_process_self()) {
471     m_task_t task;
472     task = (m_task_t) SIMIX_req_comm_get_src_buff(comm);
473     task->simdata->refcount--;
474   }
475   SIMIX_req_comm_destroy(comm);
476 }
477
478 /** \ingroup msg_gos_functions
479  * \brief Wait for the completion of a communication.
480  *
481  * It takes two parameters.
482  * \param comm the communication to wait.
483  * \param timeout Wait until the communication terminates or the timeout occurs
484  * \return MSG_error_t
485  */
486 MSG_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
487 {
488   xbt_ex_t e;
489   MSG_error_t res = MSG_OK;
490   TRY {
491     SIMIX_req_comm_wait(comm, timeout);
492
493     if (SIMIX_req_comm_get_src_proc(comm) != SIMIX_process_self()) {
494       m_task_t task;
495       task = (m_task_t) SIMIX_req_comm_get_src_buff(comm);
496       task->simdata->refcount--;
497     }
498     SIMIX_req_comm_destroy(comm);
499
500     /* FIXME: these functions are not tracable */
501   }
502   CATCH(e) {
503     switch (e.category) {
504     case host_error:
505       res = MSG_HOST_FAILURE;
506       break;
507     case network_error:
508       res = MSG_TRANSFER_FAILURE;
509       break;
510     case timeout_error:
511       res = MSG_TIMEOUT;
512       break;
513     default:
514       xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
515     }
516     xbt_ex_free(e);
517   }
518   return res;
519 }
520
521 /** \ingroup msg_gos_functions
522 * \brief This function is called by a sender and permit to wait for each communication
523 *
524 * It takes three parameters.
525 * \param comm a vector of communication
526 * \param nb_elem is the size of the comm vector
527 * \param timeout for each call of  MSG_comm_wait
528 */
529 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
530 {
531   int i = 0;
532   for (i = 0; i < nb_elem; i++) {
533     MSG_comm_wait(comm[i], timeout);
534   }
535 }
536
537 /** \ingroup msg_gos_functions
538 * \brief This function wait for the first completed communication
539 *
540 * It takes on parameter.
541 * \param comms a vector of communication
542 * \return the position of the completed communication from the xbt_dynar_t.
543 */
544 int MSG_comm_waitany(xbt_dynar_t comms)
545 {
546   return SIMIX_req_comm_waitany(comms);
547 }
548
549 /** \ingroup msg_gos_functions
550 * \brief This function wait for the first completed communication
551 *
552 * It takes on parameter.
553 * \param comms a vector of communication
554 * \return the position of the completed communication, if any, or -1 if none was completed
555 */
556 int MSG_comm_testany(xbt_dynar_t comms)
557 {
558   return SIMIX_req_comm_testany(comms);
559 }
560
561 m_task_t MSG_comm_get_task(msg_comm_t comm)
562 {
563   xbt_assert0(comm, "Invalid parameters");
564   return (m_task_t) SIMIX_req_comm_get_src_buff(comm);
565 }
566
567 /** \ingroup msg_gos_functions
568  * \brief Put a task on a channel of an host and waits for the end of the
569  * transmission.
570  *
571  * This function is used for describing the behavior of an agent. It
572  * takes three parameter.
573  * \param task a #m_task_t to send on another location. This task
574  will not be usable anymore when the function will return. There is
575  no automatic task duplication and you have to save your parameters
576  before calling this function. Tasks are unique and once it has been
577  sent to another location, you should not access it anymore. You do
578  not need to call MSG_task_destroy() but to avoid using, as an
579  effect of inattention, this task anymore, you definitely should
580  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
581  can be transfered iff it has been correctly created with
582  MSG_task_create().
583  * \param dest the destination of the message
584  * \param channel the channel on which the agent should put this
585  task. This value has to be >=0 and < than the maximal number of
586  channels fixed with MSG_set_channel_number().
587  * \return #MSG_FATAL if \a task is not properly initialized and
588  * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
589  * this function was called was shut down. Returns
590  * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
591  * (network failure, dest failure)
592  */
593 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
594 {
595   return MSG_task_put_with_timeout(task, dest, channel, -1.0);
596 }
597
598 /** \ingroup msg_gos_functions
599  * \brief Does exactly the same as MSG_task_put but with a bounded transmition
600  * rate.
601  *
602  * \sa MSG_task_put
603  */
604 MSG_error_t
605 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
606                      double maxrate)
607 {
608   task->simdata->rate = maxrate;
609   return MSG_task_put(task, dest, channel);
610 }
611
612 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
613  * host (with a timeout on the waiting of the destination host) and
614  * waits for the end of the transmission.
615  *
616  * This function is used for describing the behavior of an agent. It
617  * takes four parameter.
618  * \param task a #m_task_t to send on another location. This task
619  will not be usable anymore when the function will return. There is
620  no automatic task duplication and you have to save your parameters
621  before calling this function. Tasks are unique and once it has been
622  sent to another location, you should not access it anymore. You do
623  not need to call MSG_task_destroy() but to avoid using, as an
624  effect of inattention, this task anymore, you definitely should
625  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
626  can be transfered iff it has been correctly created with
627  MSG_task_create().
628  * \param dest the destination of the message
629  * \param channel the channel on which the agent should put this
630  task. This value has to be >=0 and < than the maximal number of
631  channels fixed with MSG_set_channel_number().
632  * \param timeout the maximum time to wait for a task before giving
633  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
634  will not be modified
635  * \return #MSG_FATAL if \a task is not properly initialized and
636 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
637 this function was called was shut down. Returns
638 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
639 (network failure, dest failure, timeout...)
640  */
641 MSG_error_t
642 MSG_task_put_with_timeout(m_task_t task, m_host_t dest,
643                           m_channel_t channel, double timeout)
644 {
645   xbt_assert1((channel >= 0)
646               && (channel < msg_global->max_channel), "Invalid channel %d",
647               channel);
648
649   DEBUG1("MSG_task_put_with_timout: Trying to send a task to '%s'", dest->name);
650   return
651       MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
652                                    (dest, channel), task, timeout);
653 }
654
655 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
656 {
657   DEBUG1("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
658   return MSG_task_send_with_timeout(task, alias, -1);
659 }
660
661
662 MSG_error_t
663 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
664 {
665   task->simdata->rate = maxrate;
666   return MSG_task_send(task, alias);
667 }
668
669
670 MSG_error_t
671 MSG_task_send_with_timeout(m_task_t task, const char *alias,
672                            double timeout)
673 {
674   return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
675                                       task, timeout);
676 }
677
678 int MSG_task_listen(const char *alias)
679 {
680   CHECK_HOST();
681
682   return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
683 }
684
685 /** \ingroup msg_gos_functions
686  * \brief Test whether there is a pending communication on a channel.
687  *
688  * It takes one parameter.
689  * \param channel the channel on which the agent should be
690  listening. This value has to be >=0 and < than the maximal
691  number of channels fixed with MSG_set_channel_number().
692  * \return 1 if there is a pending communication and 0 otherwise
693  */
694 int MSG_task_Iprobe(m_channel_t channel)
695 {
696   xbt_assert1((channel >= 0)
697               && (channel < msg_global->max_channel), "Invalid channel %d",
698               channel);
699
700   CHECK_HOST();
701
702   return
703       !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
704                             (MSG_host_self(), channel));
705 }
706
707 /** \ingroup msg_gos_functions
708
709  * \brief Return the number of tasks waiting to be received on a \a
710  channel and sent by \a host.
711  *
712  * It takes two parameters.
713  * \param channel the channel on which the agent should be
714  listening. This value has to be >=0 and < than the maximal
715  number of channels fixed with MSG_set_channel_number().
716  * \param host the host that is to be watched.
717  * \return the number of tasks waiting to be received on \a channel
718  and sent by \a host.
719  */
720 int MSG_task_probe_from_host(int channel, m_host_t host)
721 {
722   xbt_assert1((channel >= 0)
723               && (channel < msg_global->max_channel), "Invalid channel %d",
724               channel);
725
726   CHECK_HOST();
727
728   return
729       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
730                                                (MSG_host_self(), channel),
731                                                host);
732
733 }
734
735 int MSG_task_listen_from_host(const char *alias, m_host_t host)
736 {
737   CHECK_HOST();
738
739   return
740       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
741                                                (alias), host);
742 }
743
744 /** \ingroup msg_gos_functions
745  * \brief Test whether there is a pending communication on a channel, and who sent it.
746  *
747  * It takes one parameter.
748  * \param channel the channel on which the agent should be
749  listening. This value has to be >=0 and < than the maximal
750  number of channels fixed with MSG_set_channel_number().
751  * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
752  */
753 int MSG_task_probe_from(m_channel_t channel)
754 {
755   m_task_t task;
756
757   CHECK_HOST();
758
759   xbt_assert1((channel >= 0)
760               && (channel < msg_global->max_channel), "Invalid channel %d",
761               channel);
762
763   if (NULL ==
764       (task =
765        MSG_mailbox_get_head(MSG_mailbox_get_by_channel
766                             (MSG_host_self(), channel))))
767     return -1;
768
769   return MSG_process_get_PID(task->simdata->sender);
770 }
771
772 int MSG_task_listen_from(const char *alias)
773 {
774   m_task_t task;
775
776   CHECK_HOST();
777
778   if (NULL ==
779       (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
780     return -1;
781
782   return MSG_process_get_PID(task->simdata->sender);
783 }