Logo AND Algorithmique Numérique Distribuée

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