3 /* Copyright (c) 2008, 2009, 2010. 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. */
10 #include "msg/private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg,
13 "Logging specific to MSG (mailbox)");
15 static xbt_dict_t msg_mailboxes = NULL;
17 void MSG_mailbox_mod_init(void)
19 msg_mailboxes = xbt_dict_new();
22 void MSG_mailbox_mod_exit(void)
24 xbt_dict_free(&msg_mailboxes);
27 msg_mailbox_t MSG_mailbox_create(const char *alias)
29 msg_mailbox_t mailbox = xbt_new0(s_msg_mailbox_t, 1);
32 mailbox->alias = alias ? xbt_strdup(alias) : NULL;
33 mailbox->rdv = SIMIX_rdv_create(alias);
38 msg_mailbox_t MSG_mailbox_new(const char *alias)
40 msg_mailbox_t mailbox = MSG_mailbox_create(alias);
42 /* add the mbox in the dictionary */
43 xbt_dict_set(msg_mailboxes, alias, mailbox, MSG_mailbox_free);
48 void MSG_mailbox_free(void *mailbox)
50 msg_mailbox_t _mailbox = (msg_mailbox_t) mailbox;
52 free(_mailbox->alias);
53 SIMIX_rdv_destroy(_mailbox->rdv);
58 smx_cond_t MSG_mailbox_get_cond(msg_mailbox_t mailbox)
63 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
65 return (NULL == SIMIX_rdv_get_head(mailbox->rdv));
68 m_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
70 smx_comm_t comm = SIMIX_rdv_get_head(mailbox->rdv);
75 return (m_task_t) SIMIX_communication_get_data(comm);
79 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
82 return SIMIX_rdv_get_count_waiting_comm(mailbox->rdv,
83 host->simdata->smx_host);
86 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
91 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
93 return mailbox->alias;
96 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
99 msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
102 mailbox = MSG_mailbox_new(alias);
107 msg_mailbox_t MSG_mailbox_get_by_channel(m_host_t host,
110 xbt_assert0((host != NULL), "Invalid host");
111 xbt_assert1((channel >= 0)
112 && (channel < msg_global->max_channel), "Invalid channel %d",
115 return host->simdata->mailboxes[(size_t) channel];
119 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t * task,
120 m_host_t host, double timeout)
123 MSG_error_t ret = MSG_OK;
126 double start_time = 0;
128 /* We no longer support getting a task from a specific host */
134 TRACE_msg_task_get_start();
135 start_time = MSG_get_clock();
138 /* Kept for compatibility with older implementation */
139 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
140 "A process is already blocked on this channel %s",
141 MSG_mailbox_get_alias(mailbox));
144 xbt_assert0(task, "Null pointer for the task storage");
148 ("MSG_task_get() was asked to write in a non empty task struct.");
150 /* Try to receive it by calling SIMIX network layer */
152 SIMIX_network_recv(mailbox->rdv, timeout, task, NULL, &comm);
153 //INFO2("Got task %s from %s",(*task)->name,mailbox->alias);
154 (*task)->simdata->refcount--;
157 switch (e.category) {
159 ret = MSG_HOST_FAILURE;
162 ret = MSG_TRANSFER_FAILURE;
168 xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
173 if (ret != MSG_HOST_FAILURE &&
174 ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
176 TRACE_msg_task_get_end(start_time, *task);
183 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
187 MSG_error_t ret = MSG_OK;
188 simdata_task_t t_simdata = NULL;
189 m_process_t process = MSG_process_self();
196 call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
200 /* Prepare the task to send */
201 t_simdata = task->simdata;
202 t_simdata->sender = process;
203 t_simdata->source = MSG_host_self();
205 xbt_assert0(t_simdata->refcount == 1,
206 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
208 t_simdata->refcount++;
209 msg_global->sent_msg++;
211 process->simdata->waiting_task = task;
213 /* Try to send it by calling SIMIX network layer */
215 /* Kept for semantical compatibility with older implementation */
217 SIMIX_cond_signal(mailbox->cond);
219 SIMIX_network_send(mailbox->rdv, t_simdata->message_size,
220 t_simdata->rate, timeout, task, sizeof(void *),
221 &(t_simdata->comm), task);
225 switch (e.category) {
227 ret = MSG_HOST_FAILURE;
230 ret = MSG_TRANSFER_FAILURE;
236 xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
240 /* Decrement the refcount only on failure */
241 t_simdata->refcount--;
244 process->simdata->waiting_task = NULL;
247 TRACE_msg_task_put_end();