1 /* Copyright (c) 2004-2012. The SimGrid Team. All rights reserved. */
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. */
6 #include "msg_private.h"
7 #include "msg_mailbox.h"
10 #include "xbt/sysdep.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
13 "Logging specific to MSG (gos)");
15 /** \ingroup msg_task_usage
16 * \brief Executes a task and waits for its termination.
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
24 msg_error_t MSG_task_execute(msg_task_t task)
26 return MSG_parallel_task_execute(task);
29 /** \ingroup msg_task_usage
30 * \brief Executes a parallel task and waits for its termination.
32 * \param task a #msg_task_t to execute on the location on which the process is running.
34 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
35 * or #MSG_HOST_FAILURE otherwise
37 msg_error_t MSG_parallel_task_execute(msg_task_t task)
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;
47 TRACE_msg_task_execute_start(task);
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);
54 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
56 if (simdata->computation_amount == 0 && !simdata->host_nb) {
58 TRACE_msg_task_execute_end(task);
68 if (simdata->host_nb > 0) {
69 simdata->compute = simcall_host_parallel_execute(task->name,
75 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
77 simdata->compute = simcall_host_execute(task->name,
79 simdata->computation_amount,
84 simcall_set_category(simdata->compute, task->category);
86 p_simdata->waiting_action = simdata->compute;
87 comp_state = simcall_host_execution_wait(simdata->compute);
89 p_simdata->waiting_action = NULL;
93 XBT_DEBUG("Execution task '%s' finished in state %d",
94 task->name, (int)comp_state);
99 status = MSG_TASK_CANCELED;
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;
112 TRACE_msg_task_execute_end(task);
119 /** \ingroup msg_task_usage
120 * \brief Sleep for the specified number of seconds
122 * Makes the current process sleep until \a time seconds have elapsed.
124 * \param nb_sec a number of second
126 msg_error_t MSG_process_sleep(double nb_sec)
129 msg_error_t status = MSG_OK;
130 /*msg_process_t proc = MSG_process_self();*/
133 TRACE_msg_process_sleep_in(MSG_process_self());
136 /* create action to sleep */
138 /*proc->simdata->waiting_action = act_sleep;
140 FIXME: check if not setting the waiting_action breaks something on msg
142 proc->simdata->waiting_action = NULL;*/
145 simcall_process_sleep(nb_sec);
148 switch (e.category) {
150 status = MSG_TASK_CANCELED;
159 TRACE_msg_process_sleep_out(MSG_process_self());
164 /** \ingroup msg_task_usage
165 * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
167 * Sorry, this function is not supported anymore. That wouldn't be
168 * impossible to reimplement it, but we are lacking the time to do so ourselves.
169 * If you need this functionality, you can either:
171 * - implement the buffering mechanism on the user-level by queuing all messages
172 * received in the mailbox that do not match your expectation
173 * - change your application logic to leverage the mailboxes features. For example,
174 * if you have A receiving messages from B and C, you could have A waiting on
175 * mailbox "A" most of the time, but on "A#B" when it's waiting for specific
176 * messages from B and "A#C" when waiting for messages from C. You could even get A
177 * sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
178 * an example of use of this function in the @ref MSG_examples section.
179 * - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
180 * very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
181 * simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
182 * messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
183 * and that your filtering function will receive as first parameter, and then, the filter could
184 * simply compare the host names, for example. After sufficient testing, provide an example that
185 * we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
187 * \param task a memory location for storing a #msg_task_t.
188 * \param alias name of the mailbox to receive the task from
189 * \param host a #msg_host_t host from where the task was sent
192 * #MSG_OK if the task was successfully received,
193 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
196 MSG_task_receive_from_host(msg_task_t * task, const char *alias,
199 return MSG_task_receive_ext(task, alias, -1, host);
202 /** \ingroup msg_task_usage
203 * \brief Receives a task from a mailbox.
205 * This is a blocking function, the execution flow will be blocked
206 * until the task is received. See #MSG_task_irecv
207 * for receiving tasks asynchronously.
209 * \param task a memory location for storing a #msg_task_t.
210 * \param alias name of the mailbox to receive the task from
213 * #MSG_OK if the task was successfully received,
214 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
216 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
218 return MSG_task_receive_with_timeout(task, alias, -1);
221 /** \ingroup msg_task_usage
222 * \brief Receives a task from a mailbox with a given timeout.
224 * This is a blocking function with a timeout, the execution flow will be blocked
225 * until the task is received or the timeout is achieved. See #MSG_task_irecv
226 * for receiving tasks asynchronously. You can provide a -1 timeout
227 * to obtain an infinite timeout.
229 * \param task a memory location for storing a #msg_task_t.
230 * \param alias name of the mailbox to receive the task from
231 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
234 * #MSG_OK if the task was successfully received,
235 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
238 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
241 return MSG_task_receive_ext(task, alias, timeout, NULL);
244 /** \ingroup msg_task_usage
245 * \brief Receives a task from a mailbox from a specific host with a given timeout.
247 * This is a blocking function with a timeout, the execution flow will be blocked
248 * until the task is received or the timeout is achieved. See #MSG_task_irecv
249 * for receiving tasks asynchronously. You can provide a -1 timeout
250 * to obtain an infinite timeout.
252 * \param task a memory location for storing a #msg_task_t.
253 * \param alias name of the mailbox to receive the task from
254 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
255 * \param host a #msg_host_t host from where the task was sent
258 * #MSG_OK if the task was successfully received,
259 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
262 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
268 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
271 ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
275 switch (e.category) {
276 case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */
277 ret = MSG_HOST_FAILURE;
287 /** \ingroup msg_task_usage
288 * \brief Sends a task on a mailbox.
290 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
291 * to end the communication.
293 * \param task a #msg_task_t to send on another location.
294 * \param alias name of the mailbox to sent the task to
295 * \return the msg_comm_t communication created
297 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
299 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
302 /** \ingroup msg_task_usage
303 * \brief Sends a task on a mailbox with a maximum rate
305 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
306 * to end the communication. The maxrate parameter allows the application
307 * to limit the bandwidth utilization of network links when sending the task.
309 * \param task a #msg_task_t to send on another location.
310 * \param alias name of the mailbox to sent the task to
311 * \param maxrate the maximum communication rate for sending this task .
312 * \return the msg_comm_t communication created
314 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
316 task->simdata->rate = maxrate;
317 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
321 /** \ingroup msg_task_usage
322 * \brief Sends a task on a mailbox, with support for matching requests
324 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
325 * to end the communication.
327 * \param task a #msg_task_t to send on another location.
328 * \param alias name of the mailbox to sent the task to
329 * \param match_fun boolean function which parameters are:
330 * - match_data_provided_here
331 * - match_data_provided_by_other_side_if_any
332 * - the_smx_action_describing_the_other_side
333 * \param match_data user provided data passed to match_fun
334 * \return the msg_comm_t communication created
336 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
337 int (*match_fun)(void*,void*, smx_action_t),
340 simdata_task_t t_simdata = NULL;
341 msg_process_t process = MSG_process_self();
342 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
345 int call_end = TRACE_msg_task_put_start(task);
348 /* Prepare the task to send */
349 t_simdata = task->simdata;
350 t_simdata->sender = process;
351 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
353 xbt_assert(t_simdata->isused == 0,
354 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
356 t_simdata->isused = 1;
357 t_simdata->comm = NULL;
358 msg_global->sent_msg++;
360 /* Send it by calling SIMIX network layer */
361 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
362 comm->task_sent = task;
363 comm->task_received = NULL;
364 comm->status = MSG_OK;
366 simcall_comm_isend(mailbox, t_simdata->message_size,
367 t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
368 t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
370 if (TRACE_is_enabled()) {
371 simcall_set_category(comm->s_comm, task->category);
377 TRACE_msg_task_put_end();
383 /** \ingroup msg_task_usage
384 * \brief Sends a task on a mailbox.
386 * This is a non blocking detached send function.
387 * Think of it as a best effort send. Keep in mind that the third parameter
388 * is only called if the communication fails. If the communication does work,
389 * it is responsibility of the receiver code to free anything related to
390 * the task, as usual. More details on this can be obtained on
391 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
392 * in the SimGrid-user mailing list archive.
394 * \param task a #msg_task_t to send on another location.
395 * \param alias name of the mailbox to sent the task to
396 * \param cleanup a function to destroy the task if the
397 * communication fails, e.g. MSG_task_destroy
398 * (if NULL, no function will be called)
400 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
402 simdata_task_t t_simdata = NULL;
403 msg_process_t process = MSG_process_self();
404 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
406 /* Prepare the task to send */
407 t_simdata = task->simdata;
408 t_simdata->sender = process;
409 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
411 xbt_assert(t_simdata->isused == 0,
412 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
414 t_simdata->isused = 1;
415 t_simdata->comm = NULL;
416 msg_global->sent_msg++;
419 int call_end = TRACE_msg_task_put_start(task);
422 /* Send it by calling SIMIX network layer */
423 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
424 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
425 t_simdata->comm = comm;
427 if (TRACE_is_enabled()) {
428 simcall_set_category(comm, task->category);
434 TRACE_msg_task_put_end();
439 /** \ingroup msg_task_usage
440 * \brief Sends a task on a mailbox with a maximal rate.
442 * This is a non blocking detached send function.
443 * Think of it as a best effort send. Keep in mind that the third parameter
444 * is only called if the communication fails. If the communication does work,
445 * it is responsibility of the receiver code to free anything related to
446 * the task, as usual. More details on this can be obtained on
447 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
448 * in the SimGrid-user mailing list archive.
450 * \param task a #msg_task_t to send on another location.
451 * \param alias name of the mailbox to sent the task to
452 * \param cleanup a function to destroy the task if the
453 * communication fails, e.g. MSG_task_destroy
454 * (if NULL, no function will be called)
455 * \param maxrate the maximum communication rate for sending this task
458 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
460 task->simdata->rate = maxrate;
462 simdata_task_t t_simdata = NULL;
463 msg_process_t process = MSG_process_self();
464 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
466 /* Prepare the task to send */
467 t_simdata = task->simdata;
468 t_simdata->sender = process;
469 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
471 xbt_assert(t_simdata->isused == 0,
472 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
474 t_simdata->isused = 1;
475 t_simdata->comm = NULL;
476 msg_global->sent_msg++;
479 int call_end = TRACE_msg_task_put_start(task);
482 /* Send it by calling SIMIX network layer */
483 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
484 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
485 t_simdata->comm = comm;
487 if (TRACE_is_enabled()) {
488 simcall_set_category(comm, task->category);
494 TRACE_msg_task_put_end();
498 /** \ingroup msg_task_usage
499 * \brief Starts listening for receiving a task from an asynchronous communication.
501 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
502 * to end the communication.
504 * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
505 * \param name of the mailbox to receive the task on
506 * \return the msg_comm_t communication created
508 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
510 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
512 /* FIXME: these functions are not traceable */
515 xbt_assert(task, "Null pointer for the task storage");
519 ("MSG_task_irecv() was asked to write in a non empty task struct.");
521 /* Try to receive it by calling SIMIX network layer */
522 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
523 comm->task_sent = NULL;
524 comm->task_received = task;
525 comm->status = MSG_OK;
526 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
531 /** \ingroup msg_task_usage
532 * \brief Checks whether a communication is done, and if yes, finalizes it.
533 * \param comm the communication to test
534 * \return TRUE if the communication is finished
535 * (but it may have failed, use MSG_comm_get_status() to know its status)
536 * or FALSE if the communication is not finished yet
537 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
539 int MSG_comm_test(msg_comm_t comm)
545 finished = simcall_comm_test(comm->s_comm);
547 if (finished && comm->task_received != NULL) {
548 /* I am the receiver */
549 (*comm->task_received)->simdata->isused = 0;
553 switch (e.category) {
555 comm->status = MSG_TRANSFER_FAILURE;
560 comm->status = MSG_TIMEOUT;
573 /** \ingroup msg_task_usage
574 * \brief This function checks if a communication is finished.
575 * \param comms a vector of communications
576 * \return the position of the finished communication if any
577 * (but it may have failed, use MSG_comm_get_status() to know its status),
578 * or -1 if none is finished
580 int MSG_comm_testany(xbt_dynar_t comms)
583 int finished_index = -1;
585 /* create the equivalent dynar with SIMIX objects */
586 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
589 xbt_dynar_foreach(comms, cursor, comm) {
590 xbt_dynar_push(s_comms, &comm->s_comm);
593 msg_error_t status = MSG_OK;
595 finished_index = simcall_comm_testany(s_comms);
598 switch (e.category) {
600 finished_index = e.value;
601 status = MSG_TRANSFER_FAILURE;
605 finished_index = e.value;
606 status = MSG_TIMEOUT;
614 xbt_dynar_free(&s_comms);
616 if (finished_index != -1) {
617 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
618 /* the communication is finished */
619 comm->status = status;
621 if (status == MSG_OK && comm->task_received != NULL) {
622 /* I am the receiver */
623 (*comm->task_received)->simdata->isused = 0;
627 return finished_index;
630 /** \ingroup msg_task_usage
631 * \brief Destroys a communication.
632 * \param comm the communication to destroy.
634 void MSG_comm_destroy(msg_comm_t comm)
639 /** \ingroup msg_task_usage
640 * \brief Wait for the completion of a communication.
642 * It takes two parameters.
643 * \param comm the communication to wait.
644 * \param timeout Wait until the communication terminates or the timeout
645 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
646 * \return msg_error_t
648 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
652 simcall_comm_wait(comm->s_comm, timeout);
654 if (comm->task_received != NULL) {
655 /* I am the receiver */
656 (*comm->task_received)->simdata->isused = 0;
659 /* FIXME: these functions are not traceable */
662 switch (e.category) {
664 comm->status = MSG_TRANSFER_FAILURE;
667 comm->status = MSG_TIMEOUT;
678 /** \ingroup msg_task_usage
679 * \brief This function is called by a sender and permit to wait for each communication
681 * \param comm a vector of communication
682 * \param nb_elem is the size of the comm vector
683 * \param timeout for each call of MSG_comm_wait
685 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
688 for (i = 0; i < nb_elem; i++) {
689 MSG_comm_wait(comm[i], timeout);
693 /** \ingroup msg_task_usage
694 * \brief This function waits for the first communication finished in a list.
695 * \param comms a vector of communications
696 * \return the position of the first finished communication
697 * (but it may have failed, use MSG_comm_get_status() to know its status)
699 int MSG_comm_waitany(xbt_dynar_t comms)
702 int finished_index = -1;
704 /* create the equivalent dynar with SIMIX objects */
705 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
708 xbt_dynar_foreach(comms, cursor, comm) {
709 xbt_dynar_push(s_comms, &comm->s_comm);
712 msg_error_t status = MSG_OK;
714 finished_index = simcall_comm_waitany(s_comms);
717 switch (e.category) {
719 finished_index = e.value;
720 status = MSG_TRANSFER_FAILURE;
724 finished_index = e.value;
725 status = MSG_TIMEOUT;
734 xbt_assert(finished_index != -1, "WaitAny returned -1");
735 xbt_dynar_free(&s_comms);
737 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
738 /* the communication is finished */
739 comm->status = status;
741 if (comm->task_received != NULL) {
742 /* I am the receiver */
743 (*comm->task_received)->simdata->isused = 0;
746 return finished_index;
750 * \ingroup msg_task_usage
751 * \brief Returns the error (if any) that occured during a finished communication.
752 * \param comm a finished communication
753 * \return the status of the communication, or #MSG_OK if no error occured
754 * during the communication
756 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
761 /** \ingroup msg_task_usage
762 * \brief Get a task (#msg_task_t) from a communication
764 * \param comm the communication where to get the task
765 * \return the task from the communication
767 msg_task_t MSG_comm_get_task(msg_comm_t comm)
769 xbt_assert(comm, "Invalid parameter");
771 return comm->task_received ? *comm->task_received : comm->task_sent;
775 * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
776 * \param comm the comm
777 * \param buff the data copied
778 * \param buff_size size of the buffer
780 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
783 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
785 // notify the user callback if any
786 if (msg_global->task_copy_callback) {
787 msg_task_t task = buff;
788 msg_global->task_copy_callback(task,
789 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
793 /** \ingroup msg_task_usage
794 * \brief Sends a task to a mailbox
796 * This is a blocking function, the execution flow will be blocked
797 * until the task is sent (and received in the other side if #MSG_task_receive is used).
798 * See #MSG_task_isend for sending tasks asynchronously.
800 * \param task the task to be sent
801 * \param alias the mailbox name to where the task is sent
803 * \return Returns #MSG_OK if the task was successfully sent,
804 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
806 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
808 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
809 return MSG_task_send_with_timeout(task, alias, -1);
812 /** \ingroup msg_task_usage
813 * \brief Sends a task to a mailbox with a maximum rate
815 * This is a blocking function, the execution flow will be blocked
816 * until the task is sent. The maxrate parameter allows the application
817 * to limit the bandwidth utilization of network links when sending the task.
819 * \param task the task to be sent
820 * \param alias the mailbox name to where the task is sent
821 * \param maxrate the maximum communication rate for sending this task
823 * \return Returns #MSG_OK if the task was successfully sent,
824 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
827 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
829 task->simdata->rate = maxrate;
830 return MSG_task_send(task, alias);
833 /** \ingroup msg_task_usage
834 * \brief Sends a task to a mailbox with a timeout
836 * This is a blocking function, the execution flow will be blocked
837 * until the task is sent or the timeout is achieved.
839 * \param task the task to be sent
840 * \param alias the mailbox name to where the task is sent
841 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
843 * \return Returns #MSG_OK if the task was successfully sent,
844 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
847 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
850 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
854 /** \ingroup msg_task_usage
855 * \brief Sends a task to a mailbox with a timeout and with a maximum rate
857 * This is a blocking function, the execution flow will be blocked
858 * until the task is sent or the timeout is achieved.
860 * \param task the task to be sent
861 * \param alias the mailbox name to where the task is sent
862 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
863 * \param maxrate the maximum communication rate for sending this task
865 * \return Returns #MSG_OK if the task was successfully sent,
866 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
869 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
870 double timeout, double maxrate)
872 task->simdata->rate = maxrate;
873 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
877 /** \ingroup msg_task_usage
878 * \brief Check if there is a communication going on in a mailbox.
880 * \param alias the name of the mailbox to be considered
882 * \return Returns 1 if there is a communication, 0 otherwise
884 int MSG_task_listen(const char *alias)
886 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
889 /** \ingroup msg_task_usage
890 * \brief Check the number of communication actions of a given host pending in a mailbox.
892 * \param alias the name of the mailbox to be considered
893 * \param host the host to check for communication
895 * \return Returns the number of pending communication actions of the host in the
896 * given mailbox, 0 if there is no pending communication actions.
899 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
902 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
906 /** \ingroup msg_task_usage
907 * \brief Look if there is a communication on a mailbox and return the
908 * PID of the sender process.
910 * \param alias the name of the mailbox to be considered
912 * \return Returns the PID of sender process,
913 * -1 if there is no communication in the mailbox.
915 int MSG_task_listen_from(const char *alias)
920 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
923 return MSG_process_get_PID(task->simdata->sender);
926 /** \ingroup msg_task_usage
927 * \brief Sets the tracing category of a task.
929 * This function should be called after the creation of
930 * a MSG task, to define the category of that task. The
931 * first parameter task must contain a task that was
932 * created with the function #MSG_task_create. The second
933 * parameter category must contain a category that was
934 * previously declared with the function #TRACE_category
935 * (or with #TRACE_category_with_color).
937 * See \ref tracing for details on how to trace
938 * the (categorized) resource utilization.
940 * \param task the task that is going to be categorized
941 * \param category the name of the category to be associated to the task
943 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
945 void MSG_task_set_category (msg_task_t task, const char *category)
948 TRACE_msg_set_task_category (task, category);
952 /** \ingroup msg_task_usage
954 * \brief Gets the current tracing category of a task.
956 * \param task the task to be considered
958 * \see MSG_task_set_category
960 * \return Returns the name of the tracing category of the given task, NULL otherwise
962 const char *MSG_task_get_category (msg_task_t task)
965 return task->category;
972 * \brief Returns the value of a given AS or router property
974 * \param asr the name of a router or AS
975 * \param name a property name
976 * \return value of a property (or NULL if property not set)
978 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
980 return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
984 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
987 * \param asr the name of a router or AS
988 * \return a dict containing the properties
990 xbt_dict_t MSG_as_router_get_properties(const char* asr)
992 return (simcall_asr_get_properties(asr));
996 * \brief Change the value of a given AS or router
998 * \param asr the name of a router or AS
999 * \param name a property name
1000 * \param value what to change the property to
1001 * \param free_ctn the freeing function to use to kill the value on need
1003 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1004 xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1007 #ifdef MSG_USE_DEPRECATED
1008 /** \ingroup msg_deprecated_functions
1010 * \brief Return the last value returned by a MSG function (except
1011 * MSG_get_errno...).
1013 msg_error_t MSG_get_errno(void)
1015 return PROCESS_GET_ERRNO();
1018 /** \ingroup msg_deprecated_functions
1019 * \brief Put a task on a channel of an host and waits for the end of the
1022 * This function is used for describing the behavior of a process. It
1023 * takes three parameter.
1024 * \param task a #msg_task_t to send on another location. This task
1025 will not be usable anymore when the function will return. There is
1026 no automatic task duplication and you have to save your parameters
1027 before calling this function. Tasks are unique and once it has been
1028 sent to another location, you should not access it anymore. You do
1029 not need to call MSG_task_destroy() but to avoid using, as an
1030 effect of inattention, this task anymore, you definitely should
1031 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1032 can be transfered iff it has been correctly created with
1034 * \param dest the destination of the message
1035 * \param channel the channel on which the process should put this
1036 task. This value has to be >=0 and < than the maximal number of
1037 channels fixed with MSG_set_channel_number().
1038 * \return #MSG_HOST_FAILURE if the host on which
1039 * this function was called was shut down,
1040 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1041 * (network failure, dest failure) or #MSG_OK if it succeeded.
1043 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1045 XBT_WARN("DEPRECATED! Now use MSG_task_send");
1046 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1049 /** \ingroup msg_deprecated_functions
1050 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1056 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1059 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1060 task->simdata->rate = maxrate;
1061 return MSG_task_put(task, dest, channel);
1064 /** \ingroup msg_deprecated_functions
1066 * \brief Put a task on a channel of an
1067 * host (with a timeout on the waiting of the destination host) and
1068 * waits for the end of the transmission.
1070 * This function is used for describing the behavior of a process. It
1071 * takes four parameter.
1072 * \param task a #msg_task_t to send on another location. This task
1073 will not be usable anymore when the function will return. There is
1074 no automatic task duplication and you have to save your parameters
1075 before calling this function. Tasks are unique and once it has been
1076 sent to another location, you should not access it anymore. You do
1077 not need to call MSG_task_destroy() but to avoid using, as an
1078 effect of inattention, this task anymore, you definitely should
1079 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1080 can be transfered iff it has been correctly created with
1082 * \param dest the destination of the message
1083 * \param channel the channel on which the process should put this
1084 task. This value has to be >=0 and < than the maximal number of
1085 channels fixed with MSG_set_channel_number().
1086 * \param timeout the maximum time to wait for a task before giving
1087 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1088 will not be modified
1089 * \return #MSG_HOST_FAILURE if the host on which
1090 this function was called was shut down,
1091 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1092 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1095 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1096 m_channel_t channel, double timeout)
1098 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1099 xbt_assert((channel >= 0)
1100 && (channel < msg_global->max_channel), "Invalid channel %d",
1103 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
1105 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1106 (dest, channel), task, timeout);
1109 /** \ingroup msg_deprecated_functions
1110 * \brief Test whether there is a pending communication on a channel, and who sent it.
1112 * It takes one parameter.
1113 * \param channel the channel on which the process should be
1114 listening. This value has to be >=0 and < than the maximal
1115 number of channels fixed with MSG_set_channel_number().
1116 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1118 int MSG_task_probe_from(m_channel_t channel)
1120 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1123 xbt_assert((channel >= 0)
1124 && (channel < msg_global->max_channel), "Invalid channel %d",
1129 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1130 (MSG_host_self(), channel))))
1133 return MSG_process_get_PID(task->simdata->sender);
1136 /** \ingroup msg_deprecated_functions
1137 * \brief Test whether there is a pending communication on a channel.
1139 * It takes one parameter.
1140 * \param channel the channel on which the process should be
1141 listening. This value has to be >=0 and < than the maximal
1142 number of channels fixed with MSG_set_channel_number().
1143 * \return 1 if there is a pending communication and 0 otherwise
1145 int MSG_task_Iprobe(m_channel_t channel)
1147 XBT_WARN("DEPRECATED!");
1148 xbt_assert((channel >= 0)
1149 && (channel < msg_global->max_channel), "Invalid channel %d",
1153 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1154 (MSG_host_self(), channel));
1157 /** \ingroup msg_deprecated_functions
1159 * \brief Return the number of tasks waiting to be received on a \a
1160 channel and sent by \a host.
1162 * It takes two parameters.
1163 * \param channel the channel on which the process should be
1164 listening. This value has to be >=0 and < than the maximal
1165 number of channels fixed with MSG_set_channel_number().
1166 * \param host the host that is to be watched.
1167 * \return the number of tasks waiting to be received on \a channel
1168 and sent by \a host.
1170 int MSG_task_probe_from_host(int channel, msg_host_t host)
1172 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1173 xbt_assert((channel >= 0)
1174 && (channel < msg_global->max_channel), "Invalid channel %d",
1178 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1179 (MSG_host_self(), channel),
1184 /** \ingroup msg_deprecated_functions
1185 * \brief Listen on \a channel and waits for receiving a task from \a host.
1187 * It takes three parameters.
1188 * \param task a memory location for storing a #msg_task_t. It will
1189 hold a task when this function will return. Thus \a task should not
1190 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1191 those two condition does not hold, there will be a warning message.
1192 * \param channel the channel on which the process should be
1193 listening. This value has to be >=0 and < than the maximal
1194 number of channels fixed with MSG_set_channel_number().
1195 * \param host the host that is to be watched.
1196 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1199 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1201 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1202 return MSG_task_get_ext(task, channel, -1, host);
1205 /** \ingroup msg_deprecated_functions
1206 * \brief Listen on a channel and wait for receiving a task.
1208 * It takes two parameters.
1209 * \param task a memory location for storing a #msg_task_t. It will
1210 hold a task when this function will return. Thus \a task should not
1211 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1212 those two condition does not hold, there will be a warning message.
1213 * \param channel the channel on which the process should be
1214 listening. This value has to be >=0 and < than the maximal
1215 number of channels fixed with MSG_set_channel_number().
1216 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1218 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1220 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1221 return MSG_task_get_with_timeout(task, channel, -1);
1224 /** \ingroup msg_deprecated_functions
1225 * \brief Listen on a channel and wait for receiving a task with a timeout.
1227 * It takes three parameters.
1228 * \param task a memory location for storing a #msg_task_t. It will
1229 hold a task when this function will return. Thus \a task should not
1230 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1231 those two condition does not hold, there will be a warning message.
1232 * \param channel the channel on which the process should be
1233 listening. This value has to be >=0 and < than the maximal
1234 number of channels fixed with MSG_set_channel_number().
1235 * \param max_duration the maximum time to wait for a task before giving
1236 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1237 will not be modified and will still be
1238 equal to \c NULL when returning.
1239 * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1242 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1243 double max_duration)
1245 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1246 return MSG_task_get_ext(task, channel, max_duration, NULL);
1250 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1253 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1254 xbt_assert((channel >= 0)
1255 && (channel < msg_global->max_channel), "Invalid channel %d",
1259 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1260 (MSG_host_self(), channel), task, host,