#ifndef SIMGRID_S4U_MUTEX_HPP
#define SIMGRID_S4U_MUTEX_HPP
+#include <mutex>
+#include <utility>
+
#include <xbt/base.h>
#include "simgrid/simix.h"
-
namespace simgrid {
namespace s4u {
-XBT_PUBLIC_CLASS Mutex {
+class ConditionVariable;
+XBT_PUBLIC_CLASS Mutex {
+friend ConditionVariable;
public:
- Mutex();
- ~Mutex() {};
+ Mutex() :
+ mutex_(simcall_mutex_init()) {}
+ Mutex(simgrid::simix::Mutex* mutex) : mutex_(SIMIX_mutex_ref(mutex)) {}
+ ~Mutex()
+ {
+ SIMIX_mutex_unref(mutex_);
+ }
-protected:
+ // 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);
+ }
-public:
+ 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
#endif /* SIMGRID_S4U_MUTEX_HPP */