]> AND Private Git Repository - loba.git/blob - communicator.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
4b54161108fd6808bb47c8bb8b4ad6cbb163b045
[loba.git] / communicator.cpp
1 #include "communicator.h"
2
3 #include <algorithm>
4 #include <tr1/functional>
5 #include <sstream>
6 #include <msg/msg.h>
7 #include <xbt/log.h>
8 #include "simgrid_features.h"
9 #include "misc.h"
10
11 // XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(comm, simu,
13                                 "Messages from asynchronous pipes");
14
15 std::string message::to_string()
16 {
17     static const char* str[] = { "INFO", "CREDIT", "LOAD",
18                                  "CTRL_CLOSE", "DATA_CLOSE" };
19     std::ostringstream oss;
20     oss << str[type] << " (" << amount << ")";
21     return oss.str();
22 }
23
24 communicator::communicator()
25     : host((hostdata* )MSG_host_get_data(MSG_host_self()))
26     , ctrl_task(NULL)
27     , ctrl_comm(NULL)
28     , ctrl_close_is_last(false)
29     , data_task(NULL)
30     , data_comm(NULL)
31     , data_close_is_last(false)
32 {
33 }
34
35 communicator::~communicator()
36 {
37     if (ctrl_comm)
38         WARN0("ctrl_comm is pending!");
39     if (data_comm)
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()));
44 }
45
46 void communicator::listen()
47 {
48     ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
49     data_comm = MSG_task_irecv(&data_task, get_data_mbox());
50 }
51
52 void communicator::send(const char* dest, message* msg)
53 {
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);
60 }
61
62 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
63 {
64     bool restart;
65     msg = NULL;
66
67     do {
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);
72             ctrl_task = NULL;
73             ctrl_comm =
74                 (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
75                 ? MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
76                 : NULL;
77
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);
82             data_task = NULL;
83             data_comm =
84                 (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
85                 ? MSG_task_irecv(&data_task, get_data_mbox())
86                 : NULL;
87         }
88
89         restart = wait && !msg && (ctrl_comm || data_comm);
90         if (restart) {
91             xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
92             if (ctrl_comm)
93                 xbt_dynar_push(comms, &ctrl_comm);
94             if (data_comm)
95                 xbt_dynar_push(comms, &data_comm);
96             MSG_comm_waitany(comms);
97             xbt_dynar_free(&comms);
98         }
99     } while (restart);
100
101     return msg != NULL;
102 }
103
104 void communicator::flush(bool wait)
105 {
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);
116         }
117         xbt_dynar_free(&comms);
118     }
119 }
120
121 void communicator::next_close_on_ctrl_is_last()
122 {
123     ctrl_close_is_last = true;
124 }
125
126 void communicator::next_close_on_data_is_last()
127 {
128     data_close_is_last = true;
129 }
130
131 void communicator::comm_push_in_dynar(xbt_dynar_t dynar, msg_comm_t comm)
132 {
133     xbt_dynar_push(dynar, &comm);
134 }
135
136 bool communicator::comm_test_n_destroy(msg_comm_t comm)
137 {
138     if (MSG_comm_test(comm)) {
139         MSG_comm_destroy(comm);
140         return true;
141     } else
142         return false;
143 }
144
145 // Local variables:
146 // mode: c++
147 // End: