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 free(_mailbox->alias);
\r
60 MSG_mailbox_put(msg_mailbox_t mailbox, m_task_t task)
\r
62 xbt_fifo_push(mailbox->tasks, task);
\r
66 MSG_mailbox_get_cond(msg_mailbox_t mailbox)
\r
68 return mailbox->cond;
\r
72 MSG_mailbox_remove(msg_mailbox_t mailbox, m_task_t task)
\r
74 xbt_fifo_remove(mailbox->tasks,task);
\r
78 MSG_mailbox_is_empty(msg_mailbox_t mailbox)
\r
80 return (NULL == xbt_fifo_get_first_item(mailbox->tasks));
\r
84 MSG_mailbox_pop_head(msg_mailbox_t mailbox)
\r
86 return (m_task_t)xbt_fifo_shift(mailbox->tasks);
\r
90 MSG_mailbox_get_head(msg_mailbox_t mailbox)
\r
92 xbt_fifo_item_t item;
\r
94 if(NULL == (item = xbt_fifo_get_first_item(mailbox->tasks)))
\r
97 return (m_task_t)xbt_fifo_get_item_content(item);
\r
102 MSG_mailbox_get_first_host_task(msg_mailbox_t mailbox, m_host_t host)
\r
104 m_task_t task = NULL;
\r
105 xbt_fifo_item_t item = NULL;
\r
107 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
\r
108 if (task->simdata->source == host) {
\r
109 xbt_fifo_remove_item(mailbox->tasks, item);
\r
117 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox, m_host_t host)
\r
119 m_task_t task = NULL;
\r
120 xbt_fifo_item_t item = NULL;
\r
123 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
\r
125 if (task->simdata->source == host)
\r
133 MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
\r
135 mailbox->cond = cond;
\r
139 MSG_mailbox_get_alias(msg_mailbox_t mailbox)
\r
141 return mailbox->alias;
\r
145 MSG_mailbox_get_hostname(msg_mailbox_t mailbox)
\r
147 return mailbox->hostname;
\r
151 MSG_mailbox_set_hostname(msg_mailbox_t mailbox, const char* hostname)
\r
153 mailbox->hostname = xbt_strdup(hostname);
\r
157 MSG_mailbox_get_by_alias(const char* alias)
\r
160 msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes,alias);
\r
164 mailbox = MSG_mailbox_new(alias);
\r
165 MSG_mailbox_set_hostname(mailbox,MSG_host_self()->name);
\r
172 MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
\r
174 xbt_assert0((host != NULL), "Invalid host");
\r
175 xbt_assert1((channel >= 0)&& (channel < msg_global->max_channel), "Invalid channel %d",channel);
\r
177 return host->simdata->mailboxes[(size_t)channel];
\r
181 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t* task, m_host_t host, double timeout)
\r
183 m_process_t process = MSG_process_self();
\r
186 simdata_task_t t_simdata = NULL;
\r
187 simdata_host_t h_simdata = NULL;
\r
188 int first_time = 1;
\r
190 smx_cond_t cond = NULL; //conditional wait if the task isn't on the channel yet
\r
195 xbt_assert0(task, "Null pointer for the task storage");
\r
198 CRITICAL0("MSG_task_get() was asked to write in a non empty task struct.");
\r
201 h = MSG_host_self();
\r
202 h_simdata = h->simdata;
\r
204 SIMIX_mutex_lock(h->simdata->mutex);
\r
208 /* if the mailbox is empty (has no task */
\r
209 if(!MSG_mailbox_is_empty(mailbox))
\r
213 /* pop the head of the mailbox */
\r
214 t = MSG_mailbox_pop_head(mailbox);
\r
219 /* get the first task of the host */
\r
220 if(NULL != (t = MSG_mailbox_get_first_host_task(mailbox,host)))
\r
229 SIMIX_mutex_unlock(h->simdata->mutex);
\r
230 /* set the simix condition of the mailbox to NULL */
\r
231 MSG_mailbox_set_cond(mailbox, NULL);
\r
232 SIMIX_cond_destroy(cond);
\r
233 MSG_RETURN(MSG_TRANSFER_FAILURE);
\r
237 xbt_assert1(!MSG_mailbox_get_cond(mailbox),"A process is already blocked on the channel %s", MSG_mailbox_get_alias(mailbox));
\r
239 cond = SIMIX_cond_init();
\r
241 /* set the condition of the mailbox */
\r
242 MSG_mailbox_set_cond(mailbox, cond);
\r
245 SIMIX_cond_wait_timeout(cond, h->simdata->mutex, timeout);
\r
247 SIMIX_cond_wait(MSG_mailbox_get_cond(mailbox), h->simdata->mutex);
\r
250 if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
\r
251 MSG_RETURN(MSG_HOST_FAILURE);
\r
256 SIMIX_mutex_unlock(h->simdata->mutex);
\r
258 DEBUG1("OK, got a task (%s)", t->name);
\r
259 /* clean conditional */
\r
262 SIMIX_cond_destroy(cond);
\r
264 MSG_mailbox_set_cond(mailbox,NULL);
\r
267 t_simdata = t->simdata;
\r
268 t_simdata->receiver = process;
\r
271 SIMIX_mutex_lock(t_simdata->mutex);
\r
274 /* create SIMIX action to the communication */
\r
275 t_simdata->comm = SIMIX_action_communicate(
\r
276 t_simdata->sender->simdata->m_host->simdata->smx_host,
\r
277 process->simdata->m_host->simdata->smx_host,
\r
279 t_simdata->message_size,
\r
283 /* if the process is suspend, create the action but stop its execution, it will be restart when the sender process resume */
\r
284 if (MSG_process_is_suspended(t_simdata->sender))
\r
286 DEBUG1("Process sender (%s) suspended", t_simdata->sender->name);
\r
287 SIMIX_action_set_priority(t_simdata->comm, 0);
\r
290 process->simdata->waiting_task = t;
\r
291 SIMIX_register_action_to_condition(t_simdata->comm, t_simdata->cond);
\r
295 SIMIX_cond_wait(t_simdata->cond, t_simdata->mutex);
\r
297 if (SIMIX_action_get_state(t_simdata->comm) != SURF_ACTION_RUNNING)
\r
301 SIMIX_unregister_action_to_condition(t_simdata->comm, t_simdata->cond);
\r
302 process->simdata->waiting_task = NULL;
\r
304 /* the task has already finished and the pointer must be null */
\r
305 if (t->simdata->sender)
\r
307 t->simdata->sender->simdata->waiting_task = NULL;
\r
310 /* for this process, don't need to change in get function */
\r
311 t->simdata->receiver = NULL;
\r
312 SIMIX_mutex_unlock(t_simdata->mutex);
\r
315 if (SIMIX_action_get_state(t_simdata->comm) == SURF_ACTION_DONE)
\r
317 SIMIX_action_destroy(t_simdata->comm);
\r
318 t_simdata->comm = NULL;
\r
319 t_simdata->using--;
\r
320 MSG_RETURN(MSG_OK);
\r
322 else if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
\r
324 SIMIX_action_destroy(t_simdata->comm);
\r
325 t_simdata->comm = NULL;
\r
326 t_simdata->using--;
\r
327 MSG_RETURN(MSG_HOST_FAILURE);
\r
331 SIMIX_action_destroy(t_simdata->comm);
\r
332 t_simdata->comm = NULL;
\r
333 t_simdata->using--;
\r
334 MSG_RETURN(MSG_TRANSFER_FAILURE);
\r
339 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task, double timeout)
\r
341 m_process_t process = MSG_process_self();
\r
342 const char* hostname;
\r
343 simdata_task_t task_simdata = NULL;
\r
344 m_host_t local_host = NULL;
\r
345 m_host_t remote_host = NULL;
\r
346 smx_cond_t cond = NULL;
\r
350 task_simdata = task->simdata;
\r
351 task_simdata->sender = process;
\r
352 task_simdata->source = MSG_process_get_host(process);
\r
354 xbt_assert0(task_simdata->using == 1,"This task is still being used somewhere else. You cannot send it now. Go fix your code!");
\r
356 task_simdata->comm = NULL;
\r
358 task_simdata->using++;
\r
359 local_host = ((simdata_process_t) process->simdata)->m_host;
\r
361 /* get the host name containing the mailbox */
\r
362 hostname = MSG_mailbox_get_hostname(mailbox);
\r
364 remote_host = MSG_get_host_by_name(hostname);
\r
366 if(NULL == remote_host)
\r
367 THROW1(not_found_error,0,"Host %s not fount", hostname);
\r
370 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
372 SIMIX_mutex_lock(remote_host->simdata->mutex);
\r
374 /* put the task in the mailbox */
\r
375 MSG_mailbox_put(mailbox,task);
\r
377 if(NULL != (cond = MSG_mailbox_get_cond(mailbox)))
\r
379 DEBUG0("Somebody is listening. Let's wake him up!");
\r
380 SIMIX_cond_signal(cond);
\r
385 SIMIX_mutex_unlock(remote_host->simdata->mutex);
\r
387 SIMIX_mutex_lock(task->simdata->mutex);
\r
389 process->simdata->waiting_task = task;
\r
395 double time_elapsed;
\r
396 time = SIMIX_get_clock();
\r
400 /*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
403 time_elapsed = SIMIX_get_clock() - time;
\r
404 SIMIX_cond_wait_timeout(task->simdata->cond, task->simdata->mutex,timeout - time_elapsed);
\r
406 if ((task->simdata->comm != NULL) && (SIMIX_action_get_state(task->simdata->comm) != SURF_ACTION_RUNNING))
\r
412 if(e.category==timeout_error)
\r
415 /* verify if the timeout happened and the communication didn't started yet */
\r
416 if (task->simdata->comm == NULL)
\r
418 process->simdata->waiting_task = NULL;
\r
420 /* remove the task from the mailbox */
\r
421 MSG_mailbox_remove(mailbox,task);
\r
423 if (task->simdata->receiver)
\r
425 task->simdata->receiver->simdata->waiting_task = NULL;
\r
428 task->simdata->sender = NULL;
\r
430 SIMIX_mutex_unlock(task->simdata->mutex);
\r
431 MSG_RETURN(MSG_TRANSFER_FAILURE);
\r
444 SIMIX_cond_wait(task->simdata->cond, task->simdata->mutex);
\r
446 if (SIMIX_action_get_state(task->simdata->comm) != SURF_ACTION_RUNNING)
\r
451 DEBUG1("Action terminated %s", task->name);
\r
452 process->simdata->waiting_task = NULL;
\r
454 /* the task has already finished and the pointer must be null */
\r
455 if (task->simdata->receiver)
\r
457 task->simdata->receiver->simdata->waiting_task = NULL;
\r
460 task->simdata->sender = NULL;
\r
461 SIMIX_mutex_unlock(task->simdata->mutex);
\r
464 if (SIMIX_action_get_state(task->simdata->comm) == SURF_ACTION_DONE)
\r
466 MSG_RETURN(MSG_OK);
\r
468 else if (SIMIX_host_get_state(local_host->simdata->smx_host) == 0)
\r
470 MSG_RETURN(MSG_HOST_FAILURE);
\r
474 MSG_RETURN(MSG_TRANSFER_FAILURE);
\r