3 /* Copyright (c) 2008-2014. 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. */
9 #include "msg_mailbox.h"
10 #include "msg_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg,
13 "Logging specific to MSG (mailbox)");
15 msg_mailbox_t MSG_mailbox_new(const char *alias)
17 return simcall_rdv_create(alias);
20 void MSG_mailbox_free(void *mailbox)
22 simcall_rdv_destroy((msg_mailbox_t)mailbox);
25 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
27 return (NULL == simcall_rdv_get_head(mailbox));
30 msg_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
32 smx_synchro_t comm = simcall_rdv_get_head(mailbox);
37 return (msg_task_t) simcall_comm_get_src_data(comm);
41 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
44 return simcall_rdv_comm_count_by_host(mailbox, host);
47 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
50 msg_mailbox_t mailbox = simcall_rdv_get_by_name(alias);
53 mailbox = MSG_mailbox_new(alias);
58 /** \ingroup msg_mailbox_management
59 * \brief Set the mailbox to receive in asynchronous mode
61 * All messages sent to this mailbox will be transferred to
62 * the receiver without waiting for the receive call.
63 * The receive call will still be necessary to use the received data.
64 * If there is a need to receive some messages asynchronously, and some not,
65 * two different mailboxes should be used.
67 * \param alias The name of the mailbox
69 void MSG_mailbox_set_async(const char *alias){
70 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
72 simcall_rdv_set_receiver(mailbox, SIMIX_process_self());
73 XBT_VERB("%s mailbox set to receive eagerly for process %p\n",alias, SIMIX_process_self());
77 /** \ingroup msg_mailbox_management
78 * \brief Get a task from a mailbox on a given host
80 * \param mailbox The mailbox where the task was sent
81 * \param task a memory location for storing a #msg_task_t.
82 * \param host a #msg_host_t host from where the task was sent
83 * \param timeout a timeout
86 * #MSG_OK if the task was successfully received,
87 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
90 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, msg_task_t *task,
91 msg_host_t host, double timeout)
93 return MSG_mailbox_get_task_ext_bounded(mailbox, task, host, timeout, -1.0);
96 /** \ingroup msg_mailbox_management
97 * \brief Get a task from a mailbox on a given host at a given rate
99 * \param mailbox The mailbox where the task was sent
100 * \param task a memory location for storing a #msg_task_t.
101 * \param host a #msg_host_t host from where the task was sent
102 * \param timeout a timeout
106 * #MSG_OK if the task was successfully received,
107 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
110 MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * task,
111 msg_host_t host, double timeout, double rate)
114 msg_error_t ret = MSG_OK;
115 /* We no longer support getting a task from a specific host */
119 TRACE_msg_task_get_start();
120 double start_time = MSG_get_clock();
123 xbt_assert(task, "Null pointer for the task storage");
127 ("Asked to write the received task in a non empty struct -- proceeding.");
129 /* Try to receive it by calling SIMIX network layer */
131 simcall_comm_recv(mailbox, task, NULL, NULL, NULL, NULL, timeout, rate);
132 XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox);
133 if (msg_global->debug_multiple_use && (*task)->simdata->isused!=0)
134 xbt_ex_free(*(xbt_ex_t*)(*task)->simdata->isused);
135 (*task)->simdata->isused = 0;
138 switch (e.category) {
140 ret = MSG_HOST_FAILURE;
143 ret = MSG_TRANSFER_FAILURE;
149 ret = MSG_HOST_FAILURE;
157 if (ret != MSG_HOST_FAILURE &&
158 ret != MSG_TRANSFER_FAILURE &&
159 ret != MSG_TIMEOUT) {
160 TRACE_msg_task_get_end(start_time, *task);
166 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, msg_task_t task,
169 msg_error_t ret = MSG_OK;
170 simdata_task_t t_simdata = NULL;
171 msg_process_t process = MSG_process_self();
172 simdata_process_t p_simdata = SIMIX_process_self_get_data(process);
174 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
176 /* Prepare the task to send */
177 t_simdata = task->simdata;
178 t_simdata->sender = process;
179 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
181 if (t_simdata->isused != 0) {
182 if (msg_global->debug_multiple_use){
183 XBT_ERROR("This task is already used in there:");
184 xbt_backtrace_display(t_simdata->isused);
185 XBT_ERROR("And you try to reuse it from here:");
186 xbt_backtrace_display_current();
188 xbt_assert(t_simdata->isused == 0,
189 "This task is still being used somewhere else. You cannot send it now. Go fix your code! (use --cfg=msg/debug_multiple_use:on to get the backtrace of the other process)");
193 if (msg_global->debug_multiple_use)
194 MSG_BT(t_simdata->isused, "Using Backtrace");
196 t_simdata->isused = (void*)1;
197 t_simdata->comm = NULL;
198 msg_global->sent_msg++;
201 p_simdata->waiting_task = task;
204 /* Try to send it by calling SIMIX network layer */
206 smx_synchro_t comm = NULL; /* MC needs the comm to be set to NULL during the simix call */
207 comm = simcall_comm_isend(SIMIX_process_self(), mailbox,t_simdata->bytes_amount,
208 t_simdata->rate, task, sizeof(void *),
209 NULL, NULL, NULL, task, 0);
210 if (TRACE_is_enabled())
211 simcall_set_category(comm, task->category);
212 t_simdata->comm = comm;
213 simcall_comm_wait(comm, timeout);
217 switch (e.category) {
219 ret = MSG_HOST_FAILURE;
222 ret = MSG_TRANSFER_FAILURE;
232 /* If the send failed, it is not used anymore */
233 if (msg_global->debug_multiple_use && t_simdata->isused!=0)
234 xbt_ex_free(*(xbt_ex_t*)t_simdata->isused);
235 t_simdata->isused = 0;
239 p_simdata->waiting_task = NULL;
241 TRACE_msg_task_put_end();
245 #ifdef MSG_USE_DEPRECATED
246 msg_mailbox_t MSG_mailbox_get_by_channel(msg_host_t host,
249 XBT_WARN("DEPRECATED! Now use MSG_mailbox_get_by_alias");
250 xbt_assert((host != NULL), "Invalid host");
251 xbt_assert((channel >= 0)
252 && (channel < msg_global->max_channel), "Invalid channel %d",
255 return MSG_host_priv(host)->mailboxes[(size_t) channel];