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

Private GIT Repository
Bug fixed: use a timeout on receive.
[loba.git] / communicator.cpp
index 8adb258e536b83c0c85d9fbf7524ad71125732f4..90cf990ebb07c31f70d30f89e6723a94bd78bdca 100644 (file)
@@ -83,13 +83,26 @@ void communicator::send(const char* dest, message* msg)
     sent_comm.push_back(comm);
 }
 
-bool communicator::recv(message*& msg, m_host_t& from, bool wait)
+bool communicator::recv(message*& msg, m_host_t& from, double timeout)
 {
-    if (wait) {
+    if (timeout != 0) {
+        volatile double deadline =
+            timeout > 0 ? MSG_get_clock() + timeout : 0.0;
         xbt_mutex_acquire(mutex);
-        while (received.empty()) {
+        while (received.empty() && (!deadline || deadline > MSG_get_clock())) {
+            xbt_ex_t e;
             DEBUG0("waiting for a message to come");
-            xbt_cond_wait(cond, mutex);
+            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);
     }