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);
24 mailbox->alias = alias ? xbt_strdup(alias) : NULL;
25 mailbox->rdv = SIMIX_rdv_create(alias);
30 msg_mailbox_t MSG_mailbox_new(const char *alias)
32 msg_mailbox_t mailbox = MSG_mailbox_create(alias);
34 /* add the mbox in the dictionary */
35 xbt_dict_set(msg_mailboxes, alias, mailbox, MSG_mailbox_free);
40 void MSG_mailbox_free(void *mailbox)
42 msg_mailbox_t _mailbox = (msg_mailbox_t) mailbox;
44 free(_mailbox->alias);
45 SIMIX_rdv_destroy(_mailbox->rdv);
50 smx_cond_t MSG_mailbox_get_cond(msg_mailbox_t mailbox)
55 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
57 return (NULL == SIMIX_rdv_get_head(mailbox->rdv));
60 m_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
62 smx_comm_t comm = SIMIX_rdv_get_head(mailbox->rdv);
67 return (m_task_t)SIMIX_communication_get_data(comm);
71 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox, m_host_t host)
73 return SIMIX_rdv_get_count_waiting_comm (mailbox->rdv, host->simdata->smx_host);
76 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
81 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
83 return mailbox->alias;
86 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
89 msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
92 mailbox = MSG_mailbox_new(alias);
97 msg_mailbox_t MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
99 xbt_assert0((host != NULL), "Invalid host");
100 xbt_assert1((channel >= 0)
101 && (channel < msg_global->max_channel), "Invalid channel %d",
104 return host->simdata->mailboxes[(size_t) channel];
108 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t *task, m_host_t host,
112 MSG_error_t ret = MSG_OK;
113 size_t buff_size = 0;
117 /* Kept for compatibility with older implementation */
118 xbt_assert1(!MSG_mailbox_get_cond(mailbox),
119 "A process is already blocked on this channel %s",
120 MSG_mailbox_get_alias(mailbox));
123 xbt_assert0(task, "Null pointer for the task storage");
127 ("MSG_task_get() was asked to write in a non empty task struct.");
129 /* We no loger support getting a task from a specific host */
133 /* Try to receive it by calling SIMIX network layer */
135 SIMIX_network_recv(mailbox->rdv, timeout, NULL, &buff_size, &comm);
140 ret = MSG_HOST_FAILURE;
143 ret = MSG_TRANSFER_FAILURE;
146 ret = MSG_TRANSFER_FAILURE;
149 xbt_die("Unhandled SIMIX network exception");
154 *task = SIMIX_communication_get_data(comm);
156 /* If the sender didn't decremented the refcount so far then do it */
157 if (*task && (*task)->simdata->refcount > 1)
158 (*task)->simdata->refcount--;
164 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
168 MSG_error_t ret = MSG_OK;
169 simdata_task_t t_simdata = NULL;
170 m_process_t process = MSG_process_self();
174 /* Prepare the task to send */
175 t_simdata = task->simdata;
176 t_simdata->sender = process;
177 t_simdata->source = MSG_host_self();
179 xbt_assert0(t_simdata->refcount == 1,
180 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
182 t_simdata->refcount++;
183 msg_global->sent_msg++;
185 process->simdata->waiting_task = task;
187 /* Try to send it by calling SIMIX network layer */
189 /* Kept for semantical compatibility with older implementation */
191 SIMIX_cond_signal(mailbox->cond);
193 SIMIX_network_send(mailbox->rdv, t_simdata->message_size, t_simdata->rate,
194 timeout, NULL, 0, &t_simdata->comm, task);
200 ret = MSG_HOST_FAILURE;
203 ret = MSG_TRANSFER_FAILURE;
206 ret = MSG_TRANSFER_FAILURE;
209 xbt_die("Unhandled SIMIX network exception");
212 /* If the receiver end didn't decremented the refcount so far then do it */
213 if (t_simdata->refcount > 1)
214 t_simdata->refcount--;
217 process->simdata->waiting_task = NULL;
219 /* If the receiver end didn't decremented the refcount so far then do it */
220 if (t_simdata->refcount > 1)
221 t_simdata->refcount--;