2 #include <tr1/functional>
6 #include "communicator.h"
7 #include "simgrid_features.h"
10 // XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(comm, simu,
12 "Messages from asynchronous pipes");
14 std::string message::to_string()
16 static const char* str[] = { "INFO", "CREDIT", "LOAD",
17 "CTRL_CLOSE", "DATA_CLOSE" };
18 std::ostringstream oss;
19 oss << str[type] << " (" << amount << ")";
23 communicator::communicator()
25 const char* hostname = MSG_host_get_name(MSG_host_self());
31 ctrl_close_is_last = false;
37 data_close_is_last = false;
40 communicator::~communicator()
43 WARN0("ctrl_comm is pending!");
45 WARN0("data_comm is pending!");
46 if (!sent_comm.empty())
47 WARN2("lost %ld send communication%s!",
48 (long )sent_comm.size(), ESSE(sent_comm.size()));
51 void communicator::listen()
53 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
54 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
57 void communicator::send(const char* dest, message* msg)
59 double msg_size = sizeof *msg;
60 if (msg->get_type() == message::LOAD)
61 msg_size += msg->get_amount();
62 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
63 msg_comm_t comm = MSG_task_isend(task, dest);
64 sent_comm.push_back(comm);
67 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
73 if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
74 msg = (message* )MSG_task_get_data(ctrl_task);
75 from = MSG_task_get_source(ctrl_task);
76 MSG_task_destroy(ctrl_task);
79 (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
80 ? MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
83 } else if (data_comm && comm_test_n_destroy(data_comm)) {
84 msg = (message* )MSG_task_get_data(data_task);
85 from = MSG_task_get_source(data_task);
86 MSG_task_destroy(data_task);
89 (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
90 ? MSG_task_irecv(&data_task, get_data_mbox())
94 restart = wait && !msg && (ctrl_comm || data_comm);
96 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
98 xbt_dynar_push(comms, &ctrl_comm);
100 xbt_dynar_push(comms, &data_comm);
101 MSG_comm_waitany(comms);
102 xbt_dynar_free(&comms);
109 void communicator::flush(bool wait)
111 sent_comm.remove_if(comm_test_n_destroy);
112 if (wait && !sent_comm.empty()) {
113 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
114 while (!sent_comm.empty()) {
115 std::for_each(sent_comm.begin(), sent_comm.end(),
116 std::tr1::bind(comm_push_in_dynar,
117 comms, std::tr1::placeholders::_1));
118 MSG_comm_waitany(comms);
119 xbt_dynar_reset(comms);
120 sent_comm.remove_if(comm_test_n_destroy);
122 xbt_dynar_free(&comms);
126 void communicator::next_close_on_ctrl_is_last()
128 ctrl_close_is_last = true;
131 void communicator::next_close_on_data_is_last()
133 data_close_is_last = true;
136 void communicator::comm_push_in_dynar(xbt_dynar_t dynar, msg_comm_t comm)
138 xbt_dynar_push(dynar, &comm);
141 bool communicator::comm_test_n_destroy(msg_comm_t comm)
143 if (MSG_comm_test(comm)) {
144 MSG_comm_destroy(comm);