Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Biggest commit ever (SIMIX2): the user processes can now run in parallel
[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 m_task_t MSG_comm_get_task(msg_comm_t comm)
550 {
551   xbt_assert0(comm, "Invalid parameters");
552   return (m_task_t) SIMIX_req_comm_get_src_buff(comm);
553 }
554
555 /** \ingroup msg_gos_functions
556  * \brief Put a task on a channel of an host and waits for the end of the
557  * transmission.
558  *
559  * This function is used for describing the behavior of an agent. It
560  * takes three parameter.
561  * \param task a #m_task_t to send on another location. This task
562  will not be usable anymore when the function will return. There is
563  no automatic task duplication and you have to save your parameters
564  before calling this function. Tasks are unique and once it has been
565  sent to another location, you should not access it anymore. You do
566  not need to call MSG_task_destroy() but to avoid using, as an
567  effect of inattention, this task anymore, you definitely should
568  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
569  can be transfered iff it has been correctly created with
570  MSG_task_create().
571  * \param dest the destination of the message
572  * \param channel the channel on which the agent should put this
573  task. This value has to be >=0 and < than the maximal number of
574  channels fixed with MSG_set_channel_number().
575  * \return #MSG_FATAL if \a task is not properly initialized and
576  * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
577  * this function was called was shut down. Returns
578  * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
579  * (network failure, dest failure)
580  */
581 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
582 {
583   return MSG_task_put_with_timeout(task, dest, channel, -1.0);
584 }
585
586 /** \ingroup msg_gos_functions
587  * \brief Does exactly the same as MSG_task_put but with a bounded transmition
588  * rate.
589  *
590  * \sa MSG_task_put
591  */
592 MSG_error_t
593 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
594                      double maxrate)
595 {
596   task->simdata->rate = maxrate;
597   return MSG_task_put(task, dest, channel);
598 }
599
600 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
601  * host (with a timeout on the waiting of the destination host) and
602  * waits for the end of the transmission.
603  *
604  * This function is used for describing the behavior of an agent. It
605  * takes four parameter.
606  * \param task a #m_task_t to send on another location. This task
607  will not be usable anymore when the function will return. There is
608  no automatic task duplication and you have to save your parameters
609  before calling this function. Tasks are unique and once it has been
610  sent to another location, you should not access it anymore. You do
611  not need to call MSG_task_destroy() but to avoid using, as an
612  effect of inattention, this task anymore, you definitely should
613  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
614  can be transfered iff it has been correctly created with
615  MSG_task_create().
616  * \param dest the destination of the message
617  * \param channel the channel on which the agent should put this
618  task. This value has to be >=0 and < than the maximal number of
619  channels fixed with MSG_set_channel_number().
620  * \param timeout the maximum time to wait for a task before giving
621  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
622  will not be modified
623  * \return #MSG_FATAL if \a task is not properly initialized and
624 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
625 this function was called was shut down. Returns
626 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
627 (network failure, dest failure, timeout...)
628  */
629 MSG_error_t
630 MSG_task_put_with_timeout(m_task_t task, m_host_t dest,
631                           m_channel_t channel, double timeout)
632 {
633   xbt_assert1((channel >= 0)
634               && (channel < msg_global->max_channel), "Invalid channel %d",
635               channel);
636
637   DEBUG1("MSG_task_put_with_timout: Trying to send a task to '%s'", dest->name);
638   return
639       MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
640                                    (dest, channel), task, timeout);
641 }
642
643 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
644 {
645   DEBUG1("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
646   return MSG_task_send_with_timeout(task, alias, -1);
647 }
648
649
650 MSG_error_t
651 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
652 {
653   task->simdata->rate = maxrate;
654   return MSG_task_send(task, alias);
655 }
656
657
658 MSG_error_t
659 MSG_task_send_with_timeout(m_task_t task, const char *alias,
660                            double timeout)
661 {
662   return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
663                                       task, timeout);
664 }
665
666 int MSG_task_listen(const char *alias)
667 {
668   CHECK_HOST();
669
670   return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
671 }
672
673 /** \ingroup msg_gos_functions
674  * \brief Test whether there is a pending communication on a channel.
675  *
676  * It takes one parameter.
677  * \param channel the channel on which the agent should be
678  listening. This value has to be >=0 and < than the maximal
679  number of channels fixed with MSG_set_channel_number().
680  * \return 1 if there is a pending communication and 0 otherwise
681  */
682 int MSG_task_Iprobe(m_channel_t channel)
683 {
684   xbt_assert1((channel >= 0)
685               && (channel < msg_global->max_channel), "Invalid channel %d",
686               channel);
687
688   CHECK_HOST();
689
690   return
691       !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
692                             (MSG_host_self(), channel));
693 }
694
695 /** \ingroup msg_gos_functions
696
697  * \brief Return the number of tasks waiting to be received on a \a
698  channel and sent by \a host.
699  *
700  * It takes two parameters.
701  * \param channel the channel on which the agent should be
702  listening. This value has to be >=0 and < than the maximal
703  number of channels fixed with MSG_set_channel_number().
704  * \param host the host that is to be watched.
705  * \return the number of tasks waiting to be received on \a channel
706  and sent by \a host.
707  */
708 int MSG_task_probe_from_host(int channel, m_host_t host)
709 {
710   xbt_assert1((channel >= 0)
711               && (channel < msg_global->max_channel), "Invalid channel %d",
712               channel);
713
714   CHECK_HOST();
715
716   return
717       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
718                                                (MSG_host_self(), channel),
719                                                host);
720
721 }
722
723 int MSG_task_listen_from_host(const char *alias, m_host_t host)
724 {
725   CHECK_HOST();
726
727   return
728       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
729                                                (alias), host);
730 }
731
732 /** \ingroup msg_gos_functions
733  * \brief Test whether there is a pending communication on a channel, and who sent it.
734  *
735  * It takes one parameter.
736  * \param channel the channel on which the agent should be
737  listening. This value has to be >=0 and < than the maximal
738  number of channels fixed with MSG_set_channel_number().
739  * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
740  */
741 int MSG_task_probe_from(m_channel_t channel)
742 {
743   m_task_t task;
744
745   CHECK_HOST();
746
747   xbt_assert1((channel >= 0)
748               && (channel < msg_global->max_channel), "Invalid channel %d",
749               channel);
750
751   if (NULL ==
752       (task =
753        MSG_mailbox_get_head(MSG_mailbox_get_by_channel
754                             (MSG_host_self(), channel))))
755     return -1;
756
757   return MSG_process_get_PID(task->simdata->sender);
758 }
759
760 int MSG_task_listen_from(const char *alias)
761 {
762   m_task_t task;
763
764   CHECK_HOST();
765
766   if (NULL ==
767       (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
768     return -1;
769
770   return MSG_process_get_PID(task->simdata->sender);
771 }