Logo AND Algorithmique Numérique Distribuée

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