Logo AND Algorithmique Numérique Distribuée

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