Logo AND Algorithmique Numérique Distribuée

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