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 (_mailbox->hostname)
46 free(_mailbox->hostname);
48 xbt_fifo_free(_mailbox->tasks);
49 free(_mailbox->alias);
54 smx_cond_t MSG_mailbox_get_cond(msg_mailbox_t mailbox)
59 void MSG_mailbox_remove(msg_mailbox_t mailbox, m_task_t task)
61 xbt_fifo_remove(mailbox->tasks, task);
64 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
66 return (NULL == xbt_fifo_get_first_item(mailbox->tasks));
69 m_task_t MSG_mailbox_pop_head(msg_mailbox_t mailbox)
71 return (m_task_t) xbt_fifo_shift(mailbox->tasks);
74 m_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
78 if (!(item = xbt_fifo_get_first_item(mailbox->tasks)))
81 return (m_task_t) xbt_fifo_get_item_content(item);
86 MSG_mailbox_get_first_host_task(msg_mailbox_t mailbox, m_host_t host)
89 xbt_fifo_item_t item = NULL;
91 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
92 if (task->simdata->source == host) {
93 xbt_fifo_remove_item(mailbox->tasks, item);
101 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
104 m_task_t task = NULL;
105 xbt_fifo_item_t item = NULL;
108 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t) {
109 if (task->simdata->source == host)
116 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
118 mailbox->cond = cond;
121 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
123 return mailbox->alias;
126 const char *MSG_mailbox_get_hostname(msg_mailbox_t mailbox)
128 return mailbox->hostname;
131 void MSG_mailbox_set_hostname(msg_mailbox_t mailbox, const char *hostname)
133 mailbox->hostname = xbt_strdup(hostname);
136 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
139 msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
142 mailbox = MSG_mailbox_new(alias);
143 MSG_mailbox_set_hostname(mailbox, MSG_host_self()->name);
150 MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
152 xbt_assert0((host != NULL), "Invalid host");
153 xbt_assert1((channel >= 0)
154 && (channel < msg_global->max_channel), "Invalid channel %d",
157 return host->simdata->mailboxes[(size_t) channel];
161 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t * task,
162 m_host_t host, double timeout)
164 m_process_t process = MSG_process_self();
167 simdata_task_t t_simdata = NULL;
168 simdata_host_t h_simdata = NULL;
169 double start_time = SIMIX_get_clock();
171 smx_cond_t cond = NULL; //conditional wait if the task isn't on the channel yet
176 xbt_assert0(task, "Null pointer for the task storage");
180 ("MSG_task_get() was asked to write in a non empty task struct.");
184 h_simdata = h->simdata;
186 SIMIX_mutex_lock(h->simdata->mutex);
188 if (MSG_mailbox_get_cond(mailbox)) {
189 CRITICAL1("A process is already blocked on the channel %s",
190 MSG_mailbox_get_alias(mailbox));
191 SIMIX_cond_display_info(MSG_mailbox_get_cond(mailbox));
192 xbt_die("Go fix your code!");
196 /* if the mailbox is empty (has no task */
197 if (!MSG_mailbox_is_empty(mailbox)) {
199 /* pop the head of the mailbox */
200 t = MSG_mailbox_pop_head(mailbox);
203 /* get the first task of the host */
204 if ((t = MSG_mailbox_get_first_host_task(mailbox, host)))
209 if ((timeout > 0) && (SIMIX_get_clock()-start_time>=timeout)) {
210 SIMIX_mutex_unlock(h->simdata->mutex);
211 MSG_mailbox_set_cond(mailbox, NULL);
212 SIMIX_cond_destroy(cond);
213 MSG_RETURN(MSG_TRANSFER_FAILURE);
217 cond = SIMIX_cond_init();
218 MSG_mailbox_set_cond(mailbox, cond);
222 SIMIX_cond_wait_timeout(cond, h->simdata->mutex, timeout-start_time);
224 SIMIX_cond_wait(MSG_mailbox_get_cond(mailbox), h->simdata->mutex);
226 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
227 SIMIX_mutex_unlock(h->simdata->mutex);
228 MSG_mailbox_set_cond(mailbox, NULL);
229 SIMIX_cond_destroy(cond);
230 MSG_RETURN(MSG_HOST_FAILURE);
235 DEBUG1("OK, got a task (%s)", t->name);
236 /* clean conditional */
238 MSG_mailbox_set_cond(mailbox, NULL);
239 SIMIX_cond_destroy(cond);
242 SIMIX_mutex_unlock(h->simdata->mutex);
244 t_simdata = t->simdata;
245 t_simdata->receiver = process;
248 SIMIX_mutex_lock(t_simdata->mutex);
251 /* create SIMIX action to the communication */
253 SIMIX_action_communicate(t_simdata->sender->simdata->m_host->
255 process->simdata->m_host->simdata->smx_host,
256 t->name, t_simdata->message_size,
259 /* if the process is suspend, create the action but stop its execution, it will be restart when the sender process resume */
260 if (MSG_process_is_suspended(t_simdata->sender)) {
261 DEBUG1("Process sender (%s) suspended", t_simdata->sender->name);
262 SIMIX_action_set_priority(t_simdata->comm, 0);
265 process->simdata->waiting_task = t;
266 SIMIX_register_action_to_condition(t_simdata->comm, t_simdata->cond);
269 SIMIX_cond_wait(t_simdata->cond, t_simdata->mutex);
271 if (SIMIX_action_get_state(t_simdata->comm) != SURF_ACTION_RUNNING)
275 SIMIX_unregister_action_to_condition(t_simdata->comm, t_simdata->cond);
276 process->simdata->waiting_task = NULL;
278 /* the task has already finished and the pointer must be null */
279 if (t->simdata->sender) {
280 t->simdata->sender->simdata->waiting_task = NULL;
283 /* for this process, don't need to change in get function */
284 t->simdata->receiver = NULL;
285 SIMIX_mutex_unlock(t_simdata->mutex);
288 if (SIMIX_action_get_state(t_simdata->comm) == SURF_ACTION_DONE) {
289 SIMIX_action_destroy(t_simdata->comm);
290 t_simdata->comm = NULL;
291 t_simdata->refcount --;
293 } else if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
294 SIMIX_action_destroy(t_simdata->comm);
295 t_simdata->comm = NULL;
296 t_simdata->refcount --;
297 MSG_RETURN(MSG_HOST_FAILURE);
299 SIMIX_action_destroy(t_simdata->comm);
300 t_simdata->comm = NULL;
301 t_simdata->refcount --;
302 MSG_RETURN(MSG_TRANSFER_FAILURE);
307 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
310 m_process_t process = MSG_process_self();
311 const char *hostname;
312 simdata_task_t task_simdata = NULL;
313 m_host_t local_host = NULL;
314 m_host_t remote_host = NULL;
315 smx_cond_t cond = NULL;
319 task_simdata = task->simdata;
320 task_simdata->sender = process;
321 task_simdata->source = MSG_process_get_host(process);
323 xbt_assert0(task_simdata->refcount == 1,
324 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
326 task_simdata->comm = NULL;
328 task_simdata->refcount ++;
329 local_host = ((simdata_process_t) process->simdata)->m_host;
330 msg_global->sent_msg++;
332 /* get the host name containing the mailbox */
333 hostname = MSG_mailbox_get_hostname(mailbox);
335 remote_host = MSG_get_host_by_name(hostname);
338 THROW1(not_found_error, 0, "Host %s not fount", hostname);
342 ("Trying to send a task (%g kB) from %s to %s on the channel aliased by the alias %s",
343 task->simdata->message_size / 1000, local_host->name,
344 remote_host->name, MSG_mailbox_get_alias(mailbox));
346 SIMIX_mutex_lock(remote_host->simdata->mutex);
348 /* put the task in the mailbox */
349 xbt_fifo_push(mailbox->tasks, task);
351 if ((cond = MSG_mailbox_get_cond(mailbox))) {
352 DEBUG0("Somebody is listening. Let's wake him up!");
353 SIMIX_cond_signal(cond);
356 SIMIX_mutex_unlock(remote_host->simdata->mutex);
358 SIMIX_mutex_lock(task->simdata->mutex);
360 process->simdata->waiting_task = task;
366 time = SIMIX_get_clock();
369 /*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 */
371 time_elapsed = SIMIX_get_clock() - time;
372 SIMIX_cond_wait_timeout(task->simdata->cond, task->simdata->mutex,
373 timeout - time_elapsed);
375 if ((task->simdata->comm != NULL)
376 && (SIMIX_action_get_state(task->simdata->comm) !=
377 SURF_ACTION_RUNNING))
382 if (e.category == timeout_error) {
384 /* verify if the timeout happened and the communication didn't started yet */
385 if (task->simdata->comm == NULL) {
386 process->simdata->waiting_task = NULL;
388 /* remove the task from the mailbox */
389 MSG_mailbox_remove(mailbox, task);
391 if (task->simdata->receiver) {
392 task->simdata->receiver->simdata->waiting_task = NULL;
395 task->simdata->sender = NULL;
397 SIMIX_mutex_unlock(task->simdata->mutex);
398 MSG_RETURN(MSG_TRANSFER_FAILURE);
406 SIMIX_cond_wait(task->simdata->cond, task->simdata->mutex);
408 if (SIMIX_action_get_state(task->simdata->comm) !=
414 DEBUG1("Action terminated %s", task->name);
415 process->simdata->waiting_task = NULL;
417 /* the task has already finished and the pointer must be null */
418 if (task->simdata->receiver) {
419 task->simdata->receiver->simdata->waiting_task = NULL;
422 task->simdata->sender = NULL;
423 SIMIX_mutex_unlock(task->simdata->mutex);
425 if (SIMIX_action_get_state(task->simdata->comm) == SURF_ACTION_DONE) {
427 } else if (SIMIX_host_get_state(local_host->simdata->smx_host) == 0) {
428 MSG_RETURN(MSG_HOST_FAILURE);
430 MSG_RETURN(MSG_TRANSFER_FAILURE);