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 #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
24 MSG_error_t MSG_task_execute(m_task_t task)
27 simdata_task_t simdata = NULL;
28 simdata_process_t p_simdata;
29 e_smx_state_t comp_state;
31 simdata = task->simdata;
33 xbt_assert(simdata->host_nb == 0,
34 "This is a parallel task. Go to hell.");
37 TRACE_msg_task_execute_start(task);
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);
44 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
46 if (simdata->computation_amount == 0) {
48 TRACE_msg_task_execute_end(task);
53 m_process_t self = SIMIX_process_self();
54 p_simdata = SIMIX_process_self_get_data(self);
57 simcall_host_execute(task->name, p_simdata->m_host->smx_host,
58 simdata->computation_amount,
61 simcall_set_category(simdata->compute, task->category);
64 p_simdata->waiting_action = simdata->compute;
66 comp_state = simcall_host_execution_wait(simdata->compute);
67 p_simdata->waiting_action = NULL;
71 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
73 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
74 simdata->computation_amount = 0.0;
76 simdata->compute = NULL;
78 TRACE_msg_task_execute_end(task);
85 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
87 simdata->compute = NULL;
89 TRACE_msg_task_execute_end(task);
91 MSG_RETURN(MSG_HOST_FAILURE);
94 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
96 simdata->compute = NULL;
98 TRACE_msg_task_execute_end(task);
100 MSG_RETURN(MSG_TASK_CANCELED);
109 /** \ingroup m_task_management
110 * \brief Creates a new #m_task_t (a parallel one....).
112 * A constructor for #m_task_t taking six arguments and returning the
113 corresponding object.
114 * \param name a name for the object. It is for user-level information
116 * \param host_nb the number of hosts implied in the parallel task.
117 * \param host_list an array of \p host_nb m_host_t.
118 * \param computation_amount an array of \p host_nb
119 doubles. computation_amount[i] is the total number of operations
120 that have to be performed on host_list[i].
121 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
122 * \param data a pointer to any data may want to attach to the new
123 object. It is for user-level information and can be NULL. It can
124 be retrieved with the function \ref MSG_task_get_data.
126 * \return The new corresponding object.
129 MSG_parallel_task_create(const char *name, int host_nb,
130 const m_host_t * host_list,
131 double *computation_amount,
132 double *communication_amount, void *data)
135 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
136 m_task_t task = xbt_new0(s_m_task_t, 1);
137 task->simdata = simdata;
140 task->name = xbt_strdup(name);
144 simdata->computation_amount = 0;
145 simdata->message_size = 0;
146 simdata->compute = NULL;
147 simdata->comm = NULL;
148 simdata->rate = -1.0;
150 simdata->sender = NULL;
151 simdata->receiver = NULL;
152 simdata->source = NULL;
154 simdata->host_nb = host_nb;
155 simdata->host_list = xbt_new0(smx_host_t, host_nb);
156 simdata->comp_amount = computation_amount;
157 simdata->comm_amount = communication_amount;
159 for (i = 0; i < host_nb; i++)
160 simdata->host_list[i] = host_list[i]->smx_host;
165 /** \ingroup msg_task_usage
166 * \brief Executes a parallel task and waits for its termination.
168 * \param task a #m_task_t to execute on the location on which the process is running.
170 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
171 * or #MSG_HOST_FAILURE otherwise
173 MSG_error_t MSG_parallel_task_execute(m_task_t task)
175 simdata_task_t simdata = NULL;
176 e_smx_state_t comp_state;
177 simdata_process_t p_simdata;
179 simdata = task->simdata;
180 p_simdata = SIMIX_process_self_get_data(SIMIX_process_self());
182 xbt_assert((!simdata->compute)
183 && (task->simdata->isused == 0),
184 "This task is executed somewhere else. Go fix your code!");
186 xbt_assert(simdata->host_nb,
187 "This is not a parallel task. Go to hell.");
189 XBT_DEBUG("Parallel computing on %s", SIMIX_host_get_name(p_simdata->m_host->smx_host));
194 simcall_host_parallel_execute(task->name, simdata->host_nb,
196 simdata->comp_amount,
197 simdata->comm_amount, 1.0, -1.0);
198 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
200 p_simdata->waiting_action = simdata->compute;
201 comp_state = simcall_host_execution_wait(simdata->compute);
202 p_simdata->waiting_action = NULL;
204 XBT_DEBUG("Finished waiting for execution of action %p, state = %d", simdata->compute, (int)comp_state);
208 if (comp_state == SIMIX_DONE) {
209 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
210 simdata->computation_amount = 0.0;
211 simdata->comm = NULL;
212 simdata->compute = NULL;
214 } else if (simcall_host_get_state(SIMIX_host_self()) == 0) {
215 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
216 simdata->comm = NULL;
217 simdata->compute = NULL;
218 MSG_RETURN(MSG_HOST_FAILURE);
220 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
221 simdata->comm = NULL;
222 simdata->compute = NULL;
223 MSG_RETURN(MSG_TASK_CANCELED);
228 /** \ingroup msg_task_usage
229 * \brief Sleep for the specified number of seconds
231 * Makes the current process sleep until \a time seconds have elapsed.
233 * \param nb_sec a number of second
235 MSG_error_t MSG_process_sleep(double nb_sec)
238 /*m_process_t proc = MSG_process_self();*/
241 TRACE_msg_process_sleep_in(MSG_process_self());
244 /* create action to sleep */
246 /*proc->simdata->waiting_action = act_sleep;
248 FIXME: check if not setting the waiting_action breaks something on msg
250 proc->simdata->waiting_action = NULL;*/
253 simcall_process_sleep(nb_sec);
256 switch (e.category) {
259 TRACE_msg_process_sleep_out(MSG_process_self());
261 MSG_RETURN(MSG_HOST_FAILURE);
268 TRACE_msg_process_sleep_out(MSG_process_self());
273 /** \ingroup msg_task_usage
274 * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
276 * Sorry, this function is not supported anymore. That wouldn't be
277 * impossible to reimplement it, but we are lacking the time to do so ourselves.
278 * If you need this functionality, you can either:
280 * - implement the buffering mechanism on the user-level by queuing all messages
281 * received in the mailbox that do not match your expectation
282 * - change your application logic to leverage the mailboxes features. For example,
283 * if you have A receiving messages from B and C, you could have A waiting on
284 * mailbox "A" most of the time, but on "A#B" when it's waiting for specific
285 * messages from B and "A#C" when waiting for messages from C. You could even get A
286 * sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
287 * an example of use of this function in the @ref MSG_examples section.
288 * - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
289 * very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
290 * simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
291 * messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
292 * and that your filtering function will receive as first parameter, and then, the filter could
293 * simply compare the host names, for example. After sufficient testing, provide an example that
294 * we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
296 * \param task a memory location for storing a #m_task_t.
297 * \param alias name of the mailbox to receive the task from
298 * \param host a #m_host_t host from where the task was sent
301 * #MSG_OK if the task was successfully received,
302 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
305 MSG_task_receive_from_host(m_task_t * task, const char *alias,
308 return MSG_task_receive_ext(task, alias, -1, host);
311 /** \ingroup msg_task_usage
312 * \brief Receives a task from a mailbox.
314 * This is a blocking function, the execution flow will be blocked
315 * until the task is received. See #MSG_task_irecv
316 * for receiving tasks asynchronously.
318 * \param task a memory location for storing a #m_task_t.
319 * \param alias name of the mailbox to receive the task from
322 * #MSG_OK if the task was successfully received,
323 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
325 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
327 return MSG_task_receive_with_timeout(task, alias, -1);
330 /** \ingroup msg_task_usage
331 * \brief Receives a task from a mailbox with a given timeout.
333 * This is a blocking function with a timeout, the execution flow will be blocked
334 * until the task is received or the timeout is achieved. See #MSG_task_irecv
335 * for receiving tasks asynchronously. You can provide a -1 timeout
336 * to obtain an infinite timeout.
338 * \param task a memory location for storing a #m_task_t.
339 * \param alias name of the mailbox to receive the task from
340 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
343 * #MSG_OK if the task was successfully received,
344 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
347 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
350 return MSG_task_receive_ext(task, alias, timeout, NULL);
353 /** \ingroup msg_task_usage
354 * \brief Receives a task from a mailbox from a specific host with a given timeout.
356 * This is a blocking function with a timeout, the execution flow will be blocked
357 * until the task is received or the timeout is achieved. See #MSG_task_irecv
358 * for receiving tasks asynchronously. You can provide a -1 timeout
359 * to obtain an infinite timeout.
361 * \param task a memory location for storing a #m_task_t.
362 * \param alias name of the mailbox to receive the task from
363 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
364 * \param host a #m_host_t host from where the task was sent
367 * #MSG_OK if the task was successfully received,
368 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
371 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
375 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
377 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
381 /** \ingroup msg_task_usage
382 * \brief Sends a task on a mailbox.
384 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
385 * to end the communication.
387 * \param task a #m_task_t to send on another location.
388 * \param alias name of the mailbox to sent the task to
389 * \return the msg_comm_t communication created
391 msg_comm_t MSG_task_isend(m_task_t task, const char *alias)
393 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
396 /** \ingroup msg_task_usage
397 * \brief Sends a task on a mailbox, with support for matching requests
399 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
400 * to end the communication.
402 * \param task a #m_task_t to send on another location.
403 * \param alias name of the mailbox to sent the task to
404 * \param match_fun boolean function which parameters are:
405 * - match_data_provided_here
406 * - match_data_provided_by_other_side_if_any
407 * - the_smx_action_describing_the_other_side
408 * \param match_data user provided data passed to match_fun
409 * \return the msg_comm_t communication created
411 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(m_task_t task, const char *alias,
412 int (*match_fun)(void*,void*, smx_action_t),
415 simdata_task_t t_simdata = NULL;
416 m_process_t process = MSG_process_self();
417 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
419 /* FIXME: these functions are not traceable */
421 /* Prepare the task to send */
422 t_simdata = task->simdata;
423 t_simdata->sender = process;
424 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
426 xbt_assert(t_simdata->isused == 0,
427 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
429 t_simdata->isused = 1;
430 t_simdata->comm = NULL;
431 msg_global->sent_msg++;
433 /* Send it by calling SIMIX network layer */
434 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
435 comm->task_sent = task;
436 comm->task_received = NULL;
437 comm->status = MSG_OK;
439 simcall_comm_isend(mailbox, t_simdata->message_size,
440 t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
441 t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
446 /** \ingroup msg_task_usage
447 * \brief Sends a task on a mailbox.
449 * This is a non blocking detached send function.
450 * Think of it as a best effort send. Keep in mind that the third parameter
451 * is only called if the communication fails. If the communication does work,
452 * it is responsibility of the receiver code to free anything related to
453 * the task, as usual. More details on this can be obtained on
454 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
455 * in the SimGrid-user mailing list archive.
457 * \param task a #m_task_t to send on another location.
458 * \param alias name of the mailbox to sent the task to
459 * \param cleanup a function to destroy the task if the
460 * communication fails, e.g. MSG_task_destroy
461 * (if NULL, no function will be called)
463 void MSG_task_dsend(m_task_t task, const char *alias, void_f_pvoid_t cleanup)
465 simdata_task_t t_simdata = NULL;
466 m_process_t process = MSG_process_self();
467 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
469 /* FIXME: these functions are not traceable */
471 /* Prepare the task to send */
472 t_simdata = task->simdata;
473 t_simdata->sender = process;
474 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
476 xbt_assert(t_simdata->isused == 0,
477 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
479 t_simdata->isused = 1;
480 t_simdata->comm = NULL;
481 msg_global->sent_msg++;
483 /* Send it by calling SIMIX network layer */
484 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
485 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
486 t_simdata->comm = comm;
489 /** \ingroup msg_task_usage
490 * \brief Starts listening for receiving a task from an asynchronous communication.
492 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
493 * to end the communication.
495 * \param task a memory location for storing a #m_task_t. has to be valid until the end of the communication.
496 * \param name of the mailbox to receive the task on
497 * \return the msg_comm_t communication created
499 msg_comm_t MSG_task_irecv(m_task_t *task, const char *name)
501 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
503 /* FIXME: these functions are not traceable */
506 xbt_assert(task, "Null pointer for the task storage");
510 ("MSG_task_irecv() was asked to write in a non empty task struct.");
512 /* Try to receive it by calling SIMIX network layer */
513 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
514 comm->task_sent = NULL;
515 comm->task_received = task;
516 comm->status = MSG_OK;
517 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
522 /** \ingroup msg_task_usage
523 * \brief Checks whether a communication is done, and if yes, finalizes it.
524 * \param comm the communication to test
525 * \return TRUE if the communication is finished
526 * (but it may have failed, use MSG_comm_get_status() to know its status)
527 * or FALSE if the communication is not finished yet
528 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
530 int MSG_comm_test(msg_comm_t comm)
535 finished = simcall_comm_test(comm->s_comm);
537 if (finished && comm->task_received != NULL) {
538 /* I am the receiver */
539 (*comm->task_received)->simdata->isused = 0;
543 switch (e.category) {
546 comm->status = MSG_HOST_FAILURE;
551 comm->status = MSG_TRANSFER_FAILURE;
556 comm->status = MSG_TIMEOUT;
569 /** \ingroup msg_task_usage
570 * \brief This function checks if a communication is finished.
571 * \param comms a vector of communications
572 * \return the position of the finished communication if any
573 * (but it may have failed, use MSG_comm_get_status() to know its status),
574 * or -1 if none is finished
576 int MSG_comm_testany(xbt_dynar_t comms)
579 int finished_index = -1;
581 /* create the equivalent dynar with SIMIX objects */
582 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
585 xbt_dynar_foreach(comms, cursor, comm) {
586 xbt_dynar_push(s_comms, &comm->s_comm);
589 MSG_error_t status = MSG_OK;
591 finished_index = simcall_comm_testany(s_comms);
594 switch (e.category) {
597 finished_index = e.value;
598 status = MSG_HOST_FAILURE;
602 finished_index = e.value;
603 status = MSG_TRANSFER_FAILURE;
607 finished_index = e.value;
608 status = MSG_TIMEOUT;
616 xbt_dynar_free(&s_comms);
618 if (finished_index != -1) {
619 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
620 /* the communication is finished */
621 comm->status = status;
623 if (status == MSG_OK && comm->task_received != NULL) {
624 /* I am the receiver */
625 (*comm->task_received)->simdata->isused = 0;
629 return finished_index;
632 /** \ingroup msg_task_usage
633 * \brief Destroys a communication.
634 * \param comm the communication to destroy.
636 void MSG_comm_destroy(msg_comm_t comm)
641 /** \ingroup msg_task_usage
642 * \brief Wait for the completion of a communication.
644 * It takes two parameters.
645 * \param comm the communication to wait.
646 * \param timeout Wait until the communication terminates or the timeout
647 * occurs. You can provide a -1 timeout to obtain an infinite timeout.
648 * \return MSG_error_t
650 MSG_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
654 simcall_comm_wait(comm->s_comm, timeout);
656 if (comm->task_received != NULL) {
657 /* I am the receiver */
658 (*comm->task_received)->simdata->isused = 0;
661 /* FIXME: these functions are not traceable */
664 switch (e.category) {
666 comm->status = MSG_HOST_FAILURE;
669 comm->status = MSG_TRANSFER_FAILURE;
672 comm->status = MSG_TIMEOUT;
683 /** \ingroup msg_task_usage
684 * \brief This function is called by a sender and permit to wait for each communication
686 * \param comm a vector of communication
687 * \param nb_elem is the size of the comm vector
688 * \param timeout for each call of MSG_comm_wait
690 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
693 for (i = 0; i < nb_elem; i++) {
694 MSG_comm_wait(comm[i], timeout);
698 /** \ingroup msg_task_usage
699 * \brief This function waits for the first communication finished in a list.
700 * \param comms a vector of communications
701 * \return the position of the first finished communication
702 * (but it may have failed, use MSG_comm_get_status() to know its status)
704 int MSG_comm_waitany(xbt_dynar_t comms)
707 int finished_index = -1;
709 /* create the equivalent dynar with SIMIX objects */
710 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
713 xbt_dynar_foreach(comms, cursor, comm) {
714 xbt_dynar_push(s_comms, &comm->s_comm);
717 MSG_error_t status = MSG_OK;
719 finished_index = simcall_comm_waitany(s_comms);
722 switch (e.category) {
725 finished_index = e.value;
726 status = MSG_HOST_FAILURE;
730 finished_index = e.value;
731 status = MSG_TRANSFER_FAILURE;
735 finished_index = e.value;
736 status = MSG_TIMEOUT;
745 xbt_assert(finished_index != -1, "WaitAny returned -1");
746 xbt_dynar_free(&s_comms);
748 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
749 /* the communication is finished */
750 comm->status = status;
752 if (comm->task_received != NULL) {
753 /* I am the receiver */
754 (*comm->task_received)->simdata->isused = 0;
757 return finished_index;
761 * \ingroup msg_task_usage
762 * \brief Returns the error (if any) that occured during a finished communication.
763 * \param comm a finished communication
764 * \return the status of the communication, or #MSG_OK if no error occured
765 * during the communication
767 MSG_error_t MSG_comm_get_status(msg_comm_t comm) {
772 /** \ingroup msg_task_usage
773 * \brief Get a task (#m_task_t) from a communication
775 * \param comm the communication where to get the task
776 * \return the task from the communication
778 m_task_t MSG_comm_get_task(msg_comm_t comm)
780 xbt_assert(comm, "Invalid parameter");
782 return comm->task_received ? *comm->task_received : comm->task_sent;
786 * \brief This function is called by SIMIX to copy the data of a comm.
787 * \param comm the comm
788 * \param buff the data copied
789 * \param buff_size size of the buffer
791 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
794 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
796 // notify the user callback if any
797 if (msg_global->task_copy_callback) {
798 m_task_t task = buff;
799 msg_global->task_copy_callback(task,
800 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
804 /** \ingroup msg_task_usage
805 * \brief Sends a task to a mailbox
807 * This is a blocking function, the execution flow will be blocked
808 * until the task is sent (and received in the other side if #MSG_task_receive is used).
809 * See #MSG_task_isend for sending tasks asynchronously.
811 * \param task the task to be sent
812 * \param alias the mailbox name to where the task is sent
814 * \return Returns #MSG_OK if the task was successfully sent,
815 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
817 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
819 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
820 return MSG_task_send_with_timeout(task, alias, -1);
823 /** \ingroup msg_task_usage
824 * \brief Sends a task to a mailbox with a maximum rate
826 * This is a blocking function, the execution flow will be blocked
827 * until the task is sent. The maxrate parameter allows the application
828 * to limit the bandwidth utilization of network links when sending the task.
830 * \param task the task to be sent
831 * \param alias the mailbox name to where the task is sent
832 * \param maxrate the maximum communication rate for sending this task
834 * \return Returns #MSG_OK if the task was successfully sent,
835 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
838 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
840 task->simdata->rate = maxrate;
841 return MSG_task_send(task, alias);
844 /** \ingroup msg_task_usage
845 * \brief Sends a task to a mailbox with a timeout
847 * This is a blocking function, the execution flow will be blocked
848 * until the task is sent or the timeout is achieved.
850 * \param task the task to be sent
851 * \param alias the mailbox name to where the task is sent
852 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
854 * \return Returns #MSG_OK if the task was successfully sent,
855 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
858 MSG_task_send_with_timeout(m_task_t task, const char *alias,
861 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
865 /** \ingroup msg_task_usage
866 * \brief Check if there is a communication going on in a mailbox.
868 * \param alias the name of the mailbox to be considered
870 * \return Returns 1 if there is a communication, 0 otherwise
872 int MSG_task_listen(const char *alias)
874 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
877 /** \ingroup msg_task_usage
878 * \brief Check the number of communication actions of a given host pending in a mailbox.
880 * \param alias the name of the mailbox to be considered
881 * \param host the host to check for communication
883 * \return Returns the number of pending communication actions of the host in the
884 * given mailbox, 0 if there is no pending communication actions.
887 int MSG_task_listen_from_host(const char *alias, m_host_t host)
890 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
894 /** \ingroup msg_task_usage
895 * \brief Look if there is a communication on a mailbox and return the
896 * PID of the sender process.
898 * \param alias the name of the mailbox to be considered
900 * \return Returns the PID of sender process,
901 * -1 if there is no communication in the mailbox.
903 int MSG_task_listen_from(const char *alias)
908 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
911 return MSG_process_get_PID(task->simdata->sender);
914 /** \ingroup msg_task_usage
915 * \brief Sets the tracing category of a task.
917 * This function should be called after the creation of
918 * a MSG task, to define the category of that task. The
919 * first parameter task must contain a task that was
920 * created with the function #MSG_task_create. The second
921 * parameter category must contain a category that was
922 * previously declared with the function #TRACE_category
923 * (or with #TRACE_category_with_color).
925 * See \ref tracing_tracing for details on how to trace
926 * the (categorized) resource utilization.
928 * \param task the task that is going to be categorized
929 * \param category the name of the category to be associated to the task
931 * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
933 void MSG_task_set_category (m_task_t task, const char *category)
936 TRACE_msg_set_task_category (task, category);
940 /** \ingroup msg_task_usage
942 * \brief Gets the current tracing category of a task.
944 * \param task the task to be considered
946 * \see MSG_task_set_category
948 * \return Returns the name of the tracing category of the given task, NULL otherwise
950 const char *MSG_task_get_category (m_task_t task)
953 return task->category;
959 #ifdef MSG_USE_DEPRECATED
960 /** \ingroup msg_deprecated_functions
962 * \brief Return the last value returned by a MSG function (except
965 MSG_error_t MSG_get_errno(void)
967 return PROCESS_GET_ERRNO();
970 /** \ingroup msg_deprecated_functions
971 * \brief Put a task on a channel of an host and waits for the end of the
974 * This function is used for describing the behavior of a process. It
975 * takes three parameter.
976 * \param task a #m_task_t to send on another location. This task
977 will not be usable anymore when the function will return. There is
978 no automatic task duplication and you have to save your parameters
979 before calling this function. Tasks are unique and once it has been
980 sent to another location, you should not access it anymore. You do
981 not need to call MSG_task_destroy() but to avoid using, as an
982 effect of inattention, this task anymore, you definitely should
983 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
984 can be transfered iff it has been correctly created with
986 * \param dest the destination of the message
987 * \param channel the channel on which the process should put this
988 task. This value has to be >=0 and < than the maximal number of
989 channels fixed with MSG_set_channel_number().
990 * \return #MSG_HOST_FAILURE if the host on which
991 * this function was called was shut down,
992 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
993 * (network failure, dest failure) or #MSG_OK if it succeeded.
995 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
997 XBT_WARN("DEPRECATED! Now use MSG_task_send");
998 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1001 /** \ingroup msg_deprecated_functions
1002 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1008 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
1011 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1012 task->simdata->rate = maxrate;
1013 return MSG_task_put(task, dest, channel);
1016 /** \ingroup msg_deprecated_functions
1018 * \brief Put a task on a channel of an
1019 * host (with a timeout on the waiting of the destination host) and
1020 * waits for the end of the transmission.
1022 * This function is used for describing the behavior of a process. It
1023 * takes four parameter.
1024 * \param task a #m_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 * \param timeout the maximum time to wait for a task before giving
1039 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1040 will not be modified
1041 * \return #MSG_HOST_FAILURE if the host on which
1042 this function was called was shut down,
1043 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1044 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1047 MSG_task_put_with_timeout(m_task_t task, m_host_t dest,
1048 m_channel_t channel, double timeout)
1050 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1051 xbt_assert((channel >= 0)
1052 && (channel < msg_global->max_channel), "Invalid channel %d",
1055 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
1057 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1058 (dest, channel), task, timeout);
1061 /** \ingroup msg_deprecated_functions
1062 * \brief Test whether there is a pending communication on a channel, and who sent it.
1064 * It takes one parameter.
1065 * \param channel the channel on which the process should be
1066 listening. This value has to be >=0 and < than the maximal
1067 number of channels fixed with MSG_set_channel_number().
1068 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1070 int MSG_task_probe_from(m_channel_t channel)
1072 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1075 xbt_assert((channel >= 0)
1076 && (channel < msg_global->max_channel), "Invalid channel %d",
1081 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1082 (MSG_host_self(), channel))))
1085 return MSG_process_get_PID(task->simdata->sender);
1088 /** \ingroup msg_deprecated_functions
1089 * \brief Test whether there is a pending communication on a channel.
1091 * It takes one parameter.
1092 * \param channel the channel on which the process should be
1093 listening. This value has to be >=0 and < than the maximal
1094 number of channels fixed with MSG_set_channel_number().
1095 * \return 1 if there is a pending communication and 0 otherwise
1097 int MSG_task_Iprobe(m_channel_t channel)
1099 XBT_WARN("DEPRECATED!");
1100 xbt_assert((channel >= 0)
1101 && (channel < msg_global->max_channel), "Invalid channel %d",
1105 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1106 (MSG_host_self(), channel));
1109 /** \ingroup msg_deprecated_functions
1111 * \brief Return the number of tasks waiting to be received on a \a
1112 channel and sent by \a host.
1114 * It takes two parameters.
1115 * \param channel the channel on which the process should be
1116 listening. This value has to be >=0 and < than the maximal
1117 number of channels fixed with MSG_set_channel_number().
1118 * \param host the host that is to be watched.
1119 * \return the number of tasks waiting to be received on \a channel
1120 and sent by \a host.
1122 int MSG_task_probe_from_host(int channel, m_host_t host)
1124 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1125 xbt_assert((channel >= 0)
1126 && (channel < msg_global->max_channel), "Invalid channel %d",
1130 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1131 (MSG_host_self(), channel),
1136 /** \ingroup msg_deprecated_functions
1137 * \brief Listen on \a channel and waits for receiving a task from \a host.
1139 * It takes three parameters.
1140 * \param task a memory location for storing a #m_task_t. It will
1141 hold a task when this function will return. Thus \a task should not
1142 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1143 those two condition does not hold, there will be a warning message.
1144 * \param channel the channel on which the process should be
1145 listening. This value has to be >=0 and < than the maximal
1146 number of channels fixed with MSG_set_channel_number().
1147 * \param host the host that is to be watched.
1148 * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1151 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
1153 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1154 return MSG_task_get_ext(task, channel, -1, host);
1157 /** \ingroup msg_deprecated_functions
1158 * \brief Listen on a channel and wait for receiving a task.
1160 * It takes two parameters.
1161 * \param task a memory location for storing a #m_task_t. It will
1162 hold a task when this function will return. Thus \a task should not
1163 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1164 those two condition does not hold, there will be a warning message.
1165 * \param channel the channel on which the process should be
1166 listening. This value has to be >=0 and < than the maximal
1167 number of channels fixed with MSG_set_channel_number().
1168 * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1170 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
1172 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1173 return MSG_task_get_with_timeout(task, channel, -1);
1176 /** \ingroup msg_deprecated_functions
1177 * \brief Listen on a channel and wait for receiving a task with a timeout.
1179 * It takes three parameters.
1180 * \param task a memory location for storing a #m_task_t. It will
1181 hold a task when this function will return. Thus \a task should not
1182 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1183 those two condition does not hold, there will be a warning message.
1184 * \param channel the channel on which the process should be
1185 listening. This value has to be >=0 and < than the maximal
1186 number of channels fixed with MSG_set_channel_number().
1187 * \param max_duration the maximum time to wait for a task before giving
1188 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1189 will not be modified and will still be
1190 equal to \c NULL when returning.
1191 * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1194 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
1195 double max_duration)
1197 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1198 return MSG_task_get_ext(task, channel, max_duration, NULL);
1202 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
1205 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1206 xbt_assert((channel >= 0)
1207 && (channel < msg_global->max_channel), "Invalid channel %d",
1211 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1212 (MSG_host_self(), channel), task, host,