Logo AND Algorithmique Numérique Distribuée

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