2 #include "msg/private.h"
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg,
5 "Logging specific to MSG (mailbox)");
7 static xbt_dict_t msg_mailboxes = NULL;
9 void MSG_mailbox_mod_init(void)
11 msg_mailboxes = xbt_dict_new();
14 void MSG_mailbox_mod_exit(void)
16 xbt_dict_free(&msg_mailboxes);
19 msg_mailbox_t MSG_mailbox_create(const char *alias)
21 msg_mailbox_t mailbox = xbt_new0(s_msg_mailbox_t, 1);
23 mailbox->tasks = xbt_fifo_new();
25 mailbox->alias = alias ? xbt_strdup(alias) : NULL;
26 mailbox->hostname = NULL;
27 mailbox->rdv = SIMIX_rdv_create(alias);
32 msg_mailbox_t MSG_mailbox_new(const char *alias)
34 msg_mailbox_t mailbox = MSG_mailbox_create(alias);
36 /* add the mbox in the dictionary */
37 xbt_dict_set(msg_mailboxes, alias, mailbox, MSG_mailbox_free);
42 void MSG_mailbox_free(void *mailbox)
44 msg_mailbox_t _mailbox = (msg_mailbox_t) mailbox;
46 if (_mailbox->hostname)
47 free(_mailbox->hostname);
49 xbt_fifo_free(_mailbox->tasks);
50 free(_mailbox->alias);
51 SIMIX_rdv_destroy(_mailbox->rdv);
56 smx_cond_t MSG_mailbox_get_cond(msg_mailbox_t mailbox)
61 void MSG_mailbox_remove(msg_mailbox_t mailbox, m_task_t task)
63 xbt_fifo_remove(mailbox->tasks, task);
66 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
68 return (NULL == xbt_fifo_get_first_item(mailbox->tasks));
71 m_task_t MSG_mailbox_pop_head(msg_mailbox_t mailbox)
73 return (m_task_t) xbt_fifo_shift(mailbox->tasks);
76 m_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
80 if (!(item = xbt_fifo_get_first_item(mailbox->tasks)))
83 return (m_task_t) xbt_fifo_get_item_content(item);
87 m_task_t MSG_mailbox_get_first_host_task(msg_mailbox_t mailbox, m_host_t host)
90 xbt_fifo_item_t item = NULL;
92 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
93 if (task->simdata->source == host) {
94 xbt_fifo_remove_item(mailbox->tasks, item);
102 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox, m_host_t host)
104 m_task_t task = NULL;
105 xbt_fifo_item_t item = NULL;
108 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t) {
109 if (task->simdata->source == host)
116 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
118 mailbox->cond = cond;
121 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
123 return mailbox->alias;
126 const char *MSG_mailbox_get_hostname(msg_mailbox_t mailbox)
128 return mailbox->hostname;
131 void MSG_mailbox_set_hostname(msg_mailbox_t mailbox, const char *hostname)
133 mailbox->hostname = xbt_strdup(hostname);
136 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
139 msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
142 mailbox = MSG_mailbox_new(alias);
143 MSG_mailbox_set_hostname(mailbox, MSG_host_self()->name);
149 msg_mailbox_t MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
151 xbt_assert0((host != NULL), "Invalid host");
152 xbt_assert1((channel >= 0)
153 && (channel < msg_global->max_channel), "Invalid channel %d",
156 return host->simdata->mailboxes[(size_t) channel];
160 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t *task,
161 m_host_t host, double timeout)
166 size_t task_size = sizeof(void*);
170 xbt_assert0(task, "Null pointer for the task storage");
174 ("MSG_task_get() was asked to write in a non empty task struct.");
176 smx_host = host ? host->simdata->smx_host : NULL;
179 SIMIX_network_recv(mailbox->rdv, timeout, task, &task_size,
180 comm_filter_get, smx_host);
185 ret = MSG_HOST_FAILURE;
188 ret = MSG_TRANSFER_FAILURE;
191 ret = MSG_TRANSFER_FAILURE;
197 /*xbt_die("Unhandled SIMIX network exception");*/
207 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
212 m_process_t process = MSG_process_self();
213 const char *hostname;
214 simdata_task_t t_simdata = NULL;
215 m_host_t local_host = NULL;
216 m_host_t remote_host = NULL;
220 t_simdata = task->simdata;
221 t_simdata->sender = process;
222 t_simdata->source = MSG_process_get_host(process);
224 xbt_assert0(t_simdata->refcount == 1,
225 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
227 t_simdata->comm = NULL;
229 /*t_simdata->refcount++;*/
230 local_host = ((simdata_process_t) process->simdata)->m_host;
231 msg_global->sent_msg++;
233 /* get the host name containing the mailbox */
234 hostname = MSG_mailbox_get_hostname(mailbox);
236 remote_host = MSG_get_host_by_name(hostname);
239 THROW1(not_found_error, 0, "Host %s not fount", hostname);
241 DEBUG4("Trying to send a task (%g kB) from %s to %s on the channel %s",
242 t_simdata->message_size / 1000, local_host->name,
243 remote_host->name, MSG_mailbox_get_alias(mailbox));
246 SIMIX_network_send(mailbox->rdv, t_simdata->message_size, t_simdata->rate,
247 timeout, &task, sizeof(void *), comm_filter_put, NULL);
253 ret = MSG_HOST_FAILURE;
256 ret = MSG_TRANSFER_FAILURE;
259 ret = MSG_TRANSFER_FAILURE;
265 /*xbt_die("Unhandled SIMIX network exception");*/
271 /* t_simdata->refcount--;*/