Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : use XBT_DEBUG instead of XBT_INFO
[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 @ref 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 #ifdef HAVE_TRACING
298   int call_end = TRACE_msg_task_put_start(task);
299 #endif
300
301   /* Prepare the task to send */
302   t_simdata = task->simdata;
303   t_simdata->sender = process;
304   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
305
306   xbt_assert(t_simdata->isused == 0,
307               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
308
309   t_simdata->isused = 1;
310   t_simdata->comm = NULL;
311   msg_global->sent_msg++;
312
313   /* Send it by calling SIMIX network layer */
314   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
315   comm->task_sent = task;
316   comm->task_received = NULL;
317   comm->status = MSG_OK;
318   comm->s_comm =
319     simcall_comm_isend(mailbox, t_simdata->message_size,
320                          t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
321   t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
322 #ifdef HAVE_TRACING
323     if (TRACE_is_enabled()) {
324       simcall_set_category(comm->s_comm, task->category);
325     }
326 #endif
327
328 #ifdef HAVE_TRACING
329   if (call_end)
330     TRACE_msg_task_put_end();
331 #endif
332
333   return comm;
334 }
335
336 /** \ingroup msg_task_usage
337  * \brief Sends a task on a mailbox.
338  *
339  * This is a non blocking detached send function.
340  * Think of it as a best effort send. Keep in mind that the third parameter
341  * is only called if the communication fails. If the communication does work,
342  * it is responsibility of the receiver code to free anything related to
343  * the task, as usual. More details on this can be obtained on
344  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
345  * in the SimGrid-user mailing list archive.
346  *
347  * \param task a #msg_task_t to send on another location.
348  * \param alias name of the mailbox to sent the task to
349  * \param cleanup a function to destroy the task if the
350  * communication fails, e.g. MSG_task_destroy
351  * (if NULL, no function will be called)
352  */
353 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
354 {
355   simdata_task_t t_simdata = NULL;
356   msg_process_t process = MSG_process_self();
357   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
358
359   /* Prepare the task to send */
360   t_simdata = task->simdata;
361   t_simdata->sender = process;
362   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
363
364   xbt_assert(t_simdata->isused == 0,
365               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
366
367   t_simdata->isused = 1;
368   t_simdata->comm = NULL;
369   msg_global->sent_msg++;
370
371 #ifdef HAVE_TRACING
372   int call_end = TRACE_msg_task_put_start(task);
373 #endif
374
375   /* Send it by calling SIMIX network layer */
376   smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
377                        t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
378   t_simdata->comm = comm;
379 #ifdef HAVE_TRACING
380     if (TRACE_is_enabled()) {
381       simcall_set_category(comm, task->category);
382     }
383 #endif
384
385 #ifdef HAVE_TRACING
386   if (call_end)
387     TRACE_msg_task_put_end();
388 #endif
389 }
390
391 /** \ingroup msg_task_usage
392  * \brief Starts listening for receiving a task from an asynchronous communication.
393  *
394  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
395  * to end the communication.
396  * 
397  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
398  * \param name of the mailbox to receive the task on
399  * \return the msg_comm_t communication created
400  */
401 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
402 {
403   smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
404
405   /* FIXME: these functions are not traceable */
406
407   /* Sanity check */
408   xbt_assert(task, "Null pointer for the task storage");
409
410   if (*task)
411     XBT_CRITICAL
412         ("MSG_task_irecv() was asked to write in a non empty task struct.");
413
414   /* Try to receive it by calling SIMIX network layer */
415   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
416   comm->task_sent = NULL;
417   comm->task_received = task;
418   comm->status = MSG_OK;
419   comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
420
421   return comm;
422 }
423
424 /** \ingroup msg_task_usage
425  * \brief Checks whether a communication is done, and if yes, finalizes it.
426  * \param comm the communication to test
427  * \return TRUE if the communication is finished
428  * (but it may have failed, use MSG_comm_get_status() to know its status)
429  * or FALSE if the communication is not finished yet
430  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
431  */
432 int MSG_comm_test(msg_comm_t comm)
433 {
434   xbt_ex_t e;
435   int finished = 0;
436
437   /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
438   if (MC_is_active()){
439     MC_ignore_stack("e", "MSG_comm_test");
440     MC_ignore_stack("__ex_cleanup", "MSG_comm_test");
441   }
442
443   TRY {
444     finished = simcall_comm_test(comm->s_comm);
445
446     if (finished && comm->task_received != NULL) {
447       /* I am the receiver */
448       (*comm->task_received)->simdata->isused = 0;
449     }
450   }
451   CATCH(e) {
452     switch (e.category) {
453       case network_error:
454         comm->status = MSG_TRANSFER_FAILURE;
455         finished = 1;
456         break;
457
458       case timeout_error:
459         comm->status = MSG_TIMEOUT;
460         finished = 1;
461         break;
462
463       default:
464         RETHROW;
465     }
466     xbt_ex_free(e);
467   }
468
469   return finished;
470 }
471
472 /** \ingroup msg_task_usage
473  * \brief This function checks if a communication is finished.
474  * \param comms a vector of communications
475  * \return the position of the finished communication if any
476  * (but it may have failed, use MSG_comm_get_status() to know its status),
477  * or -1 if none is finished
478  */
479 int MSG_comm_testany(xbt_dynar_t comms)
480 {
481   xbt_ex_t e;
482   int finished_index = -1;
483
484   /* create the equivalent dynar with SIMIX objects */
485   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
486   msg_comm_t comm;
487   unsigned int cursor;
488   xbt_dynar_foreach(comms, cursor, comm) {
489     xbt_dynar_push(s_comms, &comm->s_comm);
490   }
491
492   msg_error_t status = MSG_OK;
493   TRY {
494     finished_index = simcall_comm_testany(s_comms);
495   }
496   CATCH(e) {
497     switch (e.category) {
498       case network_error:
499         finished_index = e.value;
500         status = MSG_TRANSFER_FAILURE;
501         break;
502
503       case timeout_error:
504         finished_index = e.value;
505         status = MSG_TIMEOUT;
506         break;
507
508       default:
509         RETHROW;
510     }
511     xbt_ex_free(e);
512   }
513   xbt_dynar_free(&s_comms);
514
515   if (finished_index != -1) {
516     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
517     /* the communication is finished */
518     comm->status = status;
519
520     if (status == MSG_OK && comm->task_received != NULL) {
521       /* I am the receiver */
522       (*comm->task_received)->simdata->isused = 0;
523     }
524   }
525
526   return finished_index;
527 }
528
529 /** \ingroup msg_task_usage
530  * \brief Destroys a communication.
531  * \param comm the communication to destroy.
532  */
533 void MSG_comm_destroy(msg_comm_t comm)
534 {
535   xbt_free(comm);
536 }
537
538 /** \ingroup msg_task_usage
539  * \brief Wait for the completion of a communication.
540  *
541  * It takes two parameters.
542  * \param comm the communication to wait.
543  * \param timeout Wait until the communication terminates or the timeout 
544  * occurs. You can provide a -1 timeout to obtain an infinite timeout.
545  * \return msg_error_t
546  */
547 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
548 {
549   xbt_ex_t e;
550   TRY {
551     simcall_comm_wait(comm->s_comm, timeout);
552
553     if (comm->task_received != NULL) {
554       /* I am the receiver */
555       (*comm->task_received)->simdata->isused = 0;
556     }
557
558     /* FIXME: these functions are not traceable */
559   }
560   CATCH(e) {
561     switch (e.category) {
562     case network_error:
563       comm->status = MSG_TRANSFER_FAILURE;
564       break;
565     case timeout_error:
566       comm->status = MSG_TIMEOUT;
567       break;
568     default:
569       RETHROW;
570     }
571     xbt_ex_free(e);
572   }
573
574   return comm->status;
575 }
576
577 /** \ingroup msg_task_usage
578 * \brief This function is called by a sender and permit to wait for each communication
579 *
580 * \param comm a vector of communication
581 * \param nb_elem is the size of the comm vector
582 * \param timeout for each call of MSG_comm_wait
583 */
584 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
585 {
586   int i = 0;
587   for (i = 0; i < nb_elem; i++) {
588     MSG_comm_wait(comm[i], timeout);
589   }
590 }
591
592 /** \ingroup msg_task_usage
593  * \brief This function waits for the first communication finished in a list.
594  * \param comms a vector of communications
595  * \return the position of the first finished communication
596  * (but it may have failed, use MSG_comm_get_status() to know its status)
597  */
598 int MSG_comm_waitany(xbt_dynar_t comms)
599 {
600   xbt_ex_t e;
601   int finished_index = -1;
602
603   /* create the equivalent dynar with SIMIX objects */
604   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
605   msg_comm_t comm;
606   unsigned int cursor;
607   xbt_dynar_foreach(comms, cursor, comm) {
608     xbt_dynar_push(s_comms, &comm->s_comm);
609   }
610
611   msg_error_t status = MSG_OK;
612   TRY {
613     finished_index = simcall_comm_waitany(s_comms);
614   }
615   CATCH(e) {
616     switch (e.category) {
617       case network_error:
618         finished_index = e.value;
619         status = MSG_TRANSFER_FAILURE;
620         break;
621
622       case timeout_error:
623         finished_index = e.value;
624         status = MSG_TIMEOUT;
625         break;
626
627       default:
628         RETHROW;
629     }
630     xbt_ex_free(e);
631   }
632
633   xbt_assert(finished_index != -1, "WaitAny returned -1");
634   xbt_dynar_free(&s_comms);
635
636   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
637   /* the communication is finished */
638   comm->status = status;
639
640   if (comm->task_received != NULL) {
641     /* I am the receiver */
642     (*comm->task_received)->simdata->isused = 0;
643   }
644
645   return finished_index;
646 }
647
648 /**
649  * \ingroup msg_task_usage
650  * \brief Returns the error (if any) that occured during a finished communication.
651  * \param comm a finished communication
652  * \return the status of the communication, or #MSG_OK if no error occured
653  * during the communication
654  */
655 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
656
657   return comm->status;
658 }
659
660 /** \ingroup msg_task_usage
661  * \brief Get a task (#msg_task_t) from a communication
662  *
663  * \param comm the communication where to get the task
664  * \return the task from the communication
665  */
666 msg_task_t MSG_comm_get_task(msg_comm_t comm)
667 {
668   xbt_assert(comm, "Invalid parameter");
669
670   return comm->task_received ? *comm->task_received : comm->task_sent;
671 }
672
673 /**
674  * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
675  * \param comm the comm
676  * \param buff the data copied
677  * \param buff_size size of the buffer
678  */
679 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
680
681   // copy the task
682   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
683
684   // notify the user callback if any
685   if (msg_global->task_copy_callback) {
686     msg_task_t task = buff;
687     msg_global->task_copy_callback(task,
688         simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
689   }
690 }
691
692 /** \ingroup msg_task_usage
693  * \brief Sends a task to a mailbox
694  *
695  * This is a blocking function, the execution flow will be blocked
696  * until the task is sent (and received in the other side if #MSG_task_receive is used).
697  * See #MSG_task_isend for sending tasks asynchronously.
698  *
699  * \param task the task to be sent
700  * \param alias the mailbox name to where the task is sent
701  *
702  * \return Returns #MSG_OK if the task was successfully sent,
703  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
704  */
705 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
706 {
707   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
708   return MSG_task_send_with_timeout(task, alias, -1);
709 }
710
711 /** \ingroup msg_task_usage
712  * \brief Sends a task to a mailbox with a maximum rate
713  *
714  * This is a blocking function, the execution flow will be blocked
715  * until the task is sent. The maxrate parameter allows the application
716  * to limit the bandwidth utilization of network links when sending the task.
717  *
718  * \param task the task to be sent
719  * \param alias the mailbox name to where the task is sent
720  * \param maxrate the maximum communication rate for sending this task
721  *
722  * \return Returns #MSG_OK if the task was successfully sent,
723  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
724  */
725 msg_error_t
726 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
727 {
728   task->simdata->rate = maxrate;
729   return MSG_task_send(task, alias);
730 }
731
732 /** \ingroup msg_task_usage
733  * \brief Sends a task to a mailbox with a timeout
734  *
735  * This is a blocking function, the execution flow will be blocked
736  * until the task is sent or the timeout is achieved.
737  *
738  * \param task the task to be sent
739  * \param alias the mailbox name to where the task is sent
740  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
741  *
742  * \return Returns #MSG_OK if the task was successfully sent,
743  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
744  */
745 msg_error_t
746 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
747                            double timeout)
748 {
749   return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
750                                       task, timeout);
751 }
752
753 /** \ingroup msg_task_usage
754  * \brief Check if there is a communication going on in a mailbox.
755  *
756  * \param alias the name of the mailbox to be considered
757  *
758  * \return Returns 1 if there is a communication, 0 otherwise
759  */
760 int MSG_task_listen(const char *alias)
761 {
762   return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
763 }
764
765 /** \ingroup msg_task_usage
766  * \brief Check the number of communication actions of a given host pending in a mailbox.
767  *
768  * \param alias the name of the mailbox to be considered
769  * \param host the host to check for communication
770  *
771  * \return Returns the number of pending communication actions of the host in the
772  * given mailbox, 0 if there is no pending communication actions.
773  *
774  */
775 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
776 {
777   return
778       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
779                                                (alias), host);
780 }
781
782 /** \ingroup msg_task_usage
783  * \brief Look if there is a communication on a mailbox and return the
784  * PID of the sender process.
785  *
786  * \param alias the name of the mailbox to be considered
787  *
788  * \return Returns the PID of sender process,
789  * -1 if there is no communication in the mailbox.
790  */
791 int MSG_task_listen_from(const char *alias)
792 {
793   msg_task_t task;
794
795   if (NULL ==
796       (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
797     return -1;
798
799   return MSG_process_get_PID(task->simdata->sender);
800 }
801
802 /** \ingroup msg_task_usage
803  * \brief Sets the tracing category of a task.
804  *
805  * This function should be called after the creation of
806  * a MSG task, to define the category of that task. The
807  * first parameter task must contain a task that was
808  * created with the function #MSG_task_create. The second
809  * parameter category must contain a category that was
810  * previously declared with the function #TRACE_category
811  * (or with #TRACE_category_with_color).
812  *
813  * See \ref tracing for details on how to trace
814  * the (categorized) resource utilization.
815  *
816  * \param task the task that is going to be categorized
817  * \param category the name of the category to be associated to the task
818  *
819  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
820  */
821 void MSG_task_set_category (msg_task_t task, const char *category)
822 {
823 #ifdef HAVE_TRACING
824   TRACE_msg_set_task_category (task, category);
825 #endif
826 }
827
828 /** \ingroup msg_task_usage
829  *
830  * \brief Gets the current tracing category of a task.
831  *
832  * \param task the task to be considered
833  *
834  * \see MSG_task_set_category
835  *
836  * \return Returns the name of the tracing category of the given task, NULL otherwise
837  */
838 const char *MSG_task_get_category (msg_task_t task)
839 {
840 #ifdef HAVE_TRACING
841   return task->category;
842 #else
843   return NULL;
844 #endif
845 }
846
847 /**
848  * \brief Returns the value of a given AS or router property
849  *
850  * \param asr the name of a router or AS
851  * \param name a property name
852  * \return value of a property (or NULL if property not set)
853  */
854 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
855 {
856   return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
857 }
858
859 /**
860  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
861  * a the AS or router
862  *
863  * \param asr the name of a router or AS
864  * \return a dict containing the properties
865  */
866 xbt_dict_t MSG_as_router_get_properties(const char* asr)
867 {
868   return (simcall_asr_get_properties(asr));
869 }
870
871 /**
872  * \brief Change the value of a given AS or router
873  *
874  * \param asr the name of a router or AS
875  * \param name a property name
876  * \param value what to change the property to
877  * \param free_ctn the freeing function to use to kill the value on need
878  */
879 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
880   xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
881 }
882
883 #ifdef MSG_USE_DEPRECATED
884 /** \ingroup msg_deprecated_functions
885  *
886  * \brief Return the last value returned by a MSG function (except
887  * MSG_get_errno...).
888  */
889 msg_error_t MSG_get_errno(void)
890 {
891   return PROCESS_GET_ERRNO();
892 }
893
894 /** \ingroup msg_deprecated_functions
895  * \brief Put a task on a channel of an host and waits for the end of the
896  * transmission.
897  *
898  * This function is used for describing the behavior of a process. It
899  * takes three parameter.
900  * \param task a #msg_task_t to send on another location. This task
901  will not be usable anymore when the function will return. There is
902  no automatic task duplication and you have to save your parameters
903  before calling this function. Tasks are unique and once it has been
904  sent to another location, you should not access it anymore. You do
905  not need to call MSG_task_destroy() but to avoid using, as an
906  effect of inattention, this task anymore, you definitely should
907  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
908  can be transfered iff it has been correctly created with
909  MSG_task_create().
910  * \param dest the destination of the message
911  * \param channel the channel on which the process should put this
912  task. This value has to be >=0 and < than the maximal number of
913  channels fixed with MSG_set_channel_number().
914  * \return #MSG_HOST_FAILURE if the host on which
915  * this function was called was shut down,
916  * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
917  * (network failure, dest failure) or #MSG_OK if it succeeded.
918  */
919 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
920 {
921   XBT_WARN("DEPRECATED! Now use MSG_task_send");
922   return MSG_task_put_with_timeout(task, dest, channel, -1.0);
923 }
924
925 /** \ingroup msg_deprecated_functions
926  * \brief Does exactly the same as MSG_task_put but with a bounded transmition
927  * rate.
928  *
929  * \sa MSG_task_put
930  */
931 msg_error_t
932 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
933                      double maxrate)
934 {
935   XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
936   task->simdata->rate = maxrate;
937   return MSG_task_put(task, dest, channel);
938 }
939
940 /** \ingroup msg_deprecated_functions
941  *
942  * \brief Put a task on a channel of an
943  * host (with a timeout on the waiting of the destination host) and
944  * waits for the end of the transmission.
945  *
946  * This function is used for describing the behavior of a process. It
947  * takes four parameter.
948  * \param task a #msg_task_t to send on another location. This task
949  will not be usable anymore when the function will return. There is
950  no automatic task duplication and you have to save your parameters
951  before calling this function. Tasks are unique and once it has been
952  sent to another location, you should not access it anymore. You do
953  not need to call MSG_task_destroy() but to avoid using, as an
954  effect of inattention, this task anymore, you definitely should
955  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
956  can be transfered iff it has been correctly created with
957  MSG_task_create().
958  * \param dest the destination of the message
959  * \param channel the channel on which the process should put this
960  task. This value has to be >=0 and < than the maximal number of
961  channels fixed with MSG_set_channel_number().
962  * \param timeout the maximum time to wait for a task before giving
963  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
964  will not be modified
965  * \return #MSG_HOST_FAILURE if the host on which
966 this function was called was shut down,
967 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
968 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
969  */
970 msg_error_t
971 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
972                           m_channel_t channel, double timeout)
973 {
974   XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
975   xbt_assert((channel >= 0)
976               && (channel < msg_global->max_channel), "Invalid channel %d",
977               channel);
978
979   XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
980   return
981       MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
982                                    (dest, channel), task, timeout);
983 }
984
985 /** \ingroup msg_deprecated_functions
986  * \brief Test whether there is a pending communication on a channel, and who sent it.
987  *
988  * It takes one parameter.
989  * \param channel the channel on which the process should be
990  listening. This value has to be >=0 and < than the maximal
991  number of channels fixed with MSG_set_channel_number().
992  * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
993  */
994 int MSG_task_probe_from(m_channel_t channel)
995 {
996   XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
997   msg_task_t task;
998
999   xbt_assert((channel >= 0)
1000               && (channel < msg_global->max_channel), "Invalid channel %d",
1001               channel);
1002
1003   if (NULL ==
1004       (task =
1005        MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1006                             (MSG_host_self(), channel))))
1007     return -1;
1008
1009   return MSG_process_get_PID(task->simdata->sender);
1010 }
1011
1012 /** \ingroup msg_deprecated_functions
1013  * \brief Test whether there is a pending communication on a channel.
1014  *
1015  * It takes one parameter.
1016  * \param channel the channel on which the process should be
1017  listening. This value has to be >=0 and < than the maximal
1018  number of channels fixed with MSG_set_channel_number().
1019  * \return 1 if there is a pending communication and 0 otherwise
1020  */
1021 int MSG_task_Iprobe(m_channel_t channel)
1022 {
1023   XBT_WARN("DEPRECATED!");
1024   xbt_assert((channel >= 0)
1025               && (channel < msg_global->max_channel), "Invalid channel %d",
1026               channel);
1027
1028   return
1029       !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1030                             (MSG_host_self(), channel));
1031 }
1032
1033 /** \ingroup msg_deprecated_functions
1034
1035  * \brief Return the number of tasks waiting to be received on a \a
1036  channel and sent by \a host.
1037  *
1038  * It takes two parameters.
1039  * \param channel the channel on which the process should be
1040  listening. This value has to be >=0 and < than the maximal
1041  number of channels fixed with MSG_set_channel_number().
1042  * \param host the host that is to be watched.
1043  * \return the number of tasks waiting to be received on \a channel
1044  and sent by \a host.
1045  */
1046 int MSG_task_probe_from_host(int channel, msg_host_t host)
1047 {
1048   XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1049   xbt_assert((channel >= 0)
1050               && (channel < msg_global->max_channel), "Invalid channel %d",
1051               channel);
1052
1053   return
1054       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1055                                                (MSG_host_self(), channel),
1056                                                host);
1057
1058 }
1059
1060 /** \ingroup msg_deprecated_functions
1061  * \brief Listen on \a channel and waits for receiving a task from \a host.
1062  *
1063  * It takes three parameters.
1064  * \param task a memory location for storing a #msg_task_t. It will
1065  hold a task when this function will return. Thus \a task should not
1066  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1067  those two condition does not hold, there will be a warning message.
1068  * \param channel the channel on which the process should be
1069  listening. This value has to be >=0 and < than the maximal
1070  number of channels fixed with MSG_set_channel_number().
1071  * \param host the host that is to be watched.
1072  * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1073  */
1074 msg_error_t
1075 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1076 {
1077   XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1078   return MSG_task_get_ext(task, channel, -1, host);
1079 }
1080
1081 /** \ingroup msg_deprecated_functions
1082  * \brief Listen on a channel and wait for receiving a task.
1083  *
1084  * It takes two 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  * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1093  */
1094 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1095 {
1096   XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1097   return MSG_task_get_with_timeout(task, channel, -1);
1098 }
1099
1100 /** \ingroup msg_deprecated_functions
1101  * \brief Listen on a channel and wait for receiving a task with a timeout.
1102  *
1103  * It takes three parameters.
1104  * \param task a memory location for storing a #msg_task_t. It will
1105  hold a task when this function will return. Thus \a task should not
1106  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1107  those two condition does not hold, there will be a warning message.
1108  * \param channel the channel on which the process should be
1109  listening. This value has to be >=0 and < than the maximal
1110  number of channels fixed with MSG_set_channel_number().
1111  * \param max_duration the maximum time to wait for a task before giving
1112  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1113  will not be modified and will still be
1114  equal to \c NULL when returning.
1115  * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1116  */
1117 msg_error_t
1118 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1119                           double max_duration)
1120 {
1121   XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1122   return MSG_task_get_ext(task, channel, max_duration, NULL);
1123 }
1124
1125 msg_error_t
1126 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1127                  msg_host_t host)
1128 {
1129   XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1130   xbt_assert((channel >= 0)
1131               && (channel < msg_global->max_channel), "Invalid channel %d",
1132               channel);
1133
1134   return
1135       MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1136                                (MSG_host_self(), channel), task, host,
1137                                timeout);
1138 }
1139
1140 #endif