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. */
10 #include "msg/private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg,
12 "Logging specific to MSG (mailbox)");
14 msg_mailbox_t MSG_mailbox_new(const char *alias)
16 return SIMIX_req_rdv_create(alias);
19 void MSG_mailbox_free(void *mailbox)
21 SIMIX_req_rdv_destroy((msg_mailbox_t)mailbox);
24 int MSG_mailbox_is_empty(msg_mailbox_t mailbox)
26 return (NULL == SIMIX_req_rdv_get_head(mailbox));
29 m_task_t MSG_mailbox_get_head(msg_mailbox_t mailbox)
31 smx_action_t comm = SIMIX_req_rdv_get_head(mailbox);
36 return (m_task_t) SIMIX_req_comm_get_src_data(comm);
40 MSG_mailbox_get_count_host_waiting_tasks(msg_mailbox_t mailbox,
43 return SIMIX_req_rdv_comm_count_by_host(mailbox,
44 host->simdata->smx_host);
47 msg_mailbox_t MSG_mailbox_get_by_alias(const char *alias)
50 msg_mailbox_t mailbox = SIMIX_req_rdv_get_by_name(alias);
53 mailbox = MSG_mailbox_new(alias);
58 msg_mailbox_t MSG_mailbox_get_by_channel(m_host_t host,
61 xbt_assert0((host != NULL), "Invalid host");
62 xbt_assert1((channel >= 0)
63 && (channel < msg_global->max_channel), "Invalid channel %d",
66 return host->simdata->mailboxes[(size_t) channel];
70 MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, m_task_t * task,
71 m_host_t host, double timeout)
74 MSG_error_t ret = MSG_OK;
75 smx_action_t comm = NULL;
77 double start_time = 0;
79 /* We no longer support getting a task from a specific host */
85 TRACE_msg_task_get_start();
86 start_time = MSG_get_clock();
90 xbt_assert0(task, "Null pointer for the task storage");
94 ("MSG_task_get() was asked to write in a non empty task struct.");
96 /* Try to receive it by calling SIMIX network layer */
98 comm = SIMIX_req_comm_irecv(mailbox, task, NULL, NULL, NULL);
99 SIMIX_req_comm_wait(comm, timeout);
100 (*task)->simdata->comm = comm;
101 SIMIX_req_comm_destroy(comm);
102 DEBUG2("Got task %s from %p",(*task)->name,mailbox);
103 (*task)->simdata->isused=0;
106 switch (e.category) {
108 ret = MSG_HOST_FAILURE;
111 ret = MSG_TRANSFER_FAILURE;
117 xbt_backtrace_display(&e);
118 xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
123 if (ret != MSG_HOST_FAILURE &&
124 ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
126 TRACE_msg_task_get_end(start_time, *task);
133 MSG_mailbox_put_with_timeout(msg_mailbox_t mailbox, m_task_t task,
137 MSG_error_t ret = MSG_OK;
138 simdata_task_t t_simdata = NULL;
139 m_process_t process = MSG_process_self();
147 call_end = TRACE_msg_task_put_start(task); //must be after CHECK_HOST()
151 /* Prepare the task to send */
152 t_simdata = task->simdata;
153 t_simdata->sender = process;
154 t_simdata->source = MSG_host_self();
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!");
160 msg_global->sent_msg++;
162 process->simdata->waiting_task = task;
164 /* Try to send it by calling SIMIX network layer */
166 comm = SIMIX_req_comm_isend(mailbox, t_simdata->message_size,
167 t_simdata->rate, task, sizeof(void *), NULL, NULL);
168 t_simdata->comm = comm;
170 SIMIX_req_set_category(comm, task->category);
172 SIMIX_req_comm_wait(comm, timeout);
173 SIMIX_req_comm_destroy(comm);
177 switch (e.category) {
179 ret = MSG_HOST_FAILURE;
182 ret = MSG_TRANSFER_FAILURE;
188 xbt_die(bprintf("Unhandled SIMIX network exception: %s", e.msg));
192 /* If the send failed, it is not used anymore */
196 process->simdata->waiting_task = NULL;
199 TRACE_msg_task_put_end();