Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5a12459c07b26de87c57f52defe77258cc8d69a6
[simgrid.git] / src / msg / msg_mailbox.c
1 #include "mailbox.h"
2 #include "msg/private.h"
3
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg,
5                                 "Logging specific to MSG (mailbox)");
6
7 static xbt_dict_t msg_mailboxes = NULL;
8
9 void MSG_mailbox_mod_init(void)
10 {
11   msg_mailboxes = xbt_dict_new();
12 }
13
14 void MSG_mailbox_mod_exit(void)
15 {
16   xbt_dict_free(&msg_mailboxes);
17 }
18
19 msg_mailbox_t MSG_mailbox_create(const char *alias)
20 {
21   msg_mailbox_t mailbox = xbt_new0(s_msg_mailbox_t, 1);
22
23   mailbox->tasks = xbt_fifo_new();
24   mailbox->cond = NULL;
25   mailbox->alias = alias ? xbt_strdup(alias) : NULL;
26   mailbox->hostname = NULL;
27
28   return mailbox;
29 }
30
31 msg_mailbox_t MSG_mailbox_new(const char *alias)
32 {
33   msg_mailbox_t mailbox = MSG_mailbox_create(alias);
34
35   /* add the mbox in the dictionary */
36   xbt_dict_set(msg_mailboxes, alias, mailbox, MSG_mailbox_free);
37
38   return mailbox;
39 }
40
41 void MSG_mailbox_free(void *mailbox)
42 {
43   msg_mailbox_t _mailbox = (msg_mailbox_t) mailbox;
44
45   if (NULL != (_mailbox->hostname))
46     free(_mailbox->hostname);
47
48   xbt_fifo_free(_mailbox->tasks);
49   free(_mailbox->alias);
50
51   free(_mailbox);
52 }
53
54 void MSG_mailbox_put(msg_mailbox_t mailbox, m_task_t task)
55 {
56   xbt_fifo_push(mailbox->tasks, task);
57 }
58
59 smx_cond_t MSG_mailbox_get_cond(msg_mailbox_t mailbox)
60 {
61   return mailbox->cond;
62 }
63
64 void MSG_mailbox_remove(msg_mailbox_t mailbox, m_task_t task)
65 {
66   xbt_fifo_remove(mailbox->tasks, task);
67 }
68
69 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
70 {
71   return (NULL == xbt_fifo_get_first_item(mailbox->tasks));
72 }
73
74 m_task_t MSG_mailbox_pop_head(msg_mailbox_t mailbox)
75 {
76   return (m_task_t) xbt_fifo_shift(mailbox->tasks);
77 }
78
79 m_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
80 {
81   xbt_fifo_item_t item;
82
83   if (NULL == (item = xbt_fifo_get_first_item(mailbox->tasks)))
84     return NULL;
85
86   return (m_task_t) xbt_fifo_get_item_content(item);
87 }
88
89
90 m_task_t
91 MSG_mailbox_get_first_host_task(msg_mailbox_t mailbox, m_host_t host)
92 {
93   m_task_t task = NULL;
94   xbt_fifo_item_t item = NULL;
95
96   xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t)
97       if (task->simdata->source == host) {
98     xbt_fifo_remove_item(mailbox->tasks, item);
99     return task;
100   }
101
102   return NULL;
103 }
104
105 int
106 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
107                                          m_host_t host)
108 {
109   m_task_t task = NULL;
110   xbt_fifo_item_t item = NULL;
111   int count = 0;
112
113   xbt_fifo_foreach(mailbox->tasks, item, task, m_task_t) {
114     if (task->simdata->source == host)
115       count++;
116   }
117
118   return count;
119 }
120
121 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
122 {
123   mailbox->cond = cond;
124 }
125
126 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
127 {
128   return mailbox->alias;
129 }
130
131 const char *MSG_mailbox_get_hostname(msg_mailbox_t mailbox)
132 {
133   return mailbox->hostname;
134 }
135
136 void MSG_mailbox_set_hostname(msg_mailbox_t mailbox, const char *hostname)
137 {
138   mailbox->hostname = xbt_strdup(hostname);
139 }
140
141 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
142 {
143
144   msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
145
146   if (!mailbox) {
147     mailbox = MSG_mailbox_new(alias);
148     MSG_mailbox_set_hostname(mailbox, MSG_host_self()->name);
149   }
150
151   return mailbox;
152 }
153
154 msg_mailbox_t
155 MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
156 {
157   xbt_assert0((host != NULL), "Invalid host");
158   xbt_assert1((channel >= 0)
159               && (channel < msg_global->max_channel), "Invalid channel %d",
160               channel);
161
162   return host->simdata->mailboxes[(size_t) channel];
163 }
164
165 MSG_error_t
166 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t * task,
167                          m_host_t host, double timeout)
168 {
169   m_process_t process = MSG_process_self();
170   m_task_t t = NULL;
171   m_host_t h = NULL;
172   simdata_task_t t_simdata = NULL;
173   simdata_host_t h_simdata = NULL;
174   int first_time = 1;
175
176   smx_cond_t cond = NULL;       //conditional wait if the task isn't on the channel yet
177
178   CHECK_HOST();
179
180   /* Sanity check */
181   xbt_assert0(task, "Null pointer for the task storage");
182
183   if (*task)
184     CRITICAL0
185         ("MSG_task_get() was asked to write in a non empty task struct.");
186
187   /* Get the task */
188   h = MSG_host_self();
189   h_simdata = h->simdata;
190
191   SIMIX_mutex_lock(h->simdata->mutex);
192
193   if (MSG_mailbox_get_cond(mailbox)) {
194     CRITICAL1("A process is already blocked on the channel %s",
195               MSG_mailbox_get_alias(mailbox));
196     SIMIX_cond_display_info(MSG_mailbox_get_cond(mailbox));
197     xbt_die("Go fix your code!");
198   }
199
200   while (1) {
201     /* if the mailbox is empty (has no task */
202     if (!MSG_mailbox_is_empty(mailbox)) {
203       if (!host) {
204         /* pop the head of the mailbox */
205         t = MSG_mailbox_pop_head(mailbox);
206         break;
207       } else {
208         /* get the first task of the host */
209         if (NULL != (t = MSG_mailbox_get_first_host_task(mailbox, host)))
210           break;
211       }
212     }
213
214     if (timeout > 0) {
215       if (!first_time) {
216         SIMIX_mutex_unlock(h->simdata->mutex);
217         /* set the simix condition of the mailbox to NULL */
218         MSG_mailbox_set_cond(mailbox, NULL);
219         SIMIX_cond_destroy(cond);
220         MSG_RETURN(MSG_TRANSFER_FAILURE);
221       }
222     }
223
224     cond = SIMIX_cond_init();
225
226     /* set the condition of the mailbox */
227     MSG_mailbox_set_cond(mailbox, cond);
228
229     if (timeout > 0)
230       SIMIX_cond_wait_timeout(cond, h->simdata->mutex, timeout);
231     else
232       SIMIX_cond_wait(MSG_mailbox_get_cond(mailbox), h->simdata->mutex);
233
234
235     if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
236       MSG_mailbox_set_cond(mailbox, NULL);
237       SIMIX_cond_destroy(cond);
238       MSG_RETURN(MSG_HOST_FAILURE);
239     }
240
241     first_time = 0;
242   }
243
244   SIMIX_mutex_unlock(h->simdata->mutex);
245
246   DEBUG1("OK, got a task (%s)", t->name);
247   /* clean conditional */
248   if (cond) {
249     SIMIX_cond_destroy(cond);
250
251     MSG_mailbox_set_cond(mailbox, NULL);
252   }
253
254   t_simdata = t->simdata;
255   t_simdata->receiver = process;
256   *task = t;
257
258   SIMIX_mutex_lock(t_simdata->mutex);
259
260   /* Transfer */
261   /* create SIMIX action to the communication */
262   t_simdata->comm =
263       SIMIX_action_communicate(t_simdata->sender->simdata->m_host->
264                                simdata->smx_host,
265                                process->simdata->m_host->simdata->smx_host,
266                                t->name, t_simdata->message_size,
267                                t_simdata->rate);
268
269   /* if the process is suspend, create the action but stop its execution, it will be restart when the sender process resume */
270   if (MSG_process_is_suspended(t_simdata->sender)) {
271     DEBUG1("Process sender (%s) suspended", t_simdata->sender->name);
272     SIMIX_action_set_priority(t_simdata->comm, 0);
273   }
274
275   process->simdata->waiting_task = t;
276   SIMIX_register_action_to_condition(t_simdata->comm, t_simdata->cond);
277
278   while (1) {
279     SIMIX_cond_wait(t_simdata->cond, t_simdata->mutex);
280
281     if (SIMIX_action_get_state(t_simdata->comm) != SURF_ACTION_RUNNING)
282       break;
283   }
284
285   SIMIX_unregister_action_to_condition(t_simdata->comm, t_simdata->cond);
286   process->simdata->waiting_task = NULL;
287
288   /* the task has already finished and the pointer must be null */
289   if (t->simdata->sender) {
290     t->simdata->sender->simdata->waiting_task = NULL;
291   }
292
293   /* for this process, don't need to change in get function */
294   t->simdata->receiver = NULL;
295   SIMIX_mutex_unlock(t_simdata->mutex);
296
297
298   if (SIMIX_action_get_state(t_simdata->comm) == SURF_ACTION_DONE) {
299     SIMIX_action_destroy(t_simdata->comm);
300     t_simdata->comm = NULL;
301     t_simdata->using--;
302     MSG_RETURN(MSG_OK);
303   } else if (SIMIX_host_get_state(h_simdata->smx_host) == 0) {
304     SIMIX_action_destroy(t_simdata->comm);
305     t_simdata->comm = NULL;
306     t_simdata->using--;
307     MSG_RETURN(MSG_HOST_FAILURE);
308   } else {
309     SIMIX_action_destroy(t_simdata->comm);
310     t_simdata->comm = NULL;
311     t_simdata->using--;
312     MSG_RETURN(MSG_TRANSFER_FAILURE);
313   }
314 }
315
316 MSG_error_t
317 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
318                              double timeout)
319 {
320   m_process_t process = MSG_process_self();
321   const char *hostname;
322   simdata_task_t task_simdata = NULL;
323   m_host_t local_host = NULL;
324   m_host_t remote_host = NULL;
325   smx_cond_t cond = NULL;
326
327   CHECK_HOST();
328
329   task_simdata = task->simdata;
330   task_simdata->sender = process;
331   task_simdata->source = MSG_process_get_host(process);
332
333   xbt_assert0(task_simdata->using == 1,
334               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
335
336   task_simdata->comm = NULL;
337
338   task_simdata->using++;
339   local_host = ((simdata_process_t) process->simdata)->m_host;
340
341   /* get the host name containing the mailbox */
342   hostname = MSG_mailbox_get_hostname(mailbox);
343
344   remote_host = MSG_get_host_by_name(hostname);
345
346   if (NULL == remote_host)
347     THROW1(not_found_error, 0, "Host %s not fount", hostname);
348
349
350   DEBUG4
351       ("Trying to send a task (%g kB) from %s to %s on the channel aliased by the alias %s",
352        task->simdata->message_size / 1000, local_host->name,
353        remote_host->name, MSG_mailbox_get_alias(mailbox));
354
355   SIMIX_mutex_lock(remote_host->simdata->mutex);
356
357   /* put the task in the mailbox */
358   MSG_mailbox_put(mailbox, task);
359
360   if (NULL != (cond = MSG_mailbox_get_cond(mailbox))) {
361     DEBUG0("Somebody is listening. Let's wake him up!");
362     SIMIX_cond_signal(cond);
363   }
364
365
366
367   SIMIX_mutex_unlock(remote_host->simdata->mutex);
368
369   SIMIX_mutex_lock(task->simdata->mutex);
370
371   process->simdata->waiting_task = task;
372
373   if (timeout > 0) {
374     xbt_ex_t e;
375     double time;
376     double time_elapsed;
377     time = SIMIX_get_clock();
378
379     TRY {
380       /*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 */
381       while (1) {
382         time_elapsed = SIMIX_get_clock() - time;
383         SIMIX_cond_wait_timeout(task->simdata->cond, task->simdata->mutex,
384                                 timeout - time_elapsed);
385
386         if ((task->simdata->comm != NULL)
387             && (SIMIX_action_get_state(task->simdata->comm) !=
388                 SURF_ACTION_RUNNING))
389           break;
390       }
391     }
392     CATCH(e) {
393       if (e.category == timeout_error) {
394         xbt_ex_free(e);
395         /* verify if the timeout happened and the communication didn't started yet */
396         if (task->simdata->comm == NULL) {
397           process->simdata->waiting_task = NULL;
398
399           /* remove the task from the mailbox */
400           MSG_mailbox_remove(mailbox, task);
401
402           if (task->simdata->receiver) {
403             task->simdata->receiver->simdata->waiting_task = NULL;
404           }
405
406           task->simdata->sender = NULL;
407
408           SIMIX_mutex_unlock(task->simdata->mutex);
409           MSG_RETURN(MSG_TRANSFER_FAILURE);
410         }
411       } else {
412         RETHROW;
413       }
414     }
415   } else {
416     while (1) {
417       SIMIX_cond_wait(task->simdata->cond, task->simdata->mutex);
418
419       if (SIMIX_action_get_state(task->simdata->comm) !=
420           SURF_ACTION_RUNNING)
421         break;
422     }
423   }
424
425   DEBUG1("Action terminated %s", task->name);
426   process->simdata->waiting_task = NULL;
427
428   /* the task has already finished and the pointer must be null */
429   if (task->simdata->receiver) {
430     task->simdata->receiver->simdata->waiting_task = NULL;
431   }
432
433   task->simdata->sender = NULL;
434   SIMIX_mutex_unlock(task->simdata->mutex);
435
436
437   if (SIMIX_action_get_state(task->simdata->comm) == SURF_ACTION_DONE) {
438     MSG_RETURN(MSG_OK);
439   } else if (SIMIX_host_get_state(local_host->simdata->smx_host) == 0) {
440     MSG_RETURN(MSG_HOST_FAILURE);
441   } else {
442     MSG_RETURN(MSG_TRANSFER_FAILURE);
443   }
444 }