3 /* Copyright (c) 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "msg_mailbox.h"
10 #include "msg_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg,
13 "Logging specific to MSG (mailbox)");
15 msg_mailbox_t MSG_mailbox_new(const char *alias)
17 return simcall_rdv_create(alias);
20 void MSG_mailbox_free(void *mailbox)
22 simcall_rdv_destroy((msg_mailbox_t)mailbox);
25 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
27 return (NULL == simcall_rdv_get_head(mailbox));
30 msg_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
32 smx_action_t comm = simcall_rdv_get_head(mailbox);
37 return (msg_task_t) simcall_comm_get_src_data(comm);
41 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
44 return simcall_rdv_comm_count_by_host(mailbox,
48 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
51 msg_mailbox_t mailbox = simcall_rdv_get_by_name(alias);
54 mailbox = MSG_mailbox_new(alias);
59 void MSG_mailbox_set_async(const char *alias){
60 msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
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());
68 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, msg_task_t * task,
69 msg_host_t host, double timeout)
72 msg_error_t ret = MSG_OK;
73 /* We no longer support getting a task from a specific host */
78 TRACE_msg_task_get_start();
79 volatile double start_time = MSG_get_clock();
83 xbt_assert(task, "Null pointer for the task storage");
87 ("Asked to write the received task in a non empty struct -- proceeding.");
89 /* Try to receive it by calling SIMIX network layer */
91 simcall_comm_recv(mailbox, task, NULL, NULL, NULL, timeout);
92 XBT_DEBUG("Got task %s from %p",(*task)->name,mailbox);
93 (*task)->simdata->isused=0;
98 ret = MSG_TRANSFER_FAILURE;
110 if (ret != MSG_HOST_FAILURE &&
111 ret != MSG_TRANSFER_FAILURE &&
112 ret != MSG_TIMEOUT) {
113 TRACE_msg_task_get_end(start_time, *task);
120 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, msg_task_t task,
124 msg_error_t ret = MSG_OK;
125 simdata_task_t t_simdata = NULL;
126 msg_process_t process = MSG_process_self();
127 simdata_process_t p_simdata = SIMIX_process_self_get_data(process);
130 int call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
133 /* Prepare the task to send */
134 t_simdata = task->simdata;
135 t_simdata->sender = process;
136 t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
138 xbt_assert(t_simdata->isused == 0,
139 "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
142 t_simdata->comm = NULL;
143 msg_global->sent_msg++;
146 p_simdata->waiting_task = task;
148 /* Try to send it by calling SIMIX network layer */
150 smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
151 t_simdata->rate, task, sizeof(void *),
152 NULL, NULL, task, 0);
154 if (TRACE_is_enabled()) {
155 simcall_set_category(comm, task->category);
158 t_simdata->comm = comm;
159 simcall_comm_wait(comm, timeout);
163 switch (e.category) {
165 ret = MSG_TRANSFER_FAILURE;
175 /* If the send failed, it is not used anymore */
176 t_simdata->isused = 0;
180 p_simdata->waiting_task = NULL;
183 TRACE_msg_task_put_end();
188 #ifdef MSG_USE_DEPRECATED
189 msg_mailbox_t MSG_mailbox_get_by_channel(msg_host_t host,
192 XBT_WARN("DEPRECATED! Now use MSG_mailbox_get_by_alias");
193 xbt_assert((host != NULL), "Invalid host");
194 xbt_assert((channel >= 0)
195 && (channel < msg_global->max_channel), "Invalid channel %d",
198 return host->mailboxes[(size_t) channel];