Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
97af3a11d484d31a5532898c111e5bf69a3e9c23
[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_assert0((host != NULL), "Invalid host");
62   xbt_assert1((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   volatile smx_action_t comm = NULL;
76   /* We no longer support getting a task from a specific host */
77   if (host)
78     THROW_UNIMPLEMENTED;
79
80   CHECK_HOST();
81 #ifdef HAVE_TRACING
82   TRACE_msg_task_get_start();
83   double start_time = MSG_get_clock();
84 #endif
85
86   /* Sanity check */
87   xbt_assert0(task, "Null pointer for the task storage");
88
89   if (*task)
90     CRITICAL0
91         ("MSG_task_get() was asked to write in a non empty task struct.");
92
93   /* Try to receive it by calling SIMIX network layer */
94   TRY {
95     comm = SIMIX_req_comm_irecv(mailbox, task, NULL, NULL, NULL);
96     SIMIX_req_comm_wait(comm, timeout);
97     (*task)->simdata->comm = comm;
98     DEBUG2("Got task %s from %p",(*task)->name,mailbox);
99     (*task)->simdata->isused=0;
100   }
101   CATCH(e) {
102     switch (e.category) {
103     case host_error:
104       ret = MSG_HOST_FAILURE;
105       break;
106     case network_error:
107       ret = MSG_TRANSFER_FAILURE;
108       break;
109     case timeout_error:
110       ret = MSG_TIMEOUT;
111       break;
112     default:
113       RETHROW;
114     }
115     xbt_ex_free(e);
116   }
117   if (comm != NULL) {
118     //SIMIX_req_comm_destroy(comm);
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, m_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   m_process_t process = MSG_process_self();
139   volatile smx_action_t comm = NULL;
140 #ifdef HAVE_TRACING
141   int call_end = 0;
142 #endif
143   CHECK_HOST();
144
145 #ifdef HAVE_TRACING
146   call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
147 #endif
148
149   /* Prepare the task to send */
150   t_simdata = task->simdata;
151   t_simdata->sender = process;
152   t_simdata->source = MSG_host_self();
153
154   xbt_assert0(t_simdata->isused == 0,
155               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
156
157   t_simdata->isused=1;
158   msg_global->sent_msg++;
159
160   process->simdata->waiting_task = task;
161
162   /* Try to send it by calling SIMIX network layer */
163   TRY {
164     comm = SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
165              t_simdata->rate, task, sizeof(void *), NULL, NULL, 0);
166     t_simdata->comm = comm;
167 #ifdef HAVE_TRACING
168     SIMIX_req_set_category(comm, task->category);
169 #endif
170     SIMIX_req_comm_wait(comm, timeout);
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   if (comm != NULL) {
194     //SIMIX_req_comm_destroy(comm);
195   }
196
197   process->simdata->waiting_task = NULL;
198 #ifdef HAVE_TRACING
199   if (call_end)
200     TRACE_msg_task_put_end();
201 #endif
202   MSG_RETURN(ret);
203 }