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