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"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
13 "Logging specific to MSG (gos)");
15 /** \ingroup msg_gos_functions
16 * \brief Executes a task and waits for its termination.
18 * This function is used for describing the behavior of an agent. It
19 * takes only one parameter.
20 * \param task a #m_task_t to execute on the location on which the
22 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
23 * or #MSG_HOST_FAILURE otherwise
25 MSG_error_t MSG_task_execute(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->simdata->smx_host,
58 simdata->computation_amount,
61 simcall_set_category(simdata->compute, task->category);
64 p_simdata->waiting_action = simdata->compute;
65 comp_state = simcall_host_execution_wait(simdata->compute);
66 p_simdata->waiting_action = NULL;
70 XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
71 if (comp_state == SIMIX_DONE) {
72 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
73 simdata->computation_amount = 0.0;
75 simdata->compute = NULL;
77 TRACE_msg_task_execute_end(task);
80 } else if (simcall_host_get_state(SIMIX_host_self()) == 0) {
81 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
83 simdata->compute = NULL;
85 TRACE_msg_task_execute_end(task);
87 MSG_RETURN(MSG_HOST_FAILURE);
89 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
91 simdata->compute = NULL;
93 TRACE_msg_task_execute_end(task);
95 MSG_RETURN(MSG_TASK_CANCELED);
99 /** \ingroup m_task_management
100 * \brief Creates a new #m_task_t (a parallel one....).
102 * A constructor for #m_task_t taking six arguments and returning the
103 corresponding object.
104 * \param name a name for the object. It is for user-level information
106 * \param host_nb the number of hosts implied in the parallel task.
107 * \param host_list an array of \p host_nb m_host_t.
108 * \param computation_amount an array of \p host_nb
109 doubles. computation_amount[i] is the total number of operations
110 that have to be performed on host_list[i].
111 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
112 * \param data a pointer to any data may want to attach to the new
113 object. It is for user-level information and can be NULL. It can
114 be retrieved with the function \ref MSG_task_get_data.
116 * \return The new corresponding object.
119 MSG_parallel_task_create(const char *name, int host_nb,
120 const m_host_t * host_list,
121 double *computation_amount,
122 double *communication_amount, void *data)
125 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
126 m_task_t task = xbt_new0(s_m_task_t, 1);
127 task->simdata = simdata;
130 task->name = xbt_strdup(name);
134 simdata->computation_amount = 0;
135 simdata->message_size = 0;
136 simdata->compute = NULL;
137 simdata->comm = NULL;
138 simdata->rate = -1.0;
140 simdata->sender = NULL;
141 simdata->receiver = NULL;
142 simdata->source = NULL;
144 simdata->host_nb = host_nb;
145 simdata->host_list = xbt_new0(smx_host_t, host_nb);
146 simdata->comp_amount = computation_amount;
147 simdata->comm_amount = communication_amount;
149 for (i = 0; i < host_nb; i++)
150 simdata->host_list[i] = host_list[i]->simdata->smx_host;
155 /** \ingroup msg_gos_functions
156 * \brief Executes a parallel task and waits for its termination.
158 * \param task a #m_task_t to execute on the location on which the agent is running.
160 * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
161 * or #MSG_HOST_FAILURE otherwise
163 MSG_error_t MSG_parallel_task_execute(m_task_t task)
165 simdata_task_t simdata = NULL;
166 e_smx_state_t comp_state;
167 simdata_process_t p_simdata;
169 simdata = task->simdata;
170 p_simdata = SIMIX_process_self_get_data(SIMIX_process_self());
172 xbt_assert((!simdata->compute)
173 && (task->simdata->isused == 0),
174 "This task is executed somewhere else. Go fix your code!");
176 xbt_assert(simdata->host_nb,
177 "This is not a parallel task. Go to hell.");
179 XBT_DEBUG("Parallel computing on %s", p_simdata->m_host->name);
184 simcall_host_parallel_execute(task->name, simdata->host_nb,
186 simdata->comp_amount,
187 simdata->comm_amount, 1.0, -1.0);
188 XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
190 p_simdata->waiting_action = simdata->compute;
191 comp_state = simcall_host_execution_wait(simdata->compute);
192 p_simdata->waiting_action = NULL;
194 XBT_DEBUG("Finished waiting for execution of action %p, state = %d", simdata->compute, (int)comp_state);
198 if (comp_state == SIMIX_DONE) {
199 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
200 simdata->computation_amount = 0.0;
201 simdata->comm = NULL;
202 simdata->compute = NULL;
204 } else if (simcall_host_get_state(SIMIX_host_self()) == 0) {
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_HOST_FAILURE);
210 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
211 simdata->comm = NULL;
212 simdata->compute = NULL;
213 MSG_RETURN(MSG_TASK_CANCELED);
218 /** \ingroup msg_gos_functions
219 * \brief Sleep for the specified number of seconds
221 * Makes the current process sleep until \a time seconds have elapsed.
223 * \param nb_sec a number of second
225 MSG_error_t MSG_process_sleep(double nb_sec)
228 /*m_process_t proc = MSG_process_self();*/
231 TRACE_msg_process_sleep_in(MSG_process_self());
234 /* create action to sleep */
235 state = simcall_process_sleep(nb_sec);
237 /*proc->simdata->waiting_action = act_sleep;
239 FIXME: check if not setting the waiting_action breaks something on msg
241 proc->simdata->waiting_action = NULL;*/
243 if (state == SIMIX_DONE) {
245 TRACE_msg_process_sleep_out(MSG_process_self());
250 TRACE_msg_process_sleep_out(MSG_process_self());
252 MSG_RETURN(MSG_HOST_FAILURE);
256 /** \ingroup msg_gos_functions
257 * \brief Receives a task from a mailbox from a specific host.
259 * This is a blocking function, the execution flow will be blocked
260 * until the task is received. See #MSG_task_irecv
261 * for receiving tasks asynchronously.
263 * \param task a memory location for storing a #m_task_t.
264 * \param alias name of the mailbox to receive the task from
265 * \param host a #m_host_t host from where the task was sent
268 * #MSG_OK if the task was successfully received,
269 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
272 MSG_task_receive_from_host(m_task_t * task, const char *alias,
275 return MSG_task_receive_ext(task, alias, -1, host);
278 /** \ingroup msg_gos_functions
279 * \brief Receives a task from a mailbox.
281 * This is a blocking function, the execution flow will be blocked
282 * until the task is received. See #MSG_task_irecv
283 * for receiving tasks asynchronously.
285 * \param task a memory location for storing a #m_task_t.
286 * \param alias name of the mailbox to receive the task from
289 * #MSG_OK if the task was successfully received,
290 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
292 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
294 return MSG_task_receive_with_timeout(task, alias, -1);
297 /** \ingroup msg_gos_functions
298 * \brief Receives a task from a mailbox with a given timeout.
300 * This is a blocking function with a timeout, the execution flow will be blocked
301 * until the task is received or the timeout is achieved. See #MSG_task_irecv
302 * for receiving tasks asynchronously. You can provide a -1 timeout
303 * to obtain an infinite timeout.
305 * \param task a memory location for storing a #m_task_t.
306 * \param alias name of the mailbox to receive the task from
307 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
310 * #MSG_OK if the task was successfully received,
311 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
314 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
317 return MSG_task_receive_ext(task, alias, timeout, NULL);
320 /** \ingroup msg_gos_functions
321 * \brief Receives a task from a mailbox from a specific host with a given timeout.
323 * This is a blocking function with a timeout, the execution flow will be blocked
324 * until the task is received or the timeout is achieved. See #MSG_task_irecv
325 * for receiving tasks asynchronously. You can provide a -1 timeout
326 * to obtain an infinite timeout.
328 * \param task a memory location for storing a #m_task_t.
329 * \param alias name of the mailbox to receive the task from
330 * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
331 * \param host a #m_host_t host from where the task was sent
334 * #MSG_OK if the task was successfully received,
335 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
338 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
342 ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
344 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
348 /** \ingroup msg_gos_functions
349 * \brief Sends a task on a mailbox.
351 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
352 * to end the communication.
354 * \param task a #m_task_t to send on another location.
355 * \param alias name of the mailbox to sent the task to
356 * \return the msg_comm_t communication created
358 msg_comm_t MSG_task_isend(m_task_t task, const char *alias)
360 return MSG_task_isend_with_matching(task,alias,NULL,NULL);
363 /** \ingroup msg_gos_functions
364 * \brief Sends a task on a mailbox, with support for matching requests
366 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
367 * to end the communication.
369 * \param task a #m_task_t to send on another location.
370 * \param alias name of the mailbox to sent the task to
371 * \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
372 * \param match_data user provided data passed to match_fun
373 * \return the msg_comm_t communication created
375 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(m_task_t task, const char *alias,
376 int (*match_fun)(void*,void*),
379 simdata_task_t t_simdata = NULL;
380 m_process_t process = MSG_process_self();
381 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
383 /* FIXME: these functions are not traceable */
385 /* Prepare the task to send */
386 t_simdata = task->simdata;
387 t_simdata->sender = process;
388 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
390 xbt_assert(t_simdata->isused == 0,
391 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
393 t_simdata->isused = 1;
394 t_simdata->comm = NULL;
395 msg_global->sent_msg++;
397 /* Send it by calling SIMIX network layer */
398 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
399 comm->task_sent = task;
400 comm->task_received = NULL;
401 comm->status = MSG_OK;
403 simcall_comm_isend(mailbox, t_simdata->message_size,
404 t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
405 t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
410 /** \ingroup msg_gos_functions
411 * \brief Sends a task on a mailbox.
413 * This is a non blocking detached send function.
414 * Think of it as a best effort send. Keep in mind that the third parameter
415 * is only called if the communication fails. If the communication does work,
416 * it is responsibility of the receiver code to free anything related to
417 * the task, as usual. More details on this can be obtained on
418 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
419 * in the SimGrid-user mailing list archive.
421 * \param task a #m_task_t to send on another location.
422 * \param alias name of the mailbox to sent the task to
423 * \param cleanup a function to destroy the task if the
424 * communication fails, e.g. MSG_task_destroy
425 * (if NULL, no function will be called)
427 void MSG_task_dsend(m_task_t task, const char *alias, void_f_pvoid_t cleanup)
429 simdata_task_t t_simdata = NULL;
430 m_process_t process = MSG_process_self();
431 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
433 /* FIXME: these functions are not traceable */
435 /* Prepare the task to send */
436 t_simdata = task->simdata;
437 t_simdata->sender = process;
438 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
440 xbt_assert(t_simdata->isused == 0,
441 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
443 t_simdata->isused = 1;
444 t_simdata->comm = NULL;
445 msg_global->sent_msg++;
447 /* Send it by calling SIMIX network layer */
448 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
449 t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
450 t_simdata->comm = comm;
453 /** \ingroup msg_gos_functions
454 * \brief Starts listening for receiving a task from an asynchronous communication.
456 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
457 * to end the communication.
459 * \param task a memory location for storing a #m_task_t.
460 * \param name of the mailbox to receive the task on
461 * \return the msg_comm_t communication created
463 msg_comm_t MSG_task_irecv(m_task_t *task, const char *name)
465 smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
467 /* FIXME: these functions are not traceable */
470 xbt_assert(task, "Null pointer for the task storage");
474 ("MSG_task_irecv() was asked to write in a non empty task struct.");
476 /* Try to receive it by calling SIMIX network layer */
477 msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
478 comm->task_sent = NULL;
479 comm->task_received = task;
480 comm->status = MSG_OK;
481 comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
486 /** \ingroup msg_gos_functions
487 * \brief Checks whether a communication is done, and if yes, finalizes it.
488 * \param comm the communication to test
489 * \return TRUE if the communication is finished
490 * (but it may have failed, use MSG_comm_get_status() to know its status)
491 * or FALSE if the communication is not finished yet
492 * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
494 int MSG_comm_test(msg_comm_t comm)
499 finished = simcall_comm_test(comm->s_comm);
501 if (finished && comm->task_received != NULL) {
502 /* I am the receiver */
503 (*comm->task_received)->simdata->isused = 0;
507 switch (e.category) {
510 comm->status = MSG_HOST_FAILURE;
515 comm->status = MSG_TRANSFER_FAILURE;
520 comm->status = MSG_TIMEOUT;
533 /** \ingroup msg_gos_functions
534 * \brief This function checks if a communication is finished.
535 * \param comms a vector of communications
536 * \return the position of the finished communication if any
537 * (but it may have failed, use MSG_comm_get_status() to know its status),
538 * or -1 if none is finished
540 int MSG_comm_testany(xbt_dynar_t comms)
543 int finished_index = -1;
545 /* create the equivalent dynar with SIMIX objects */
546 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
549 xbt_dynar_foreach(comms, cursor, comm) {
550 xbt_dynar_push(s_comms, &comm->s_comm);
553 MSG_error_t status = MSG_OK;
555 finished_index = simcall_comm_testany(s_comms);
558 switch (e.category) {
561 finished_index = e.value;
562 status = MSG_HOST_FAILURE;
566 finished_index = e.value;
567 status = MSG_TRANSFER_FAILURE;
571 finished_index = e.value;
572 status = MSG_TIMEOUT;
580 xbt_dynar_free(&s_comms);
582 if (finished_index != -1) {
583 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
584 /* the communication is finished */
585 comm->status = status;
587 if (status == MSG_OK && comm->task_received != NULL) {
588 /* I am the receiver */
589 (*comm->task_received)->simdata->isused = 0;
593 return finished_index;
596 /** \ingroup msg_gos_functions
597 * \brief Destroys a communication.
598 * \param comm the communication to destroy.
600 void MSG_comm_destroy(msg_comm_t comm)
605 /** \ingroup msg_gos_functions
606 * \brief Wait for the completion of a communication.
608 * It takes two parameters.
609 * \param comm the communication to wait.
610 * \param timeout Wait until the communication terminates or the timeout occurs
611 * \return MSG_error_t
613 MSG_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
617 simcall_comm_wait(comm->s_comm, timeout);
619 if (comm->task_received != NULL) {
620 /* I am the receiver */
621 (*comm->task_received)->simdata->isused = 0;
624 /* FIXME: these functions are not traceable */
627 switch (e.category) {
629 comm->status = MSG_HOST_FAILURE;
632 comm->status = MSG_TRANSFER_FAILURE;
635 comm->status = MSG_TIMEOUT;
646 /** \ingroup msg_gos_functions
647 * \brief This function is called by a sender and permit to wait for each communication
649 * \param comm a vector of communication
650 * \param nb_elem is the size of the comm vector
651 * \param timeout for each call of MSG_comm_wait
653 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
656 for (i = 0; i < nb_elem; i++) {
657 MSG_comm_wait(comm[i], timeout);
661 /** \ingroup msg_gos_functions
662 * \brief This function waits for the first communication finished in a list.
663 * \param comms a vector of communications
664 * \return the position of the first finished communication
665 * (but it may have failed, use MSG_comm_get_status() to know its status)
667 int MSG_comm_waitany(xbt_dynar_t comms)
670 int finished_index = -1;
672 /* create the equivalent dynar with SIMIX objects */
673 xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
676 xbt_dynar_foreach(comms, cursor, comm) {
677 xbt_dynar_push(s_comms, &comm->s_comm);
680 MSG_error_t status = MSG_OK;
682 finished_index = simcall_comm_waitany(s_comms);
685 switch (e.category) {
688 finished_index = e.value;
689 status = MSG_HOST_FAILURE;
693 finished_index = e.value;
694 status = MSG_TRANSFER_FAILURE;
698 finished_index = e.value;
699 status = MSG_TIMEOUT;
708 xbt_assert(finished_index != -1, "WaitAny returned -1");
709 xbt_dynar_free(&s_comms);
711 comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
712 /* the communication is finished */
713 comm->status = status;
715 if (comm->task_received != NULL) {
716 /* I am the receiver */
717 (*comm->task_received)->simdata->isused = 0;
720 return finished_index;
724 * \ingroup msg_gos_functions
725 * \brief Returns the error (if any) that occured during a finished communication.
726 * \param comm a finished communication
727 * \return the status of the communication, or #MSG_OK if no error occured
728 * during the communication
730 MSG_error_t MSG_comm_get_status(msg_comm_t comm) {
735 /** \ingroup msg_gos_functions
736 * \brief Get a task (#m_task_t) from a communication
738 * \param comm the communication where to get the task
739 * \return the task from the communication
741 m_task_t MSG_comm_get_task(msg_comm_t comm)
743 xbt_assert(comm, "Invalid parameter");
745 return comm->task_received ? *comm->task_received : comm->task_sent;
749 * \brief This function is called by SIMIX to copy the data of a comm.
750 * \param comm the comm
751 * \param buff the data copied
752 * \param buff_size size of the buffer
754 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
757 SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
759 // notify the user callback if any
760 if (msg_global->task_copy_callback) {
761 m_task_t task = buff;
762 msg_global->task_copy_callback(task,
763 simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
767 /** \ingroup msg_gos_functions
768 * \brief Sends a task to a mailbox
770 * This is a blocking function, the execution flow will be blocked
771 * until the task is sent (and received in the other side if #MSG_task_receive is used).
772 * See #MSG_task_isend for sending tasks asynchronously.
774 * \param task the task to be sent
775 * \param alias the mailbox name to where the task is sent
777 * \return Returns #MSG_OK if the task was successfully sent,
778 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
780 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
782 XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
783 return MSG_task_send_with_timeout(task, alias, -1);
786 /** \ingroup msg_gos_functions
787 * \brief Sends a task to a mailbox with a maximum rate
789 * This is a blocking function, the execution flow will be blocked
790 * until the task is sent. The maxrate parameter allows the application
791 * to limit the bandwidth utilization of network links when sending the task.
793 * \param task the task to be sent
794 * \param alias the mailbox name to where the task is sent
795 * \param maxrate the maximum communication rate for sending this task
797 * \return Returns #MSG_OK if the task was successfully sent,
798 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
801 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
803 task->simdata->rate = maxrate;
804 return MSG_task_send(task, alias);
807 /** \ingroup msg_gos_functions
808 * \brief Sends a task to a mailbox with a timeout
810 * This is a blocking function, the execution flow will be blocked
811 * until the task is sent or the timeout is achieved.
813 * \param task the task to be sent
814 * \param alias the mailbox name to where the task is sent
815 * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
817 * \return Returns #MSG_OK if the task was successfully sent,
818 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
821 MSG_task_send_with_timeout(m_task_t task, const char *alias,
824 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
828 /** \ingroup msg_gos_functions
829 * \brief Check if there is a communication going on in a mailbox.
831 * \param alias the name of the mailbox to be considered
833 * \return Returns 1 if there is a communication, 0 otherwise
835 int MSG_task_listen(const char *alias)
837 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
840 /** \ingroup msg_gos_functions
841 * \brief Check the number of communication actions of a given host pending in a mailbox.
843 * \param alias the name of the mailbox to be considered
844 * \param host the host to check for communication
846 * \return Returns the number of pending communication actions of the host in the
847 * given mailbox, 0 if there is no pending communication actions.
850 int MSG_task_listen_from_host(const char *alias, m_host_t host)
853 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
857 /** \ingroup msg_gos_functions
858 * \brief Look if there is a communication on a mailbox and return the
859 * PID of the sender process.
861 * \param alias the name of the mailbox to be considered
863 * \return Returns the PID of sender process,
864 * -1 if there is no communication in the mailbox.
866 int MSG_task_listen_from(const char *alias)
871 (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
874 return MSG_process_get_PID(task->simdata->sender);
877 #ifdef MSG_USE_DEPRECATED
878 /** \ingroup msg_gos_functions
880 * \brief Return the last value returned by a MSG function (except
883 MSG_error_t MSG_get_errno(void)
885 return PROCESS_GET_ERRNO();
888 /** \ingroup msg_gos_functions
889 * \brief Put a task on a channel of an host and waits for the end of the
892 * This function is used for describing the behavior of an agent. It
893 * takes three parameter.
894 * \param task a #m_task_t to send on another location. This task
895 will not be usable anymore when the function will return. There is
896 no automatic task duplication and you have to save your parameters
897 before calling this function. Tasks are unique and once it has been
898 sent to another location, you should not access it anymore. You do
899 not need to call MSG_task_destroy() but to avoid using, as an
900 effect of inattention, this task anymore, you definitely should
901 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
902 can be transfered iff it has been correctly created with
904 * \param dest the destination of the message
905 * \param channel the channel on which the agent should put this
906 task. This value has to be >=0 and < than the maximal number of
907 channels fixed with MSG_set_channel_number().
908 * \return #MSG_HOST_FAILURE if the host on which
909 * this function was called was shut down,
910 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
911 * (network failure, dest failure) or #MSG_OK if it succeeded.
913 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
915 XBT_WARN("DEPRECATED! Now use MSG_task_send");
916 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
919 /** \ingroup msg_gos_functions
920 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
926 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
929 XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
930 task->simdata->rate = maxrate;
931 return MSG_task_put(task, dest, channel);
934 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
935 * host (with a timeout on the waiting of the destination host) and
936 * waits for the end of the transmission.
938 * This function is used for describing the behavior of an agent. It
939 * takes four parameter.
940 * \param task a #m_task_t to send on another location. This task
941 will not be usable anymore when the function will return. There is
942 no automatic task duplication and you have to save your parameters
943 before calling this function. Tasks are unique and once it has been
944 sent to another location, you should not access it anymore. You do
945 not need to call MSG_task_destroy() but to avoid using, as an
946 effect of inattention, this task anymore, you definitely should
947 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
948 can be transfered iff it has been correctly created with
950 * \param dest the destination of the message
951 * \param channel the channel on which the agent should put this
952 task. This value has to be >=0 and < than the maximal number of
953 channels fixed with MSG_set_channel_number().
954 * \param timeout the maximum time to wait for a task before giving
955 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
957 * \return #MSG_HOST_FAILURE if the host on which
958 this function was called was shut down,
959 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
960 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
963 MSG_task_put_with_timeout(m_task_t task, m_host_t dest,
964 m_channel_t channel, double timeout)
966 XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
967 xbt_assert((channel >= 0)
968 && (channel < msg_global->max_channel), "Invalid channel %d",
971 XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", dest->name);
973 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
974 (dest, channel), task, timeout);
977 /** \ingroup msg_gos_functions
978 * \brief Test whether there is a pending communication on a channel, and who sent it.
980 * It takes one parameter.
981 * \param channel the channel on which the agent should be
982 listening. This value has to be >=0 and < than the maximal
983 number of channels fixed with MSG_set_channel_number().
984 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
986 int MSG_task_probe_from(m_channel_t channel)
988 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
991 xbt_assert((channel >= 0)
992 && (channel < msg_global->max_channel), "Invalid channel %d",
997 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
998 (MSG_host_self(), channel))))
1001 return MSG_process_get_PID(task->simdata->sender);
1004 /** \ingroup msg_gos_functions
1005 * \brief Test whether there is a pending communication on a channel.
1007 * It takes one parameter.
1008 * \param channel the channel on which the agent should be
1009 listening. This value has to be >=0 and < than the maximal
1010 number of channels fixed with MSG_set_channel_number().
1011 * \return 1 if there is a pending communication and 0 otherwise
1013 int MSG_task_Iprobe(m_channel_t channel)
1015 XBT_WARN("DEPRECATED!");
1016 xbt_assert((channel >= 0)
1017 && (channel < msg_global->max_channel), "Invalid channel %d",
1021 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1022 (MSG_host_self(), channel));
1025 /** \ingroup msg_gos_functions
1027 * \brief Return the number of tasks waiting to be received on a \a
1028 channel and sent by \a host.
1030 * It takes two parameters.
1031 * \param channel the channel on which the agent should be
1032 listening. This value has to be >=0 and < than the maximal
1033 number of channels fixed with MSG_set_channel_number().
1034 * \param host the host that is to be watched.
1035 * \return the number of tasks waiting to be received on \a channel
1036 and sent by \a host.
1038 int MSG_task_probe_from_host(int channel, m_host_t host)
1040 XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1041 xbt_assert((channel >= 0)
1042 && (channel < msg_global->max_channel), "Invalid channel %d",
1046 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1047 (MSG_host_self(), channel),
1052 /** \ingroup msg_gos_functions
1053 * \brief Listen on \a channel and waits for receiving a task from \a host.
1055 * It takes three parameters.
1056 * \param task a memory location for storing a #m_task_t. It will
1057 hold a task when this function will return. Thus \a task should not
1058 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1059 those two condition does not hold, there will be a warning message.
1060 * \param channel the channel on which the agent should be
1061 listening. This value has to be >=0 and < than the maximal
1062 number of channels fixed with MSG_set_channel_number().
1063 * \param host the host that is to be watched.
1064 * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1067 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
1069 XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1070 return MSG_task_get_ext(task, channel, -1, host);
1073 /** \ingroup msg_gos_functions
1074 * \brief Listen on a channel and wait for receiving a task.
1076 * It takes two parameters.
1077 * \param task a memory location for storing a #m_task_t. It will
1078 hold a task when this function will return. Thus \a task should not
1079 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1080 those two condition does not hold, there will be a warning message.
1081 * \param channel the channel on which the agent should be
1082 listening. This value has to be >=0 and < than the maximal
1083 number of channels fixed with MSG_set_channel_number().
1084 * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1086 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
1088 XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1089 return MSG_task_get_with_timeout(task, channel, -1);
1092 /** \ingroup msg_gos_functions
1093 * \brief Listen on a channel and wait for receiving a task with a timeout.
1095 * It takes three parameters.
1096 * \param task a memory location for storing a #m_task_t. It will
1097 hold a task when this function will return. Thus \a task should not
1098 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1099 those two condition does not hold, there will be a warning message.
1100 * \param channel the channel on which the agent should be
1101 listening. This value has to be >=0 and < than the maximal
1102 number of channels fixed with MSG_set_channel_number().
1103 * \param max_duration the maximum time to wait for a task before giving
1104 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1105 will not be modified and will still be
1106 equal to \c NULL when returning.
1107 * \return a #MSG_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1110 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
1111 double max_duration)
1113 XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1114 return MSG_task_get_ext(task, channel, max_duration, NULL);
1118 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
1121 XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1122 xbt_assert((channel >= 0)
1123 && (channel < msg_global->max_channel), "Invalid channel %d",
1127 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1128 (MSG_host_self(), channel), task, host,