3 /* Copyright (c) 2008-2015. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "simgrid/msg.h"
12 #include "msg_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg, "Logging specific to MSG (mailbox)");
16 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
18 return (nullptr == simcall_mbox_front(mailbox));
21 msg_task_t MSG_mailbox_front(msg_mailbox_t mailbox)
23 simgrid::simix::Comm* comm = static_cast<simgrid::simix::Comm*>(simcall_mbox_front(mailbox));
28 return (msg_task_t) comm->src_data;
31 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
33 msg_mailbox_t mailbox = simcall_mbox_get_by_name(alias);
36 mailbox = simcall_mbox_create(alias);
41 /** \ingroup msg_mailbox_management
42 * \brief Set the mailbox to receive in asynchronous mode
44 * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
45 * The receive call will still be necessary to use the received data.
46 * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
48 * \param alias The name of the mailbox
50 void MSG_mailbox_set_async(const char *alias){
51 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
53 simcall_mbox_set_receiver(mailbox, SIMIX_process_self());
54 XBT_VERB("%s mailbox set to receive eagerly for process %p\n",alias, SIMIX_process_self());
57 /** \ingroup msg_mailbox_management
58 * \brief Get a task from a mailbox on a given host
60 * \param mailbox The mailbox where the task was sent
61 * \param task a memory location for storing a #msg_task_t.
62 * \param host a #msg_host_t host from where the task was sent
63 * \param timeout a timeout
66 * #MSG_OK if the task was successfully received,
67 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
69 msg_error_t MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, msg_task_t *task, msg_host_t host, double timeout)
71 return MSG_mailbox_get_task_ext_bounded(mailbox, task, host, timeout, -1.0);
74 /** \ingroup msg_mailbox_management
75 * \brief Get a task from a mailbox on a given host at a given rate
77 * \param mailbox The mailbox where the task was sent
78 * \param task a memory location for storing a #msg_task_t.
79 * \param host a #msg_host_t host from where the task was sent
80 * \param timeout a timeout
84 * #MSG_OK if the task was successfully received,
85 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
87 msg_error_t MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * task, msg_host_t host, double timeout,
90 msg_error_t ret = MSG_OK;
91 /* We no longer support getting a task from a specific host */
95 TRACE_msg_task_get_start();
96 double start_time = MSG_get_clock();
99 xbt_assert(task, "Null pointer for the task storage");
102 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
104 /* Try to receive it by calling SIMIX network layer */
106 simcall_comm_recv(MSG_process_self(), mailbox, task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
107 XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox);
108 (*task)->simdata->setNotUsed();
111 switch (e.category) {
113 ret = MSG_HOST_FAILURE;
116 ret = MSG_TRANSFER_FAILURE;
122 ret = MSG_HOST_FAILURE;
129 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
130 TRACE_msg_task_get_end(start_time, *task);