Logo AND Algorithmique Numérique Distribuée

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