Logo AND Algorithmique Numérique Distribuée

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