Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Introduce sg_storage_size_t type
[simgrid.git] / src / msg / msg_mailbox.c
1 /* Mailboxes in MSG */
2
3 /* Copyright (c) 2008-2013. 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 "msg_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 msg_mailbox_t MSG_mailbox_new(const char *alias)
16 {
17   return simcall_rdv_create(alias);
18 }
19
20 void MSG_mailbox_free(void *mailbox)
21 {
22   simcall_rdv_destroy((msg_mailbox_t)mailbox);
23 }
24
25 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
26 {
27   return (NULL == simcall_rdv_get_head(mailbox));
28 }
29
30 msg_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
31 {
32   smx_action_t comm = simcall_rdv_get_head(mailbox);
33
34   if (!comm)
35     return NULL;
36
37   return (msg_task_t) simcall_comm_get_src_data(comm);
38 }
39
40 int
41 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
42                                          msg_host_t host)
43 {
44   return simcall_rdv_comm_count_by_host(mailbox, host);
45 }
46
47 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
48 {
49
50   msg_mailbox_t mailbox = simcall_rdv_get_by_name(alias);
51
52   if (!mailbox)
53     mailbox = MSG_mailbox_new(alias);
54
55   return mailbox;
56 }
57
58 /** \ingroup msg_mailbox_management
59  * \brief Set the mailbox to receive in asynchronous mode
60  *
61  * All messages sent to this mailbox will be transferred to 
62  * the receiver without waiting for the receive call. 
63  * The receive call will still be necessary to use the received data.
64  * If there is a need to receive some messages asynchronously, and some not, 
65  * two different mailboxes should be used.
66  *
67  * \param alias The name of the mailbox 
68  */
69 void MSG_mailbox_set_async(const char *alias){
70   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
71
72   simcall_rdv_set_receiver(mailbox, SIMIX_process_self());
73   XBT_VERB("%s mailbox set to receive eagerly for process %p\n",alias, SIMIX_process_self());
74
75 }
76
77 /** \ingroup msg_mailbox_management
78  * \brief Get a task from a mailbox on a given host
79  *
80  * \param mailbox The mailbox where the task was sent
81  * \param task a memory location for storing a #msg_task_t.
82  * \param host a #msg_host_t host from where the task was sent
83  * \param timeout a timeout
84
85  * \return Returns
86  * #MSG_OK if the task was successfully received,
87  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
88  */
89 msg_error_t
90 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, msg_task_t * task,
91                          msg_host_t host, double timeout)
92 {
93   xbt_ex_t e;
94   msg_error_t ret = MSG_OK;
95   /* We no longer support getting a task from a specific host */
96   if (host)
97     THROW_UNIMPLEMENTED;
98
99 #ifdef HAVE_TRACING
100   TRACE_msg_task_get_start();
101   double start_time = MSG_get_clock();
102 #endif
103
104   /* Sanity check */
105   xbt_assert(task, "Null pointer for the task storage");
106
107   if (*task)
108     XBT_WARN
109         ("Asked to write the received task in a non empty struct -- proceeding.");
110
111   /* Try to receive it by calling SIMIX network layer */
112   TRY {
113     simcall_comm_recv(mailbox, task, NULL, NULL, NULL, timeout);
114     XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox);
115     (*task)->simdata->isused=0;
116   }
117   CATCH(e) {
118     switch (e.category) {
119     case cancel_error:
120       ret = MSG_HOST_FAILURE;
121       break;
122     case network_error:
123       ret = MSG_TRANSFER_FAILURE;
124       break;
125     case timeout_error:
126       ret = MSG_TIMEOUT;
127       break;
128     default:
129       RETHROW;
130     }
131     xbt_ex_free(e);
132   }
133
134 #ifdef HAVE_TRACING
135   if (ret != MSG_HOST_FAILURE &&
136       ret != MSG_TRANSFER_FAILURE &&
137       ret != MSG_TIMEOUT) {
138     TRACE_msg_task_get_end(start_time, *task);
139   }
140 #endif
141   MSG_RETURN(ret);
142 }
143
144 /** \ingroup msg_mailbox_management
145  * \brief Get a task from a mailbox on a given host at a given rate
146  *
147  * \param mailbox The mailbox where the task was sent
148  * \param task a memory location for storing a #msg_task_t.
149  * \param host a #msg_host_t host from where the task was sent
150  * \param timeout a timeout
151  * \param rate a rate
152
153  * \return Returns
154  * #MSG_OK if the task was successfully received,
155  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
156  */
157 msg_error_t
158 MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * task,
159                          msg_host_t host, double timeout, double rate)
160 {
161   xbt_ex_t e;
162   msg_error_t ret = MSG_OK;
163   /* We no longer support getting a task from a specific host */
164   if (host)
165     THROW_UNIMPLEMENTED;
166
167 #ifdef HAVE_TRACING
168   TRACE_msg_task_get_start();
169   double start_time = MSG_get_clock();
170 #endif
171
172   /* Sanity check */
173   xbt_assert(task, "Null pointer for the task storage");
174
175   if (*task)
176     XBT_WARN
177         ("Asked to write the received task in a non empty struct -- proceeding.");
178
179   /* Try to receive it by calling SIMIX network layer */
180   TRY {
181     simcall_comm_recv_bounded(mailbox, task, NULL, NULL, NULL, timeout, rate);
182     XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox);
183     (*task)->simdata->isused=0;
184   }
185   CATCH(e) {
186     switch (e.category) {
187     case cancel_error:
188       ret = MSG_HOST_FAILURE;
189       break;
190     case network_error:
191       ret = MSG_TRANSFER_FAILURE;
192       break;
193     case timeout_error:
194       ret = MSG_TIMEOUT;
195       break;
196     default:
197       RETHROW;
198     }
199     xbt_ex_free(e);
200   }
201
202 #ifdef HAVE_TRACING
203   if (ret != MSG_HOST_FAILURE &&
204       ret != MSG_TRANSFER_FAILURE &&
205       ret != MSG_TIMEOUT) {
206     TRACE_msg_task_get_end(start_time, *task);
207   }
208 #endif
209   MSG_RETURN(ret);
210 }
211
212 msg_error_t
213 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, msg_task_t task,
214                              double timeout)
215 {
216   xbt_ex_t e;
217   msg_error_t ret = MSG_OK;
218   simdata_task_t t_simdata = NULL;
219   msg_process_t process = MSG_process_self();
220   simdata_process_t p_simdata = SIMIX_process_self_get_data(process);
221
222 #ifdef HAVE_TRACING
223   int call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
224 #endif
225
226   /* Prepare the task to send */
227   t_simdata = task->simdata;
228   t_simdata->sender = process;
229   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
230
231   xbt_assert(t_simdata->isused == 0,
232               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
233
234   t_simdata->isused=1;
235   t_simdata->comm = NULL;
236   msg_global->sent_msg++;
237
238
239   p_simdata->waiting_task = task;
240
241   /* Try to send it by calling SIMIX network layer */
242   TRY {
243       smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
244                                   t_simdata->rate, task, sizeof(void *),
245                                   NULL, NULL, task, 0);
246 #ifdef HAVE_TRACING
247     if (TRACE_is_enabled()) {
248       simcall_set_category(comm, task->category);
249     }
250 #endif
251      t_simdata->comm = comm;
252      simcall_comm_wait(comm, timeout);
253   }
254
255   CATCH(e) {
256     switch (e.category) {
257     case cancel_error:
258       ret = MSG_HOST_FAILURE;
259       break;
260     case network_error:
261       ret = MSG_TRANSFER_FAILURE;
262       break;
263     case timeout_error:
264       ret = MSG_TIMEOUT;
265       break;
266     default:
267       RETHROW;
268     }
269     xbt_ex_free(e);
270
271     /* If the send failed, it is not used anymore */
272     t_simdata->isused = 0;
273   }
274
275
276   p_simdata->waiting_task = NULL;
277 #ifdef HAVE_TRACING
278   if (call_end)
279     TRACE_msg_task_put_end();
280 #endif
281   MSG_RETURN(ret);
282 }
283
284 #ifdef MSG_USE_DEPRECATED
285 msg_mailbox_t MSG_mailbox_get_by_channel(msg_host_t host,
286                                          m_channel_t channel)
287 {
288   XBT_WARN("DEPRECATED! Now use MSG_mailbox_get_by_alias");
289   xbt_assert((host != NULL), "Invalid host");
290   xbt_assert((channel >= 0)
291               && (channel < msg_global->max_channel), "Invalid channel %d",
292               channel);
293
294   return host->mailboxes[(size_t) channel];
295 }
296 #endif