Logo AND Algorithmique Numérique Distribuée

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