1 #include "communicator.h"
4 #include <tr1/functional>
8 #include "simgrid_features.h"
11 // XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(comm, simu,
13 "Messages from asynchronous pipes");
15 std::string message::to_string()
17 static const char* str[] = { "INFO", "CREDIT", "LOAD",
18 "CTRL_CLOSE", "DATA_CLOSE" };
19 std::ostringstream oss;
20 oss << str[type] << " (" << amount << ")";
24 communicator::communicator()
25 : host((hostdata* )MSG_host_get_data(MSG_host_self()))
28 , ctrl_close_is_last(false)
31 , data_close_is_last(false)
35 communicator::~communicator()
38 WARN0("ctrl_comm is pending!");
40 WARN0("data_comm is pending!");
41 if (!sent_comm.empty())
42 WARN2("lost %ld send communication%s!",
43 (long )sent_comm.size(), ESSE(sent_comm.size()));
46 void communicator::listen()
48 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
49 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
52 void communicator::send(const char* dest, message* msg)
54 double msg_size = sizeof *msg;
55 if (msg->get_type() == message::LOAD)
56 msg_size += msg->get_amount();
57 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
58 msg_comm_t comm = MSG_task_isend(task, dest);
59 sent_comm.push_back(comm);
62 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
68 if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
69 msg = (message* )MSG_task_get_data(ctrl_task);
70 from = MSG_task_get_source(ctrl_task);
71 MSG_task_destroy(ctrl_task);
74 (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
75 ? MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
78 } else if (data_comm && comm_test_n_destroy(data_comm)) {
79 msg = (message* )MSG_task_get_data(data_task);
80 from = MSG_task_get_source(data_task);
81 MSG_task_destroy(data_task);
84 (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
85 ? MSG_task_irecv(&data_task, get_data_mbox())
89 restart = wait && !msg && (ctrl_comm || data_comm);
91 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
93 xbt_dynar_push(comms, &ctrl_comm);
95 xbt_dynar_push(comms, &data_comm);
96 MSG_comm_waitany(comms);
97 xbt_dynar_free(&comms);
104 void communicator::flush(bool wait)
106 sent_comm.remove_if(comm_test_n_destroy);
107 if (wait && !sent_comm.empty()) {
108 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
109 while (!sent_comm.empty()) {
110 std::for_each(sent_comm.begin(), sent_comm.end(),
111 std::tr1::bind(comm_push_in_dynar,
112 comms, std::tr1::placeholders::_1));
113 MSG_comm_waitany(comms);
114 xbt_dynar_reset(comms);
115 sent_comm.remove_if(comm_test_n_destroy);
117 xbt_dynar_free(&comms);
121 void communicator::next_close_on_ctrl_is_last()
123 ctrl_close_is_last = true;
126 void communicator::next_close_on_data_is_last()
128 data_close_is_last = true;
131 void communicator::comm_push_in_dynar(xbt_dynar_t dynar, msg_comm_t comm)
133 xbt_dynar_push(dynar, &comm);
136 bool communicator::comm_test_n_destroy(msg_comm_t comm)
138 if (MSG_comm_test(comm)) {
139 MSG_comm_destroy(comm);