X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/c642311b1135f0d4a8f55e5822d9c0b4fb8e5e5d..be3155e24d0c71e2bbd31dd282cfd49bff7290fa:/include/simgrid/s4u/mutex.hpp diff --git a/include/simgrid/s4u/mutex.hpp b/include/simgrid/s4u/mutex.hpp index 52accd1686..201f0710a9 100644 --- a/include/simgrid/s4u/mutex.hpp +++ b/include/simgrid/s4u/mutex.hpp @@ -6,30 +6,55 @@ #ifndef SIMGRID_S4U_MUTEX_HPP #define SIMGRID_S4U_MUTEX_HPP +#include + +#include #include #include "simgrid/simix.h" - namespace simgrid { namespace s4u { XBT_PUBLIC_CLASS Mutex { 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 _mutex; - + simgrid::simix::Mutex* mutex_; }; }} // namespace simgrid::s4u