Algorithmique Numérique Distribuée Public GIT Repository
 author Gabriel Corona Tue, 21 Jun 2016 09:10:01 +0000 (11:10 +0200) committer Gabriel Corona Tue, 21 Jun 2016 09:35:24 +0000 (11:35 +0200)

index ce06ef0..f29a26d 100644 (file)
@@ -6,6 +6,7 @@
#ifndef SIMGRID_S4U_COND_VARIABLE_HPP
#define SIMGRID_S4U_COND_VARIABLE_HPP

+#include <condition_variable>
#include <mutex>
#include <utility> // std::swap

@@ -52,7 +53,7 @@ public:
*/
void wait(std::unique_lock<Mutex>& lock);
// TODO, return std::cv_status
-  void wait_for(std::unique_lock<Mutex>& lock, double duration);
+  std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration);
// TODO, wait_until

/** Variant which takes a predice */
@@ -62,9 +63,10 @@ public:
while (!pred())
wait(lock);
}
+
// TODO, return std::cv_status
template<class P>
-  void wait_for(std::unique_lock<Mutex>& lock, double duration, P pred)
+  std::cv_status wait_for(std::unique_lock<Mutex>& lock, double duration, P pred)
{
while (!pred())
wait_for(lock, duration);
index 32c8aab..bb367af 100644 (file)
@@ -1,5 +1,8 @@
+#include <exception>
#include <mutex>

+#include <xbt/log.hpp>
+
#include "simgrid/s4u/conditionVariable.hpp"
#include "simgrid/simix.h"

@@ -20,8 +23,27 @@ void s4u::ConditionVariable::wait(std::unique_lock<Mutex>& lock) {
simcall_cond_wait(cond_, lock.mutex()->mutex_);
}

-void s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout) {
-  simcall_cond_wait_timeout(cond_, lock.mutex()->mutex_, timeout);
+std::cv_status s4u::ConditionVariable::wait_for(std::unique_lock<Mutex>& lock, double timeout) {
+  try {
+    simcall_cond_wait_timeout(cond_, lock.mutex()->mutex_, timeout);
+    return std::cv_status::timeout;
+  }
+  catch (xbt_ex& e) {
+    if (e.category == timeout_error) {
+      // We have to take the lock:
+      try {
+        lock.mutex()->lock();
+      }
+      catch (...) {
+        std::terminate();
+      }
+      return std::cv_status::timeout;
+    }
+    std::terminate();
+  }
+  catch (...) {
+    std::terminate();
+  }
}

/**