2 #include "msg/private.h"
\r
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg,
\r
5 "Logging specific to MSG (mailbox)");
\r
8 msg_mailboxes = NULL;
\r
11 MSG_mailbox_mod_init(void)
\r
13 msg_mailboxes = xbt_dict_new();
\r
17 MSG_mailbox_mod_exit(void)
\r
19 xbt_dict_free(&msg_mailboxes);
\r
23 MSG_mailbox_new(const char *alias)
\r
25 msg_mailbox_t mailbox = xbt_new0(s_msg_mailbox_t,1);
\r
27 mailbox->tasks = xbt_fifo_new();
\r
28 mailbox->cond = NULL;
\r
29 mailbox->alias = xbt_strdup(alias);
\r
30 mailbox->hostname = NULL;
\r
32 /* add the mbox in the dictionary */
\r
33 xbt_dict_set(msg_mailboxes, alias, mailbox, MSG_mailbox_free);
\r
39 MSG_mailbox_create(const char *alias)
\r
41 msg_mailbox_t mailbox = xbt_new0(s_msg_mailbox_t,1);
\r
43 mailbox->tasks = xbt_fifo_new();
\r
44 mailbox->cond = NULL;
\r
45 mailbox->alias = xbt_strdup(alias);
\r
46 mailbox->hostname = NULL;
\r
52 MSG_mailbox_free(void* mailbox)
\r
54 msg_mailbox_t _mailbox = (msg_mailbox_t)mailbox;
\r
56 if(NULL != (_mailbox->hostname))
\r
57 free(_mailbox->hostname);
\r
59 free(_mailbox->alias);
\r
65 MSG_mailbox_put(msg_mailbox_t mailbox, m_task_t task)
\r
67 xbt_fifo_push(mailbox->tasks, task);
\r
71 MSG_mailbox_get_cond(msg_mailbox_t mailbox)
\r
73 return mailbox->cond;
\r
77 MSG_mailbox_remove(msg_mailbox_t mailbox, m_task_t task)
\r
79 xbt_fifo_remove(mailbox->tasks,task);
\r
83 MSG_mailbox_is_empty(msg_mailbox_t mailbox)
\r
85 return (NULL == xbt_fifo_get_first_item(mailbox->tasks));
\r
89 MSG_mailbox_pop_head(msg_mailbox_t mailbox)
\r
91 return (m_task_t)xbt_fifo_shift(mailbox->tasks);
\r
95 MSG_mailbox_get_head(msg_mailbox_t mailbox)
\r
97 xbt_fifo_item_t item;
\r
99 if(NULL == (item = xbt_fifo_get_first_item(mailbox->tasks)))
\r
102 return (m_task_t)xbt_fifo_get_item_content(item);
\r
107 MSG_mailbox_get_first_host_task(msg_mailbox_t mailbox, m_host_t host)
\r
109 m_task_t task = NULL;
\r
110 xbt_fifo_item_t item = NULL;
\r
112 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
\r
113 if (task->simdata->source == host) {
\r
114 xbt_fifo_remove_item(mailbox->tasks, item);
\r
122 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox, m_host_t host)
\r
124 m_task_t task = NULL;
\r
125 xbt_fifo_item_t item = NULL;
\r
128 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
\r
130 if (task->simdata->source == host)
\r
138 MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
\r
140 mailbox->cond = cond;
\r
144 MSG_mailbox_get_alias(msg_mailbox_t mailbox)
\r
146 return mailbox->alias;
\r
150 MSG_mailbox_get_hostname(msg_mailbox_t mailbox)
\r
152 return mailbox->hostname;
\r
156 MSG_mailbox_set_hostname(msg_mailbox_t mailbox, const char* hostname)
\r
158 mailbox->hostname = xbt_strdup(hostname);
\r
162 MSG_mailbox_get_by_alias(const char* alias)
\r
165 msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes,alias);
\r
169 mailbox = MSG_mailbox_new(alias);
\r
170 MSG_mailbox_set_hostname(mailbox,MSG_host_self()->name);
\r
177 MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
\r
179 xbt_assert0((host != NULL), "Invalid host");
\r
180 xbt_assert1((channel >= 0)&& (channel < msg_global->max_channel), "Invalid channel %d",channel);
\r
182 return host->simdata->mailboxes[(size_t)channel];
\r
186 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t* task, m_host_t host, double timeout)
\r
188 m_process_t process = MSG_process_self();
\r
191 simdata_task_t t_simdata = NULL;
\r
192 simdata_host_t h_simdata = NULL;
\r
193 int first_time = 1;
\r
195 smx_cond_t cond = NULL; //conditional wait if the task isn't on the channel yet
\r
200 xbt_assert0(task, "Null pointer for the task storage");
\r
203 CRITICAL0("MSG_task_get() was asked to write in a non empty task struct.");
\r
206 h = MSG_host_self();
\r
207 h_simdata = h->simdata;
\r
209 SIMIX_mutex_lock(h->simdata->mutex);
\r
213 /* if the mailbox is empty (has no task */
\r
214 if(!MSG_mailbox_is_empty(mailbox))
\r
218 /* pop the head of the mailbox */
\r
219 t = MSG_mailbox_pop_head(mailbox);
\r
224 /* get the first task of the host */
\r
225 if(NULL != (t = MSG_mailbox_get_first_host_task(mailbox,host)))
\r
234 SIMIX_mutex_unlock(h->simdata->mutex);
\r
235 /* set the simix condition of the mailbox to NULL */
\r
236 MSG_mailbox_set_cond(mailbox, NULL);
\r
237 SIMIX_cond_destroy(cond);
\r
238 MSG_RETURN(MSG_TRANSFER_FAILURE);
\r
242 xbt_assert1(!MSG_mailbox_get_cond(mailbox),"A process is already blocked on the channel %s", MSG_mailbox_get_alias(mailbox));
\r
244 cond = SIMIX_cond_init();
\r
246 /* set the condition of the mailbox */
\r
247 MSG_mailbox_set_cond(mailbox, cond);
\r
250 SIMIX_cond_wait_timeout(cond, h->simdata->mutex, timeout);
\r
252 SIMIX_cond_wait(MSG_mailbox_get_cond(mailbox), h->simdata->mutex);
\r
255 if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
\r
256 MSG_RETURN(MSG_HOST_FAILURE);
\r
261 SIMIX_mutex_unlock(h->simdata->mutex);
\r
263 DEBUG1("OK, got a task (%s)", t->name);
\r
264 /* clean conditional */
\r
267 SIMIX_cond_destroy(cond);
\r
269 MSG_mailbox_set_cond(mailbox,NULL);
\r
272 t_simdata = t->simdata;
\r
273 t_simdata->receiver = process;
\r
276 SIMIX_mutex_lock(t_simdata->mutex);
\r
279 /* create SIMIX action to the communication */
\r
280 t_simdata->comm = SIMIX_action_communicate(
\r
281 t_simdata->sender->simdata->m_host->simdata->smx_host,
\r
282 process->simdata->m_host->simdata->smx_host,
\r
284 t_simdata->message_size,
\r
288 /* if the process is suspend, create the action but stop its execution, it will be restart when the sender process resume */
\r
289 if (MSG_process_is_suspended(t_simdata->sender))
\r
291 DEBUG1("Process sender (%s) suspended", t_simdata->sender->name);
\r
292 SIMIX_action_set_priority(t_simdata->comm, 0);
\r
295 process->simdata->waiting_task = t;
\r
296 SIMIX_register_action_to_condition(t_simdata->comm, t_simdata->cond);
\r
300 SIMIX_cond_wait(t_simdata->cond, t_simdata->mutex);
\r
302 if (SIMIX_action_get_state(t_simdata->comm) != SURF_ACTION_RUNNING)
\r
306 SIMIX_unregister_action_to_condition(t_simdata->comm, t_simdata->cond);
\r
307 process->simdata->waiting_task = NULL;
\r
309 /* the task has already finished and the pointer must be null */
\r
310 if (t->simdata->sender)
\r
312 t->simdata->sender->simdata->waiting_task = NULL;
\r
315 /* for this process, don't need to change in get function */
\r
316 t->simdata->receiver = NULL;
\r
317 SIMIX_mutex_unlock(t_simdata->mutex);
\r
320 if (SIMIX_action_get_state(t_simdata->comm) == SURF_ACTION_DONE)
\r
322 SIMIX_action_destroy(t_simdata->comm);
\r
323 t_simdata->comm = NULL;
\r
324 t_simdata->using--;
\r
325 MSG_RETURN(MSG_OK);
\r
327 else if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
\r
329 SIMIX_action_destroy(t_simdata->comm);
\r
330 t_simdata->comm = NULL;
\r
331 t_simdata->using--;
\r
332 MSG_RETURN(MSG_HOST_FAILURE);
\r
336 SIMIX_action_destroy(t_simdata->comm);
\r
337 t_simdata->comm = NULL;
\r
338 t_simdata->using--;
\r
339 MSG_RETURN(MSG_TRANSFER_FAILURE);
\r
344 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task, double timeout)
\r
346 m_process_t process = MSG_process_self();
\r
347 const char* hostname;
\r
348 simdata_task_t task_simdata = NULL;
\r
349 m_host_t local_host = NULL;
\r
350 m_host_t remote_host = NULL;
\r
351 smx_cond_t cond = NULL;
\r
355 task_simdata = task->simdata;
\r
356 task_simdata->sender = process;
\r
357 task_simdata->source = MSG_process_get_host(process);
\r
359 xbt_assert0(task_simdata->using == 1,"This task is still being used somewhere else. You cannot send it now. Go fix your code!");
\r
361 task_simdata->comm = NULL;
\r
363 task_simdata->using++;
\r
364 local_host = ((simdata_process_t) process->simdata)->m_host;
\r
366 /* get the host name containing the mailbox */
\r
367 hostname = MSG_mailbox_get_hostname(mailbox);
\r
369 remote_host = MSG_get_host_by_name(hostname);
\r
371 if(NULL == remote_host)
\r
372 THROW1(not_found_error,0,"Host %s not fount", hostname);
\r
375 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));
\r
377 SIMIX_mutex_lock(remote_host->simdata->mutex);
\r
379 /* put the task in the mailbox */
\r
380 MSG_mailbox_put(mailbox,task);
\r
382 if(NULL != (cond = MSG_mailbox_get_cond(mailbox)))
\r
384 DEBUG0("Somebody is listening. Let's wake him up!");
\r
385 SIMIX_cond_signal(cond);
\r
390 SIMIX_mutex_unlock(remote_host->simdata->mutex);
\r
392 SIMIX_mutex_lock(task->simdata->mutex);
\r
394 process->simdata->waiting_task = task;
\r
400 double time_elapsed;
\r
401 time = SIMIX_get_clock();
\r
405 /*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 */
\r
408 time_elapsed = SIMIX_get_clock() - time;
\r
409 SIMIX_cond_wait_timeout(task->simdata->cond, task->simdata->mutex,timeout - time_elapsed);
\r
411 if ((task->simdata->comm != NULL) && (SIMIX_action_get_state(task->simdata->comm) != SURF_ACTION_RUNNING))
\r
417 if(e.category==timeout_error)
\r
420 /* verify if the timeout happened and the communication didn't started yet */
\r
421 if (task->simdata->comm == NULL)
\r
423 process->simdata->waiting_task = NULL;
\r
425 /* remove the task from the mailbox */
\r
426 MSG_mailbox_remove(mailbox,task);
\r
428 if (task->simdata->receiver)
\r
430 task->simdata->receiver->simdata->waiting_task = NULL;
\r
433 task->simdata->sender = NULL;
\r
435 SIMIX_mutex_unlock(task->simdata->mutex);
\r
436 MSG_RETURN(MSG_TRANSFER_FAILURE);
\r
449 SIMIX_cond_wait(task->simdata->cond, task->simdata->mutex);
\r
451 if (SIMIX_action_get_state(task->simdata->comm) != SURF_ACTION_RUNNING)
\r
456 DEBUG1("Action terminated %s", task->name);
\r
457 process->simdata->waiting_task = NULL;
\r
459 /* the task has already finished and the pointer must be null */
\r
460 if (task->simdata->receiver)
\r
462 task->simdata->receiver->simdata->waiting_task = NULL;
\r
465 task->simdata->sender = NULL;
\r
466 SIMIX_mutex_unlock(task->simdata->mutex);
\r
469 if (SIMIX_action_get_state(task->simdata->comm) == SURF_ACTION_DONE)
\r
471 MSG_RETURN(MSG_OK);
\r
473 else if (SIMIX_host_get_state(local_host->simdata->smx_host) == 0)
\r
475 MSG_RETURN(MSG_HOST_FAILURE);
\r
479 MSG_RETURN(MSG_TRANSFER_FAILURE);
\r