Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2ceacc2cf741dac2d3b653308a4ae407eb4a119f
[simgrid.git] / src / msg / msg_gos.c
1 /* Copyright (c) 2004-2012. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "msg_private.h"
7 #include "msg_mailbox.h"
8 #include "mc/mc.h"
9 #include "xbt/log.h"
10 #include "xbt/sysdep.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
13                                 "Logging specific to MSG (gos)");
14
15 /** \ingroup msg_task_usage
16  * \brief Executes a task and waits for its termination.
17  *
18  * This function is used for describing the behavior of a process. It
19  * takes only one parameter.
20  * \param task a #msg_task_t to execute on the location on which the process is running.
21  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
22  * or #MSG_HOST_FAILURE otherwise
23  */
24 msg_error_t MSG_task_execute(msg_task_t task)
25 {
26   return MSG_parallel_task_execute(task);
27 }
28
29 /** \ingroup msg_task_usage
30  * \brief Executes a parallel task and waits for its termination.
31  *
32  * \param task a #msg_task_t to execute on the location on which the process is running.
33  *
34  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
35  * or #MSG_HOST_FAILURE otherwise
36  */
37 msg_error_t MSG_parallel_task_execute(msg_task_t task)
38 {
39   xbt_ex_t e;
40   simdata_task_t simdata = task->simdata;
41   msg_process_t self = SIMIX_process_self();
42   simdata_process_t p_simdata = SIMIX_process_self_get_data(self);
43   e_smx_state_t comp_state;
44   msg_error_t status = MSG_OK;
45
46 #ifdef HAVE_TRACING
47   TRACE_msg_task_execute_start(task);
48 #endif
49
50   xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
51              "This task is executed somewhere else. Go fix your code! %d",
52              task->simdata->isused);
53
54   XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
55
56   if (simdata->computation_amount == 0 && !simdata->host_nb) {
57 #ifdef HAVE_TRACING
58     TRACE_msg_task_execute_end(task);
59 #endif
60     return MSG_OK;
61   }
62
63
64   TRY {
65
66     simdata->isused=1;
67
68     if (simdata->host_nb > 0) {
69       simdata->compute = simcall_host_parallel_execute(task->name,
70                                                        simdata->host_nb,
71                                                        simdata->host_list,
72                                                        simdata->comp_amount,
73                                                        simdata->comm_amount,
74                                                        1.0, -1.0);
75       XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
76     } else {
77       simdata->compute = simcall_host_execute(task->name,
78                                               p_simdata->m_host->smx_host,
79                                               simdata->computation_amount,
80                                               simdata->priority);
81
82     }
83 #ifdef HAVE_TRACING
84     simcall_set_category(simdata->compute, task->category);
85 #endif
86     p_simdata->waiting_action = simdata->compute;
87     comp_state = simcall_host_execution_wait(simdata->compute);
88
89     p_simdata->waiting_action = NULL;
90
91     simdata->isused=0;
92
93     XBT_DEBUG("Execution task '%s' finished in state %d",
94               task->name, (int)comp_state);
95   }
96   CATCH(e) {
97     switch (e.category) {
98     case cancel_error:
99       status = MSG_TASK_CANCELED;
100       break;
101     default:
102       RETHROW;
103     }
104     xbt_ex_free(e);
105   }
106   /* action ended, set comm and compute = NULL, the actions is already destroyed
107    * in the main function */
108   simdata->computation_amount = 0.0;
109   simdata->comm = NULL;
110   simdata->compute = NULL;
111 #ifdef HAVE_TRACING
112   TRACE_msg_task_execute_end(task);
113 #endif
114
115   MSG_RETURN(status);
116 }
117
118
119 /** \ingroup msg_task_usage
120  * \brief Sleep for the specified number of seconds
121  *
122  * Makes the current process sleep until \a time seconds have elapsed.
123  *
124  * \param nb_sec a number of second
125  */
126 msg_error_t MSG_process_sleep(double nb_sec)
127 {
128   msg_error_t status = MSG_OK;
129   /*msg_process_t proc = MSG_process_self();*/
130
131 #ifdef HAVE_TRACING
132   TRACE_msg_process_sleep_in(MSG_process_self());
133 #endif
134
135   /* create action to sleep */
136
137   /*proc->simdata->waiting_action = act_sleep;
138
139   FIXME: check if not setting the waiting_action breaks something on msg
140   
141   proc->simdata->waiting_action = NULL;*/
142
143   simcall_process_sleep(nb_sec);
144
145   #ifdef HAVE_TRACING
146     TRACE_msg_process_sleep_out(MSG_process_self());
147   #endif
148   MSG_RETURN(status);
149 }
150
151 /** \ingroup msg_task_usage
152  * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
153  *
154  * Sorry, this function is not supported anymore. That wouldn't be
155  * impossible to reimplement it, but we are lacking the time to do so ourselves.
156  * If you need this functionality, you can either:
157  *
158  *  - implement the buffering mechanism on the user-level by queuing all messages
159  *    received in the mailbox that do not match your expectation
160  *  - change your application logic to leverage the mailboxes features. For example,
161  *    if you have A receiving messages from B and C, you could have A waiting on
162  *    mailbox "A" most of the time, but on "A#B" when it's waiting for specific
163  *    messages from B and "A#C" when waiting for messages from C. You could even get A
164  *    sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
165  *    an example of use of this function in the @ref MSG_examples section.
166  *  - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
167  *    very difficult actually. Check the function #MSG_mailbox_get_task_ext. During its call to
168  *    simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
169  *    messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
170  *    and that your filtering function will receive as first parameter, and then, the filter could
171  *    simply compare the host names, for example. After sufficient testing, provide an example that
172  *    we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
173  *
174  * \param task a memory location for storing a #msg_task_t.
175  * \param alias name of the mailbox to receive the task from
176  * \param host a #msg_host_t host from where the task was sent
177  *
178  * \return Returns
179  * #MSG_OK if the task was successfully received,
180  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
181  */
182 msg_error_t
183 MSG_task_receive_from_host(msg_task_t * task, const char *alias,
184                            msg_host_t host)
185 {
186   return MSG_task_receive_ext(task, alias, -1, host);
187 }
188
189 /** \ingroup msg_task_usage
190  * \brief Receives a task from a mailbox.
191  *
192  * This is a blocking function, the execution flow will be blocked
193  * until the task is received. See #MSG_task_irecv
194  * for receiving tasks asynchronously.
195  *
196  * \param task a memory location for storing a #msg_task_t.
197  * \param alias name of the mailbox to receive the task from
198  *
199  * \return Returns
200  * #MSG_OK if the task was successfully received,
201  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
202  */
203 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
204 {
205   return MSG_task_receive_with_timeout(task, alias, -1);
206 }
207
208 /** \ingroup msg_task_usage
209  * \brief Receives a task from a mailbox with a given timeout.
210  *
211  * This is a blocking function with a timeout, the execution flow will be blocked
212  * until the task is received or the timeout is achieved. See #MSG_task_irecv
213  * for receiving tasks asynchronously.  You can provide a -1 timeout
214  * to obtain an infinite timeout.
215  *
216  * \param task a memory location for storing a #msg_task_t.
217  * \param alias name of the mailbox to receive the task from
218  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
219  *
220  * \return Returns
221  * #MSG_OK if the task was successfully received,
222  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
223  */
224 msg_error_t
225 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
226                               double timeout)
227 {
228   return MSG_task_receive_ext(task, alias, timeout, NULL);
229 }
230
231 /** \ingroup msg_task_usage
232  * \brief Receives a task from a mailbox from a specific host with a given timeout.
233  *
234  * This is a blocking function with a timeout, the execution flow will be blocked
235  * until the task is received or the timeout is achieved. See #MSG_task_irecv
236  * for receiving tasks asynchronously. You can provide a -1 timeout
237  * to obtain an infinite timeout.
238  *
239  * \param task a memory location for storing a #msg_task_t.
240  * \param alias name of the mailbox to receive the task from
241  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
242  * \param host a #msg_host_t host from where the task was sent
243  *
244  * \return Returns
245  * #MSG_OK if the task was successfully received,
246 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
247  */
248 msg_error_t
249 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
250                      msg_host_t host)
251 {
252   XBT_DEBUG
253       ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
254        alias);
255   return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
256                                   host, timeout);
257 }
258
259 /** \ingroup msg_task_usage
260  * \brief Sends a task on a mailbox.
261  *
262  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
263  * to end the communication.
264  *
265  * \param task a #msg_task_t to send on another location.
266  * \param alias name of the mailbox to sent the task to
267  * \return the msg_comm_t communication created
268  */
269 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
270 {
271   return MSG_task_isend_with_matching(task,alias,NULL,NULL);
272 }
273
274 /** \ingroup msg_task_usage
275  * \brief Sends a task on a mailbox, with support for matching requests
276  *
277  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
278  * to end the communication.
279  *
280  * \param task a #msg_task_t to send on another location.
281  * \param alias name of the mailbox to sent the task to
282  * \param match_fun boolean function which parameters are:
283  *        - match_data_provided_here
284  *        - match_data_provided_by_other_side_if_any
285  *        - the_smx_action_describing_the_other_side
286  * \param match_data user provided data passed to match_fun
287  * \return the msg_comm_t communication created
288  */
289 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
290     int (*match_fun)(void*,void*, smx_action_t),
291     void *match_data)
292 {
293   simdata_task_t t_simdata = NULL;
294   msg_process_t process = MSG_process_self();
295   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
296
297   /* FIXME: these functions are not traceable */
298
299   /* Prepare the task to send */
300   t_simdata = task->simdata;
301   t_simdata->sender = process;
302   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
303
304   xbt_assert(t_simdata->isused == 0,
305               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
306
307   t_simdata->isused = 1;
308   t_simdata->comm = NULL;
309   msg_global->sent_msg++;
310
311   /* Send it by calling SIMIX network layer */
312   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
313   comm->task_sent = task;
314   comm->task_received = NULL;
315   comm->status = MSG_OK;
316   comm->s_comm =
317     simcall_comm_isend(mailbox, t_simdata->message_size,
318                          t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
319   t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
320
321   return comm;
322 }
323
324 /** \ingroup msg_task_usage
325  * \brief Sends a task on a mailbox.
326  *
327  * This is a non blocking detached send function.
328  * Think of it as a best effort send. Keep in mind that the third parameter
329  * is only called if the communication fails. If the communication does work,
330  * it is responsibility of the receiver code to free anything related to
331  * the task, as usual. More details on this can be obtained on
332  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
333  * in the SimGrid-user mailing list archive.
334  *
335  * \param task a #msg_task_t to send on another location.
336  * \param alias name of the mailbox to sent the task to
337  * \param cleanup a function to destroy the task if the
338  * communication fails, e.g. MSG_task_destroy
339  * (if NULL, no function will be called)
340  */
341 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
342 {
343   simdata_task_t t_simdata = NULL;
344   msg_process_t process = MSG_process_self();
345   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
346
347   /* Prepare the task to send */
348   t_simdata = task->simdata;
349   t_simdata->sender = process;
350   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
351
352   xbt_assert(t_simdata->isused == 0,
353               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
354
355   t_simdata->isused = 1;
356   t_simdata->comm = NULL;
357   msg_global->sent_msg++;
358
359 #ifdef HAVE_TRACING
360   int call_end = TRACE_msg_task_put_start(task);
361 #endif
362
363   /* Send it by calling SIMIX network layer */
364   smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
365                        t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
366   t_simdata->comm = comm;
367 #ifdef HAVE_TRACING
368     if (TRACE_is_enabled()) {
369       simcall_set_category(comm, task->category);
370     }
371 #endif
372
373 #ifdef HAVE_TRACING
374   if (call_end)
375     TRACE_msg_task_put_end();
376 #endif
377 }
378
379 /** \ingroup msg_task_usage
380  * \brief Starts listening for receiving a task from an asynchronous communication.
381  *
382  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
383  * to end the communication.
384  * 
385  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
386  * \param name of the mailbox to receive the task on
387  * \return the msg_comm_t communication created
388  */
389 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
390 {
391   smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
392
393   /* FIXME: these functions are not traceable */
394
395   /* Sanity check */
396   xbt_assert(task, "Null pointer for the task storage");
397
398   if (*task)
399     XBT_CRITICAL
400         ("MSG_task_irecv() was asked to write in a non empty task struct.");
401
402   /* Try to receive it by calling SIMIX network layer */
403   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
404   comm->task_sent = NULL;
405   comm->task_received = task;
406   comm->status = MSG_OK;
407   comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
408
409   return comm;
410 }
411
412 /** \ingroup msg_task_usage
413  * \brief Checks whether a communication is done, and if yes, finalizes it.
414  * \param comm the communication to test
415  * \return TRUE if the communication is finished
416  * (but it may have failed, use MSG_comm_get_status() to know its status)
417  * or FALSE if the communication is not finished yet
418  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
419  */
420 int MSG_comm_test(msg_comm_t comm)
421 {
422   xbt_ex_t e;
423   int finished = 0;
424   TRY {
425     finished = simcall_comm_test(comm->s_comm);
426
427     if (finished && comm->task_received != NULL) {
428       /* I am the receiver */
429       (*comm->task_received)->simdata->isused = 0;
430     }
431   }
432   CATCH(e) {
433     switch (e.category) {
434       case network_error:
435         comm->status = MSG_TRANSFER_FAILURE;
436         finished = 1;
437         break;
438
439       case timeout_error:
440         comm->status = MSG_TIMEOUT;
441         finished = 1;
442         break;
443
444       default:
445         RETHROW;
446     }
447     xbt_ex_free(e);
448   }
449
450   return finished;
451 }
452
453 /** \ingroup msg_task_usage
454  * \brief This function checks if a communication is finished.
455  * \param comms a vector of communications
456  * \return the position of the finished communication if any
457  * (but it may have failed, use MSG_comm_get_status() to know its status),
458  * or -1 if none is finished
459  */
460 int MSG_comm_testany(xbt_dynar_t comms)
461 {
462   xbt_ex_t e;
463   int finished_index = -1;
464
465   /* create the equivalent dynar with SIMIX objects */
466   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
467   msg_comm_t comm;
468   unsigned int cursor;
469   xbt_dynar_foreach(comms, cursor, comm) {
470     xbt_dynar_push(s_comms, &comm->s_comm);
471   }
472
473   msg_error_t status = MSG_OK;
474   TRY {
475     finished_index = simcall_comm_testany(s_comms);
476   }
477   CATCH(e) {
478     switch (e.category) {
479       case network_error:
480         finished_index = e.value;
481         status = MSG_TRANSFER_FAILURE;
482         break;
483
484       case timeout_error:
485         finished_index = e.value;
486         status = MSG_TIMEOUT;
487         break;
488
489       default:
490         RETHROW;
491     }
492     xbt_ex_free(e);
493   }
494   xbt_dynar_free(&s_comms);
495
496   if (finished_index != -1) {
497     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
498     /* the communication is finished */
499     comm->status = status;
500
501     if (status == MSG_OK && comm->task_received != NULL) {
502       /* I am the receiver */
503       (*comm->task_received)->simdata->isused = 0;
504     }
505   }
506
507   return finished_index;
508 }
509
510 /** \ingroup msg_task_usage
511  * \brief Destroys a communication.
512  * \param comm the communication to destroy.
513  */
514 void MSG_comm_destroy(msg_comm_t comm)
515 {
516   xbt_free(comm);
517 }
518
519 /** \ingroup msg_task_usage
520  * \brief Wait for the completion of a communication.
521  *
522  * It takes two parameters.
523  * \param comm the communication to wait.
524  * \param timeout Wait until the communication terminates or the timeout 
525  * occurs. You can provide a -1 timeout to obtain an infinite timeout.
526  * \return msg_error_t
527  */
528 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
529 {
530   xbt_ex_t e;
531   TRY {
532     simcall_comm_wait(comm->s_comm, timeout);
533
534     if (comm->task_received != NULL) {
535       /* I am the receiver */
536       (*comm->task_received)->simdata->isused = 0;
537     }
538
539     /* FIXME: these functions are not traceable */
540   }
541   CATCH(e) {
542     switch (e.category) {
543     case network_error:
544       comm->status = MSG_TRANSFER_FAILURE;
545       break;
546     case timeout_error:
547       comm->status = MSG_TIMEOUT;
548       break;
549     default:
550       RETHROW;
551     }
552     xbt_ex_free(e);
553   }
554
555   return comm->status;
556 }
557
558 /** \ingroup msg_task_usage
559 * \brief This function is called by a sender and permit to wait for each communication
560 *
561 * \param comm a vector of communication
562 * \param nb_elem is the size of the comm vector
563 * \param timeout for each call of MSG_comm_wait
564 */
565 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
566 {
567   int i = 0;
568   for (i = 0; i < nb_elem; i++) {
569     MSG_comm_wait(comm[i], timeout);
570   }
571 }
572
573 /** \ingroup msg_task_usage
574  * \brief This function waits for the first communication finished in a list.
575  * \param comms a vector of communications
576  * \return the position of the first finished communication
577  * (but it may have failed, use MSG_comm_get_status() to know its status)
578  */
579 int MSG_comm_waitany(xbt_dynar_t comms)
580 {
581   xbt_ex_t e;
582   int finished_index = -1;
583
584   /* create the equivalent dynar with SIMIX objects */
585   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
586   msg_comm_t comm;
587   unsigned int cursor;
588   xbt_dynar_foreach(comms, cursor, comm) {
589     xbt_dynar_push(s_comms, &comm->s_comm);
590   }
591
592   msg_error_t status = MSG_OK;
593   TRY {
594     finished_index = simcall_comm_waitany(s_comms);
595   }
596   CATCH(e) {
597     switch (e.category) {
598       case network_error:
599         finished_index = e.value;
600         status = MSG_TRANSFER_FAILURE;
601         break;
602
603       case timeout_error:
604         finished_index = e.value;
605         status = MSG_TIMEOUT;
606         break;
607
608       default:
609         RETHROW;
610     }
611     xbt_ex_free(e);
612   }
613
614   xbt_assert(finished_index != -1, "WaitAny returned -1");
615   xbt_dynar_free(&s_comms);
616
617   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
618   /* the communication is finished */
619   comm->status = status;
620
621   if (comm->task_received != NULL) {
622     /* I am the receiver */
623     (*comm->task_received)->simdata->isused = 0;
624   }
625
626   return finished_index;
627 }
628
629 /**
630  * \ingroup msg_task_usage
631  * \brief Returns the error (if any) that occured during a finished communication.
632  * \param comm a finished communication
633  * \return the status of the communication, or #MSG_OK if no error occured
634  * during the communication
635  */
636 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
637
638   return comm->status;
639 }
640
641 /** \ingroup msg_task_usage
642  * \brief Get a task (#msg_task_t) from a communication
643  *
644  * \param comm the communication where to get the task
645  * \return the task from the communication
646  */
647 msg_task_t MSG_comm_get_task(msg_comm_t comm)
648 {
649   xbt_assert(comm, "Invalid parameter");
650
651   return comm->task_received ? *comm->task_received : comm->task_sent;
652 }
653
654 /**
655  * \brief This function is called by SIMIX to copy the data of a comm.
656  * \param comm the comm
657  * \param buff the data copied
658  * \param buff_size size of the buffer
659  */
660 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
661
662   // copy the task
663   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
664
665   // notify the user callback if any
666   if (msg_global->task_copy_callback) {
667     msg_task_t task = buff;
668     msg_global->task_copy_callback(task,
669         simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
670   }
671 }
672
673 /** \ingroup msg_task_usage
674  * \brief Sends a task to a mailbox
675  *
676  * This is a blocking function, the execution flow will be blocked
677  * until the task is sent (and received in the other side if #MSG_task_receive is used).
678  * See #MSG_task_isend for sending tasks asynchronously.
679  *
680  * \param task the task to be sent
681  * \param alias the mailbox name to where the task is sent
682  *
683  * \return Returns #MSG_OK if the task was successfully sent,
684  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
685  */
686 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
687 {
688   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
689   return MSG_task_send_with_timeout(task, alias, -1);
690 }
691
692 /** \ingroup msg_task_usage
693  * \brief Sends a task to a mailbox with a maximum rate
694  *
695  * This is a blocking function, the execution flow will be blocked
696  * until the task is sent. The maxrate parameter allows the application
697  * to limit the bandwidth utilization of network links when sending the task.
698  *
699  * \param task the task to be sent
700  * \param alias the mailbox name to where the task is sent
701  * \param maxrate the maximum communication rate for sending this task
702  *
703  * \return Returns #MSG_OK if the task was successfully sent,
704  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
705  */
706 msg_error_t
707 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
708 {
709   task->simdata->rate = maxrate;
710   return MSG_task_send(task, alias);
711 }
712
713 /** \ingroup msg_task_usage
714  * \brief Sends a task to a mailbox with a timeout
715  *
716  * This is a blocking function, the execution flow will be blocked
717  * until the task is sent or the timeout is achieved.
718  *
719  * \param task the task to be sent
720  * \param alias the mailbox name to where the task is sent
721  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
722  *
723  * \return Returns #MSG_OK if the task was successfully sent,
724  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
725  */
726 msg_error_t
727 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
728                            double timeout)
729 {
730   return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
731                                       task, timeout);
732 }
733
734 /** \ingroup msg_task_usage
735  * \brief Check if there is a communication going on in a mailbox.
736  *
737  * \param alias the name of the mailbox to be considered
738  *
739  * \return Returns 1 if there is a communication, 0 otherwise
740  */
741 int MSG_task_listen(const char *alias)
742 {
743   return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
744 }
745
746 /** \ingroup msg_task_usage
747  * \brief Check the number of communication actions of a given host pending in a mailbox.
748  *
749  * \param alias the name of the mailbox to be considered
750  * \param host the host to check for communication
751  *
752  * \return Returns the number of pending communication actions of the host in the
753  * given mailbox, 0 if there is no pending communication actions.
754  *
755  */
756 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
757 {
758   return
759       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
760                                                (alias), host);
761 }
762
763 /** \ingroup msg_task_usage
764  * \brief Look if there is a communication on a mailbox and return the
765  * PID of the sender process.
766  *
767  * \param alias the name of the mailbox to be considered
768  *
769  * \return Returns the PID of sender process,
770  * -1 if there is no communication in the mailbox.
771  */
772 int MSG_task_listen_from(const char *alias)
773 {
774   msg_task_t task;
775
776   if (NULL ==
777       (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
778     return -1;
779
780   return MSG_process_get_PID(task->simdata->sender);
781 }
782
783 /** \ingroup msg_task_usage
784  * \brief Sets the tracing category of a task.
785  *
786  * This function should be called after the creation of
787  * a MSG task, to define the category of that task. The
788  * first parameter task must contain a task that was
789  * created with the function #MSG_task_create. The second
790  * parameter category must contain a category that was
791  * previously declared with the function #TRACE_category
792  * (or with #TRACE_category_with_color).
793  *
794  * See \ref tracing for details on how to trace
795  * the (categorized) resource utilization.
796  *
797  * \param task the task that is going to be categorized
798  * \param category the name of the category to be associated to the task
799  *
800  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
801  */
802 void MSG_task_set_category (msg_task_t task, const char *category)
803 {
804 #ifdef HAVE_TRACING
805   TRACE_msg_set_task_category (task, category);
806 #endif
807 }
808
809 /** \ingroup msg_task_usage
810  *
811  * \brief Gets the current tracing category of a task.
812  *
813  * \param task the task to be considered
814  *
815  * \see MSG_task_set_category
816  *
817  * \return Returns the name of the tracing category of the given task, NULL otherwise
818  */
819 const char *MSG_task_get_category (msg_task_t task)
820 {
821 #ifdef HAVE_TRACING
822   return task->category;
823 #else
824   return NULL;
825 #endif
826 }
827
828 /**
829  * \brief Returns the value of a given AS or router property
830  *
831  * \param asr the name of a router or AS
832  * \param name a property name
833  * \return value of a property (or NULL if property not set)
834  */
835 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
836 {
837   return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
838 }
839
840 /**
841  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
842  * a the AS or router
843  *
844  * \param asr the name of a router or AS
845  * \return a dict containing the properties
846  */
847 xbt_dict_t MSG_as_router_get_properties(const char* asr)
848 {
849   return (simcall_asr_get_properties(asr));
850 }
851
852 /**
853  * \brief Change the value of a given AS or router
854  *
855  * \param asr the name of a router or AS
856  * \param name a property name
857  * \param value what to change the property to
858  * \param free_ctn the freeing function to use to kill the value on need
859  */
860 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
861   xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
862 }
863
864 #ifdef MSG_USE_DEPRECATED
865 /** \ingroup msg_deprecated_functions
866  *
867  * \brief Return the last value returned by a MSG function (except
868  * MSG_get_errno...).
869  */
870 msg_error_t MSG_get_errno(void)
871 {
872   return PROCESS_GET_ERRNO();
873 }
874
875 /** \ingroup msg_deprecated_functions
876  * \brief Put a task on a channel of an host and waits for the end of the
877  * transmission.
878  *
879  * This function is used for describing the behavior of a process. It
880  * takes three parameter.
881  * \param task a #msg_task_t to send on another location. This task
882  will not be usable anymore when the function will return. There is
883  no automatic task duplication and you have to save your parameters
884  before calling this function. Tasks are unique and once it has been
885  sent to another location, you should not access it anymore. You do
886  not need to call MSG_task_destroy() but to avoid using, as an
887  effect of inattention, this task anymore, you definitely should
888  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
889  can be transfered iff it has been correctly created with
890  MSG_task_create().
891  * \param dest the destination of the message
892  * \param channel the channel on which the process should put this
893  task. This value has to be >=0 and < than the maximal number of
894  channels fixed with MSG_set_channel_number().
895  * \return #MSG_HOST_FAILURE if the host on which
896  * this function was called was shut down,
897  * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
898  * (network failure, dest failure) or #MSG_OK if it succeeded.
899  */
900 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
901 {
902   XBT_WARN("DEPRECATED! Now use MSG_task_send");
903   return MSG_task_put_with_timeout(task, dest, channel, -1.0);
904 }
905
906 /** \ingroup msg_deprecated_functions
907  * \brief Does exactly the same as MSG_task_put but with a bounded transmition
908  * rate.
909  *
910  * \sa MSG_task_put
911  */
912 msg_error_t
913 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
914                      double maxrate)
915 {
916   XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
917   task->simdata->rate = maxrate;
918   return MSG_task_put(task, dest, channel);
919 }
920
921 /** \ingroup msg_deprecated_functions
922  *
923  * \brief Put a task on a channel of an
924  * host (with a timeout on the waiting of the destination host) and
925  * waits for the end of the transmission.
926  *
927  * This function is used for describing the behavior of a process. It
928  * takes four parameter.
929  * \param task a #msg_task_t to send on another location. This task
930  will not be usable anymore when the function will return. There is
931  no automatic task duplication and you have to save your parameters
932  before calling this function. Tasks are unique and once it has been
933  sent to another location, you should not access it anymore. You do
934  not need to call MSG_task_destroy() but to avoid using, as an
935  effect of inattention, this task anymore, you definitely should
936  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
937  can be transfered iff it has been correctly created with
938  MSG_task_create().
939  * \param dest the destination of the message
940  * \param channel the channel on which the process should put this
941  task. This value has to be >=0 and < than the maximal number of
942  channels fixed with MSG_set_channel_number().
943  * \param timeout the maximum time to wait for a task before giving
944  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
945  will not be modified
946  * \return #MSG_HOST_FAILURE if the host on which
947 this function was called was shut down,
948 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
949 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
950  */
951 msg_error_t
952 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
953                           m_channel_t channel, double timeout)
954 {
955   XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
956   xbt_assert((channel >= 0)
957               && (channel < msg_global->max_channel), "Invalid channel %d",
958               channel);
959
960   XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
961   return
962       MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
963                                    (dest, channel), task, timeout);
964 }
965
966 /** \ingroup msg_deprecated_functions
967  * \brief Test whether there is a pending communication on a channel, and who sent it.
968  *
969  * It takes one parameter.
970  * \param channel the channel on which the process should be
971  listening. This value has to be >=0 and < than the maximal
972  number of channels fixed with MSG_set_channel_number().
973  * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
974  */
975 int MSG_task_probe_from(m_channel_t channel)
976 {
977   XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
978   msg_task_t task;
979
980   xbt_assert((channel >= 0)
981               && (channel < msg_global->max_channel), "Invalid channel %d",
982               channel);
983
984   if (NULL ==
985       (task =
986        MSG_mailbox_get_head(MSG_mailbox_get_by_channel
987                             (MSG_host_self(), channel))))
988     return -1;
989
990   return MSG_process_get_PID(task->simdata->sender);
991 }
992
993 /** \ingroup msg_deprecated_functions
994  * \brief Test whether there is a pending communication on a channel.
995  *
996  * It takes one parameter.
997  * \param channel the channel on which the process should be
998  listening. This value has to be >=0 and < than the maximal
999  number of channels fixed with MSG_set_channel_number().
1000  * \return 1 if there is a pending communication and 0 otherwise
1001  */
1002 int MSG_task_Iprobe(m_channel_t channel)
1003 {
1004   XBT_WARN("DEPRECATED!");
1005   xbt_assert((channel >= 0)
1006               && (channel < msg_global->max_channel), "Invalid channel %d",
1007               channel);
1008
1009   return
1010       !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1011                             (MSG_host_self(), channel));
1012 }
1013
1014 /** \ingroup msg_deprecated_functions
1015
1016  * \brief Return the number of tasks waiting to be received on a \a
1017  channel and sent by \a host.
1018  *
1019  * It takes two parameters.
1020  * \param channel the channel on which the process should be
1021  listening. This value has to be >=0 and < than the maximal
1022  number of channels fixed with MSG_set_channel_number().
1023  * \param host the host that is to be watched.
1024  * \return the number of tasks waiting to be received on \a channel
1025  and sent by \a host.
1026  */
1027 int MSG_task_probe_from_host(int channel, msg_host_t host)
1028 {
1029   XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1030   xbt_assert((channel >= 0)
1031               && (channel < msg_global->max_channel), "Invalid channel %d",
1032               channel);
1033
1034   return
1035       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1036                                                (MSG_host_self(), channel),
1037                                                host);
1038
1039 }
1040
1041 /** \ingroup msg_deprecated_functions
1042  * \brief Listen on \a channel and waits for receiving a task from \a host.
1043  *
1044  * It takes three parameters.
1045  * \param task a memory location for storing a #msg_task_t. It will
1046  hold a task when this function will return. Thus \a task should not
1047  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1048  those two condition does not hold, there will be a warning message.
1049  * \param channel the channel on which the process should be
1050  listening. This value has to be >=0 and < than the maximal
1051  number of channels fixed with MSG_set_channel_number().
1052  * \param host the host that is to be watched.
1053  * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1054  */
1055 msg_error_t
1056 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1057 {
1058   XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1059   return MSG_task_get_ext(task, channel, -1, host);
1060 }
1061
1062 /** \ingroup msg_deprecated_functions
1063  * \brief Listen on a channel and wait for receiving a task.
1064  *
1065  * It takes two parameters.
1066  * \param task a memory location for storing a #msg_task_t. It will
1067  hold a task when this function will return. Thus \a task should not
1068  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1069  those two condition does not hold, there will be a warning message.
1070  * \param channel the channel on which the process should be
1071  listening. This value has to be >=0 and < than the maximal
1072  number of channels fixed with MSG_set_channel_number().
1073  * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1074  */
1075 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1076 {
1077   XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1078   return MSG_task_get_with_timeout(task, channel, -1);
1079 }
1080
1081 /** \ingroup msg_deprecated_functions
1082  * \brief Listen on a channel and wait for receiving a task with a timeout.
1083  *
1084  * It takes three parameters.
1085  * \param task a memory location for storing a #msg_task_t. It will
1086  hold a task when this function will return. Thus \a task should not
1087  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1088  those two condition does not hold, there will be a warning message.
1089  * \param channel the channel on which the process should be
1090  listening. This value has to be >=0 and < than the maximal
1091  number of channels fixed with MSG_set_channel_number().
1092  * \param max_duration the maximum time to wait for a task before giving
1093  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1094  will not be modified and will still be
1095  equal to \c NULL when returning.
1096  * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1097  */
1098 msg_error_t
1099 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1100                           double max_duration)
1101 {
1102   XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1103   return MSG_task_get_ext(task, channel, max_duration, NULL);
1104 }
1105
1106 msg_error_t
1107 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1108                  msg_host_t host)
1109 {
1110   XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1111   xbt_assert((channel >= 0)
1112               && (channel < msg_global->max_channel), "Invalid channel %d",
1113               channel);
1114
1115   return
1116       MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1117                                (MSG_host_self(), channel), task, host,
1118                                timeout);
1119 }
1120
1121 #endif