Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
194c133aea2c38b0ff901ac678a3aa9d65966a69
[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 #ifdef HAVE_TRACING
77   double start_time = 0;
78 #endif
79   /* We no longer support getting a task from a specific host */
80   if (host)
81     THROW_UNIMPLEMENTED;
82
83   CHECK_HOST();
84 #ifdef HAVE_TRACING
85   TRACE_msg_task_get_start();
86   start_time = MSG_get_clock();
87 #endif
88
89   /* Sanity check */
90   xbt_assert0(task, "Null pointer for the task storage");
91
92   if (*task)
93     CRITICAL0
94         ("MSG_task_get() was asked to write in a non empty task struct.");
95
96   /* Try to receive it by calling SIMIX network layer */
97   TRY {
98     comm = SIMIX_req_comm_irecv(mailbox, task, NULL, NULL, NULL);
99     SIMIX_req_comm_wait(comm, timeout);
100     (*task)->simdata->comm = comm;
101     DEBUG2("Got task %s from %p",(*task)->name,mailbox);
102     (*task)->simdata->isused=0;
103   }
104   CATCH(e) {
105     switch (e.category) {
106     case host_error:
107       ret = MSG_HOST_FAILURE;
108       break;
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   if (comm != NULL) {
121     //SIMIX_req_comm_destroy(comm);
122   }
123
124   if (ret != MSG_HOST_FAILURE &&
125       ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
126 #ifdef HAVE_TRACING
127     TRACE_msg_task_get_end(start_time, *task);
128 #endif
129   }
130   MSG_RETURN(ret);
131 }
132
133 MSG_error_t
134 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
135                              double timeout)
136 {
137   xbt_ex_t e;
138   MSG_error_t ret = MSG_OK;
139   simdata_task_t t_simdata = NULL;
140   m_process_t process = MSG_process_self();
141   volatile smx_action_t comm = NULL;
142 #ifdef HAVE_TRACING
143   int call_end = 0;
144 #endif
145   CHECK_HOST();
146
147 #ifdef HAVE_TRACING
148   call_end = TRACE_msg_task_put_start(task);    //must be after CHECK_HOST()
149 #endif
150
151   /* Prepare the task to send */
152   t_simdata = task->simdata;
153   t_simdata->sender = process;
154   t_simdata->source = MSG_host_self();
155
156   xbt_assert0(t_simdata->isused == 0,
157               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
158
159   t_simdata->isused=1;
160   msg_global->sent_msg++;
161
162   process->simdata->waiting_task = task;
163
164   /* Try to send it by calling SIMIX network layer */
165   TRY {
166     comm = SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
167              t_simdata->rate, task, sizeof(void *), NULL, NULL, 0);
168     t_simdata->comm = comm;
169 #ifdef HAVE_TRACING
170     SIMIX_req_set_category(comm, task->category);
171 #endif
172     SIMIX_req_comm_wait(comm, timeout);
173   }
174
175   CATCH(e) {
176     switch (e.category) {
177     case host_error:
178       ret = MSG_HOST_FAILURE;
179       break;
180     case network_error:
181       ret = MSG_TRANSFER_FAILURE;
182       break;
183     case timeout_error:
184       ret = MSG_TIMEOUT;
185       break;
186     default:
187       RETHROW;
188     }
189     xbt_ex_free(e);
190
191     /* If the send failed, it is not used anymore */
192     t_simdata->isused=0;
193   }
194
195   if (comm != NULL) {
196     //SIMIX_req_comm_destroy(comm);
197   }
198
199   process->simdata->waiting_task = NULL;
200 #ifdef HAVE_TRACING
201   if (call_end)
202     TRACE_msg_task_put_end();
203 #endif
204   MSG_RETURN(ret);
205 }