X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/94a6ea22dbd2f12d1015925d3c3fe7a95b38d2e3..73382ccaaf4ed5534b97beb5e56a9116c7a1e773:/include/simgrid/s4u/mutex.hpp diff --git a/include/simgrid/s4u/mutex.hpp b/include/simgrid/s4u/mutex.hpp index 201f0710a9..5b0f7f5f4f 100644 --- a/include/simgrid/s4u/mutex.hpp +++ b/include/simgrid/s4u/mutex.hpp @@ -6,56 +6,53 @@ #ifndef SIMGRID_S4U_MUTEX_HPP #define SIMGRID_S4U_MUTEX_HPP +#include #include #include + #include #include "simgrid/simix.h" namespace simgrid { namespace s4u { -XBT_PUBLIC_CLASS Mutex { +class ConditionVariable; +XBT_PUBLIC_CLASS Mutex { +friend ConditionVariable; +private: + friend simgrid::simix::Mutex; + simgrid::simix::Mutex* mutex_; + Mutex(simgrid::simix::Mutex* mutex) : mutex_(mutex) {} public: - 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) + friend void intrusive_ptr_add_ref(Mutex* mutex) { - swap(*this, mutex); - return *this; + xbt_assert(mutex); + SIMIX_mutex_ref(mutex->mutex_); } - Mutex(Mutex&& mutex) : mutex_(nullptr) + friend void intrusive_ptr_release(Mutex* mutex) { - swap(*this, mutex); + xbt_assert(mutex); + SIMIX_mutex_unref(mutex->mutex_); } + using Ptr = boost::intrusive_ptr; - bool valid() const - { - return mutex_ != nullptr; - } + // No copy: + Mutex(Mutex const&) = delete; + Mutex& operatori(Mutex const&) = delete; + + static Ptr createMutex(); public: void lock(); void unlock(); bool try_lock(); - -private: - simgrid::simix::Mutex* mutex_; }; + +using MutexPtr = Mutex::Ptr; + }} // namespace simgrid::s4u #endif /* SIMGRID_S4U_MUTEX_HPP */