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

Private GIT Repository
Make boolean option togglable.
[loba.git] / communicator.cpp
index f3e6bf9fae1eddd27ec2f25e11977dfd727d15c4..90cf990ebb07c31f70d30f89e6723a94bd78bdca 100644 (file)
@@ -1,4 +1,5 @@
 #include <algorithm>
+#include <cstring>
 #include <tr1/functional>
 #include <sstream>
 #include <msg/msg.h>
@@ -20,35 +21,55 @@ std::string message::to_string()
     return oss.str();
 }
 
-const int communicator::send_count_before_flush = 4;
-
 communicator::communicator()
     : host((hostdata* )MSG_host_get_data(MSG_host_self()))
-    , send_counter(0)
+    , mutex(xbt_mutex_init())
+    , cond(xbt_cond_init())
     , ctrl_task(NULL)
     , ctrl_comm(NULL)
-    , ctrl_close_is_last(false)
     , data_task(NULL)
     , data_comm(NULL)
-    , data_close_is_last(false)
 {
+    xbt_mutex_acquire(mutex);
+    receiver_process =
+        MSG_process_create("receiver", communicator::receiver_wrapper,
+                           this, MSG_host_self());
+    xbt_cond_wait(cond, mutex); // wait for the receiver to be ready
+    xbt_mutex_release(mutex);
 }
 
 communicator::~communicator()
 {
+    m_task_t task;
+
+    DEBUG0("send finalize to receiver/ctrl");
+    task = MSG_task_create("finalize", 0.0, 0, NULL);
+    MSG_task_send(task, get_ctrl_mbox());
+
+    DEBUG0("send finalize to receiver/data");
+    task = MSG_task_create("finalize", 0.0, 0, NULL);
+    MSG_task_send(task, get_data_mbox());
+
+    xbt_mutex_acquire(mutex);
+    while (receiver_process) {
+        DEBUG0("waiting for receiver to terminate");
+        xbt_cond_wait(cond, mutex);
+    }
+    xbt_mutex_release(mutex);
+
     if (ctrl_comm)
         WARN0("ctrl_comm is pending!");
     if (data_comm)
         WARN0("data_comm is pending!");
+    if (!received.empty())
+        WARN2("lost %lu received message%s!",
+              (unsigned long )received.size(), ESSE(received.size()));
     if (!sent_comm.empty())
-        WARN2("lost %ld send communication%s!",
-              (long )sent_comm.size(), ESSE(sent_comm.size()));
-}
+        WARN2("lost %lu sent message%s!",
+              (unsigned long )sent_comm.size(), ESSE(sent_comm.size()));
 
-void communicator::listen()
-{
-    ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
-    data_comm = MSG_task_irecv(&data_task, get_data_mbox());
+    xbt_cond_destroy(cond);
+    xbt_mutex_destroy(mutex);
 }
 
 void communicator::send(const char* dest, message* msg)
@@ -60,57 +81,45 @@ void communicator::send(const char* dest, message* msg)
     m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);    
     msg_comm_t comm = MSG_task_isend(task, dest);
     sent_comm.push_back(comm);
-
-    if (++send_counter >= send_count_before_flush) {
-        flush(false);
-        send_counter = 0;
-    }
 }
 
