1 /* Copyright (c) 2004-2013. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "msg_private.h"
8 #include "msg_mailbox.h"
11 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
14 "Logging specific to MSG (gos)");
16 /** \ingroup msg_task_usage
17 * \brief Executes a task and waits for its termination.
19 * This function is used for describing the behavior of a process. It
20 * takes only one parameter.
21 * \param task a #msg_task_t to execute on the location on which the process is running.
22 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
23 * or #MSG_HOST_FAILURE otherwise
25 msg_error_t MSG_task_execute(msg_task_t task)
27 return MSG_parallel_task_execute(task);
30 /** \ingroup msg_task_usage
31 * \brief Executes a parallel task and waits for its termination.
33 * \param task a #msg_task_t to execute on the location on which the process is running.
35 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
36 * or #MSG_HOST_FAILURE otherwise
38 msg_error_t MSG_parallel_task_execute(msg_task_t task)
41 simdata_task_t simdata = task->simdata;
42 msg_process_t self = SIMIX_process_self();
43 simdata_process_t p_simdata = SIMIX_process_self_get_data(self);
44 e_smx_state_t comp_state;
45 msg_error_t status = MSG_OK;
48 TRACE_msg_task_execute_start(task);
51 xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
52 "This task is executed somewhere else. Go fix your code! %d",
53 task->simdata->isused);
55 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
57 if (simdata->computation_amount == 0 && !simdata->host_nb) {
59 TRACE_msg_task_execute_end(task);
69 if (simdata->host_nb > 0) {
70 simdata->compute = simcall_host_parallel_execute(task->name,
76 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
78 simdata->compute = simcall_host_execute(task->name,
80 simdata->computation_amount,
85 simcall_set_category(simdata->compute, task->category);
87 p_simdata->waiting_action = simdata->compute;
88 comp_state = simcall_host_execution_wait(simdata->compute);
90 p_simdata->waiting_action = NULL;
94 XBT_DEBUG("Execution task '%s' finished in state %d",
95 task->name, (int)comp_state);
100 status = MSG_TASK_CANCELED;
107 /* action ended, set comm and compute = NULL, the actions is already destroyed
108 * in the main function */
109 simdata->computation_amount = 0.0;
110 simdata->comm = NULL;
111 simdata->compute = NULL;
113 TRACE_msg_task_execute_end(task);
120 /** \ingroup msg_task_usage
121 * \brief Sleep for the specified number of seconds
123 * Makes the current process sleep until \a time seconds have elapsed.
125 * \param nb_sec a number of second
127 msg_error_t MSG_process_sleep(double nb_sec)
130 msg_error_t status = MSG_OK;
131 /*msg_process_t proc = MSG_process_self();*/
134 TRACE_msg_process_sleep_in(MSG_process_self());
137 /* create action to sleep */
139 /*proc->simdata->waiting_action = act_sleep;
141 FIXME: check if not setting the waiting_action breaks something on msg
143 proc->simdata->waiting_action = NULL;*/
146 simcall_process_sleep(nb_sec);
149 switch (e.category) {
151 status = MSG_TASK_CANCELED;
160 TRACE_msg_process_sleep_out(MSG_process_self());
165 /** \ingroup msg_task_usage
166 * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
168 * Sorry, this function is not supported anymore. That wouldn't be
169 * impossible to reimplement it, but we are lacking the time to do so ourselves.
170 * If you need this functionality, you can either:
172 * - implement the buffering mechanism on the user-level by queuing all messages
173 * received in the mailbox that do not match your expectation
174 * - change your application logic to leverage the mailboxes features. For example,
175 * if you have A receiving messages from B and C, you could have A waiting on
176 * mailbox "A" most of the time, but on "A#B" when it's waiting for specific
177 * messages from B and "A#C" when waiting for messages from C. You could even get A
178 * sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
179 * an example of use of this function in the @ref MSG_examples section.
180 * - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
181 * very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
182 * simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
183 * messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
184 * and that your filtering function will receive as first parameter, and then, the filter could
185 * simply compare the host names, for example. After sufficient testing, provide an example that
186 * we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
188 * \param task a memory location for storing a #msg_task_t.
189 * \param alias name of the mailbox to receive the task from
190 * \param host a #msg_host_t host from where the task was sent
193 * #MSG_OK if the task was successfully received,
194 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
197 MSG_task_receive_from_host(msg_task_t * task, const char *alias,
200 return MSG_task_receive_ext(task, alias, -1, host);
204 *\brief Deprecated function that used to receive a task from a mailbox from a specific host
205 *\brief at a given rate
207 * \param task a memory location for storing a #msg_task_t.
208 * \param alias name of the mailbox to receive the task from
209 * \param host a #msg_host_t host from where the task was sent
210 * \param rate limit the reception to rate bandwidth
213 * #MSG_OK if the task was successfully received,
214 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
217 MSG_task_receive_from_host_bounded(msg_task_t * task, const char *alias,
218 msg_host_t host, double rate)
220 return MSG_task_receive_ext_bounded(task, alias, -1, host, rate);
223 /** \ingroup msg_task_usage
224 * \brief Receives a task from a mailbox.
226 * This is a blocking function, the execution flow will be blocked
227 * until the task is received. See #MSG_task_irecv
228 * for receiving tasks asynchronously.
230 * \param task a memory location for storing a #msg_task_t.
231 * \param alias name of the mailbox to receive the task from
234 * #MSG_OK if the task was successfully received,
235 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
237 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
239 return MSG_task_receive_with_timeout(task, alias, -1);
242 /** \ingroup msg_task_usage
243 * \brief Receives a task from a mailbox at a given rate.
245 * \param task a memory location for storing a #msg_task_t.
246 * \param alias name of the mailbox to receive the task from
247 * \param rate limit the reception to rate bandwidth
250 * #MSG_OK if the task was successfully received,
251 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
253 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
255 return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
258 /** \ingroup msg_task_usage
259 * \brief Receives a task from a mailbox with a given timeout.
261 * This is a blocking function with a timeout, the execution flow will be blocked
262 * until the task is received or the timeout is achieved. See #MSG_task_irecv
263 * for receiving tasks asynchronously. You can provide a -1 timeout
264 * to obtain an infinite timeout.
266 * \param task a memory location for storing a #msg_task_t.
267 * \param alias name of the mailbox to receive the task from
268 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
271 * #MSG_OK if the task was successfully received,
272 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
275 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
278 return MSG_task_receive_ext(task, alias, timeout, NULL);
281 /** \ingroup msg_task_usage
282 * \brief Receives a task from a mailbox with a given timeout and at a given rate.
284 * \param task a memory location for storing a #msg_task_t.
285 * \param alias name of the mailbox to receive the task from
286 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
287 * \param rate limit the reception to rate bandwidth
290 * #MSG_OK if the task was successfully received,
291 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
294 MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias,
295 double timeout,double rate)
297 return MSG_task_receive_ext_bounded(task, alias, timeout, NULL,rate);
300 /** \ingroup msg_task_usage
301 * \brief Receives a task from a mailbox from a specific host with a given timeout.
303 * This is a blocking function with a timeout, the execution flow will be blocked
304 * until the task is received or the timeout is achieved. See #MSG_task_irecv
305 * for receiving tasks asynchronously. You can provide a -1 timeout
306 * to obtain an infinite timeout.
308 * \param task a memory location for storing a #msg_task_t.
309 * \param alias name of the mailbox to receive the task from
310 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
311 * \param host a #msg_host_t host from where the task was sent
314 * #MSG_OK if the task was successfully received,
315 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
318 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
322 msg_error_t ret = MSG_OK;
324 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
327 ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
331 switch (e.category) {
332 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
333 ret = MSG_HOST_FAILURE;
343 /** \ingroup msg_task_usage
344 * \brief Receives a task from a mailbox from a specific host with a given timeout
345 * and at a given rate.
347 * \param task a memory location for storing a #msg_task_t.
348 * \param alias name of the mailbox to receive the task from
349 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
350 * \param host a #msg_host_t host from where the task was sent
351 * \param rate limit the reception to rate bandwidth
354 * #MSG_OK if the task was successfully received,
355 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
358 MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout,
359 msg_host_t host, double rate)
362 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
364 return MSG_mailbox_get_task_ext_bounded(MSG_mailbox_get_by_alias(alias), task,
365 host, timeout, rate);
368 /** \ingroup msg_task_usage
369 * \brief Sends a task on a mailbox.
371 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
372 * to end the communication.
374 * \param task a #msg_task_t to send on another location.
375 * \param alias name of the mailbox to sent the task to
376 * \return the msg_comm_t communication created
378 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
380 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
383 /** \ingroup msg_task_usage
384 * \brief Sends a task on a mailbox with a maximum rate
386 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
387 * to end the communication. The maxrate parameter allows the application
388 * to limit the bandwidth utilization of network links when sending the task.
390 * \param task a #msg_task_t to send on another location.
391 * \param alias name of the mailbox to sent the task to
392 * \param maxrate the maximum communication rate for sending this task .
393 * \return the msg_comm_t communication created
395 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
397 task->simdata->rate = maxrate;
398 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
402 /** \ingroup msg_task_usage
403 * \brief Sends a task on a mailbox, with support for matching requests
405 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
406 * to end the communication.
408 * \param task a #msg_task_t to send on another location.
409 * \param alias name of the mailbox to sent the task to
410 * \param match_fun boolean function which parameters are:
411 * - match_data_provided_here
412 * - match_data_provided_by_other_side_if_any
413 * - the_smx_action_describing_the_other_side
414 * \param match_data user provided data passed to match_fun
415 * \return the msg_comm_t communication created
417 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
418 int (*match_fun)(void*,void*, smx_action_t),
421 simdata_task_t t_simdata = NULL;
422 msg_process_t process = MSG_process_self();
423 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
426 int call_end = TRACE_msg_task_put_start(task);
429 /* Prepare the task to send */
430 t_simdata = task->simdata;
431 t_simdata->sender = process;
432 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
434 xbt_assert(t_simdata->isused == 0,
435 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
437 t_simdata->isused = 1;
438 t_simdata->comm = NULL;
439 msg_global->sent_msg++;
441 /* Send it by calling SIMIX network layer */
442 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
443 comm->task_sent = task;
444 comm->task_received = NULL;
445 comm->status = MSG_OK;
447 simcall_comm_isend(mailbox, t_simdata->message_size,
448 t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
449 t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
451 if (TRACE_is_enabled()) {
452 simcall_set_category(comm->s_comm, task->category);
458 TRACE_msg_task_put_end();
464 /** \ingroup msg_task_usage
465 * \brief Sends a task on a mailbox.
467 * This is a non blocking detached send function.
468 * Think of it as a best effort send. Keep in mind that the third parameter
469 * is only called if the communication fails. If the communication does work,
470 * it is responsibility of the receiver code to free anything related to
471 * the task, as usual. More details on this can be obtained on
472 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
473 * in the SimGrid-user mailing list archive.
475 * \param task a #msg_task_t to send on another location.
476 * \param alias name of the mailbox to sent the task to
477 * \param cleanup a function to destroy the task if the
478 * communication fails, e.g. MSG_task_destroy
479 * (if NULL, no function will be called)
481 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
483 simdata_task_t t_simdata = NULL;
484 msg_process_t process = MSG_process_self();
485 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
487 /* Prepare the task to send */
488 t_simdata = task->simdata;
489 t_simdata->sender = process;
490 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
492 xbt_assert(t_simdata->isused == 0,
493 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
495 t_simdata->isused = 1;
496 t_simdata->comm = NULL;
497 msg_global->sent_msg++;
500 int call_end = TRACE_msg_task_put_start(task);
503 /* Send it by calling SIMIX network layer */
504 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
505 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
506 t_simdata->comm = comm;
508 if (TRACE_is_enabled()) {
509 simcall_set_category(comm, task->category);
515 TRACE_msg_task_put_end();
520 /** \ingroup msg_task_usage
521 * \brief Sends a task on a mailbox with a maximal rate.
523 * This is a non blocking detached send function.
524 * Think of it as a best effort send. Keep in mind that the third parameter
525 * is only called if the communication fails. If the communication does work,
526 * it is responsibility of the receiver code to free anything related to
527 * the task, as usual. More details on this can be obtained on
528 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
529 * in the SimGrid-user mailing list archive.
531 * \param task a #msg_task_t to send on another location.
532 * \param alias name of the mailbox to sent the task to
533 * \param cleanup a function to destroy the task if the
534 * communication fails, e.g. MSG_task_destroy
535 * (if NULL, no function will be called)
536 * \param maxrate the maximum communication rate for sending this task
539 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
541 task->simdata->rate = maxrate;
543 simdata_task_t t_simdata = NULL;
544 msg_process_t process = MSG_process_self();
545 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
547 /* Prepare the task to send */
548 t_simdata = task->simdata;
549 t_simdata->sender = process;
550 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
552 xbt_assert(t_simdata->isused == 0,
553 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
555 t_simdata->isused = 1;
556 t_simdata->comm = NULL;
557 msg_global->sent_msg++;
560 int call_end = TRACE_msg_task_put_start(task);
563 /* Send it by calling SIMIX network layer */
564 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
565 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
566 t_simdata->comm = comm;
568 if (TRACE_is_enabled()) {
569 simcall_set_category(comm, task->category);
575 TRACE_msg_task_put_end();
579 /** \ingroup msg_task_usage
580 * \brief Starts listening for receiving a task from an asynchronous communication.
582 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
583 * to end the communication.
585 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
586 * \param name of the mailbox to receive the task on
587 * \return the msg_comm_t communication created
589 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
591 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
593 /* FIXME: these functions are not traceable */
596 xbt_assert(task, "Null pointer for the task storage");
600 ("MSG_task_irecv() was asked to write in a non empty task struct.");
602 /* Try to receive it by calling SIMIX network layer */
603 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
604 comm->task_sent = NULL;
605 comm->task_received = task;
606 comm->status = MSG_OK;
607 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
612 /** \ingroup msg_task_usage
613 * \brief Starts listening for receiving a task from an asynchronous communication
616 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
617 * \param name of the mailbox to receive the task on
618 * \param rate limit the bandwidth to the given rate
619 * \return the msg_comm_t communication created
621 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
625 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
627 /* FIXME: these functions are not traceable */
630 xbt_assert(task, "Null pointer for the task storage");
634 ("MSG_task_irecv() was asked to write in a non empty task struct.");
636 /* Try to receive it by calling SIMIX network layer */
637 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
638 comm->task_sent = NULL;
639 comm->task_received = task;
640 comm->status = MSG_OK;
641 comm->s_comm = simcall_comm_irecv_bounded(rdv, task, NULL, NULL, NULL, rate);
646 /** \ingroup msg_task_usage
647 * \brief Checks whether a communication is done, and if yes, finalizes it.
648 * \param comm the communication to test
649 * \return TRUE if the communication is finished
650 * (but it may have failed, use MSG_comm_get_status() to know its status)
651 * or FALSE if the communication is not finished yet
652 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
654 int MSG_comm_test(msg_comm_t comm)
660 finished = simcall_comm_test(comm->s_comm);
662 if (finished && comm->task_received != NULL) {
663 /* I am the receiver */
664 (*comm->task_received)->simdata->isused = 0;
668 switch (e.category) {
670 comm->status = MSG_TRANSFER_FAILURE;
675 comm->status = MSG_TIMEOUT;
688 /** \ingroup msg_task_usage
689 * \brief This function checks if a communication is finished.
690 * \param comms a vector of communications
691 * \return the position of the finished communication if any
692 * (but it may have failed, use MSG_comm_get_status() to know its status),
693 * or -1 if none is finished
695 int MSG_comm_testany(xbt_dynar_t comms)
698 int finished_index = -1;
700 /* create the equivalent dynar with SIMIX objects */
701 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
704 xbt_dynar_foreach(comms, cursor, comm) {
705 xbt_dynar_push(s_comms, &comm->s_comm);
708 msg_error_t status = MSG_OK;
710 finished_index = simcall_comm_testany(s_comms);
713 switch (e.category) {
715 finished_index = e.value;
716 status = MSG_TRANSFER_FAILURE;
720 finished_index = e.value;
721 status = MSG_TIMEOUT;
729 xbt_dynar_free(&s_comms);
731 if (finished_index != -1) {
732 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
733 /* the communication is finished */
734 comm->status = status;
736 if (status == MSG_OK && comm->task_received != NULL) {
737 /* I am the receiver */
738 (*comm->task_received)->simdata->isused = 0;
742 return finished_index;
745 /** \ingroup msg_task_usage
746 * \brief Destroys a communication.
747 * \param comm the communication to destroy.
749 void MSG_comm_destroy(msg_comm_t comm)
754 /** \ingroup msg_task_usage
755 * \brief Wait for the completion of a communication.
757 * It takes two parameters.
758 * \param comm the communication to wait.
759 * \param timeout Wait until the communication terminates or the timeout
760 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
761 * \return msg_error_t
763 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
767 simcall_comm_wait(comm->s_comm, timeout);
769 if (comm->task_received != NULL) {
770 /* I am the receiver */
771 (*comm->task_received)->simdata->isused = 0;
774 /* FIXME: these functions are not traceable */
777 switch (e.category) {
779 comm->status = MSG_TRANSFER_FAILURE;
782 comm->status = MSG_TIMEOUT;
793 /** \ingroup msg_task_usage
794 * \brief This function is called by a sender and permit to wait for each communication
796 * \param comm a vector of communication
797 * \param nb_elem is the size of the comm vector
798 * \param timeout for each call of MSG_comm_wait
800 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
803 for (i = 0; i < nb_elem; i++) {
804 MSG_comm_wait(comm[i], timeout);
808 /** \ingroup msg_task_usage
809 * \brief This function waits for the first communication finished in a list.
810 * \param comms a vector of communications
811 * \return the position of the first finished communication
812 * (but it may have failed, use MSG_comm_get_status() to know its status)
814 int MSG_comm_waitany(xbt_dynar_t comms)
817 int finished_index = -1;
819 /* create the equivalent dynar with SIMIX objects */
820 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
823 xbt_dynar_foreach(comms, cursor, comm) {
824 xbt_dynar_push(s_comms, &comm->s_comm);
827 msg_error_t status = MSG_OK;
829 finished_index = simcall_comm_waitany(s_comms);
832 switch (e.category) {
834 finished_index = e.value;
835 status = MSG_TRANSFER_FAILURE;
839 finished_index = e.value;
840 status = MSG_TIMEOUT;
849 xbt_assert(finished_index != -1, "WaitAny returned -1");
850 xbt_dynar_free(&s_comms);
852 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
853 /* the communication is finished */
854 comm->status = status;
856 if (comm->task_received != NULL) {
857 /* I am the receiver */
858 (*comm->task_received)->simdata->isused = 0;
861 return finished_index;
865 * \ingroup msg_task_usage
866 * \brief Returns the error (if any) that occured during a finished communication.
867 * \param comm a finished communication
868 * \return the status of the communication, or #MSG_OK if no error occured
869 * during the communication
871 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
876 /** \ingroup msg_task_usage
877 * \brief Get a task (#msg_task_t) from a communication
879 * \param comm the communication where to get the task
880 * \return the task from the communication
882 msg_task_t MSG_comm_get_task(msg_comm_t comm)
884 xbt_assert(comm, "Invalid parameter");
886 return comm->task_received ? *comm->task_received : comm->task_sent;
890 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
891 * \param comm the comm
892 * \param buff the data copied
893 * \param buff_size size of the buffer
895 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
898 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
900 // notify the user callback if any
901 if (msg_global->task_copy_callback) {
902 msg_task_t task = buff;
903 msg_global->task_copy_callback(task,
904 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
908 /** \ingroup msg_task_usage
909 * \brief Sends a task to a mailbox
911 * This is a blocking function, the execution flow will be blocked
912 * until the task is sent (and received in the other side if #MSG_task_receive is used).
913 * See #MSG_task_isend for sending tasks asynchronously.
915 * \param task the task to be sent
916 * \param alias the mailbox name to where the task is sent
918 * \return Returns #MSG_OK if the task was successfully sent,
919 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
921 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
923 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
924 return MSG_task_send_with_timeout(task, alias, -1);
927 /** \ingroup msg_task_usage
928 * \brief Sends a task to a mailbox with a maximum rate
930 * This is a blocking function, the execution flow will be blocked
931 * until the task is sent. The maxrate parameter allows the application
932 * to limit the bandwidth utilization of network links when sending the task.
934 * \param task the task to be sent
935 * \param alias the mailbox name to where the task is sent
936 * \param maxrate the maximum communication rate for sending this task
938 * \return Returns #MSG_OK if the task was successfully sent,
939 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
942 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
944 task->simdata->rate = maxrate;
945 return MSG_task_send(task, alias);
948 /** \ingroup msg_task_usage
949 * \brief Sends a task to a mailbox with a timeout
951 * This is a blocking function, the execution flow will be blocked
952 * until the task is sent or the timeout is achieved.
954 * \param task the task to be sent
955 * \param alias the mailbox name to where the task is sent
956 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
958 * \return Returns #MSG_OK if the task was successfully sent,
959 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
962 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
965 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
969 /** \ingroup msg_task_usage
970 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
972 * This is a blocking function, the execution flow will be blocked
973 * until the task is sent or the timeout is achieved.
975 * \param task the task to be sent
976 * \param alias the mailbox name to where the task is sent
977 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
978 * \param maxrate the maximum communication rate for sending this task
980 * \return Returns #MSG_OK if the task was successfully sent,
981 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
984 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
985 double timeout, double maxrate)
987 task->simdata->rate = maxrate;
988 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
992 /** \ingroup msg_task_usage
993 * \brief Check if there is a communication going on in a mailbox.
995 * \param alias the name of the mailbox to be considered
997 * \return Returns 1 if there is a communication, 0 otherwise
999 int MSG_task_listen(const char *alias)
1001 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
1004 /** \ingroup msg_task_usage
1005 * \brief Check the number of communication actions of a given host pending in a mailbox.
1007 * \param alias the name of the mailbox to be considered
1008 * \param host the host to check for communication
1010 * \return Returns the number of pending communication actions of the host in the
1011 * given mailbox, 0 if there is no pending communication actions.
1014 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
1017 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
1021 /** \ingroup msg_task_usage
1022 * \brief Look if there is a communication on a mailbox and return the
1023 * PID of the sender process.
1025 * \param alias the name of the mailbox to be considered
1027 * \return Returns the PID of sender process,
1028 * -1 if there is no communication in the mailbox.
1030 int MSG_task_listen_from(const char *alias)
1035 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
1038 return MSG_process_get_PID(task->simdata->sender);
1041 /** \ingroup msg_task_usage
1042 * \brief Sets the tracing category of a task.
1044 * This function should be called after the creation of
1045 * a MSG task, to define the category of that task. The
1046 * first parameter task must contain a task that was
1047 * created with the function #MSG_task_create. The second
1048 * parameter category must contain a category that was
1049 * previously declared with the function #TRACE_category
1050 * (or with #TRACE_category_with_color).
1052 * See \ref tracing for details on how to trace
1053 * the (categorized) resource utilization.
1055 * \param task the task that is going to be categorized
1056 * \param category the name of the category to be associated to the task
1058 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
1060 void MSG_task_set_category (msg_task_t task, const char *category)
1063 TRACE_msg_set_task_category (task, category);
1067 /** \ingroup msg_task_usage
1069 * \brief Gets the current tracing category of a task.
1071 * \param task the task to be considered
1073 * \see MSG_task_set_category
1075 * \return Returns the name of the tracing category of the given task, NULL otherwise
1077 const char *MSG_task_get_category (msg_task_t task)
1080 return task->category;
1087 * \brief Returns the value of a given AS or router property
1089 * \param asr the name of a router or AS
1090 * \param name a property name
1091 * \return value of a property (or NULL if property not set)
1093 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
1095 return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
1099 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
1100 * a the AS or router
1102 * \param asr the name of a router or AS
1103 * \return a dict containing the properties
1105 xbt_dict_t MSG_as_router_get_properties(const char* asr)
1107 return (simcall_asr_get_properties(asr));
1111 * \brief Change the value of a given AS or router
1113 * \param asr the name of a router or AS
1114 * \param name a property name
1115 * \param value what to change the property to
1116 * \param free_ctn the freeing function to use to kill the value on need
1118 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1119 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1122 #ifdef MSG_USE_DEPRECATED
1123 /** \ingroup msg_deprecated_functions
1125 * \brief Return the last value returned by a MSG function (except
1126 * MSG_get_errno...).
1128 msg_error_t MSG_get_errno(void)
1130 return PROCESS_GET_ERRNO();
1133 /** \ingroup msg_deprecated_functions
1134 * \brief Put a task on a channel of an host and waits for the end of the
1137 * This function is used for describing the behavior of a process. It
1138 * takes three parameter.
1139 * \param task a #msg_task_t to send on another location. This task
1140 will not be usable anymore when the function will return. There is
1141 no automatic task duplication and you have to save your parameters
1142 before calling this function. Tasks are unique and once it has been
1143 sent to another location, you should not access it anymore. You do
1144 not need to call MSG_task_destroy() but to avoid using, as an
1145 effect of inattention, this task anymore, you definitely should
1146 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1147 can be transfered iff it has been correctly created with
1149 * \param dest the destination of the message
1150 * \param channel the channel on which the process should put this
1151 task. This value has to be >=0 and < than the maximal number of
1152 channels fixed with MSG_set_channel_number().
1153 * \return #MSG_HOST_FAILURE if the host on which
1154 * this function was called was shut down,
1155 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1156 * (network failure, dest failure) or #MSG_OK if it succeeded.
1158 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1160 XBT_WARN("DEPRECATED! Now use MSG_task_send");
1161 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1164 /** \ingroup msg_deprecated_functions
1165 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1171 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1174 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1175 task->simdata->rate = maxrate;
1176 return MSG_task_put(task, dest, channel);
1179 /** \ingroup msg_deprecated_functions
1181 * \brief Put a task on a channel of an
1182 * host (with a timeout on the waiting of the destination host) and
1183 * waits for the end of the transmission.
1185 * This function is used for describing the behavior of a process. It
1186 * takes four parameter.
1187 * \param task a #msg_task_t to send on another location. This task
1188 will not be usable anymore when the function will return. There is
1189 no automatic task duplication and you have to save your parameters
1190 before calling this function. Tasks are unique and once it has been
1191 sent to another location, you should not access it anymore. You do
1192 not need to call MSG_task_destroy() but to avoid using, as an
1193 effect of inattention, this task anymore, you definitely should
1194 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1195 can be transfered iff it has been correctly created with
1197 * \param dest the destination of the message
1198 * \param channel the channel on which the process should put this
1199 task. This value has to be >=0 and < than the maximal number of
1200 channels fixed with MSG_set_channel_number().
1201 * \param timeout the maximum time to wait for a task before giving
1202 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1203 will not be modified
1204 * \return #MSG_HOST_FAILURE if the host on which
1205 this function was called was shut down,
1206 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1207 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1210 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1211 m_channel_t channel, double timeout)
1213 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1214 xbt_assert((channel >= 0)
1215 && (channel < msg_global->max_channel), "Invalid channel %d",
1218 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
1220 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1221 (dest, channel), task, timeout);
1224 /** \ingroup msg_deprecated_functions
1225 * \brief Test whether there is a pending communication on a channel, and who sent it.
1227 * It takes one parameter.
1228 * \param channel the channel on which the process should be
1229 listening. This value has to be >=0 and < than the maximal
1230 number of channels fixed with MSG_set_channel_number().
1231 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1233 int MSG_task_probe_from(m_channel_t channel)
1235 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1238 xbt_assert((channel >= 0)
1239 && (channel < msg_global->max_channel), "Invalid channel %d",
1244 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1245 (MSG_host_self(), channel))))
1248 return MSG_process_get_PID(task->simdata->sender);
1251 /** \ingroup msg_deprecated_functions
1252 * \brief Test whether there is a pending communication on a channel.
1254 * It takes one parameter.
1255 * \param channel the channel on which the process should be
1256 listening. This value has to be >=0 and < than the maximal
1257 number of channels fixed with MSG_set_channel_number().
1258 * \return 1 if there is a pending communication and 0 otherwise
1260 int MSG_task_Iprobe(m_channel_t channel)
1262 XBT_WARN("DEPRECATED!");
1263 xbt_assert((channel >= 0)
1264 && (channel < msg_global->max_channel), "Invalid channel %d",
1268 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1269 (MSG_host_self(), channel));
1272 /** \ingroup msg_deprecated_functions
1274 * \brief Return the number of tasks waiting to be received on a \a
1275 channel and sent by \a host.
1277 * It takes two parameters.
1278 * \param channel the channel on which the process should be
1279 listening. This value has to be >=0 and < than the maximal
1280 number of channels fixed with MSG_set_channel_number().
1281 * \param host the host that is to be watched.
1282 * \return the number of tasks waiting to be received on \a channel
1283 and sent by \a host.
1285 int MSG_task_probe_from_host(int channel, msg_host_t host)
1287 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1288 xbt_assert((channel >= 0)
1289 && (channel < msg_global->max_channel), "Invalid channel %d",
1293 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1294 (MSG_host_self(), channel),
1299 /** \ingroup msg_deprecated_functions
1300 * \brief Listen on \a channel and waits for receiving a task from \a host.
1302 * It takes three parameters.
1303 * \param task a memory location for storing a #msg_task_t. It will
1304 hold a task when this function will return. Thus \a task should not
1305 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1306 those two condition does not hold, there will be a warning message.
1307 * \param channel the channel on which the process should be
1308 listening. This value has to be >=0 and < than the maximal
1309 number of channels fixed with MSG_set_channel_number().
1310 * \param host the host that is to be watched.
1311 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1314 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1316 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1317 return MSG_task_get_ext(task, channel, -1, host);
1320 /** \ingroup msg_deprecated_functions
1321 * \brief Listen on a channel and wait for receiving a task.
1323 * It takes two parameters.
1324 * \param task a memory location for storing a #msg_task_t. It will
1325 hold a task when this function will return. Thus \a task should not
1326 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1327 those two condition does not hold, there will be a warning message.
1328 * \param channel the channel on which the process should be
1329 listening. This value has to be >=0 and < than the maximal
1330 number of channels fixed with MSG_set_channel_number().
1331 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1333 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1335 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1336 return MSG_task_get_with_timeout(task, channel, -1);
1339 /** \ingroup msg_deprecated_functions
1340 * \brief Listen on a channel and wait for receiving a task with a timeout.
1342 * It takes three parameters.
1343 * \param task a memory location for storing a #msg_task_t. It will
1344 hold a task when this function will return. Thus \a task should not
1345 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1346 those two condition does not hold, there will be a warning message.
1347 * \param channel the channel on which the process should be
1348 listening. This value has to be >=0 and < than the maximal
1349 number of channels fixed with MSG_set_channel_number().
1350 * \param max_duration the maximum time to wait for a task before giving
1351 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1352 will not be modified and will still be
1353 equal to \c NULL when returning.
1354 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1357 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1358 double max_duration)
1360 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1361 return MSG_task_get_ext(task, channel, max_duration, NULL);
1365 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1368 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1369 xbt_assert((channel >= 0)
1370 && (channel < msg_global->max_channel), "Invalid channel %d",
1374 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1375 (MSG_host_self(), channel), task, host,