Logo AND Algorithmique Numérique Distribuée

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