Logo AND Algorithmique Numérique Distribuée

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