/** \ingroup msg_task_usage * \brief Receives a task from a mailbox from a specific host with a given timeout. * * This is a blocking function with a timeout, the execution flow will be blocked * until the task is received or the timeout is achieved. See #MSG_task_irecv * for receiving tasks asynchronously. You can provide a -1 timeout * to obtain an infinite timeout. * * \param task a memory location for storing a #msg_task_t. * \param alias name of the mailbox to receive the task from * \param timeout is the maximum wait time for completion (provide -1 for no timeout) * \param host a #msg_host_t host from where the task was sent * * \return Returns * #MSG_OK if the task was successfully received, * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise. */ msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host) { xbt_ex_t e; msg_error_t ret = MSG_OK; XBT_DEBUG ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias); TRY { ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host, timeout); } CATCH(e) { switch (e.category) { case cancel_error: /* may be thrown by MSG_mailbox_get_by_alias */ ret = MSG_HOST_FAILURE; break; default: RETHROW; } xbt_ex_free(e); } return ret; }
/** Receiver function */ int slave(int argc, char *argv[]) { m_task_t task = NULL; int res; int id = -1; xbt_assert1(sscanf(argv[1],"%d", &id), "Invalid argument %s\n",argv[1]); MSG_process_sleep(1); /* Make sure the master is done creating the mailboxes */ while(1) { res = MSG_mailbox_get_task_ext(mb[id], &(task), NULL, -1); xbt_assert0(res == MSG_OK, "MSG_task_get failed"); INFO1("Received \"%s\"", MSG_task_get_name(task)); if (!strcmp(MSG_task_get_name(task),"finalize")) { MSG_task_destroy(task); break; } INFO1("Processing \"%s\"", MSG_task_get_name(task)); MSG_task_execute(task); INFO1("\"%s\" done", MSG_task_get_name(task)); MSG_task_destroy(task); task = NULL; } INFO0("I'm done. See you!"); return 0; } /* end_of_slave */
/** \ingroup msg_task_usage * \brief Receives a task from a mailbox from a specific host with a given timeout. * * This is a blocking function with a timeout, the execution flow will be blocked * until the task is received or the timeout is achieved. See #MSG_task_irecv * for receiving tasks asynchronously. You can provide a -1 timeout * to obtain an infinite timeout. * * \param task a memory location for storing a #msg_task_t. * \param alias name of the mailbox to receive the task from * \param timeout is the maximum wait time for completion (provide -1 for no timeout) * \param host a #msg_host_t host from where the task was sent * * \return Returns * #MSG_OK if the task was successfully received, * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise. */ msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host) { XBT_DEBUG ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias); return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host, timeout); }