Logo AND Algorithmique Numérique Distribuée

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