Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix use after free when using SIMIX_network_wait().
[simgrid.git] / src / msg / msg_mailbox.c
1 /* Mailboxes in MSG */
2
3 /* Copyright (c) 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "mailbox.h"
10 #include "msg/private.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg,
13                                 "Logging specific to MSG (mailbox)");
14
15 static xbt_dict_t msg_mailboxes = NULL;
16
17 void MSG_mailbox_mod_init(void)
18 {
19   msg_mailboxes = xbt_dict_new();
20 }
21
22 void MSG_mailbox_mod_exit(void)
23 {
24   xbt_dict_free(&msg_mailboxes);
25 }
26
27 msg_mailbox_t MSG_mailbox_create(const char *alias)
28 {
29   msg_mailbox_t mailbox = xbt_new0(s_msg_mailbox_t, 1);
30
31   mailbox->cond = NULL;
32   mailbox->alias = alias ? xbt_strdup(alias) : NULL;
33   mailbox->rdv = SIMIX_rdv_create(alias);
34
35   return mailbox;
36 }
37
38 msg_mailbox_t MSG_mailbox_new(const char *alias)
39 {
40   msg_mailbox_t mailbox = MSG_mailbox_create(alias);
41
42   /* add the mbox in the dictionary */
43   xbt_dict_set(msg_mailboxes, alias, mailbox, MSG_mailbox_free);
44
45   return mailbox;
46 }
47
48 void MSG_mailbox_free(void *mailbox)
49 {
50   msg_mailbox_t _mailbox = (msg_mailbox_t) mailbox;
51
52   free(_mailbox->alias);
53   SIMIX_rdv_destroy(_mailbox->rdv);
54
55   free(_mailbox);
56 }
57
58 smx_cond_t MSG_mailbox_get_cond(msg_mailbox_t mailbox)
59 {
60   return mailbox->cond;
61 }
62
63 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
64 {
65   return (NULL == SIMIX_rdv_get_head(mailbox->rdv));
66 }
67
68 m_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
69 {
70   smx_comm_t comm = SIMIX_rdv_get_head(mailbox->rdv);
71
72   if (!comm)
73     return NULL;
74
75   return (m_task_t) SIMIX_communication_get_data(comm);
76 }
77
78 int
79 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
80                                          m_host_t host)
81 {
82   return SIMIX_rdv_get_count_waiting_comm(mailbox->rdv,
83                                           host->simdata->smx_host);
84 }
85
86 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
87 {
88   mailbox->cond = cond;
89 }
90
91 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
92 {
93   return mailbox->alias;
94 }
95
96 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
97 {
98
99   msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
100
101   if (!mailbox)
102     mailbox = MSG_mailbox_new(alias);
103
104   return mailbox;
105 }
106
107 msg_mailbox_t MSG_mailbox_get_by_channel(m_host_t host,
108                                          m_channel_t channel)
109 {
110   xbt_assert0((host != NULL), "Invalid host");
111   xbt_assert1((channel >= 0)
112               && (channel < msg_global->max_channel), "Invalid channel %d",
113               channel);
114
115   return host->simdata->mailboxes[(size_t) channel];
116 }
117
118 MSG_error_t
119 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t * task,
120                          m_host_t host, double timeout)
121 {
122   xbt_ex_t e;
123   MSG_error_t ret = MSG_OK;
124   smx_comm_t comm;
125 #ifdef HAVE_TRACING
126   double start_time = 0;
127 #endif
128   /* We no longer support getting a task from a specific host */
129   if (host)
130     THROW_UNIMPLEMENTED;
131
132   CHECK_HOST();
133 #ifdef HAVE_TRACING
134   TRACE_msg_task_get_start();
135   start_time = MSG_get_clock();
136 #endif
137
138   /* Kept for compatibility with older implementation */
139   xbt_assert1(!MSG_mailbox_get_cond(mailbox),
140               "A process is already blocked on this channel %s",
141               MSG_mailbox_get_alias(mailbox));
142
143   /* Sanity check */
144   xbt_assert0(task, "Null pointer for the task storage");
145
146   if (*task)
147     CRITICAL0
148         ("MSG_task_get() was asked to write in a non empty task struct.");
149
150   /* Try to receive it by calling SIMIX network layer */
151   TRY {
152     SIMIX_network_recv(mailbox->rdv, timeout, task, NULL, &comm);
153     //INFO2("Got task %s from %s",(*task)->name,mailbox->alias);
154     (*task)->simdata->refcount--;
155   }
156   CATCH(e) {
157     switch (e.category) {
158     case host_error:
159       ret = MSG_HOST_FAILURE;
160       break;
161     case network_error:
162       ret = MSG_TRANSFER_FAILURE;
163       break;
164     case timeout_error:
165       ret = MSG_TIMEOUT;
166       break;
167     default:
168       xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
169     }
170     xbt_ex_free(e);
171   }
172
173   if (ret != MSG_HOST_FAILURE &&
174       ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
175 #ifdef HAVE_TRACING
176     TRACE_msg_task_get_end(start_time, *task);
177 #endif
178   }
179   MSG_RETURN(ret);
180 }
181
182 MSG_error_t
183 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
184                              double timeout)
185 {
186   xbt_ex_t e;
187   MSG_error_t ret = MSG_OK;
188   smx_comm_t comm;
189   simdata_task_t t_simdata = NULL;
190   m_process_t process = MSG_process_self();
191 #ifdef HAVE_TRACING
192   int call_end = 0;
193 #endif
194   CHECK_HOST();
195
196 #ifdef HAVE_TRACING
197   call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
198 #endif
199
200
201   /* Prepare the task to send */
202   t_simdata = task->simdata;
203   t_simdata->sender = process;
204   t_simdata->source = MSG_host_self();
205
206   xbt_assert0(t_simdata->refcount == 1,
207               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
208
209   t_simdata->refcount++;
210   msg_global->sent_msg++;
211
212   process->simdata->waiting_task = task;
213
214   /* Try to send it by calling SIMIX network layer */
215   TRY {
216     /* Kept for semantical compatibility with older implementation */
217     if (mailbox->cond)
218       SIMIX_cond_signal(mailbox->cond);
219
220     SIMIX_network_send(mailbox->rdv, t_simdata->message_size,
221                        t_simdata->rate, timeout, task, sizeof(void *),
222                        &comm, task);
223   }
224
225   CATCH(e) {
226     switch (e.category) {
227     case host_error:
228       ret = MSG_HOST_FAILURE;
229       break;
230     case network_error:
231       ret = MSG_TRANSFER_FAILURE;
232       break;
233     case timeout_error:
234       ret = MSG_TIMEOUT;
235       break;
236     default:
237       xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
238     }
239     xbt_ex_free(e);
240
241     /* Decrement the refcount only on failure */
242     t_simdata->refcount--;
243   }
244
245   process->simdata->waiting_task = NULL;
246 #ifdef HAVE_TRACING
247   if (call_end)
248     TRACE_msg_task_put_end();
249 #endif
250   MSG_RETURN(ret);
251 }