1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "msg/private.h"
8 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
14 "Logging specific to MSG (gos)");
16 /** \ingroup msg_gos_functions
18 * \brief Return the last value returned by a MSG function (except
21 MSG_error_t MSG_get_errno(void)
23 return PROCESS_GET_ERRNO();
26 /** \ingroup msg_gos_functions
27 * \brief Executes a task and waits for its termination.
29 * This function is used for describing the behavior of an agent. It
30 * takes only one parameter.
31 * \param task a #m_task_t to execute on the location on which the
33 * \return #MSG_FATAL if \a task is not properly initialized and
36 MSG_error_t MSG_task_execute(m_task_t task)
38 simdata_task_t simdata = NULL;
39 m_process_t self = MSG_process_self();
40 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
43 TRACE_msg_task_execute_start (task);
46 simdata = task->simdata;
48 xbt_assert1((!simdata->compute) && (task->simdata->refcount == 1),
49 "This task is executed somewhere else. Go fix your code! %d", task->simdata->refcount);
51 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
53 if (simdata->computation_amount == 0) {
55 TRACE_msg_task_execute_end (task);
60 SIMIX_mutex_lock(simdata->mutex);
62 SIMIX_action_execute(SIMIX_host_self(), task->name,
63 simdata->computation_amount);
64 SIMIX_action_set_priority(simdata->compute, simdata->priority);
66 /* changed to waiting action since we are always waiting one action (execute, communicate or sleep) */
67 self->simdata->waiting_action = simdata->compute;
68 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
70 SIMIX_cond_wait(simdata->cond, simdata->mutex);
71 state = SIMIX_action_get_state(simdata->compute);
72 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
73 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
74 self->simdata->waiting_action = NULL;
76 SIMIX_mutex_unlock(simdata->mutex);
79 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
80 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
81 SIMIX_action_destroy(task->simdata->compute);
82 simdata->computation_amount = 0.0;
84 simdata->compute = NULL;
86 TRACE_msg_task_execute_end (task);
89 } else if (SIMIX_host_get_state(SIMIX_host_self()) == 0) {
90 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
91 SIMIX_action_destroy(task->simdata->compute);
93 simdata->compute = NULL;
95 TRACE_msg_task_execute_end (task);
97 MSG_RETURN(MSG_HOST_FAILURE);
99 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
100 SIMIX_action_destroy(task->simdata->compute);
101 simdata->comm = NULL;
102 simdata->compute = NULL;
104 TRACE_msg_task_execute_end (task);
106 MSG_RETURN(MSG_TASK_CANCELLED);
110 /** \ingroup m_task_management
111 * \brief Creates a new #m_task_t (a parallel one....).
113 * A constructor for #m_task_t taking six arguments and returning the
114 corresponding object.
115 * \param name a name for the object. It is for user-level information
117 * \param host_nb the number of hosts implied in the parallel task.
118 * \param host_list an array of \p host_nb m_host_t.
119 * \param computation_amount an array of \p host_nb
120 doubles. computation_amount[i] is the total number of operations
121 that have to be performed on host_list[i].
122 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
123 * \param data a pointer to any data may want to attach to the new
124 object. It is for user-level information and can be NULL. It can
125 be retrieved with the function \ref MSG_task_get_data.
127 * \return The new corresponding object.
130 MSG_parallel_task_create(const char *name, int host_nb,
131 const m_host_t * host_list,
132 double *computation_amount,
133 double *communication_amount, void *data)
136 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
137 m_task_t task = xbt_new0(s_m_task_t, 1);
138 task->simdata = simdata;
141 task->name = xbt_strdup(name);
145 simdata->computation_amount = 0;
146 simdata->message_size = 0;
147 simdata->cond = SIMIX_cond_init();
148 simdata->mutex = SIMIX_mutex_init();
149 simdata->compute = NULL;
150 simdata->comm = NULL;
151 simdata->rate = -1.0;
152 simdata->refcount = 1;
153 simdata->sender = NULL;
154 simdata->receiver = NULL;
155 simdata->source = NULL;
157 simdata->host_nb = host_nb;
158 simdata->host_list = xbt_new0(smx_host_t, host_nb);
159 simdata->comp_amount = computation_amount;
160 simdata->comm_amount = communication_amount;
162 for (i = 0; i < host_nb; i++)
163 simdata->host_list[i] = host_list[i]->simdata->smx_host;
168 MSG_error_t MSG_parallel_task_execute(m_task_t task)
170 simdata_task_t simdata = NULL;
171 m_process_t self = MSG_process_self();
172 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
175 simdata = task->simdata;
177 xbt_assert0((!simdata->compute)
178 && (task->simdata->refcount == 1),
179 "This task is executed somewhere else. Go fix your code!");
181 xbt_assert0(simdata->host_nb, "This is not a parallel task. Go to hell.");
183 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
187 SIMIX_mutex_lock(simdata->mutex);
189 SIMIX_action_parallel_execute(task->name, simdata->host_nb,
190 simdata->host_list, simdata->comp_amount,
191 simdata->comm_amount, 1.0, -1.0);
193 self->simdata->waiting_action = simdata->compute;
194 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
196 SIMIX_cond_wait(simdata->cond, simdata->mutex);
197 state = SIMIX_action_get_state(task->simdata->compute);
198 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
200 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
201 self->simdata->waiting_action = NULL;
204 SIMIX_mutex_unlock(simdata->mutex);
207 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
208 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
209 SIMIX_action_destroy(task->simdata->compute);
210 simdata->computation_amount = 0.0;
211 simdata->comm = NULL;
212 simdata->compute = NULL;
214 } else if (SIMIX_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 SIMIX_action_destroy(task->simdata->compute);
217 simdata->comm = NULL;
218 simdata->compute = NULL;
219 MSG_RETURN(MSG_HOST_FAILURE);
221 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
222 SIMIX_action_destroy(task->simdata->compute);
223 simdata->comm = NULL;
224 simdata->compute = NULL;
225 MSG_RETURN(MSG_TASK_CANCELLED);
231 /** \ingroup msg_gos_functions
232 * \brief Sleep for the specified number of seconds
234 * Makes the current process sleep until \a time seconds have elapsed.
236 * \param nb_sec a number of second
238 MSG_error_t MSG_process_sleep(double nb_sec)
240 smx_action_t act_sleep;
241 m_process_t proc = MSG_process_self();
242 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
247 TRACE_msg_process_sleep_in (MSG_process_self());
250 /* create action to sleep */
252 SIMIX_action_sleep(SIMIX_process_get_host(proc->simdata->s_process),
255 mutex = SIMIX_mutex_init();
256 SIMIX_mutex_lock(mutex);
258 /* create conditional and register action to it */
259 cond = SIMIX_cond_init();
261 proc->simdata->waiting_action = act_sleep;
262 SIMIX_register_action_to_condition(act_sleep, cond);
264 SIMIX_cond_wait(cond, mutex);
265 state = SIMIX_action_get_state(act_sleep);
266 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
267 proc->simdata->waiting_action = NULL;
268 SIMIX_unregister_action_to_condition(act_sleep, cond);
269 SIMIX_mutex_unlock(mutex);
271 /* remove variables */
272 SIMIX_cond_destroy(cond);
273 SIMIX_mutex_destroy(mutex);
275 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
276 if (SIMIX_host_get_state(SIMIX_host_self()) == SURF_RESOURCE_OFF) {
277 SIMIX_action_destroy(act_sleep);
279 TRACE_msg_process_sleep_out (MSG_process_self());
281 MSG_RETURN(MSG_HOST_FAILURE);
284 SIMIX_action_destroy(act_sleep);
286 TRACE_msg_process_sleep_out (MSG_process_self());
288 MSG_RETURN(MSG_HOST_FAILURE);
291 SIMIX_action_destroy(act_sleep);
293 TRACE_msg_process_sleep_out (MSG_process_self());
298 /** \ingroup msg_gos_functions
299 * \brief Listen on \a channel and waits for receiving a task from \a host.
301 * It takes three parameters.
302 * \param task a memory location for storing a #m_task_t. It will
303 hold a task when this function will return. Thus \a task should not
304 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
305 those two condition does not hold, there will be a warning message.
306 * \param channel the channel on which the agent should be
307 listening. This value has to be >=0 and < than the maximal
308 number of channels fixed with MSG_set_channel_number().
309 * \param host the host that is to be watched.
310 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
311 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
314 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
316 return MSG_task_get_ext(task, channel, -1, host);
319 /** \ingroup msg_gos_functions
320 * \brief Listen on a channel and wait for receiving a task.
322 * It takes two parameters.
323 * \param task a memory location for storing a #m_task_t. It will
324 hold a task when this function will return. Thus \a task should not
325 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
326 those two condition does not hold, there will be a warning message.
327 * \param channel the channel on which the agent should be
328 listening. This value has to be >=0 and < than the maximal
329 number of channels fixed with MSG_set_channel_number().
330 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
331 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
333 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
335 return MSG_task_get_with_timeout(task, channel, -1);
338 /** \ingroup msg_gos_functions
339 * \brief Listen on a channel and wait for receiving a task with a timeout.
341 * It takes three parameters.
342 * \param task a memory location for storing a #m_task_t. It will
343 hold a task when this function will return. Thus \a task should not
344 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
345 those two condition does not hold, there will be a warning message.
346 * \param channel the channel on which the agent should be
347 listening. This value has to be >=0 and < than the maximal
348 number of channels fixed with MSG_set_channel_number().
349 * \param max_duration the maximum time to wait for a task before giving
350 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
351 will not be modified and will still be
352 equal to \c NULL when returning.
353 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
354 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
357 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
360 return MSG_task_get_ext(task, channel, max_duration, NULL);
363 /** \defgroup msg_gos_functions MSG Operating System Functions
364 * \brief This section describes the functions that can be used
365 * by an agent for handling some task.
369 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
372 xbt_assert1((channel >= 0)
373 && (channel < msg_global->max_channel), "Invalid channel %d",
377 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
378 (MSG_host_self(), channel), task, host, timeout);
382 MSG_task_receive_from_host(m_task_t * task, const char *alias, m_host_t host)
384 return MSG_task_receive_ext(task, alias, -1, host);
387 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
389 return MSG_task_receive_with_timeout(task, alias, -1);
393 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
396 return MSG_task_receive_ext(task, alias, timeout, NULL);
400 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
403 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host,
407 msg_comm_t MSG_task_isend(m_task_t task, const char *alias) {
408 simdata_task_t t_simdata = NULL;
409 m_process_t process = MSG_process_self();
410 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
414 /* FIXME: these functions are not tracable */
416 /* Prepare the task to send */
417 t_simdata = task->simdata;
418 t_simdata->sender = process;
419 t_simdata->source = MSG_host_self();
421 xbt_assert0(t_simdata->refcount == 1,
422 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
424 t_simdata->refcount++;
425 msg_global->sent_msg++;
427 process->simdata->waiting_task = task;
429 /* Send it by calling SIMIX network layer */
431 /* Kept for semantical compatibility with older implementation */
433 SIMIX_cond_signal(mailbox->cond);
435 return SIMIX_network_isend(mailbox->rdv, t_simdata->message_size, t_simdata->rate,
436 task, sizeof(void*), &t_simdata->comm);
439 msg_comm_t MSG_task_irecv(m_task_t * task, const char *alias) {
441 smx_rdv_t rdv = MSG_mailbox_get_by_alias(alias)->rdv;
442 msg_mailbox_t mailbox=MSG_mailbox_get_by_alias(alias);
443 size_t size = sizeof(void*);
447 /* FIXME: these functions are not tracable */
449 memset(&comm,0,sizeof(comm));
451 /* Kept for compatibility with older implementation */
452 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
453 "A process is already blocked on this channel %s",
454 MSG_mailbox_get_alias(mailbox));
457 xbt_assert0(task, "Null pointer for the task storage");
460 CRITICAL0("MSG_task_get() was asked to write in a non empty task struct.");
462 /* Try to receive it by calling SIMIX network layer */
463 return SIMIX_network_irecv(rdv, task, &size);
465 int MSG_comm_test(msg_comm_t comm) {
466 return SIMIX_network_test(comm);
468 MSG_error_t MSG_comm_wait(msg_comm_t comm,double timeout) {
470 MSG_error_t res = MSG_OK;
472 SIMIX_network_wait(comm,timeout);
473 // (*task)->simdata->refcount--;
475 /* FIXME: these functions are not tracable */
479 res = MSG_HOST_FAILURE;
482 res = MSG_TRANSFER_FAILURE;
488 xbt_die(bprintf("Unhandled SIMIX network exception: %s",e.msg));
495 /** \ingroup msg_gos_functions
496 * \brief Put a task on a channel of an host and waits for the end of the
499 * This function is used for describing the behavior of an agent. It
500 * takes three parameter.
501 * \param task a #m_task_t to send on another location. This task
502 will not be usable anymore when the function will return. There is
503 no automatic task duplication and you have to save your parameters
504 before calling this function. Tasks are unique and once it has been
505 sent to another location, you should not access it anymore. You do
506 not need to call MSG_task_destroy() but to avoid using, as an
507 effect of inattention, this task anymore, you definitely should
508 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
509 can be transfered iff it has been correctly created with
511 * \param dest the destination of the message
512 * \param channel the channel on which the agent should put this
513 task. This value has to be >=0 and < than the maximal number of
514 channels fixed with MSG_set_channel_number().
515 * \return #MSG_FATAL if \a task is not properly initialized and
516 * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
517 * this function was called was shut down. Returns
518 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
519 * (network failure, dest failure)
521 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
523 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
526 /** \ingroup msg_gos_functions
527 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
533 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
536 task->simdata->rate = maxrate;
537 return MSG_task_put(task, dest, channel);
540 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
541 * host (with a timeout on the waiting of the destination host) and
542 * waits for the end of the transmission.
544 * This function is used for describing the behavior of an agent. It
545 * takes four parameter.
546 * \param task a #m_task_t to send on another location. This task
547 will not be usable anymore when the function will return. There is
548 no automatic task duplication and you have to save your parameters
549 before calling this function. Tasks are unique and once it has been
550 sent to another location, you should not access it anymore. You do
551 not need to call MSG_task_destroy() but to avoid using, as an
552 effect of inattention, this task anymore, you definitely should
553 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
554 can be transfered iff it has been correctly created with
556 * \param dest the destination of the message
557 * \param channel the channel on which the agent should put this
558 task. This value has to be >=0 and < than the maximal number of
559 channels fixed with MSG_set_channel_number().
560 * \param timeout the maximum time to wait for a task before giving
561 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
563 * \return #MSG_FATAL if \a task is not properly initialized and
564 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
565 this function was called was shut down. Returns
566 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
567 (network failure, dest failure, timeout...)
570 MSG_task_put_with_timeout(m_task_t task, m_host_t dest, m_channel_t channel,
573 xbt_assert1((channel >= 0)
574 && (channel < msg_global->max_channel), "Invalid channel %d",
578 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel(dest, channel),
582 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
584 return MSG_task_send_with_timeout(task, alias, -1);
589 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
591 task->simdata->rate = maxrate;
592 return MSG_task_send(task, alias);
597 MSG_task_send_with_timeout(m_task_t task, const char *alias, double timeout)
599 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias), task,
603 int MSG_task_listen(const char *alias)
607 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
610 /** \ingroup msg_gos_functions
611 * \brief Test whether there is a pending communication on a channel.
613 * It takes one parameter.
614 * \param channel the channel on which the agent should be
615 listening. This value has to be >=0 and < than the maximal
616 number of channels fixed with MSG_set_channel_number().
617 * \return 1 if there is a pending communication and 0 otherwise
619 int MSG_task_Iprobe(m_channel_t channel)
621 xbt_assert1((channel >= 0)
622 && (channel < msg_global->max_channel), "Invalid channel %d",
628 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
629 (MSG_host_self(), channel));
632 /** \ingroup msg_gos_functions
634 * \brief Return the number of tasks waiting to be received on a \a
635 channel and sent by \a host.
637 * It takes two parameters.
638 * \param channel the channel on which the agent should be
639 listening. This value has to be >=0 and < than the maximal
640 number of channels fixed with MSG_set_channel_number().
641 * \param host the host that is to be watched.
642 * \return the number of tasks waiting to be received on \a channel
645 int MSG_task_probe_from_host(int channel, m_host_t host)
647 xbt_assert1((channel >= 0)
648 && (channel < msg_global->max_channel), "Invalid channel %d",
654 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
655 (MSG_host_self(), channel),
660 int MSG_task_listen_from_host(const char *alias, m_host_t host)
665 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias(alias),
669 /** \ingroup msg_gos_functions
670 * \brief Test whether there is a pending communication on a channel, and who sent it.
672 * It takes one parameter.
673 * \param channel the channel on which the agent should be
674 listening. This value has to be >=0 and < than the maximal
675 number of channels fixed with MSG_set_channel_number().
676 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
678 int MSG_task_probe_from(m_channel_t channel)
684 xbt_assert1((channel >= 0)
685 && (channel < msg_global->max_channel), "Invalid channel %d",
690 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
691 (MSG_host_self(), channel))))
694 return MSG_process_get_PID(task->simdata->sender);
697 int MSG_task_listen_from(const char *alias)
703 if (NULL == (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
706 return MSG_process_get_PID(task->simdata->sender);
709 /** \ingroup msg_gos_functions
710 * \brief Wait for at most \a max_duration second for a task reception
713 * \a PID is updated with the PID of the first process that triggered this event if any.
715 * It takes three parameters:
716 * \param channel the channel on which the agent should be
717 listening. This value has to be >=0 and < than the maximal.
718 number of channels fixed with MSG_set_channel_number().
719 * \param PID a memory location for storing an int.
720 * \param timeout the maximum time to wait for a task before
721 giving up. In the case of a reception, *\a PID will be updated
722 with the PID of the first process to send a task.
723 * \return #MSG_HOST_FAILURE if the host is shut down in the meantime
724 and #MSG_OK otherwise.
727 MSG_channel_select_from(m_channel_t channel, double timeout, int *PID)
730 simdata_host_t h_simdata = NULL;
734 msg_mailbox_t mailbox;
736 xbt_assert1((channel >= 0)
737 && (channel < msg_global->max_channel), "Invalid channel %d",
744 if (timeout == 0.0) {
745 *PID = MSG_task_probe_from(channel);
750 h_simdata = h->simdata;
752 mailbox = MSG_mailbox_get_by_channel(MSG_host_self(), channel);
754 while (MSG_mailbox_is_empty(mailbox)) {
761 SIMIX_mutex_lock(h_simdata->mutex);
763 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
764 "A process is already blocked on this channel %d", channel);
766 cond = SIMIX_cond_init();
768 MSG_mailbox_set_cond(mailbox, cond);
771 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
773 SIMIX_cond_wait(cond, h_simdata->mutex);
776 SIMIX_cond_destroy(cond);
777 SIMIX_mutex_unlock(h_simdata->mutex);
779 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
780 MSG_RETURN(MSG_HOST_FAILURE);
783 MSG_mailbox_set_cond(mailbox, NULL);
787 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
792 *PID = MSG_process_get_PID(t->simdata->sender);
800 MSG_error_t MSG_alias_select_from(const char *alias, double timeout, int *PID)
803 simdata_host_t h_simdata = NULL;
807 msg_mailbox_t mailbox;
813 if (timeout == 0.0) {
814 *PID = MSG_task_listen_from(alias);
819 h_simdata = h->simdata;
821 DEBUG2("Probing on alias %s (%s)", alias, h->name);
823 mailbox = MSG_mailbox_get_by_alias(alias);
825 while (MSG_mailbox_is_empty(mailbox)) {
832 SIMIX_mutex_lock(h_simdata->mutex);
834 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
835 "A process is already blocked on this alias %s", alias);
837 cond = SIMIX_cond_init();
839 MSG_mailbox_set_cond(mailbox, cond);
842 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
844 SIMIX_cond_wait(cond, h_simdata->mutex);
847 SIMIX_cond_destroy(cond);
848 SIMIX_mutex_unlock(h_simdata->mutex);
850 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
851 MSG_RETURN(MSG_HOST_FAILURE);
854 MSG_mailbox_set_cond(mailbox, NULL);
858 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
863 *PID = MSG_process_get_PID(t->simdata->sender);