Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] add condition variables
[simgrid.git] / include / simgrid / s4u / mutex.hpp
index 52accd168641c8addb119dff19ff9d8d564bb6a6..75b7d09c030b579fc11086e4ffc6e484d46e6bd0 100644 (file)
@@ -6,30 +6,57 @@
 #ifndef SIMGRID_S4U_MUTEX_HPP
 #define SIMGRID_S4U_MUTEX_HPP
 
+#include <utility>
+
+#include <boost/intrusive_ptr.hpp>
 #include <xbt/base.h>
 #include "simgrid/simix.h"
-
+#include <simgrid/s4u/conditionVariable.hpp>
 
 namespace simgrid {
 namespace s4u {
 
+class ConditionVariable;
 XBT_PUBLIC_CLASS Mutex {
-
+friend ConditionVariable;
 public:
-  Mutex();
-  ~Mutex() {};
-
-protected:
+  Mutex() :
+    mutex_(simcall_mutex_init()) {}
+  Mutex(simgrid::simix::Mutex* mutex) : mutex_(SIMIX_mutex_ref(mutex)) {}
+  ~Mutex()
+  {
+    SIMIX_mutex_unref(mutex_);
+  }
+
+  // Copy+move (with the copy-and-swap idiom):
+  Mutex(Mutex const& mutex) : mutex_(SIMIX_mutex_ref(mutex.mutex_)) {}
+  friend void swap(Mutex& first, Mutex& second)
+  {
+    using std::swap;
+    swap(first.mutex_, second.mutex_);
+  }
+  Mutex& operator=(Mutex mutex)
+  {
+    swap(*this, mutex);
+    return *this;
+  }
+  Mutex(Mutex&& mutex) : mutex_(nullptr)
+  {
+    swap(*this, mutex);
+  }
+
+  bool valid() const
+  {
+    return mutex_ != nullptr;
+  }
 
 public:
-
   void lock();
   void unlock();
   bool try_lock();
 
 private:
-  std::shared_ptr<simgrid::simix::Mutex> _mutex;
-
+  simgrid::simix::Mutex* mutex_;
 };
 }} // namespace simgrid::s4u