2 #include "msg/private.h"
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg,
5 "Logging specific to MSG (mailbox)");
11 MSG_mailbox_mod_init(void)
13 msg_mailboxes = xbt_dict_new();
17 MSG_mailbox_mod_exit(void)
19 xbt_dict_free(&msg_mailboxes);
23 MSG_mailbox_create(const char *alias)
25 msg_mailbox_t mailbox = xbt_new0(s_msg_mailbox_t,1);
27 mailbox->tasks = xbt_fifo_new();
29 mailbox->alias = alias ? xbt_strdup(alias) : NULL;
30 mailbox->hostname = NULL;
36 MSG_mailbox_new(const char *alias)
38 msg_mailbox_t mailbox = MSG_mailbox_create(alias);
40 /* add the mbox in the dictionary */
41 xbt_dict_set(msg_mailboxes, alias, mailbox, MSG_mailbox_free);
47 MSG_mailbox_free(void* mailbox)
49 msg_mailbox_t _mailbox = (msg_mailbox_t)mailbox;
51 if(NULL != (_mailbox->hostname))
52 free(_mailbox->hostname);
54 xbt_fifo_free(_mailbox->tasks);
55 free(_mailbox->alias);
61 MSG_mailbox_put(msg_mailbox_t mailbox, m_task_t task)
63 xbt_fifo_push(mailbox->tasks, task);
67 MSG_mailbox_get_cond(msg_mailbox_t mailbox)
73 MSG_mailbox_remove(msg_mailbox_t mailbox, m_task_t task)
75 xbt_fifo_remove(mailbox->tasks,task);
79 MSG_mailbox_is_empty(msg_mailbox_t mailbox)
81 return (NULL == xbt_fifo_get_first_item(mailbox->tasks));
85 MSG_mailbox_pop_head(msg_mailbox_t mailbox)
87 return (m_task_t)xbt_fifo_shift(mailbox->tasks);
91 MSG_mailbox_get_head(msg_mailbox_t mailbox)
95 if(NULL == (item = xbt_fifo_get_first_item(mailbox->tasks)))
98 return (m_task_t)xbt_fifo_get_item_content(item);
103 MSG_mailbox_get_first_host_task(msg_mailbox_t mailbox, m_host_t host)
105 m_task_t task = NULL;
106 xbt_fifo_item_t item = NULL;
108 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
109 if (task->simdata->source == host) {
110 xbt_fifo_remove_item(mailbox->tasks, item);
118 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox, m_host_t host)
120 m_task_t task = NULL;
121 xbt_fifo_item_t item = NULL;
124 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
126 if (task->simdata->source == host)
134 MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
136 mailbox->cond = cond;
140 MSG_mailbox_get_alias(msg_mailbox_t mailbox)
142 return mailbox->alias;
146 MSG_mailbox_get_hostname(msg_mailbox_t mailbox)
148 return mailbox->hostname;
152 MSG_mailbox_set_hostname(msg_mailbox_t mailbox, const char* hostname)
154 mailbox->hostname = xbt_strdup(hostname);
158 MSG_mailbox_get_by_alias(const char* alias)
161 msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes,alias);
165 mailbox = MSG_mailbox_new(alias);
166 MSG_mailbox_set_hostname(mailbox,MSG_host_self()->name);
173 MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
175 xbt_assert0((host != NULL), "Invalid host");
176 xbt_assert1((channel >= 0)&& (channel < msg_global->max_channel), "Invalid channel %d",channel);
178 return host->simdata->mailboxes[(size_t)channel];
182 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t* task, m_host_t host, double timeout)
184 m_process_t process = MSG_process_self();
187 simdata_task_t t_simdata = NULL;
188 simdata_host_t h_simdata = NULL;
191 smx_cond_t cond = NULL; //conditional wait if the task isn't on the channel yet
196 xbt_assert0(task, "Null pointer for the task storage");
199 CRITICAL0("MSG_task_get() was asked to write in a non empty task struct.");
203 h_simdata = h->simdata;
205 SIMIX_mutex_lock(h->simdata->mutex);
209 /* if the mailbox is empty (has no task */
210 if(!MSG_mailbox_is_empty(mailbox))
214 /* pop the head of the mailbox */
215 t = MSG_mailbox_pop_head(mailbox);
220 /* get the first task of the host */
221 if(NULL != (t = MSG_mailbox_get_first_host_task(mailbox,host)))
230 SIMIX_mutex_unlock(h->simdata->mutex);
231 /* set the simix condition of the mailbox to NULL */
232 MSG_mailbox_set_cond(mailbox, NULL);
233 SIMIX_cond_destroy(cond);
234 MSG_RETURN(MSG_TRANSFER_FAILURE);
238 xbt_assert1(!MSG_mailbox_get_cond(mailbox),"A process is already blocked on the channel %s", MSG_mailbox_get_alias(mailbox));
240 cond = SIMIX_cond_init();
242 /* set the condition of the mailbox */
243 MSG_mailbox_set_cond(mailbox, cond);
246 SIMIX_cond_wait_timeout(cond, h->simdata->mutex, timeout);
248 SIMIX_cond_wait(MSG_mailbox_get_cond(mailbox), h->simdata->mutex);
251 if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
252 MSG_RETURN(MSG_HOST_FAILURE);
257 SIMIX_mutex_unlock(h->simdata->mutex);
259 DEBUG1("OK, got a task (%s)", t->name);
260 /* clean conditional */
263 SIMIX_cond_destroy(cond);
265 MSG_mailbox_set_cond(mailbox,NULL);
268 t_simdata = t->simdata;
269 t_simdata->receiver = process;
272 SIMIX_mutex_lock(t_simdata->mutex);
275 /* create SIMIX action to the communication */
276 t_simdata->comm = SIMIX_action_communicate(
277 t_simdata->sender->simdata->m_host->simdata->smx_host,
278 process->simdata->m_host->simdata->smx_host,
280 t_simdata->message_size,
284 /* if the process is suspend, create the action but stop its execution, it will be restart when the sender process resume */
285 if (MSG_process_is_suspended(t_simdata->sender))
287 DEBUG1("Process sender (%s) suspended", t_simdata->sender->name);
288 SIMIX_action_set_priority(t_simdata->comm, 0);
291 process->simdata->waiting_task = t;
292 SIMIX_register_action_to_condition(t_simdata->comm, t_simdata->cond);
296 SIMIX_cond_wait(t_simdata->cond, t_simdata->mutex);
298 if (SIMIX_action_get_state(t_simdata->comm) != SURF_ACTION_RUNNING)
302 SIMIX_unregister_action_to_condition(t_simdata->comm, t_simdata->cond);
303 process->simdata->waiting_task = NULL;
305 /* the task has already finished and the pointer must be null */
306 if (t->simdata->sender)
308 t->simdata->sender->simdata->waiting_task = NULL;
311 /* for this process, don't need to change in get function */
312 t->simdata->receiver = NULL;
313 SIMIX_mutex_unlock(t_simdata->mutex);
316 if (SIMIX_action_get_state(t_simdata->comm) == SURF_ACTION_DONE)
318 SIMIX_action_destroy(t_simdata->comm);
319 t_simdata->comm = NULL;
323 else if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
325 SIMIX_action_destroy(t_simdata->comm);
326 t_simdata->comm = NULL;
328 MSG_RETURN(MSG_HOST_FAILURE);
332 SIMIX_action_destroy(t_simdata->comm);
333 t_simdata->comm = NULL;
335 MSG_RETURN(MSG_TRANSFER_FAILURE);
340 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task, double timeout)
342 m_process_t process = MSG_process_self();
343 const char* hostname;
344 simdata_task_t task_simdata = NULL;
345 m_host_t local_host = NULL;
346 m_host_t remote_host = NULL;
347 smx_cond_t cond = NULL;
351 task_simdata = task->simdata;
352 task_simdata->sender = process;
353 task_simdata->source = MSG_process_get_host(process);
355 xbt_assert0(task_simdata->using == 1,"This task is still being used somewhere else. You cannot send it now. Go fix your code!");
357 task_simdata->comm = NULL;
359 task_simdata->using++;
360 local_host = ((simdata_process_t) process->simdata)->m_host;
362 /* get the host name containing the mailbox */
363 hostname = MSG_mailbox_get_hostname(mailbox);
365 remote_host = MSG_get_host_by_name(hostname);
367 if(NULL == remote_host)
368 THROW1(not_found_error,0,"Host %s not fount", hostname);
371 DEBUG4("Trying to send a task (%g kB) from %s to %s on the channel aliased by the alias %s",task->simdata->message_size / 1000, local_host->name,remote_host->name, MSG_mailbox_get_alias(mailbox));
373 SIMIX_mutex_lock(remote_host->simdata->mutex);
375 /* put the task in the mailbox */
376 MSG_mailbox_put(mailbox,task);
378 if(NULL != (cond = MSG_mailbox_get_cond(mailbox)))
380 DEBUG0("Somebody is listening. Let's wake him up!");
381 SIMIX_cond_signal(cond);
386 SIMIX_mutex_unlock(remote_host->simdata->mutex);
388 SIMIX_mutex_lock(task->simdata->mutex);
390 process->simdata->waiting_task = task;
397 time = SIMIX_get_clock();
401 /*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 */
404 time_elapsed = SIMIX_get_clock() - time;
405 SIMIX_cond_wait_timeout(task->simdata->cond, task->simdata->mutex,timeout - time_elapsed);
407 if ((task->simdata->comm != NULL) && (SIMIX_action_get_state(task->simdata->comm) != SURF_ACTION_RUNNING))
413 if(e.category==timeout_error)
416 /* verify if the timeout happened and the communication didn't started yet */
417 if (task->simdata->comm == NULL)
419 process->simdata->waiting_task = NULL;
421 /* remove the task from the mailbox */
422 MSG_mailbox_remove(mailbox,task);
424 if (task->simdata->receiver)
426 task->simdata->receiver->simdata->waiting_task = NULL;
429 task->simdata->sender = NULL;
431 SIMIX_mutex_unlock(task->simdata->mutex);
432 MSG_RETURN(MSG_TRANSFER_FAILURE);
445 SIMIX_cond_wait(task->simdata->cond, task->simdata->mutex);
447 if (SIMIX_action_get_state(task->simdata->comm) != SURF_ACTION_RUNNING)
452 DEBUG1("Action terminated %s", task->name);
453 process->simdata->waiting_task = NULL;
455 /* the task has already finished and the pointer must be null */
456 if (task->simdata->receiver)
458 task->simdata->receiver->simdata->waiting_task = NULL;
461 task->simdata->sender = NULL;
462 SIMIX_mutex_unlock(task->simdata->mutex);
465 if (SIMIX_action_get_state(task->simdata->comm) == SURF_ACTION_DONE)
469 else if (SIMIX_host_get_state(local_host->simdata->smx_host) == 0)
471 MSG_RETURN(MSG_HOST_FAILURE);
475 MSG_RETURN(MSG_TRANSFER_FAILURE);