Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f88edeb91efc1739df2a59f772700d51728e8e33
[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 "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 static xbt_dict_t msg_mailboxes = NULL;
16
17 void MSG_mailbox_mod_init(void)
18 {
19   msg_mailboxes = xbt_dict_new();
20 }
21
22 void MSG_mailbox_mod_exit(void)
23 {
24   xbt_dict_free(&msg_mailboxes);
25 }
26
27 msg_mailbox_t MSG_mailbox_create(const char *alias)
28 {
29   msg_mailbox_t mailbox = xbt_new0(s_msg_mailbox_t, 1);
30
31   mailbox->cond = NULL;
32   mailbox->alias = alias ? xbt_strdup(alias) : NULL;
33   mailbox->rdv = SIMIX_rdv_create(alias);
34
35   return mailbox;
36 }
37
38 msg_mailbox_t MSG_mailbox_new(const char *alias)
39 {
40   msg_mailbox_t mailbox = MSG_mailbox_create(alias);
41
42   /* add the mbox in the dictionary */
43   xbt_dict_set(msg_mailboxes, alias, mailbox, MSG_mailbox_free);
44
45   return mailbox;
46 }
47
48 void MSG_mailbox_free(void *mailbox)
49 {
50   msg_mailbox_t _mailbox = (msg_mailbox_t) mailbox;
51
52   free(_mailbox->alias);
53   SIMIX_rdv_destroy(_mailbox->rdv);
54
55   free(_mailbox);
56 }
57
58 smx_cond_t MSG_mailbox_get_cond(msg_mailbox_t mailbox)
59 {
60   return mailbox->cond;
61 }
62
63 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
64 {
65   return (NULL == SIMIX_rdv_get_head(mailbox->rdv));
66 }
67
68 m_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
69 {
70   smx_comm_t comm = SIMIX_rdv_get_head(mailbox->rdv);
71
72   if (!comm)
73     return NULL;
74
75   return (m_task_t) SIMIX_communication_get_data(comm);
76 }
77
78 int
79 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
80                                          m_host_t host)
81 {
82   return SIMIX_rdv_get_count_waiting_comm(mailbox->rdv,
83                                           host->simdata->smx_host);
84 }
85
86 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
87 {
88   mailbox->cond = cond;
89 }
90
91 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
92 {
93   return mailbox->alias;
94 }
95
96 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
97 {
98
99   msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
100
101   if (!mailbox)
102     mailbox = MSG_mailbox_new(alias);
103
104   return mailbox;
105 }
106
107 msg_mailbox_t MSG_mailbox_get_by_channel(m_host_t host,
108                                          m_channel_t channel)
109 {
110   xbt_assert0((host != NULL), "Invalid host");
111   xbt_assert1((channel >= 0)
112               && (channel < msg_global->max_channel), "Invalid channel %d",
113               channel);
114
115   return host->simdata->mailboxes[(size_t) channel];
116 }
117
118 MSG_error_t
119 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t * task,
120                          m_host_t host, double timeout)
121 {
122   xbt_ex_t e;
123   MSG_error_t ret = MSG_OK;
124   smx_comm_t comm;
125 #ifdef HAVE_TRACING
126   double start_time = 0;
127 #endif
128   /* We no longer support getting a task from a specific host */
129   if (host)
130     THROW_UNIMPLEMENTED;
131
132   CHECK_HOST();
133 #ifdef HAVE_TRACING
134   TRACE_msg_task_get_start();
135   start_time = MSG_get_clock();
136 #endif
137
138   memset(&comm, 0, sizeof(comm));
139
140   /* Kept for compatibility with older implementation */
141   xbt_assert1(!MSG_mailbox_get_cond(mailbox),
142               "A process is already blocked on this channel %s",
143               MSG_mailbox_get_alias(mailbox));
144
145   /* Sanity check */
146   xbt_assert0(task, "Null pointer for the task storage");
147
148   if (*task)
149     CRITICAL0
150         ("MSG_task_get() was asked to write in a non empty task struct.");
151
152   /* Try to receive it by calling SIMIX network layer */
153   TRY {
154     SIMIX_network_recv(mailbox->rdv, timeout, task, NULL, &comm);
155     //INFO2("Got task %s from %s",(*task)->name,mailbox->alias);
156     (*task)->simdata->refcount--;
157   }
158   CATCH(e) {
159     switch (e.category) {
160     case host_error:
161       ret = MSG_HOST_FAILURE;
162       break;
163     case network_error:
164       ret = MSG_TRANSFER_FAILURE;
165       break;
166     case timeout_error:
167       ret = MSG_TIMEOUT;
168       break;
169     default:
170       xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
171     }
172     xbt_ex_free(e);
173   }
174
175   if (ret != MSG_HOST_FAILURE &&
176       ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
177 #ifdef HAVE_TRACING
178     TRACE_msg_task_get_end(start_time, *task);
179 #endif
180   }
181   MSG_RETURN(ret);
182 }
183
184 MSG_error_t
185 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
186                              double timeout)
187 {
188   xbt_ex_t e;
189   MSG_error_t ret = MSG_OK;
190   simdata_task_t t_simdata = NULL;
191   m_process_t process = MSG_process_self();
192 #ifdef HAVE_TRACING
193   int call_end = 0;
194 #endif
195   CHECK_HOST();
196
197 #ifdef HAVE_TRACING
198   call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
199 #endif
200
201
202   /* Prepare the task to send */
203   t_simdata = task->simdata;
204   t_simdata->sender = process;
205   t_simdata->source = MSG_host_self();
206
207   xbt_assert0(t_simdata->refcount == 1,
208               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
209
210   t_simdata->refcount++;
211   msg_global->sent_msg++;
212
213   process->simdata->waiting_task = task;
214
215   /* Try to send it by calling SIMIX network layer */
216   TRY {
217     /* Kept for semantical compatibility with older implementation */
218     if (mailbox->cond)
219       SIMIX_cond_signal(mailbox->cond);
220
221     SIMIX_network_send(mailbox->rdv, t_simdata->message_size,
222                        t_simdata->rate, timeout, task, sizeof(void *),
223                        &t_simdata->comm, task);
224   }
225
226   CATCH(e) {
227     switch (e.category) {
228     case host_error:
229       ret = MSG_HOST_FAILURE;
230       break;
231     case network_error:
232       ret = MSG_TRANSFER_FAILURE;
233       break;
234     case timeout_error:
235       ret = MSG_TIMEOUT;
236       break;
237     default:
238       xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
239     }
240     xbt_ex_free(e);
241
242     /* Decrement the refcount only on failure */
243     t_simdata->refcount--;
244   }
245
246   process->simdata->waiting_task = NULL;
247 #ifdef HAVE_TRACING
248   if (call_end)
249     TRACE_msg_task_put_end();
250 #endif
251   MSG_RETURN(ret);
252 }