1 /* Copyright (c) 2004-2011. 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"
11 #include "simix/private.h" // FIXME
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
14 "Logging specific to MSG (gos)");
16 /** \ingroup msg_gos_functions
17 * \brief Executes a task and waits for its termination.
19 * This function is used for describing the behavior of an agent. It
20 * takes only one parameter.
21 * \param task a #m_task_t to execute on the location on which the
23 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
24 * or #MSG_HOST_FAILURE otherwise
26 MSG_error_t MSG_task_execute(m_task_t task)
28 simdata_task_t simdata = NULL;
29 simdata_process_t p_simdata;
30 e_smx_state_t comp_state;
33 simdata = task->simdata;
35 xbt_assert(simdata->host_nb == 0,
36 "This is a parallel task. Go to hell.");
39 TRACE_msg_task_execute_start(task);
42 xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
43 "This task is executed somewhere else. Go fix your code! %d",
44 task->simdata->isused);
46 XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
48 if (simdata->computation_amount == 0) {
50 TRACE_msg_task_execute_end(task);
55 m_process_t self = SIMIX_process_self();
56 p_simdata = SIMIX_process_self_get_data(self);
59 SIMIX_req_host_execute(task->name, p_simdata->m_host->simdata->smx_host,
60 simdata->computation_amount,
63 SIMIX_req_set_category(simdata->compute, task->category);
66 p_simdata->waiting_action = simdata->compute;
67 comp_state = SIMIX_req_host_execution_wait(simdata->compute);
68 p_simdata->waiting_action = NULL;
72 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, comp_state);
73 if (comp_state == SIMIX_DONE) {
74 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
75 simdata->computation_amount = 0.0;
77 simdata->compute = NULL;
79 TRACE_msg_task_execute_end(task);
82 } else if (SIMIX_req_host_get_state(SIMIX_host_self()) == 0) {
83 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
85 simdata->compute = NULL;
87 TRACE_msg_task_execute_end(task);
89 MSG_RETURN(MSG_HOST_FAILURE);
91 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
93 simdata->compute = NULL;
95 TRACE_msg_task_execute_end(task);
97 MSG_RETURN(MSG_TASK_CANCELED);
101 /** \ingroup m_task_management
102 * \brief Creates a new #m_task_t (a parallel one....).
104 * A constructor for #m_task_t taking six arguments and returning the
105 corresponding object.
106 * \param name a name for the object. It is for user-level information
108 * \param host_nb the number of hosts implied in the parallel task.
109 * \param host_list an array of \p host_nb m_host_t.
110 * \param computation_amount an array of \p host_nb
111 doubles. computation_amount[i] is the total number of operations
112 that have to be performed on host_list[i].
113 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
114 * \param data a pointer to any data may want to attach to the new
115 object. It is for user-level information and can be NULL. It can
116 be retrieved with the function \ref MSG_task_get_data.
118 * \return The new corresponding object.
121 MSG_parallel_task_create(const char *name, int host_nb,
122 const m_host_t * host_list,
123 double *computation_amount,
124 double *communication_amount, void *data)
127 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
128 m_task_t task = xbt_new0(s_m_task_t, 1);
129 task->simdata = simdata;
132 task->name = xbt_strdup(name);
136 simdata->computation_amount = 0;
137 simdata->message_size = 0;
138 simdata->compute = NULL;
139 simdata->comm = NULL;
140 simdata->rate = -1.0;
142 simdata->sender = NULL;
143 simdata->receiver = NULL;
144 simdata->source = NULL;
146 simdata->host_nb = host_nb;
147 simdata->host_list = xbt_new0(smx_host_t, host_nb);
148 simdata->comp_amount = computation_amount;
149 simdata->comm_amount = communication_amount;
151 for (i = 0; i < host_nb; i++)
152 simdata->host_list[i] = host_list[i]->simdata->smx_host;
157 MSG_error_t MSG_parallel_task_execute(m_task_t task)
159 simdata_task_t simdata = NULL;
160 e_smx_state_t comp_state;
161 simdata_process_t p_simdata;
164 simdata = task->simdata;
165 p_simdata = SIMIX_process_self_get_data(SIMIX_process_self());
167 xbt_assert((!simdata->compute)
168 && (task->simdata->isused == 0),
169 "This task is executed somewhere else. Go fix your code!");
171 xbt_assert(simdata->host_nb,
172 "This is not a parallel task. Go to hell.");
174 XBT_DEBUG("Parallel computing on %s", p_simdata->m_host->name);
179 SIMIX_req_host_parallel_execute(task->name, simdata->host_nb,
181 simdata->comp_amount,
182 simdata->comm_amount, 1.0, -1.0);
183 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
185 p_simdata->waiting_action = simdata->compute;
186 comp_state = SIMIX_req_host_execution_wait(simdata->compute);
187 p_simdata->waiting_action = NULL;
189 XBT_DEBUG("Finished waiting for execution of action %p, state = %d", simdata->compute, comp_state);
193 if (comp_state == SIMIX_DONE) {
194 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
195 simdata->computation_amount = 0.0;
196 simdata->comm = NULL;
197 simdata->compute = NULL;
199 } else if (SIMIX_req_host_get_state(SIMIX_host_self()) == 0) {
200 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
201 simdata->comm = NULL;
202 simdata->compute = NULL;
203 MSG_RETURN(MSG_HOST_FAILURE);
205 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
206 simdata->comm = NULL;
207 simdata->compute = NULL;
208 MSG_RETURN(MSG_TASK_CANCELED);
213 /** \ingroup msg_gos_functions
214 * \brief Sleep for the specified number of seconds
216 * Makes the current process sleep until \a time seconds have elapsed.
218 * \param nb_sec a number of second
220 MSG_error_t MSG_process_sleep(double nb_sec)
223 /*m_process_t proc = MSG_process_self();*/
226 TRACE_msg_process_sleep_in(MSG_process_self());
229 /* create action to sleep */
230 state = SIMIX_req_process_sleep(nb_sec);
232 /*proc->simdata->waiting_action = act_sleep;
234 FIXME: check if not setting the waiting_action breaks something on msg
236 proc->simdata->waiting_action = NULL;*/
238 if (state == SIMIX_DONE) {
240 TRACE_msg_process_sleep_out(MSG_process_self());
245 TRACE_msg_process_sleep_out(MSG_process_self());
247 MSG_RETURN(MSG_HOST_FAILURE);
251 /** \ingroup msg_gos_functions
252 * \brief Listen on \a channel and waits for receiving a task from \a host.
254 * It takes three parameters.
255 * \param task a memory location for storing a #m_task_t. It will
256 hold a task when this function will return. Thus \a task should not
257 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
258 those two condition does not hold, there will be a warning message.
259 * \param channel the channel on which the agent should be
260 listening. This value has to be >=0 and < than the maximal
261 number of channels fixed with MSG_set_channel_number().
262 * \param host the host that is to be watched.
263 * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
266 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
268 return MSG_task_get_ext(task, channel, -1, host);
271 /** \ingroup msg_gos_functions
272 * \brief Listen on a channel and wait for receiving a task.
274 * It takes two parameters.
275 * \param task a memory location for storing a #m_task_t. It will
276 hold a task when this function will return. Thus \a task should not
277 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
278 those two condition does not hold, there will be a warning message.
279 * \param channel the channel on which the agent should be
280 listening. This value has to be >=0 and < than the maximal
281 number of channels fixed with MSG_set_channel_number().
282 * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
284 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
286 return MSG_task_get_with_timeout(task, channel, -1);
289 /** \ingroup msg_gos_functions
290 * \brief Listen on a channel and wait for receiving a task with a timeout.
292 * It takes three parameters.
293 * \param task a memory location for storing a #m_task_t. It will
294 hold a task when this function will return. Thus \a task should not
295 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
296 those two condition does not hold, there will be a warning message.
297 * \param channel the channel on which the agent should be
298 listening. This value has to be >=0 and < than the maximal
299 number of channels fixed with MSG_set_channel_number().
300 * \param max_duration the maximum time to wait for a task before giving
301 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
302 will not be modified and will still be
303 equal to \c NULL when returning.
304 * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
307 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
310 return MSG_task_get_ext(task, channel, max_duration, NULL);
313 /** \defgroup msg_gos_functions MSG Operating System Functions
314 * \brief This section describes the functions that can be used
315 * by an agent for handling some task.
319 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
322 xbt_assert((channel >= 0)
323 && (channel < msg_global->max_channel), "Invalid channel %d",
327 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
328 (MSG_host_self(), channel), task, host,
333 MSG_task_receive_from_host(m_task_t * task, const char *alias,
336 return MSG_task_receive_ext(task, alias, -1, host);
339 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
341 return MSG_task_receive_with_timeout(task, alias, -1);
345 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
348 return MSG_task_receive_ext(task, alias, timeout, NULL);
352 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
356 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
358 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
362 /** \ingroup msg_gos_functions
363 * \brief Sends a task on a mailbox.
365 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
366 * to end the communication.
368 * \param task a #m_task_t to send on another location.
369 * \param alias name of the mailbox to sent the task to
370 * \return the msg_comm_t communication created
372 msg_comm_t MSG_task_isend(m_task_t task, const char *alias)
374 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
377 /** \ingroup msg_gos_functions
378 * \brief Sends a task on a mailbox, with support for matching requests
380 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
381 * to end the communication.
383 * \param task a #m_task_t to send on another location.
384 * \param alias name of the mailbox to sent the task to
385 * \param match_fun boolean function taking the #match_data provided by sender (here), and the one of the receiver (if any) and returning whether they match
386 * \param match_data user provided data passed to match_fun
387 * \return the msg_comm_t communication created
389 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(m_task_t task, const char *alias,
390 int (*match_fun)(void*,void*),
393 simdata_task_t t_simdata = NULL;
394 m_process_t process = MSG_process_self();
395 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
399 /* FIXME: these functions are not traceable */
401 /* Prepare the task to send */
402 t_simdata = task->simdata;
403 t_simdata->sender = process;
404 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
406 xbt_assert(t_simdata->isused == 0,
407 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
409 t_simdata->isused = 1;
410 msg_global->sent_msg++;
412 /* Send it by calling SIMIX network layer */
413 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
414 comm->task_sent = task;
415 comm->task_received = NULL;
416 comm->status = MSG_OK;
418 SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
419 t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
420 t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
425 /** \ingroup msg_gos_functions
426 * \brief Sends a task on a mailbox.
428 * This is a non blocking detached send function.
429 * Think of it as a best effort send. Keep in mind that the third parameter
430 * is only called if the communication fails. If the communication does work,
431 * it is responsibility of the receiver code to free anything related to
432 * the task, as usual. More details on this can be obtained on
433 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
434 * in the SimGrid-user mailing list archive.
436 * \param task a #m_task_t to send on another location.
437 * \param alias name of the mailbox to sent the task to
438 * \param cleanup a function to destroy the task if the
439 * communication fails (if NULL, MSG_task_destroy() will
440 * be used by default)
442 void MSG_task_dsend(m_task_t task, const char *alias, void_f_pvoid_t cleanup)
444 simdata_task_t t_simdata = NULL;
445 m_process_t process = MSG_process_self();
446 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
450 if (cleanup == NULL) {
451 cleanup = (void_f_pvoid_t) MSG_task_destroy;
454 /* FIXME: these functions are not traceable */
456 /* Prepare the task to send */
457 t_simdata = task->simdata;
458 t_simdata->sender = process;
459 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
461 xbt_assert(t_simdata->isused == 0,
462 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
464 t_simdata->isused = 1;
465 msg_global->sent_msg++;
467 /* Send it by calling SIMIX network layer */
468 smx_action_t comm = SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
469 t_simdata->rate, task, sizeof(void *), NULL,cleanup, NULL, 1);
470 t_simdata->comm = comm;
473 /** \ingroup msg_gos_functions
474 * \brief Starts listening for receiving a task from an asynchronous communication.
476 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
477 * to end the communication.
479 * \param task a memory location for storing a #m_task_t.
480 * \param name of the mailbox to receive the task on
481 * \return the msg_comm_t communication created
483 msg_comm_t MSG_task_irecv(m_task_t *task, const char *name)
485 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
489 /* FIXME: these functions are not tracable */
492 xbt_assert(task, "Null pointer for the task storage");
496 ("MSG_task_irecv() was asked to write in a non empty task struct.");
498 /* Try to receive it by calling SIMIX network layer */
499 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
500 comm->task_sent = NULL;
501 comm->task_received = task;
502 comm->status = MSG_OK;
503 comm->s_comm = SIMIX_req_comm_irecv(rdv, task, NULL, NULL, NULL);
508 /** \ingroup msg_gos_functions
509 * \brief Checks whether a communication is done, and if yes, finalizes it.
510 * \param comm the communication to test
511 * \return TRUE if the communication is finished
512 * (but it may have failed, use MSG_comm_get_status() to know its status)
513 * or FALSE if the communication is not finished yet
514 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
516 int MSG_comm_test(msg_comm_t comm)
521 finished = SIMIX_req_comm_test(comm->s_comm);
523 if (finished && comm->task_received != NULL) {
524 /* I am the receiver */
525 (*comm->task_received)->simdata->isused = 0;
529 switch (e.category) {
532 comm->status = MSG_HOST_FAILURE;
537 comm->status = MSG_TRANSFER_FAILURE;
542 comm->status = MSG_TIMEOUT;
555 /** \ingroup msg_gos_functions
556 * \brief This function checks if a communication is finished.
557 * \param comms a vector of communications
558 * \return the position of the finished communication if any
559 * (but it may have failed, use MSG_comm_get_status() to know its status),
560 * or -1 if none is finished
562 int MSG_comm_testany(xbt_dynar_t comms)
565 int finished_index = -1;
567 /* create the equivalent dynar with SIMIX objects */
568 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
571 xbt_dynar_foreach(comms, cursor, comm) {
572 xbt_dynar_push(s_comms, &comm->s_comm);
575 MSG_error_t status = MSG_OK;
577 finished_index = SIMIX_req_comm_testany(s_comms);
580 switch (e.category) {
583 finished_index = e.value;
584 status = MSG_HOST_FAILURE;
588 finished_index = e.value;
589 status = MSG_TRANSFER_FAILURE;
593 finished_index = e.value;
594 status = MSG_TIMEOUT;
602 xbt_dynar_free(&s_comms);
604 if (finished_index != -1) {
605 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
606 /* the communication is finished */
607 comm->status = status;
609 if (status == MSG_OK && comm->task_received != NULL) {
610 /* I am the receiver */
611 (*comm->task_received)->simdata->isused = 0;
615 return finished_index;
618 /** \ingroup msg_gos_functions
619 * \brief Destroys a communication.
620 * \param comm the communication to destroy.
622 void MSG_comm_destroy(msg_comm_t comm)
627 /** \ingroup msg_gos_functions
628 * \brief Wait for the completion of a communication.
630 * It takes two parameters.
631 * \param comm the communication to wait.
632 * \param timeout Wait until the communication terminates or the timeout occurs
633 * \return MSG_error_t
635 MSG_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
639 SIMIX_req_comm_wait(comm->s_comm, timeout);
641 if (comm->task_received != NULL) {
642 /* I am the receiver */
643 (*comm->task_received)->simdata->isused = 0;
646 /* FIXME: these functions are not traceable */
649 switch (e.category) {
651 comm->status = MSG_HOST_FAILURE;
654 comm->status = MSG_TRANSFER_FAILURE;
657 comm->status = MSG_TIMEOUT;
668 /** \ingroup msg_gos_functions
669 * \brief This function is called by a sender and permit to wait for each communication
671 * \param comm a vector of communication
672 * \param nb_elem is the size of the comm vector
673 * \param timeout for each call of MSG_comm_wait
675 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
678 for (i = 0; i < nb_elem; i++) {
679 MSG_comm_wait(comm[i], timeout);
683 /** \ingroup msg_gos_functions
684 * \brief This function waits for the first communication finished in a list.
685 * \param comms a vector of communications
686 * \return the position of the first finished communication
687 * (but it may have failed, use MSG_comm_get_status() to know its status)
689 int MSG_comm_waitany(xbt_dynar_t comms)
692 int finished_index = -1;
694 /* create the equivalent dynar with SIMIX objects */
695 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
698 xbt_dynar_foreach(comms, cursor, comm) {
699 xbt_dynar_push(s_comms, &comm->s_comm);
702 MSG_error_t status = MSG_OK;
704 finished_index = SIMIX_req_comm_waitany(s_comms);
707 switch (e.category) {
710 finished_index = e.value;
711 status = MSG_HOST_FAILURE;
715 finished_index = e.value;
716 status = MSG_TRANSFER_FAILURE;
720 finished_index = e.value;
721 status = MSG_TIMEOUT;
730 xbt_assert(finished_index != -1, "WaitAny returned -1");
731 xbt_dynar_free(&s_comms);
733 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
734 /* the communication is finished */
735 comm->status = status;
737 if (comm->task_received != NULL) {
738 /* I am the receiver */
739 (*comm->task_received)->simdata->isused = 0;
742 return finished_index;
746 * \ingroup msg_gos_functions
747 * \brief Returns the error (if any) that occured during a finished communication.
748 * \param comm a finished communication
749 * \return the status of the communication, or MSG_OK if no error occured
750 * during the communication
752 MSG_error_t MSG_comm_get_status(msg_comm_t comm) {
757 m_task_t MSG_comm_get_task(msg_comm_t comm)
759 xbt_assert(comm, "Invalid parameter");
761 return comm->task_received ? *comm->task_received : comm->task_sent;
765 * \brief This function is called by SIMIX to copy the data of a comm.
766 * \param comm the comm
767 * \param buff_size size of the buffer
769 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, size_t buff_size) {
772 SIMIX_comm_copy_pointer_callback(comm, buff_size);
774 // notify the user callback if any
775 if (msg_global->task_copy_callback) {
776 msg_global->task_copy_callback(SIMIX_req_comm_get_src_data(comm),
777 SIMIX_req_comm_get_src_proc(comm), SIMIX_req_comm_get_dst_proc(comm));
781 /** \ingroup msg_gos_functions
782 * \brief Put a task on a channel of an host and waits for the end of the
785 * This function is used for describing the behavior of an agent. It
786 * takes three parameter.
787 * \param task a #m_task_t to send on another location. This task
788 will not be usable anymore when the function will return. There is
789 no automatic task duplication and you have to save your parameters
790 before calling this function. Tasks are unique and once it has been
791 sent to another location, you should not access it anymore. You do
792 not need to call MSG_task_destroy() but to avoid using, as an
793 effect of inattention, this task anymore, you definitely should
794 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
795 can be transfered iff it has been correctly created with
797 * \param dest the destination of the message
798 * \param channel the channel on which the agent should put this
799 task. This value has to be >=0 and < than the maximal number of
800 channels fixed with MSG_set_channel_number().
801 * \return #MSG_HOST_FAILURE if the host on which
802 * this function was called was shut down,
803 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
804 * (network failure, dest failure) or #MSG_OK if it succeeded.
806 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
808 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
811 /** \ingroup msg_gos_functions
812 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
818 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
821 task->simdata->rate = maxrate;
822 return MSG_task_put(task, dest, channel);
825 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
826 * host (with a timeout on the waiting of the destination host) and
827 * waits for the end of the transmission.
829 * This function is used for describing the behavior of an agent. It
830 * takes four parameter.
831 * \param task a #m_task_t to send on another location. This task
832 will not be usable anymore when the function will return. There is
833 no automatic task duplication and you have to save your parameters
834 before calling this function. Tasks are unique and once it has been
835 sent to another location, you should not access it anymore. You do
836 not need to call MSG_task_destroy() but to avoid using, as an
837 effect of inattention, this task anymore, you definitely should
838 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
839 can be transfered iff it has been correctly created with
841 * \param dest the destination of the message
842 * \param channel the channel on which the agent should put this
843 task. This value has to be >=0 and < than the maximal number of
844 channels fixed with MSG_set_channel_number().
845 * \param timeout the maximum time to wait for a task before giving
846 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
848 * \return #MSG_HOST_FAILURE if the host on which
849 this function was called was shut down,
850 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
851 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
854 MSG_task_put_with_timeout(m_task_t task, m_host_t dest,
855 m_channel_t channel, double timeout)
857 xbt_assert((channel >= 0)
858 && (channel < msg_global->max_channel), "Invalid channel %d",
861 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", dest->name);
863 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
864 (dest, channel), task, timeout);
867 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
869 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
870 return MSG_task_send_with_timeout(task, alias, -1);
875 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
877 task->simdata->rate = maxrate;
878 return MSG_task_send(task, alias);
883 MSG_task_send_with_timeout(m_task_t task, const char *alias,
886 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
890 int MSG_task_listen(const char *alias)
894 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
897 /** \ingroup msg_gos_functions
898 * \brief Test whether there is a pending communication on a channel.
900 * It takes one parameter.
901 * \param channel the channel on which the agent should be
902 listening. This value has to be >=0 and < than the maximal
903 number of channels fixed with MSG_set_channel_number().
904 * \return 1 if there is a pending communication and 0 otherwise
906 int MSG_task_Iprobe(m_channel_t channel)
908 xbt_assert((channel >= 0)
909 && (channel < msg_global->max_channel), "Invalid channel %d",
915 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
916 (MSG_host_self(), channel));
919 /** \ingroup msg_gos_functions
921 * \brief Return the number of tasks waiting to be received on a \a
922 channel and sent by \a host.
924 * It takes two parameters.
925 * \param channel the channel on which the agent should be
926 listening. This value has to be >=0 and < than the maximal
927 number of channels fixed with MSG_set_channel_number().
928 * \param host the host that is to be watched.
929 * \return the number of tasks waiting to be received on \a channel
932 int MSG_task_probe_from_host(int channel, m_host_t host)
934 xbt_assert((channel >= 0)
935 && (channel < msg_global->max_channel), "Invalid channel %d",
941 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
942 (MSG_host_self(), channel),
947 int MSG_task_listen_from_host(const char *alias, m_host_t host)
952 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
956 /** \ingroup msg_gos_functions
957 * \brief Test whether there is a pending communication on a channel, and who sent it.
959 * It takes one parameter.
960 * \param channel the channel on which the agent should be
961 listening. This value has to be >=0 and < than the maximal
962 number of channels fixed with MSG_set_channel_number().
963 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
965 int MSG_task_probe_from(m_channel_t channel)
971 xbt_assert((channel >= 0)
972 && (channel < msg_global->max_channel), "Invalid channel %d",
977 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
978 (MSG_host_self(), channel))))
981 return MSG_process_get_PID(task->simdata->sender);
984 int MSG_task_listen_from(const char *alias)
991 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
994 return MSG_process_get_PID(task->simdata->sender);
997 #ifdef MSG_USE_DEPRECATED
998 /** \ingroup msg_gos_functions
1000 * \brief Return the last value returned by a MSG function (except
1001 * MSG_get_errno...).
1003 MSG_error_t MSG_get_errno(void)
1005 return PROCESS_GET_ERRNO();