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);
85 m_task_t MSG_mailbox_get_first_host_task(msg_mailbox_t mailbox, m_host_t host)
88 xbt_fifo_item_t item = NULL;
90 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
91 if (task->simdata->source == host) {
92 xbt_fifo_remove_item(mailbox->tasks, item);
100 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox, m_host_t host)
102 m_task_t task = NULL;
103 xbt_fifo_item_t item = NULL;
106 xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t) {
107 if (task->simdata->source == host)
114 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
116 mailbox->cond = cond;
119 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
121 return mailbox->alias;
124 const char *MSG_mailbox_get_hostname(msg_mailbox_t mailbox)
126 return mailbox->hostname;
129 void MSG_mailbox_set_hostname(msg_mailbox_t mailbox, const char *hostname)
131 mailbox->hostname = xbt_strdup(hostname);
134 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
137 msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
140 mailbox = MSG_mailbox_new(alias);
141 MSG_mailbox_set_hostname(mailbox, MSG_host_self()->name);
147 msg_mailbox_t MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
149 xbt_assert0((host != NULL), "Invalid host");
150 xbt_assert1((channel >= 0)
151 && (channel < msg_global->max_channel), "Invalid channel %d",
154 return host->simdata->mailboxes[(size_t) channel];
158 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t * task,
159 m_host_t host, double timeout)
161 m_process_t process = MSG_process_self();
164 simdata_task_t t_simdata = NULL;
165 simdata_host_t h_simdata = NULL;
166 double start_time = SIMIX_get_clock();
168 smx_cond_t cond = NULL; //conditional wait if the task isn't on the channel yet
173 xbt_assert0(task, "Null pointer for the task storage");
177 ("MSG_task_get() was asked to write in a non empty task struct.");
181 h_simdata = h->simdata;
183 SIMIX_mutex_lock(h_simdata->mutex); //FIXME: lock the mailbox instead
185 if (MSG_mailbox_get_cond(mailbox)) {
187 ("A process is already blocked on the channel %s (meaning that someone is already doing a get on this)",
188 MSG_mailbox_get_alias(mailbox));
189 SIMIX_cond_display_info(MSG_mailbox_get_cond(mailbox));
190 xbt_die("Go fix your code!");
194 /* if the mailbox is not empty (has a task) */
195 if (!MSG_mailbox_is_empty(mailbox)) {
197 /* pop the head of the mailbox */
198 t = MSG_mailbox_pop_head(mailbox);
201 /* get the first task of the host */
202 if ((t = MSG_mailbox_get_first_host_task(mailbox, host)))
207 if ((timeout > 0) && (SIMIX_get_clock() - start_time >= timeout)) { // Timeout already elapsed
208 SIMIX_mutex_unlock(h_simdata->mutex);
209 MSG_mailbox_set_cond(mailbox, NULL);
210 SIMIX_cond_destroy(cond);
211 MSG_RETURN(MSG_TRANSFER_FAILURE);
215 cond = SIMIX_cond_init();
216 MSG_mailbox_set_cond(mailbox, cond);
220 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout - start_time);
222 SIMIX_cond_wait(MSG_mailbox_get_cond(mailbox), h_simdata->mutex);
224 if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
225 SIMIX_mutex_unlock(h_simdata->mutex);
226 MSG_mailbox_set_cond(mailbox, NULL);
227 SIMIX_cond_destroy(cond);
228 MSG_RETURN(MSG_HOST_FAILURE);
233 DEBUG1("OK, got a task (%s)", t->name);
234 /* clean conditional */
236 MSG_mailbox_set_cond(mailbox, NULL);
237 SIMIX_cond_destroy(cond);
240 SIMIX_mutex_unlock(h_simdata->mutex);
242 t_simdata = t->simdata;
243 t_simdata->receiver = process;
246 SIMIX_mutex_lock(t_simdata->mutex);
249 /* create SIMIX action to the communication */
251 SIMIX_action_communicate(t_simdata->sender->simdata->m_host->simdata->
253 process->simdata->m_host->simdata->smx_host,
254 t->name, t_simdata->message_size,
257 SIMIX_action_use(t_simdata->comm);
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);
264 SIMIX_register_action_to_condition(t_simdata->comm, t_simdata->cond);
265 // breaking point if asynchrounous
266 process->simdata->waiting_task = t;
269 SIMIX_cond_wait(t_simdata->cond, t_simdata->mutex);
271 if (SIMIX_action_get_state(t_simdata->comm) != SURF_ACTION_RUNNING)
273 if (!SIMIX_host_get_state(h_simdata->smx_host))
275 if (!SIMIX_host_get_state(process->simdata->m_host->simdata->smx_host))
279 SIMIX_unregister_action_to_condition(t_simdata->comm, t_simdata->cond);
280 process->simdata->waiting_task = NULL;
282 /* for this process, don't need to change in get function */
283 SIMIX_mutex_unlock(t_simdata->mutex);
285 if (SIMIX_action_get_state(t_simdata->comm) == SURF_ACTION_DONE) {
286 if (SIMIX_action_destroy(t_simdata->comm))
287 t_simdata->comm = NULL;
289 } else if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
290 if (SIMIX_action_destroy(t_simdata->comm))
291 t_simdata->comm = NULL;
292 MSG_RETURN(MSG_HOST_FAILURE);
294 if (SIMIX_action_destroy(t_simdata->comm))
295 t_simdata->comm = NULL;
296 MSG_RETURN(MSG_TRANSFER_FAILURE);
301 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
304 m_process_t process = MSG_process_self();
305 const char *hostname;
306 simdata_task_t t_simdata = NULL;
307 m_host_t local_host = NULL;
308 m_host_t remote_host = NULL;
309 smx_cond_t cond = NULL;
313 t_simdata = task->simdata;
314 t_simdata->sender = process;
315 t_simdata->source = MSG_process_get_host(process);
317 xbt_assert0(t_simdata->refcount == 1,
318 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
320 t_simdata->comm = NULL;
322 t_simdata->refcount++;
323 local_host = ((simdata_process_t) process->simdata)->m_host;
324 msg_global->sent_msg++;
326 /* get the host name containing the mailbox */
327 hostname = MSG_mailbox_get_hostname(mailbox);
329 remote_host = MSG_get_host_by_name(hostname);
332 THROW1(not_found_error, 0, "Host %s not fount", hostname);
335 DEBUG4("Trying to send a task (%g kB) from %s to %s on the channel %s",
336 t_simdata->message_size / 1000, local_host->name,
337 remote_host->name, MSG_mailbox_get_alias(mailbox));
339 SIMIX_mutex_lock(remote_host->simdata->mutex); /* FIXME: lock the mailbox instead */
341 /* put the task in the mailbox */
342 xbt_fifo_push(mailbox->tasks, task);
344 if ((cond = MSG_mailbox_get_cond(mailbox))) {
345 DEBUG0("Somebody is listening. Let's wake him up!");
346 SIMIX_cond_signal(cond);
349 SIMIX_mutex_unlock(remote_host->simdata->mutex);
351 SIMIX_mutex_lock(t_simdata->mutex);
353 process->simdata->waiting_task = task; // for debugging and status displaying purpose
359 time = SIMIX_get_clock();
362 /*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 */
364 time_elapsed = SIMIX_get_clock() - time;
365 SIMIX_cond_wait_timeout(t_simdata->cond, t_simdata->mutex,
366 timeout - time_elapsed);
369 SIMIX_action_use(t_simdata->comm);
370 if (t_simdata->comm && (SIMIX_action_get_state(t_simdata->comm) !=
371 SURF_ACTION_RUNNING))
373 if (!SIMIX_host_get_state(local_host->simdata->smx_host))
375 if (!SIMIX_host_get_state(remote_host->simdata->smx_host))
380 if (e.category == timeout_error) {
382 /* verify if the timeout happened and the communication didn't started yet */
383 if (t_simdata->comm == NULL) {
384 DEBUG1("Action terminated %s (there was a timeout)", task->name);
385 process->simdata->waiting_task = NULL;
387 /* remove the task from the mailbox */
388 MSG_mailbox_remove(mailbox, task);
390 /* if (t_simdata->receiver && t_simdata->receiver->simdata) { /\* receiver still around *\/ */
391 /* t_simdata->receiver->simdata->waiting_task = NULL; */
394 SIMIX_mutex_unlock(t_simdata->mutex);
395 MSG_RETURN(MSG_TRANSFER_FAILURE);
402 while (1) { //FIXME: factorize with the code right above
403 SIMIX_cond_wait(t_simdata->cond, t_simdata->mutex);
406 SIMIX_action_use(t_simdata->comm);
408 && SIMIX_action_get_state(t_simdata->comm) != SURF_ACTION_RUNNING)
410 if (!SIMIX_host_get_state(local_host->simdata->smx_host))
412 if (!SIMIX_host_get_state(remote_host->simdata->smx_host))
417 DEBUG1("Action terminated %s", task->name);
418 process->simdata->waiting_task = NULL;
419 /* if (t_simdata->receiver && t_simdata->receiver->simdata) { /\* receiver still around *\/ */
420 /* t_simdata->receiver->simdata->waiting_task = NULL; */
423 SIMIX_mutex_unlock(t_simdata->mutex);
426 && SIMIX_action_get_state(t_simdata->comm) == SURF_ACTION_DONE) {
427 if (SIMIX_action_destroy(t_simdata->comm))
428 t_simdata->comm = NULL;
429 t_simdata->refcount--;
431 } else if (SIMIX_host_get_state(local_host->simdata->smx_host) == 0) {
432 if (t_simdata->comm && SIMIX_action_destroy(t_simdata->comm))
433 t_simdata->comm = NULL;
434 MSG_RETURN(MSG_HOST_FAILURE);
436 if (t_simdata->comm && SIMIX_action_destroy(t_simdata->comm))
437 t_simdata->comm = NULL;
438 MSG_RETURN(MSG_TRANSFER_FAILURE);