Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge "mc"
[simgrid.git] / src / msg / msg_mailbox.c
1 /* Mailboxes in MSG */
2
3 /* Copyright (c) 2008-2014. 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   return MSG_mailbox_get_task_ext_bounded(mailbox, task, host, timeout, -1.0);
94 }
95
96 /** \ingroup msg_mailbox_management
97  * \brief Get a task from a mailbox on a given host at a given rate
98  *
99  * \param mailbox The mailbox where the task was sent
100  * \param task a memory location for storing a #msg_task_t.
101  * \param host a #msg_host_t host from where the task was sent
102  * \param timeout a timeout
103  * \param rate a rate
104
105  * \return Returns
106  * #MSG_OK if the task was successfully received,
107  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
108  */
109 msg_error_t
110 MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * task,
111                                  msg_host_t host, double timeout, double rate)
112 {
113   xbt_ex_t e;
114   msg_error_t ret = MSG_OK;
115   /* We no longer support getting a task from a specific host */
116   if (host)
117     THROW_UNIMPLEMENTED;
118
119 #ifdef HAVE_TRACING
120   TRACE_msg_task_get_start();
121   double start_time = MSG_get_clock();
122 #endif
123
124   /* Sanity check */
125   xbt_assert(task, "Null pointer for the task storage");
126
127   if (*task)
128     XBT_WARN
129         ("Asked to write the received task in a non empty struct -- proceeding.");
130
131   /* Try to receive it by calling SIMIX network layer */
132   TRY {
133     simcall_comm_recv(mailbox, task, NULL, NULL, NULL, timeout, rate);
134     XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox);
135     if (msg_global->debug_multiple_use && (*task)->simdata->isused!=0)
136       xbt_ex_free(*(xbt_ex_t*)(*task)->simdata->isused);
137     (*task)->simdata->isused = 0;
138   }
139   CATCH(e) {
140     switch (e.category) {
141     case cancel_error:
142       ret = MSG_HOST_FAILURE;
143       break;
144     case network_error:
145       ret = MSG_TRANSFER_FAILURE;
146       break;
147     case timeout_error:
148       ret = MSG_TIMEOUT;
149       break;
150     default:
151       RETHROW;
152     }
153     xbt_ex_free(e);
154   }
155
156 #ifdef HAVE_TRACING
157   if (ret != MSG_HOST_FAILURE &&
158       ret != MSG_TRANSFER_FAILURE &&
159       ret != MSG_TIMEOUT) {
160     TRACE_msg_task_get_end(start_time, *task);
161   }
162 #endif
163   MSG_RETURN(ret);
164 }
165
166 msg_error_t
167 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, msg_task_t task,
168                              double timeout)
169 {
170   xbt_ex_t e;
171   msg_error_t ret = MSG_OK;
172   simdata_task_t t_simdata = NULL;
173   msg_process_t process = MSG_process_self();
174   simdata_process_t p_simdata = SIMIX_process_self_get_data(process);
175
176 #ifdef HAVE_TRACING
177   int call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
178 #endif
179
180   /* Prepare the task to send */
181   t_simdata = task->simdata;
182   t_simdata->sender = process;
183   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
184
185   if (t_simdata->isused != 0) {
186     if (msg_global->debug_multiple_use){
187       XBT_ERROR("This task is already used in there:");
188       xbt_backtrace_display(t_simdata->isused);
189       XBT_ERROR("And you try to reuse it from here:");
190       xbt_backtrace_display_current();
191     } else {
192       xbt_assert(t_simdata->isused == 0,
193                  "This task is still being used somewhere else. You cannot send it now. Go fix your code! (use --cfg=msg/debug_multiple_use:on to get the backtrace of the other process)");
194     }
195   }
196
197   if (msg_global->debug_multiple_use)
198     MSG_BT(t_simdata->isused, "Using Backtrace");
199   else
200     t_simdata->isused = (void*)1;
201   t_simdata->comm = NULL;
202   msg_global->sent_msg++;
203
204
205   p_simdata->waiting_task = task;
206
207   /* Try to send it by calling SIMIX network layer */
208   TRY {
209     smx_action_t comm = NULL; /* MC needs the comm to be set to NULL during the simix call  */
210     comm = simcall_comm_isend(mailbox, t_simdata->message_size,
211                                   t_simdata->rate, task, sizeof(void *),
212                                   NULL, NULL, task, 0);
213 #ifdef HAVE_TRACING
214     if (TRACE_is_enabled()) {
215       simcall_set_category(comm, task->category);
216     }
217 #endif
218      t_simdata->comm = comm;
219      simcall_comm_wait(comm, timeout);
220   }
221
222   CATCH(e) {
223     switch (e.category) {
224     case cancel_error:
225       ret = MSG_HOST_FAILURE;
226       break;
227     case network_error:
228       ret = MSG_TRANSFER_FAILURE;
229       break;
230     case timeout_error:
231       ret = MSG_TIMEOUT;
232       break;
233     default:
234       RETHROW;
235     }
236     xbt_ex_free(e);
237
238     /* If the send failed, it is not used anymore */
239     if (msg_global->debug_multiple_use && t_simdata->isused!=0)
240       xbt_ex_free(*(xbt_ex_t*)t_simdata->isused);
241     t_simdata->isused = 0;
242   }
243
244
245   p_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 }
252
253 #ifdef MSG_USE_DEPRECATED
254 msg_mailbox_t MSG_mailbox_get_by_channel(msg_host_t host,
255                                          m_channel_t channel)
256 {
257   XBT_WARN("DEPRECATED! Now use MSG_mailbox_get_by_alias");
258   xbt_assert((host != NULL), "Invalid host");
259   xbt_assert((channel >= 0)
260               && (channel < msg_global->max_channel), "Invalid channel %d",
261               channel);
262
263   return MSG_host_priv(host)->mailboxes[(size_t) channel];
264 }
265 #endif