Logo AND Algorithmique Numérique Distribuée

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