Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
11d04eca03a3501b472ef9233389130bde0a3555
[simgrid.git] / src / msg / msg_mailbox.cpp
1 /* Mailboxes in MSG */
2
3 /* Copyright (c) 2008-2015. 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 "simgrid/msg.h"
10 #include "msg_private.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg, "Logging specific to MSG (mailbox)");
13
14 msg_mailbox_t MSG_mailbox_new(const char *alias)
15 {
16   return simcall_mbox_create(alias);
17 }
18
19 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
20 {
21   return (NULL == simcall_mbox_get_head(mailbox));
22 }
23
24 msg_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
25 {
26   smx_synchro_t comm = simcall_mbox_get_head(mailbox);
27
28   if (!comm)
29     return NULL;
30
31   return (msg_task_t) simcall_comm_get_src_data(comm);
32 }
33
34 int MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox, msg_host_t host)
35 {
36   return simcall_mbox_comm_count_by_host(mailbox, host);
37 }
38
39 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
40 {
41   msg_mailbox_t mailbox = simcall_mbox_get_by_name(alias);
42
43   if (!mailbox)
44     mailbox = MSG_mailbox_new(alias);
45
46   return mailbox;
47 }
48
49 /** \ingroup msg_mailbox_management
50  * \brief Set the mailbox to receive in asynchronous mode
51  *
52  * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
53  * The receive call will still be necessary to use the received data.
54  * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
55  *
56  * \param alias The name of the mailbox
57  */
58 void MSG_mailbox_set_async(const char *alias){
59   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
60
61   simcall_mbox_set_receiver(mailbox, SIMIX_process_self());
62   XBT_VERB("%s mailbox set to receive eagerly for process %p\n",alias, SIMIX_process_self());
63 }
64
65 /** \ingroup msg_mailbox_management
66  * \brief Get a task from a mailbox on a given host
67  *
68  * \param mailbox The mailbox where the task was sent
69  * \param task a memory location for storing a #msg_task_t.
70  * \param host a #msg_host_t host from where the task was sent
71  * \param timeout a timeout
72
73  * \return Returns
74  * #MSG_OK if the task was successfully received,
75  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
76  */
77 msg_error_t MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, msg_task_t *task, msg_host_t host, double timeout)
78 {
79   return MSG_mailbox_get_task_ext_bounded(mailbox, task, host, timeout, -1.0);
80 }
81
82 /** \ingroup msg_mailbox_management
83  * \brief Get a task from a mailbox on a given host at a given rate
84  *
85  * \param mailbox The mailbox where the task was sent
86  * \param task a memory location for storing a #msg_task_t.
87  * \param host a #msg_host_t host from where the task was sent
88  * \param timeout a timeout
89  * \param rate a rate
90
91  * \return Returns
92  * #MSG_OK if the task was successfully received,
93  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
94  */
95 msg_error_t MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * task, msg_host_t host, double timeout,
96                                              double rate)
97 {
98   xbt_ex_t e;
99   msg_error_t ret = MSG_OK;
100   /* We no longer support getting a task from a specific host */
101   if (host)
102     THROW_UNIMPLEMENTED;
103
104   TRACE_msg_task_get_start();
105   double start_time = MSG_get_clock();
106
107   /* Sanity check */
108   xbt_assert(task, "Null pointer for the task storage");
109
110   if (*task)
111     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
112
113   /* Try to receive it by calling SIMIX network layer */
114   TRY {
115     simcall_comm_recv(MSG_process_self(), mailbox, task, NULL, NULL, NULL, NULL, timeout, rate);
116     XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox);
117     if (msg_global->debug_multiple_use && (*task)->simdata->isused!=0)
118       xbt_ex_free(*(xbt_ex_t*)(*task)->simdata->isused);
119     (*task)->simdata->isused = 0;
120   }
121   CATCH(e) {
122     switch (e.category) {
123     case cancel_error:
124       ret = MSG_HOST_FAILURE;
125       break;
126     case network_error:
127       ret = MSG_TRANSFER_FAILURE;
128       break;
129     case timeout_error:
130       ret = MSG_TIMEOUT;
131       break;
132     case host_error:
133       ret = MSG_HOST_FAILURE;
134       break;
135     default:
136       RETHROW;
137     }
138     xbt_ex_free(e);
139   }
140
141   if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
142     TRACE_msg_task_get_end(start_time, *task);
143   }
144   MSG_RETURN(ret);
145 }
146
147 msg_error_t MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, msg_task_t task, double timeout)
148 {
149   msg_error_t ret = MSG_OK;
150   simdata_task_t t_simdata = NULL;
151   msg_process_t process = MSG_process_self();
152   simdata_process_t p_simdata = (simdata_process_t) SIMIX_process_self_get_data();
153
154   int call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
155
156   /* Prepare the task to send */
157   t_simdata = task->simdata;
158   t_simdata->sender = process;
159   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data())->m_host;
160
161   if (t_simdata->isused != 0) {
162     if (msg_global->debug_multiple_use){
163       XBT_ERROR("This task is already used in there:");
164       xbt_backtrace_display((xbt_ex_t*) t_simdata->isused);
165       XBT_ERROR("And you try to reuse it from here:");
166       xbt_backtrace_display_current();
167     } else {
168       xbt_assert(t_simdata->isused == 0,
169                  "This task is still being used somewhere else. You cannot send it now. Go fix your code!"
170                  " (use --cfg=msg/debug_multiple_use:on to get the backtrace of the other process)");
171     }
172   }
173
174   if (msg_global->debug_multiple_use)
175     MSG_BT(t_simdata->isused, "Using Backtrace");
176   else
177     t_simdata->isused = (void*)1;
178   t_simdata->comm = NULL;
179   msg_global->sent_msg++;
180
181   p_simdata->waiting_task = task;
182
183   xbt_ex_t e;
184   /* Try to send it by calling SIMIX network layer */
185   TRY {
186     smx_synchro_t comm = NULL; /* MC needs the comm to be set to NULL during the simix call  */
187     comm = simcall_comm_isend(SIMIX_process_self(), mailbox,t_simdata->bytes_amount,
188                               t_simdata->rate, task, sizeof(void *), NULL, NULL, NULL, task, 0);
189     if (TRACE_is_enabled())
190       simcall_set_category(comm, task->category);
191      t_simdata->comm = comm;
192      simcall_comm_wait(comm, timeout);
193   }
194
195   CATCH(e) {
196     switch (e.category) {
197     case cancel_error:
198       ret = MSG_HOST_FAILURE;
199       break;
200     case network_error:
201       ret = MSG_TRANSFER_FAILURE;
202       break;
203     case timeout_error:
204       ret = MSG_TIMEOUT;
205       break;
206     default:
207       RETHROW;
208     }
209     xbt_ex_free(e);
210
211     /* If the send failed, it is not used anymore */
212     if (msg_global->debug_multiple_use && t_simdata->isused!=0)
213       xbt_ex_free(*(xbt_ex_t*)t_simdata->isused);
214     t_simdata->isused = 0;
215   }
216
217   p_simdata->waiting_task = NULL;
218   if (call_end)
219     TRACE_msg_task_put_end();
220   MSG_RETURN(ret);
221 }