Logo AND Algorithmique Numérique Distribuée

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