Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
409801c85f1e7525be3b2503b88cce84cc38047b
[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, m_host_t host)
80 {
81   return SIMIX_rdv_get_count_waiting_comm (mailbox->rdv, host->simdata->smx_host);
82 }
83
84 void MSG_mailbox_set_cond(msg_mailbox_t mailbox, smx_cond_t cond)
85 {
86   mailbox->cond = cond;
87 }
88
89 const char *MSG_mailbox_get_alias(msg_mailbox_t mailbox)
90 {
91   return mailbox->alias;
92 }
93
94 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
95 {
96
97   msg_mailbox_t mailbox = xbt_dict_get_or_null(msg_mailboxes, alias);
98
99   if (!mailbox)
100     mailbox = MSG_mailbox_new(alias);
101
102   return mailbox;
103 }
104
105 msg_mailbox_t MSG_mailbox_get_by_channel(m_host_t host, m_channel_t channel)
106 {
107   xbt_assert0((host != NULL), "Invalid host");
108   xbt_assert1((channel >= 0)
109               && (channel < msg_global->max_channel), "Invalid channel %d",
110               channel);
111
112   return host->simdata->mailboxes[(size_t) channel];
113 }
114
115 MSG_error_t
116 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t *task, m_host_t host,
117                          double timeout)
118 {
119   xbt_ex_t e;
120   MSG_error_t ret = MSG_OK;
121   smx_comm_t comm;
122
123   /* We no longer support getting a task from a specific host */
124   if (host) THROW_UNIMPLEMENTED;
125
126   CHECK_HOST();
127 #ifdef HAVE_TRACING
128   TRACE_msg_task_get_start ();
129   double start_time = MSG_get_clock();
130 #endif
131
132   memset(&comm,0,sizeof(comm));
133
134   /* Kept for compatibility with older implementation */
135   xbt_assert1(!MSG_mailbox_get_cond(mailbox),
136               "A process is already blocked on this channel %s", 
137               MSG_mailbox_get_alias(mailbox));
138
139   /* Sanity check */
140   xbt_assert0(task, "Null pointer for the task storage");
141
142   if (*task)
143     CRITICAL0("MSG_task_get() was asked to write in a non empty task struct.");
144
145   /* Try to receive it by calling SIMIX network layer */
146   TRY{
147     SIMIX_network_recv(mailbox->rdv, timeout, task, NULL, &comm);
148     //INFO2("Got task %s from %s",(*task)->name,mailbox->alias);
149     (*task)->simdata->refcount--;
150   }
151   CATCH(e){
152     switch(e.category){
153       case host_error:
154         ret = MSG_HOST_FAILURE;
155         break;
156       case network_error:
157         ret = MSG_TRANSFER_FAILURE;
158         break;
159       case timeout_error:
160         ret = MSG_TIMEOUT;
161         break;      
162       default:
163         xbt_die(bprintf("Unhandled SIMIX network exception: %s",e.msg));
164     }
165     xbt_ex_free(e);        
166   }
167
168   if (ret != MSG_HOST_FAILURE &&
169       ret != MSG_TRANSFER_FAILURE &&
170       ret != MSG_TIMEOUT){
171 #ifdef HAVE_TRACING
172     TRACE_msg_task_get_end (start_time, *task);
173 #endif
174   }
175   MSG_RETURN(ret);        
176 }
177
178 MSG_error_t
179 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
180                              double timeout)
181 {
182   xbt_ex_t e;
183   MSG_error_t ret = MSG_OK;
184   simdata_task_t t_simdata = NULL;
185   m_process_t process = MSG_process_self();
186   
187   CHECK_HOST();
188
189 #ifdef HAVE_TRACING
190   int call_end = TRACE_msg_task_put_start (task); //must be after CHECK_HOST()
191 #endif
192
193
194   /* Prepare the task to send */
195   t_simdata = task->simdata;
196   t_simdata->sender = process;
197   t_simdata->source = MSG_host_self();
198
199   xbt_assert0(t_simdata->refcount == 1,
200               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
201
202   t_simdata->refcount++;
203   msg_global->sent_msg++;
204
205   process->simdata->waiting_task = task;
206   
207   /* Try to send it by calling SIMIX network layer */
208   TRY{
209     /* Kept for semantical compatibility with older implementation */
210     if(mailbox->cond)
211       SIMIX_cond_signal(mailbox->cond);
212
213     SIMIX_network_send(mailbox->rdv, t_simdata->message_size, t_simdata->rate,
214                        timeout, task, sizeof(void*), &t_simdata->comm, task);
215   }
216
217   CATCH(e){
218     switch(e.category){
219       case host_error:
220         ret = MSG_HOST_FAILURE;
221         break;
222       case network_error:
223         ret = MSG_TRANSFER_FAILURE;
224         break;
225       case timeout_error:
226         ret = MSG_TIMEOUT;
227         break;
228       default:
229         xbt_die(bprintf("Unhandled SIMIX network exception: %s",e.msg));
230     }
231     xbt_ex_free(e);
232
233     /* Decrement the refcount only on failure */
234     t_simdata->refcount--;
235   }
236
237   process->simdata->waiting_task = NULL;
238 #ifdef HAVE_TRACING
239   if (call_end) TRACE_msg_task_put_end ();
240 #endif
241   MSG_RETURN(ret);        
242 }