5 #include "communicator.h"
6 #include "simgrid_features.h"
8 // XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(comm, simu,
10 "Messages from asynchronous pipes");
14 bool comm_test_n_destroy(msg_comm_t& comm)
16 if (MSG_comm_test(comm)) {
17 MSG_comm_destroy(comm);
25 communicator::communicator()
27 const char* hostname = MSG_host_get_name(MSG_host_self());
31 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
36 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
39 communicator::~communicator()
41 // fixme: don't know how to free pending communications
42 // (data_comm, ctrl_comm and sent_comm)
45 if (!sent_comm.empty())
46 WARN1("Lost %ld send communications!", (long )sent_comm.size());
49 void communicator::send(const char* dest, message* msg)
51 double msg_size = sizeof *msg;
52 if (msg->get_type() == message::LOAD)
53 msg_size += msg->get_amount();
54 m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);
55 sent_comm.push_back(MSG_task_isend(task, dest));
59 bool communicator::recv(message*& msg, m_host_t& from)
63 if (comm_test_n_destroy(ctrl_comm)) {
64 msg = (message* )MSG_task_get_data(ctrl_task);
65 from = MSG_task_get_source(ctrl_task);
66 MSG_task_destroy(ctrl_task);
68 ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
70 } else if (comm_test_n_destroy(data_comm)) {
71 msg = (message* )MSG_task_get_data(data_task);
72 from = MSG_task_get_source(data_task);
73 MSG_task_destroy(data_task);
75 data_comm = MSG_task_irecv(&data_task, get_data_mbox());
81 int communicator::send_backlog()
84 return sent_comm.size();
87 void communicator::flush_sent()
89 std::remove_if(sent_comm.begin(), sent_comm.end(), comm_test_n_destroy);