Logo AND Algorithmique Numérique Distribuée

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