Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
22917b66d155f741a6032e7b36b1613a627f41e9
[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_mailbox_t MSG_mailbox_get_by_channel(m_host_t host,
59                                          m_channel_t channel)
60 {
61   XBT_WARN("DEPRECATED! Now use MSG_mailbox_get_by_alias");
62   xbt_assert((host != NULL), "Invalid host");
63   xbt_assert((channel >= 0)
64               && (channel < msg_global->max_channel), "Invalid channel %d",
65               channel);
66
67   return host->simdata->mailboxes[(size_t) channel];
68 }
69
70 MSG_error_t
71 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t * task,
72                          m_host_t host, double timeout)
73 {
74   xbt_ex_t e;
75   MSG_error_t ret = MSG_OK;
76   /* We no longer support getting a task from a specific host */
77   if (host)
78     THROW_UNIMPLEMENTED;
79
80   CHECK_HOST();
81 #ifdef HAVE_TRACING
82   TRACE_msg_task_get_start();
83   volatile double start_time = MSG_get_clock();
84 #endif
85
86   /* Sanity check */
87   xbt_assert(task, "Null pointer for the task storage");
88
89   if (*task)
90     XBT_WARN
91         ("Asked to write the received task in a non empty struct -- proceeding.");
92
93   /* Try to receive it by calling SIMIX network layer */
94   TRY {
95     simcall_comm_recv(mailbox, task, NULL, NULL, NULL, timeout);
96     XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox);
97     (*task)->simdata->isused=0;
98   }
99   CATCH(e) {
100     switch (e.category) {
101     case host_error:
102       ret = MSG_HOST_FAILURE;
103       break;
104     case network_error:
105       ret = MSG_TRANSFER_FAILURE;
106       break;
107     case timeout_error:
108       ret = MSG_TIMEOUT;
109       break;
110     default:
111       RETHROW;
112     }
113     xbt_ex_free(e);
114   }
115
116 #ifdef HAVE_TRACING
117   if (ret != MSG_HOST_FAILURE &&
118       ret != MSG_TRANSFER_FAILURE &&
119       ret != MSG_TIMEOUT) {
120     TRACE_msg_task_get_end(start_time, *task);
121   }
122 #endif
123   MSG_RETURN(ret);
124 }
125
126 MSG_error_t
127 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
128                              double timeout)
129 {
130   xbt_ex_t e;
131   MSG_error_t ret = MSG_OK;
132   simdata_task_t t_simdata = NULL;
133   m_process_t process = MSG_process_self();
134   simdata_process_t p_simdata = SIMIX_process_self_get_data(process);
135   CHECK_HOST();
136
137 #ifdef HAVE_TRACING
138   int call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
139 #endif
140
141   /* Prepare the task to send */
142   t_simdata = task->simdata;
143   t_simdata->sender = process;
144   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
145
146   xbt_assert(t_simdata->isused == 0,
147               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
148
149   t_simdata->isused=1;
150   t_simdata->comm = NULL;
151   msg_global->sent_msg++;
152
153
154   p_simdata->waiting_task = task;
155
156   /* Try to send it by calling SIMIX network layer */
157   TRY {
158       smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
159                                   t_simdata->rate, task, sizeof(void *),
160                                   NULL, NULL, task, 0);
161 #ifdef HAVE_TRACING
162     if (TRACE_is_enabled()) {
163       simcall_set_category(comm, task->category);
164     }
165 #endif
166      t_simdata->comm = comm;
167      simcall_comm_wait(comm, timeout);
168   }
169
170   CATCH(e) {
171     switch (e.category) {
172     case host_error:
173       ret = MSG_HOST_FAILURE;
174       break;
175     case network_error:
176       ret = MSG_TRANSFER_FAILURE;
177       break;
178     case timeout_error:
179       ret = MSG_TIMEOUT;
180       break;
181     default:
182       RETHROW;
183     }
184     xbt_ex_free(e);
185
186     /* If the send failed, it is not used anymore */
187     t_simdata->isused = 0;
188   }
189
190
191   p_simdata->waiting_task = NULL;
192 #ifdef HAVE_TRACING
193   if (call_end)
194     TRACE_msg_task_put_end();
195 #endif
196   MSG_RETURN(ret);
197 }