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 communicator::communicator()
16 const char* hostname = MSG_host_get_name(MSG_host_self());
21 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
22 ctrl_close_is_last = false;
27 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
28 data_close_is_last = false;
31 communicator::~communicator()
34 WARN0("ctrl_comm is pending!");
36 WARN0("data_comm is pending!");
37 if (!sent_comm.empty())
38 WARN2("lost %ld send communication%s!",
39 (long )sent_comm.size(), ESSE(sent_comm.size()));
42 void communicator::send(const char* dest, message* msg)
44 double msg_size = sizeof *msg;
45 if (msg->get_type() == message::LOAD)
46 msg_size += msg->get_amount();
47 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
48 sent_comm.push_back(MSG_task_isend(task, dest));
52 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
58 if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
59 msg = (message* )MSG_task_get_data(ctrl_task);
60 from = MSG_task_get_source(ctrl_task);
61 MSG_task_destroy(ctrl_task);
64 (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
65 ? ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
68 } else if (data_comm && comm_test_n_destroy(data_comm)) {
69 msg = (message* )MSG_task_get_data(data_task);
70 from = MSG_task_get_source(data_task);
71 MSG_task_destroy(data_task);
74 (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
75 ? data_comm = MSG_task_irecv(&data_task, get_data_mbox())
79 restart = wait && !msg && (ctrl_comm || data_comm);
81 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
83 xbt_dynar_push(comms, &ctrl_comm);
85 xbt_dynar_push(comms, &data_comm);
86 MSG_comm_waitany(comms);
87 xbt_dynar_free(&comms);
94 void communicator::wait_for_sent()
96 xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
97 while (!sent_comm.empty()) {
98 std::for_each(sent_comm.begin(), sent_comm.end(),
99 std::tr1::bind(xbt_dynar_push,
100 comms, std::tr1::placeholders::_1));
101 MSG_comm_waitany(comms);
102 xbt_dynar_reset(comms);
105 xbt_dynar_free(&comms);
108 void communicator::next_close_on_ctrl_is_last()
110 ctrl_close_is_last = true;
113 void communicator::next_close_on_data_is_last()
115 data_close_is_last = true;
118 int communicator::send_backlog()
121 return sent_comm.size();
124 bool communicator::comm_test_n_destroy(msg_comm_t& comm)
126 if (MSG_comm_test(comm)) {
127 MSG_comm_destroy(comm);
133 void communicator::flush_sent()
135 std::remove_if(sent_comm.begin(), sent_comm.end(), comm_test_n_destroy);