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;
31 msg_mailbox_t MSG_mailbox_new(const char *alias)
33 msg_mailbox_t mailbox = MSG_mailbox_create(alias);
35 /* add the mbox in the dictionary */
36 xbt_dict_set(msg_mailboxes, alias, mailbox, MSG_mailbox_free);
41 void MSG_mailbox_free(void *mailbox)
43 msg_mailbox_t _mailbox = (msg_mailbox_t) mailbox;
45 if (NULL != (_mailbox->hostname))
46 free(_mailbox->hostname);
48 xbt_fifo_free(_mailbox->tasks);
49 free(_mailbox->alias);
54 void MSG_mailbox_put(msg_mailbox_t mailbox, m_task_t task)
56 xbt_fifo_push(mailbox->tasks, task);
59 smx_cond_t MSG_mailbox_get_cond(msg_mailbox_t mailbox)
64 void MSG_mailbox_remove(msg_mailbox_t mailbox, m_task_t task)
66 xbt_fifo_remove(mailbox->tasks, task);
69 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
71 return (NULL == xbt_fifo_get_first_item(mailbox->tasks));
74 m_task_t MSG_mailbox_pop_head(msg_mailbox_t mailbox)
76 return (m_task_t) xbt_fifo_shift(mailbox->tasks);
79 m_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
83 if (NULL == (item = xbt_fifo_get_first_item(mailbox->tasks)))
86 return (m_task_t) xbt_fifo_get_item_content(item);
91 MSG_mailbox_get_first_host_task(msg_mailbox_t mailbox, m_host_t host)
94 xbt_fifo_item_t item = NULL;
96 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
97 if (task->simdata->source == host) {
98 xbt_fifo_remove_item(mailbox->tasks, item);
106 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
109 m_task_t task = NULL;
110 xbt_fifo_item_t item = NULL;
113 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t) {
114 if (task->simdata->source == host)
121 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
123 mailbox->cond = cond;
126 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
128 return mailbox->alias;
131 const char *MSG_mailbox_get_hostname(msg_mailbox_t mailbox)
133 return mailbox->hostname;
136 void MSG_mailbox_set_hostname(msg_mailbox_t mailbox, const char *hostname)
138 mailbox->hostname = xbt_strdup(hostname);
141 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
144 msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
147 mailbox = MSG_mailbox_new(alias);
148 MSG_mailbox_set_hostname(mailbox, MSG_host_self()->name);
155 MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
157 xbt_assert0((host != NULL), "Invalid host");
158 xbt_assert1((channel >= 0)
159 && (channel < msg_global->max_channel), "Invalid channel %d",
162 return host->simdata->mailboxes[(size_t) channel];
166 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t * task,
167 m_host_t host, double timeout)
169 m_process_t process = MSG_process_self();
172 simdata_task_t t_simdata = NULL;
173 simdata_host_t h_simdata = NULL;
176 smx_cond_t cond = NULL; //conditional wait if the task isn't on the channel yet
181 xbt_assert0(task, "Null pointer for the task storage");
185 ("MSG_task_get() was asked to write in a non empty task struct.");
189 h_simdata = h->simdata;
191 SIMIX_mutex_lock(h->simdata->mutex);
194 /* if the mailbox is empty (has no task */
195 if (!MSG_mailbox_is_empty(mailbox)) {
197 /* pop the head of the mailbox */
198 t = MSG_mailbox_pop_head(mailbox);
201 /* get the first task of the host */
202 if (NULL != (t = MSG_mailbox_get_first_host_task(mailbox, host)))
209 SIMIX_mutex_unlock(h->simdata->mutex);
210 /* set the simix condition of the mailbox to NULL */
211 MSG_mailbox_set_cond(mailbox, NULL);
212 SIMIX_cond_destroy(cond);
213 MSG_RETURN(MSG_TRANSFER_FAILURE);
217 if (MSG_mailbox_get_cond(mailbox)) {
218 CRITICAL1("A process is already blocked on the channel %s",
219 MSG_mailbox_get_alias(mailbox));
220 SIMIX_cond_display_info(MSG_mailbox_get_cond(mailbox));
221 xbt_die("Go fix your code!");
224 cond = SIMIX_cond_init();
226 /* set the condition of the mailbox */
227 MSG_mailbox_set_cond(mailbox, cond);
230 SIMIX_cond_wait_timeout(cond, h->simdata->mutex, timeout);
232 SIMIX_cond_wait(MSG_mailbox_get_cond(mailbox), h->simdata->mutex);
235 if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
236 MSG_RETURN(MSG_HOST_FAILURE);
241 SIMIX_mutex_unlock(h->simdata->mutex);
243 DEBUG1("OK, got a task (%s)", t->name);
244 /* clean conditional */
246 SIMIX_cond_destroy(cond);
248 MSG_mailbox_set_cond(mailbox, NULL);
251 t_simdata = t->simdata;
252 t_simdata->receiver = process;
255 SIMIX_mutex_lock(t_simdata->mutex);
258 /* create SIMIX action to the communication */
260 SIMIX_action_communicate(t_simdata->sender->simdata->m_host->
262 process->simdata->m_host->simdata->smx_host,
263 t->name, t_simdata->message_size,
266 /* if the process is suspend, create the action but stop its execution, it will be restart when the sender process resume */
267 if (MSG_process_is_suspended(t_simdata->sender)) {
268 DEBUG1("Process sender (%s) suspended", t_simdata->sender->name);
269 SIMIX_action_set_priority(t_simdata->comm, 0);
272 process->simdata->waiting_task = t;
273 SIMIX_register_action_to_condition(t_simdata->comm, t_simdata->cond);
276 SIMIX_cond_wait(t_simdata->cond, t_simdata->mutex);
278 if (SIMIX_action_get_state(t_simdata->comm) != SURF_ACTION_RUNNING)
282 SIMIX_unregister_action_to_condition(t_simdata->comm, t_simdata->cond);
283 process->simdata->waiting_task = NULL;
285 /* the task has already finished and the pointer must be null */
286 if (t->simdata->sender) {
287 t->simdata->sender->simdata->waiting_task = NULL;
290 /* for this process, don't need to change in get function */
291 t->simdata->receiver = NULL;
292 SIMIX_mutex_unlock(t_simdata->mutex);
295 if (SIMIX_action_get_state(t_simdata->comm) == SURF_ACTION_DONE) {
296 SIMIX_action_destroy(t_simdata->comm);
297 t_simdata->comm = NULL;
300 } else if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
301 SIMIX_action_destroy(t_simdata->comm);
302 t_simdata->comm = NULL;
304 MSG_RETURN(MSG_HOST_FAILURE);
306 SIMIX_action_destroy(t_simdata->comm);
307 t_simdata->comm = NULL;
309 MSG_RETURN(MSG_TRANSFER_FAILURE);
314 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
317 m_process_t process = MSG_process_self();
318 const char *hostname;
319 simdata_task_t task_simdata = NULL;
320 m_host_t local_host = NULL;
321 m_host_t remote_host = NULL;
322 smx_cond_t cond = NULL;
326 task_simdata = task->simdata;
327 task_simdata->sender = process;
328 task_simdata->source = MSG_process_get_host(process);
330 xbt_assert0(task_simdata->using == 1,
331 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
333 task_simdata->comm = NULL;
335 task_simdata->using++;
336 local_host = ((simdata_process_t) process->simdata)->m_host;
338 /* get the host name containing the mailbox */
339 hostname = MSG_mailbox_get_hostname(mailbox);
341 remote_host = MSG_get_host_by_name(hostname);
343 if (NULL == remote_host)
344 THROW1(not_found_error, 0, "Host %s not fount", hostname);
348 ("Trying to send a task (%g kB) from %s to %s on the channel aliased by the alias %s",
349 task->simdata->message_size / 1000, local_host->name,
350 remote_host->name, MSG_mailbox_get_alias(mailbox));
352 SIMIX_mutex_lock(remote_host->simdata->mutex);
354 /* put the task in the mailbox */
355 MSG_mailbox_put(mailbox, task);
357 if (NULL != (cond = MSG_mailbox_get_cond(mailbox))) {
358 DEBUG0("Somebody is listening. Let's wake him up!");
359 SIMIX_cond_signal(cond);
364 SIMIX_mutex_unlock(remote_host->simdata->mutex);
366 SIMIX_mutex_lock(task->simdata->mutex);
368 process->simdata->waiting_task = task;
374 time = SIMIX_get_clock();
377 /*verify if the action that ends is the correct. Call the wait_timeout with the new time. If the timeout occurs, an exception is raised */
379 time_elapsed = SIMIX_get_clock() - time;
380 SIMIX_cond_wait_timeout(task->simdata->cond, task->simdata->mutex,
381 timeout - time_elapsed);
383 if ((task->simdata->comm != NULL)
384 && (SIMIX_action_get_state(task->simdata->comm) !=
385 SURF_ACTION_RUNNING))
390 if (e.category == timeout_error) {
392 /* verify if the timeout happened and the communication didn't started yet */
393 if (task->simdata->comm == NULL) {
394 process->simdata->waiting_task = NULL;
396 /* remove the task from the mailbox */
397 MSG_mailbox_remove(mailbox, task);
399 if (task->simdata->receiver) {
400 task->simdata->receiver->simdata->waiting_task = NULL;
403 task->simdata->sender = NULL;
405 SIMIX_mutex_unlock(task->simdata->mutex);
406 MSG_RETURN(MSG_TRANSFER_FAILURE);
414 SIMIX_cond_wait(task->simdata->cond, task->simdata->mutex);
416 if (SIMIX_action_get_state(task->simdata->comm) !=
422 DEBUG1("Action terminated %s", task->name);
423 process->simdata->waiting_task = NULL;
425 /* the task has already finished and the pointer must be null */
426 if (task->simdata->receiver) {
427 task->simdata->receiver->simdata->waiting_task = NULL;
430 task->simdata->sender = NULL;
431 SIMIX_mutex_unlock(task->simdata->mutex);
434 if (SIMIX_action_get_state(task->simdata->comm) == SURF_ACTION_DONE) {
436 } else if (SIMIX_host_get_state(local_host->simdata->smx_host) == 0) {
437 MSG_RETURN(MSG_HOST_FAILURE);
439 MSG_RETURN(MSG_TRANSFER_FAILURE);