-bool communicator::recv(message*& msg, m_host_t& from, bool wait)
+bool communicator::recv(message*& msg, m_host_t& from, double timeout)
 {
-    bool restart;
-    msg = NULL;
-
-    do {
-        if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
-            msg = (message* )MSG_task_get_data(ctrl_task);
-            from = MSG_task_get_source(ctrl_task);
-            MSG_task_destroy(ctrl_task);
-            ctrl_task = NULL;
-            ctrl_comm =
-                (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
-                ? MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
-                : NULL;
-
-        } else if (data_comm && comm_test_n_destroy(data_comm)) {
-            msg = (message* )MSG_task_get_data(data_task);
-            from = MSG_task_get_source(data_task);
-            MSG_task_destroy(data_task);
-            data_task = NULL;
-            data_comm =
-                (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
-                ? MSG_task_irecv(&data_task, get_data_mbox())
-                : NULL;
+    if (timeout != 0) {
+        volatile double deadline =
+            timeout > 0 ? MSG_get_clock() + timeout : 0.0;
+        xbt_mutex_acquire(mutex);
+        while (received.empty() && (!deadline || deadline > MSG_get_clock())) {
+            xbt_ex_t e;
+            DEBUG0("waiting for a message to come");
+            TRY {
+                if (deadline)
+                    xbt_cond_timedwait(cond, mutex, deadline - MSG_get_clock());
+                else
+                    xbt_cond_wait(cond, mutex);
+            }
+            CATCH (e) {
+                if (e.category != timeout_error)
+                    RETHROW;
+                xbt_ex_free(e);
+            }
         }
+        xbt_mutex_release(mutex);
+    }
 
-        restart = wait && !msg && (ctrl_comm || data_comm);
-        if (restart) {
-            xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
-            if (ctrl_comm)
-                xbt_dynar_push(comms, &ctrl_comm);
-            if (data_comm)
-                xbt_dynar_push(comms, &data_comm);
-            MSG_comm_waitany(comms);
-            xbt_dynar_free(&comms);
-        }
-    } while (restart);
+    if (received.empty())
+        return false;
+
+    m_task_t task = received.front();
+    received.pop();
+    msg = (message* )MSG_task_get_data(task);
+    from = MSG_task_get_source(task);
+    MSG_task_destroy(task);
 
-    if (msg)
-        DEBUG2("received %s from %s",
-               msg->to_string().c_str(), MSG_host_get_name(from));
+    DEBUG2("received %s from %s",
+           msg->to_string().c_str(), MSG_host_get_name(from));
 
-    return msg != NULL;
+    return true;
 }
 
 void communicator::flush(bool wait)
@@ -133,16 +142,6 @@ void communicator::flush(bool wait)
     }
 }
 
-void communicator::next_close_on_ctrl_is_last()
-{
-    ctrl_close_is_last = true;
-}
-
-void communicator::next_close_on_data_is_last()
-{
-    data_close_is_last = true;
-}
-
 bool communicator::comm_test_n_destroy(msg_comm_t comm)
 {
     if (MSG_comm_test(comm)) {
@@ -152,6 +151,80 @@ bool communicator::comm_test_n_destroy(msg_comm_t comm)
         return false;
 }
 
+int communicator::receiver_wrapper(int, char* [])
+{
+    communicator* comm;
+    comm = (communicator* )MSG_process_get_data(MSG_process_self());
+    int result = comm->receiver();
+
+    DEBUG0("terminate");
+    xbt_mutex_acquire(comm->mutex);
+    comm->receiver_process = NULL;
+    xbt_cond_signal(comm->cond);
+    xbt_mutex_release(comm->mutex);
+
+    return result;
+}
+
+int communicator::receiver()
+{
+    ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
+    data_comm = MSG_task_irecv(&data_task, get_data_mbox());
+    DEBUG0("receiver ready");
+    xbt_mutex_acquire(mutex);
+    xbt_cond_signal(cond);      // signal master that we are ready
+    xbt_mutex_release(mutex);
+
+    xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
+    while (ctrl_comm || data_comm) {
+
+        if (ctrl_comm)
+            xbt_dynar_push(comms, &ctrl_comm);
+        if (data_comm)
+            xbt_dynar_push(comms, &data_comm);
+        MSG_comm_waitany(comms);
+        xbt_dynar_reset(comms);
+
+        if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
+            if (strcmp(MSG_task_get_name(ctrl_task), "finalize")) {
+                DEBUG0("received message from ctrl");
+                xbt_mutex_acquire(mutex);
+                received.push(ctrl_task);
+                xbt_mutex_release(mutex);
+                ctrl_task = NULL;
+                ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
+            } else {
+                DEBUG0("received finalize from ctrl");
+                MSG_task_destroy(ctrl_task);
+                ctrl_task = NULL;
+                ctrl_comm = NULL;
+            }
+        }
+
+        if (data_comm && comm_test_n_destroy(data_comm)) {
+            if (strcmp(MSG_task_get_name(data_task), "finalize")) {
+                DEBUG0("received message from data");
+                xbt_mutex_acquire(mutex);
+                received.push(data_task);
+                xbt_mutex_release(mutex);
+                data_task = NULL;
+                data_comm = MSG_task_irecv(&data_task, get_data_mbox());
+            } else {
+                DEBUG0("received finalize from data");
+                MSG_task_destroy(data_task);
+                data_task = NULL;
+                data_comm = NULL;
+            }
+        }
+        xbt_mutex_acquire(mutex);
+        if (!received.empty())
+            xbt_cond_signal(cond);
+        xbt_mutex_release(mutex);
+    }
+    xbt_dynar_free(&comms);
+    return 0;
+}
+
 // Local variables:
 // mode: c++
 // End: