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. */
9 #include "simgrid/msg.h"
10 #include "msg_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg, "Logging specific to MSG (mailbox)");
14 msg_mailbox_t MSG_mailbox_new(const char *alias)
16 return simcall_mbox_create(alias);
19 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
21 return (NULL == simcall_mbox_get_head(mailbox));
24 msg_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
26 smx_synchro_t comm = simcall_mbox_get_head(mailbox);
31 return (msg_task_t) simcall_comm_get_src_data(comm);
34 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
36 msg_mailbox_t mailbox = simcall_mbox_get_by_name(alias);
39 mailbox = MSG_mailbox_new(alias);
44 /** \ingroup msg_mailbox_management
45 * \brief Set the mailbox to receive in asynchronous mode
47 * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
48 * The receive call will still be necessary to use the received data.
49 * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
51 * \param alias The name of the mailbox
53 void MSG_mailbox_set_async(const char *alias){
54 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
56 simcall_mbox_set_receiver(mailbox, SIMIX_process_self());
57 XBT_VERB("%s mailbox set to receive eagerly for process %p\n",alias, SIMIX_process_self());
60 /** \ingroup msg_mailbox_management
61 * \brief Get a task from a mailbox on a given host
63 * \param mailbox The mailbox where the task was sent
64 * \param task a memory location for storing a #msg_task_t.
65 * \param host a #msg_host_t host from where the task was sent
66 * \param timeout a timeout
69 * #MSG_OK if the task was successfully received,
70 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
72 msg_error_t MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, msg_task_t *task, msg_host_t host, double timeout)
74 return MSG_mailbox_get_task_ext_bounded(mailbox, task, host, timeout, -1.0);
77 /** \ingroup msg_mailbox_management
78 * \brief Get a task from a mailbox on a given host at a given rate
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
87 * #MSG_OK if the task was successfully received,
88 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
90 msg_error_t MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * task, msg_host_t host, double timeout,
94 msg_error_t ret = MSG_OK;
95 /* We no longer support getting a task from a specific host */
99 TRACE_msg_task_get_start();
100 double start_time = MSG_get_clock();
103 xbt_assert(task, "Null pointer for the task storage");
106 XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
108 /* Try to receive it by calling SIMIX network layer */
110 simcall_comm_recv(MSG_process_self(), mailbox, task, NULL, NULL, NULL, NULL, timeout, rate);
111 XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox);
112 if (msg_global->debug_multiple_use && (*task)->simdata->isused!=0)
113 xbt_ex_free(*(xbt_ex_t*)(*task)->simdata->isused);
114 (*task)->simdata->isused = 0;
117 switch (e.category) {
119 ret = MSG_HOST_FAILURE;
122 ret = MSG_TRANSFER_FAILURE;
128 ret = MSG_HOST_FAILURE;
136 if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
137 TRACE_msg_task_get_end(start_time, *task);
142 msg_error_t MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, msg_task_t task, double timeout)
144 msg_error_t ret = MSG_OK;
145 simdata_task_t t_simdata = NULL;
146 msg_process_t process = MSG_process_self();
147 simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data();
149 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
151 /* Prepare the task to send */
152 t_simdata = task->simdata;
153 t_simdata->sender = process;
154 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
156 if (t_simdata->isused != 0) {
157 if (msg_global->debug_multiple_use){
158 XBT_ERROR("This task is already used in there:");
159 xbt_backtrace_display((xbt_ex_t*) t_simdata->isused);
160 XBT_ERROR("And you try to reuse it from here:");
161 xbt_backtrace_display_current();
163 xbt_assert(t_simdata->isused == 0,
164 "This task is still being used somewhere else. You cannot send it now. Go fix your code!"
165 " (use --cfg=msg/debug_multiple_use:on to get the backtrace of the other process)");
169 if (msg_global->debug_multiple_use)
170 MSG_BT(t_simdata->isused, "Using Backtrace");
172 t_simdata->isused = (void*)1;
173 t_simdata->comm = NULL;
174 msg_global->sent_msg++;
176 p_simdata->waiting_task = task;
179 /* Try to send it by calling SIMIX network layer */
181 smx_synchro_t comm = NULL; /* MC needs the comm to be set to NULL during the simix call */
182 comm = simcall_comm_isend(SIMIX_process_self(), mailbox,t_simdata->bytes_amount,
183 t_simdata->rate, task, sizeof(void *), NULL, NULL, NULL, task, 0);
184 if (TRACE_is_enabled())
185 simcall_set_category(comm, task->category);
186 t_simdata->comm = comm;
187 simcall_comm_wait(comm, timeout);
191 switch (e.category) {
193 ret = MSG_HOST_FAILURE;
196 ret = MSG_TRANSFER_FAILURE;
206 /* If the send failed, it is not used anymore */
207 if (msg_global->debug_multiple_use && t_simdata->isused!=0)
208 xbt_ex_free(*(xbt_ex_t*)t_simdata->isused);
209 t_simdata->isused = 0;
212 p_simdata->waiting_task = NULL;
214 TRACE_msg_task_put_end();