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 "simix/private.h"
9 #include "xbt/sysdep.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
16 "Logging specific to MSG (gos)");
18 /** \ingroup msg_gos_functions
20 * \brief Return the last value returned by a MSG function (except
23 MSG_error_t MSG_get_errno(void)
25 return PROCESS_GET_ERRNO();
28 /** \ingroup msg_gos_functions
29 * \brief Executes a task and waits for its termination.
31 * This function is used for describing the behavior of an agent. It
32 * takes only one parameter.
33 * \param task a #m_task_t to execute on the location on which the
35 * \return #MSG_FATAL if \a task is not properly initialized and
38 MSG_error_t MSG_task_execute(m_task_t task)
40 simdata_task_t simdata = NULL;
41 m_process_t self = MSG_process_self();
42 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
45 simdata = task->simdata;
47 xbt_assert0(simdata->host_nb==0, "This is a parallel task. Go to hell.");
50 TRACE_msg_task_execute_start (task);
53 xbt_assert1((!simdata->compute) && (task->simdata->refcount == 1),
54 "This task is executed somewhere else. Go fix your code! %d", task->simdata->refcount);
56 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
58 if (simdata->computation_amount == 0) {
60 TRACE_msg_task_execute_end (task);
65 SIMIX_mutex_lock(simdata->mutex);
67 SIMIX_action_execute(SIMIX_host_self(), task->name,
68 simdata->computation_amount);
69 SIMIX_action_set_priority(simdata->compute, simdata->priority);
71 /* changed to waiting action since we are always waiting one action (execute, communicate or sleep) */
72 self->simdata->waiting_action = simdata->compute;
73 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
75 SIMIX_cond_wait(simdata->cond, simdata->mutex);
76 state = SIMIX_action_get_state(simdata->compute);
77 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
78 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
79 self->simdata->waiting_action = NULL;
81 SIMIX_mutex_unlock(simdata->mutex);
84 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
85 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
86 SIMIX_action_destroy(task->simdata->compute);
87 simdata->computation_amount = 0.0;
89 simdata->compute = NULL;
91 TRACE_msg_task_execute_end (task);
94 } else if (SIMIX_host_get_state(SIMIX_host_self()) == 0) {
95 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
96 SIMIX_action_destroy(task->simdata->compute);
98 simdata->compute = NULL;
100 TRACE_msg_task_execute_end (task);
102 MSG_RETURN(MSG_HOST_FAILURE);
104 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
105 SIMIX_action_destroy(task->simdata->compute);
106 simdata->comm = NULL;
107 simdata->compute = NULL;
109 TRACE_msg_task_execute_end (task);
111 MSG_RETURN(MSG_TASK_CANCELLED);
115 /** \ingroup m_task_management
116 * \brief Creates a new #m_task_t (a parallel one....).
118 * A constructor for #m_task_t taking six arguments and returning the
119 corresponding object.
120 * \param name a name for the object. It is for user-level information
122 * \param host_nb the number of hosts implied in the parallel task.
123 * \param host_list an array of \p host_nb m_host_t.
124 * \param computation_amount an array of \p host_nb
125 doubles. computation_amount[i] is the total number of operations
126 that have to be performed on host_list[i].
127 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
128 * \param data a pointer to any data may want to attach to the new
129 object. It is for user-level information and can be NULL. It can
130 be retrieved with the function \ref MSG_task_get_data.
132 * \return The new corresponding object.
135 MSG_parallel_task_create(const char *name, int host_nb,
136 const m_host_t * host_list,
137 double *computation_amount,
138 double *communication_amount, void *data)
141 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
142 m_task_t task = xbt_new0(s_m_task_t, 1);
143 task->simdata = simdata;
146 task->name = xbt_strdup(name);
150 simdata->computation_amount = 0;
151 simdata->message_size = 0;
152 simdata->cond = SIMIX_cond_init();
153 simdata->mutex = SIMIX_mutex_init();
154 simdata->compute = NULL;
155 simdata->comm = NULL;
156 simdata->rate = -1.0;
157 simdata->refcount = 1;
158 simdata->sender = NULL;
159 simdata->receiver = NULL;
160 simdata->source = NULL;
162 simdata->host_nb = host_nb;
163 simdata->host_list = xbt_new0(smx_host_t, host_nb);
164 simdata->comp_amount = computation_amount;
165 simdata->comm_amount = communication_amount;
167 for (i = 0; i < host_nb; i++)
168 simdata->host_list[i] = host_list[i]->simdata->smx_host;
173 MSG_error_t MSG_parallel_task_execute(m_task_t task)
175 simdata_task_t simdata = NULL;
176 m_process_t self = MSG_process_self();
177 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
180 simdata = task->simdata;
182 xbt_assert0((!simdata->compute)
183 && (task->simdata->refcount == 1),
184 "This task is executed somewhere else. Go fix your code!");
186 xbt_assert0(simdata->host_nb, "This is not a parallel task. Go to hell.");
188 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
192 SIMIX_mutex_lock(simdata->mutex);
194 SIMIX_action_parallel_execute(task->name, simdata->host_nb,
195 simdata->host_list, simdata->comp_amount,
196 simdata->comm_amount, 1.0, -1.0);
198 self->simdata->waiting_action = simdata->compute;
199 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
201 SIMIX_cond_wait(simdata->cond, simdata->mutex);
202 state = SIMIX_action_get_state(task->simdata->compute);
203 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
205 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
206 self->simdata->waiting_action = NULL;
209 SIMIX_mutex_unlock(simdata->mutex);
212 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE) {
213 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
214 SIMIX_action_destroy(task->simdata->compute);
215 simdata->computation_amount = 0.0;
216 simdata->comm = NULL;
217 simdata->compute = NULL;
219 } else if (SIMIX_host_get_state(SIMIX_host_self()) == 0) {
220 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
221 SIMIX_action_destroy(task->simdata->compute);
222 simdata->comm = NULL;
223 simdata->compute = NULL;
224 MSG_RETURN(MSG_HOST_FAILURE);
226 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
227 SIMIX_action_destroy(task->simdata->compute);
228 simdata->comm = NULL;
229 simdata->compute = NULL;
230 MSG_RETURN(MSG_TASK_CANCELLED);
236 /** \ingroup msg_gos_functions
237 * \brief Sleep for the specified number of seconds
239 * Makes the current process sleep until \a time seconds have elapsed.
241 * \param nb_sec a number of second
243 MSG_error_t MSG_process_sleep(double nb_sec)
245 smx_action_t act_sleep;
246 m_process_t proc = MSG_process_self();
247 e_surf_action_state_t state = SURF_ACTION_NOT_IN_THE_SYSTEM;
252 TRACE_msg_process_sleep_in (MSG_process_self());
255 /* create action to sleep */
257 SIMIX_action_sleep(SIMIX_process_get_host(proc->simdata->s_process),
260 mutex = SIMIX_mutex_init();
261 SIMIX_mutex_lock(mutex);
263 /* create conditional and register action to it */
264 cond = SIMIX_cond_init();
266 proc->simdata->waiting_action = act_sleep;
267 SIMIX_register_action_to_condition(act_sleep, cond);
269 SIMIX_cond_wait(cond, mutex);
270 state = SIMIX_action_get_state(act_sleep);
271 } while (state == SURF_ACTION_READY || state == SURF_ACTION_RUNNING);
272 proc->simdata->waiting_action = NULL;
273 SIMIX_unregister_action_to_condition(act_sleep, cond);
274 SIMIX_mutex_unlock(mutex);
276 /* remove variables */
277 SIMIX_cond_destroy(cond);
278 SIMIX_mutex_destroy(mutex);
280 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) {
281 if (SIMIX_host_get_state(SIMIX_host_self()) == SURF_RESOURCE_OFF) {
282 SIMIX_action_destroy(act_sleep);
284 TRACE_msg_process_sleep_out (MSG_process_self());
286 MSG_RETURN(MSG_HOST_FAILURE);
289 SIMIX_action_destroy(act_sleep);
291 TRACE_msg_process_sleep_out (MSG_process_self());
293 MSG_RETURN(MSG_HOST_FAILURE);
296 SIMIX_action_destroy(act_sleep);
298 TRACE_msg_process_sleep_out (MSG_process_self());
303 /** \ingroup msg_gos_functions
304 * \brief Listen on \a channel and waits for receiving a task from \a host.
306 * It takes three parameters.
307 * \param task a memory location for storing a #m_task_t. It will
308 hold a task when this function will return. Thus \a task should not
309 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
310 those two condition does not hold, there will be a warning message.
311 * \param channel the channel on which the agent should be
312 listening. This value has to be >=0 and < than the maximal
313 number of channels fixed with MSG_set_channel_number().
314 * \param host the host that is to be watched.
315 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
316 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
319 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
321 return MSG_task_get_ext(task, channel, -1, host);
324 /** \ingroup msg_gos_functions
325 * \brief Listen on a channel and wait for receiving a task.
327 * It takes two parameters.
328 * \param task a memory location for storing a #m_task_t. It will
329 hold a task when this function will return. Thus \a task should not
330 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
331 those two condition does not hold, there will be a warning message.
332 * \param channel the channel on which the agent should be
333 listening. This value has to be >=0 and < than the maximal
334 number of channels fixed with MSG_set_channel_number().
335 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
336 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
338 MSG_error_t MSG_task_get(m_task_t * task, m_channel_t channel)
340 return MSG_task_get_with_timeout(task, channel, -1);
343 /** \ingroup msg_gos_functions
344 * \brief Listen on a channel and wait for receiving a task with a timeout.
346 * It takes three parameters.
347 * \param task a memory location for storing a #m_task_t. It will
348 hold a task when this function will return. Thus \a task should not
349 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
350 those two condition does not hold, there will be a warning message.
351 * \param channel the channel on which the agent should be
352 listening. This value has to be >=0 and < than the maximal
353 number of channels fixed with MSG_set_channel_number().
354 * \param max_duration the maximum time to wait for a task before giving
355 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
356 will not be modified and will still be
357 equal to \c NULL when returning.
358 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
359 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
362 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel,
365 return MSG_task_get_ext(task, channel, max_duration, NULL);
368 /** \defgroup msg_gos_functions MSG Operating System Functions
369 * \brief This section describes the functions that can be used
370 * by an agent for handling some task.
374 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,
377 xbt_assert1((channel >= 0)
378 && (channel < msg_global->max_channel), "Invalid channel %d",
382 MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
383 (MSG_host_self(), channel), task, host, timeout);
387 MSG_task_receive_from_host(m_task_t * task, const char *alias, m_host_t host)
389 return MSG_task_receive_ext(task, alias, -1, host);
392 MSG_error_t MSG_task_receive(m_task_t * task, const char *alias)
394 return MSG_task_receive_with_timeout(task, alias, -1);
398 MSG_task_receive_with_timeout(m_task_t * task, const char *alias,
401 return MSG_task_receive_ext(task, alias, timeout, NULL);
405 MSG_task_receive_ext(m_task_t * task, const char *alias, double timeout,
408 DEBUG1("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",alias);
409 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host,
413 msg_comm_t MSG_task_isend(m_task_t task, const char *alias) {
414 simdata_task_t t_simdata = NULL;
415 m_process_t process = MSG_process_self();
416 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
420 /* FIXME: these functions are not tracable */
422 /* Prepare the task to send */
423 t_simdata = task->simdata;
424 t_simdata->sender = process;
425 t_simdata->source = MSG_host_self();
427 xbt_assert0(t_simdata->refcount == 1,
428 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
430 t_simdata->refcount++;
431 msg_global->sent_msg++;
432 process->simdata->waiting_task = task;
434 /* Send it by calling SIMIX network layer */
436 /* Kept for semantical compatibility with older implementation */
438 SIMIX_cond_signal(mailbox->cond);
440 return SIMIX_network_isend(mailbox->rdv, t_simdata->message_size, t_simdata->rate,
441 task, sizeof(void*), &t_simdata->comm);
444 msg_comm_t MSG_task_irecv(m_task_t * task, const char *alias) {
446 smx_rdv_t rdv = MSG_mailbox_get_by_alias(alias)->rdv;
447 msg_mailbox_t mailbox=MSG_mailbox_get_by_alias(alias);
448 size_t size = sizeof(void*);
452 /* FIXME: these functions are not tracable */
454 memset(&comm,0,sizeof(comm));
456 /* Kept for compatibility with older implementation */
457 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
458 "A process is already blocked on this channel %s",
459 MSG_mailbox_get_alias(mailbox));
462 xbt_assert0(task, "Null pointer for the task storage");
465 CRITICAL0("MSG_task_get() was asked to write in a non empty task struct.");
467 /* Try to receive it by calling SIMIX network layer */
468 return SIMIX_network_irecv(rdv, task, &size);
471 int MSG_comm_test(msg_comm_t comm) {
472 return SIMIX_network_test(comm);
475 /* After received TRUE to MSG_comm_test() function we have to destroy the communication */
476 void MSG_comm_destroy(msg_comm_t comm) {
477 if(!(comm->src_proc == SIMIX_process_self()))
480 task = (m_task_t) SIMIX_communication_get_src_buf(comm);
481 task->simdata->refcount--;
483 SIMIX_communication_destroy(comm);
486 MSG_error_t MSG_comm_wait(msg_comm_t comm, double timeout) {
488 MSG_error_t res = MSG_OK;
490 SIMIX_network_wait(comm,timeout);
492 if(!(comm->src_proc == SIMIX_process_self()))
495 task = (m_task_t) SIMIX_communication_get_src_buf(comm);
496 task->simdata->refcount--;
499 /* FIXME: these functions are not tracable */
503 res = MSG_HOST_FAILURE;
506 res = MSG_TRANSFER_FAILURE;
512 xbt_die(bprintf("Unhandled SIMIX network exception: %s",e.msg));
520 /* This function is called by a sender and permit to wait for each communication
523 * - msg_comm_t *comm a vector of communication
524 * - int nb_elem is th esize of the comm vector
525 * - timeout for each call of MSG_comm_wait
528 void MSG_comm_waitall(msg_comm_t *comm,int nb_elem, double timeout) {
530 for(i=0; i<nb_elem; i++)
532 MSG_comm_wait(comm[i],timeout);
536 int MSG_comm_waitany(xbt_dynar_t comms) {
537 return SIMIX_network_waitany(comms);
540 m_task_t MSG_comm_get_task(msg_comm_t comm) {
541 xbt_assert0(comm, "Invalid parameters");
542 return (m_task_t) SIMIX_communication_get_src_buf(comm);
545 /** \ingroup msg_gos_functions
546 * \brief Put a task on a channel of an host and waits for the end of the
549 * This function is used for describing the behavior of an agent. It
550 * takes three parameter.
551 * \param task a #m_task_t to send on another location. This task
552 will not be usable anymore when the function will return. There is
553 no automatic task duplication and you have to save your parameters
554 before calling this function. Tasks are unique and once it has been
555 sent to another location, you should not access it anymore. You do
556 not need to call MSG_task_destroy() but to avoid using, as an
557 effect of inattention, this task anymore, you definitely should
558 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
559 can be transfered iff it has been correctly created with
561 * \param dest the destination of the message
562 * \param channel the channel on which the agent should put this
563 task. This value has to be >=0 and < than the maximal number of
564 channels fixed with MSG_set_channel_number().
565 * \return #MSG_FATAL if \a task is not properly initialized and
566 * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
567 * this function was called was shut down. Returns
568 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
569 * (network failure, dest failure)
571 MSG_error_t MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
573 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
576 /** \ingroup msg_gos_functions
577 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
583 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel,
586 task->simdata->rate = maxrate;
587 return MSG_task_put(task, dest, channel);
590 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
591 * host (with a timeout on the waiting of the destination host) and
592 * waits for the end of the transmission.
594 * This function is used for describing the behavior of an agent. It
595 * takes four parameter.
596 * \param task a #m_task_t to send on another location. This task
597 will not be usable anymore when the function will return. There is
598 no automatic task duplication and you have to save your parameters
599 before calling this function. Tasks are unique and once it has been
600 sent to another location, you should not access it anymore. You do
601 not need to call MSG_task_destroy() but to avoid using, as an
602 effect of inattention, this task anymore, you definitely should
603 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
604 can be transfered iff it has been correctly created with
606 * \param dest the destination of the message
607 * \param channel the channel on which the agent should put this
608 task. This value has to be >=0 and < than the maximal number of
609 channels fixed with MSG_set_channel_number().
610 * \param timeout the maximum time to wait for a task before giving
611 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
613 * \return #MSG_FATAL if \a task is not properly initialized and
614 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
615 this function was called was shut down. Returns
616 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
617 (network failure, dest failure, timeout...)
620 MSG_task_put_with_timeout(m_task_t task, m_host_t dest, m_channel_t channel,
623 xbt_assert1((channel >= 0)
624 && (channel < msg_global->max_channel), "Invalid channel %d",
628 MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel(dest, channel),
632 MSG_error_t MSG_task_send(m_task_t task, const char *alias)
634 DEBUG1("MSG_task_send: Trying to send a message on mailbox '%s'",alias);
635 return MSG_task_send_with_timeout(task, alias, -1);
640 MSG_task_send_bounded(m_task_t task, const char *alias, double maxrate)
642 task->simdata->rate = maxrate;
643 return MSG_task_send(task, alias);
648 MSG_task_send_with_timeout(m_task_t task, const char *alias, double timeout)
650 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias), task,
654 int MSG_task_listen(const char *alias)
658 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
661 /** \ingroup msg_gos_functions
662 * \brief Test whether there is a pending communication on a channel.
664 * It takes one parameter.
665 * \param channel the channel on which the agent should be
666 listening. This value has to be >=0 and < than the maximal
667 number of channels fixed with MSG_set_channel_number().
668 * \return 1 if there is a pending communication and 0 otherwise
670 int MSG_task_Iprobe(m_channel_t channel)
672 xbt_assert1((channel >= 0)
673 && (channel < msg_global->max_channel), "Invalid channel %d",
679 !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
680 (MSG_host_self(), channel));
683 /** \ingroup msg_gos_functions
685 * \brief Return the number of tasks waiting to be received on a \a
686 channel and sent by \a host.
688 * It takes two parameters.
689 * \param channel the channel on which the agent should be
690 listening. This value has to be >=0 and < than the maximal
691 number of channels fixed with MSG_set_channel_number().
692 * \param host the host that is to be watched.
693 * \return the number of tasks waiting to be received on \a channel
696 int MSG_task_probe_from_host(int channel, m_host_t host)
698 xbt_assert1((channel >= 0)
699 && (channel < msg_global->max_channel), "Invalid channel %d",
705 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
706 (MSG_host_self(), channel),
711 int MSG_task_listen_from_host(const char *alias, m_host_t host)
716 MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias(alias),
720 /** \ingroup msg_gos_functions
721 * \brief Test whether there is a pending communication on a channel, and who sent it.
723 * It takes one parameter.
724 * \param channel the channel on which the agent should be
725 listening. This value has to be >=0 and < than the maximal
726 number of channels fixed with MSG_set_channel_number().
727 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
729 int MSG_task_probe_from(m_channel_t channel)
735 xbt_assert1((channel >= 0)
736 && (channel < msg_global->max_channel), "Invalid channel %d",
741 MSG_mailbox_get_head(MSG_mailbox_get_by_channel
742 (MSG_host_self(), channel))))
745 return MSG_process_get_PID(task->simdata->sender);
748 int MSG_task_listen_from(const char *alias)
754 if (NULL == (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
757 return MSG_process_get_PID(task->simdata->sender);
760 /** \ingroup msg_gos_functions
761 * \brief Wait for at most \a max_duration second for a task reception
764 * \a PID is updated with the PID of the first process that triggered this event if any.
766 * It takes three parameters:
767 * \param channel the channel on which the agent should be
768 listening. This value has to be >=0 and < than the maximal.
769 number of channels fixed with MSG_set_channel_number().
770 * \param PID a memory location for storing an int.
771 * \param timeout the maximum time to wait for a task before
772 giving up. In the case of a reception, *\a PID will be updated
773 with the PID of the first process to send a task.
774 * \return #MSG_HOST_FAILURE if the host is shut down in the meantime
775 and #MSG_OK otherwise.
778 MSG_channel_select_from(m_channel_t channel, double timeout, int *PID)
781 simdata_host_t h_simdata = NULL;
785 msg_mailbox_t mailbox;
787 xbt_assert1((channel >= 0)
788 && (channel < msg_global->max_channel), "Invalid channel %d",
795 if (timeout == 0.0) {
796 *PID = MSG_task_probe_from(channel);
801 h_simdata = h->simdata;
803 mailbox = MSG_mailbox_get_by_channel(MSG_host_self(), channel);
805 while (MSG_mailbox_is_empty(mailbox)) {
812 SIMIX_mutex_lock(h_simdata->mutex);
814 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
815 "A process is already blocked on this channel %d", channel);
817 cond = SIMIX_cond_init();
819 MSG_mailbox_set_cond(mailbox, cond);
822 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
824 SIMIX_cond_wait(cond, h_simdata->mutex);
827 SIMIX_cond_destroy(cond);
828 SIMIX_mutex_unlock(h_simdata->mutex);
830 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
831 MSG_RETURN(MSG_HOST_FAILURE);
834 MSG_mailbox_set_cond(mailbox, NULL);
838 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
843 *PID = MSG_process_get_PID(t->simdata->sender);
851 MSG_error_t MSG_alias_select_from(const char *alias, double timeout, int *PID)
854 simdata_host_t h_simdata = NULL;
858 msg_mailbox_t mailbox;
864 if (timeout == 0.0) {
865 *PID = MSG_task_listen_from(alias);
870 h_simdata = h->simdata;
872 DEBUG2("Probing on alias %s (%s)", alias, h->name);
874 mailbox = MSG_mailbox_get_by_alias(alias);
876 while (MSG_mailbox_is_empty(mailbox)) {
883 SIMIX_mutex_lock(h_simdata->mutex);
885 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
886 "A process is already blocked on this alias %s", alias);
888 cond = SIMIX_cond_init();
890 MSG_mailbox_set_cond(mailbox, cond);
893 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
895 SIMIX_cond_wait(cond, h_simdata->mutex);
898 SIMIX_cond_destroy(cond);
899 SIMIX_mutex_unlock(h_simdata->mutex);
901 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
902 MSG_RETURN(MSG_HOST_FAILURE);
905 MSG_mailbox_set_cond(mailbox, NULL);
909 if (NULL == (t = MSG_mailbox_get_head(mailbox)))
914 *PID = MSG_process_get_PID(t->simdata->sender);