Logo AND Algorithmique Numérique Distribuée

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