Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Fix compilation of ConditionVariable::wait_for(lock, duration, pred)
[simgrid.git] / include / simgrid / s4u / mutex.hpp
1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_S4U_MUTEX_HPP
7 #define SIMGRID_S4U_MUTEX_HPP
8
9 #include <mutex>
10 #include <utility>
11
12 #include <boost/intrusive_ptr.hpp>
13 #include <xbt/base.h>
14 #include "simgrid/simix.h"
15
16 namespace simgrid {
17 namespace s4u {
18
19 class ConditionVariable;
20
21 XBT_PUBLIC_CLASS Mutex {
22 friend ConditionVariable;
23 public:
24   Mutex() :
25     mutex_(simcall_mutex_init()) {}
26   Mutex(simgrid::simix::Mutex* mutex) : mutex_(SIMIX_mutex_ref(mutex)) {}
27   ~Mutex()
28   {
29     SIMIX_mutex_unref(mutex_);
30   }
31
32   // Copy+move (with the copy-and-swap idiom):
33   Mutex(Mutex const& mutex) : mutex_(SIMIX_mutex_ref(mutex.mutex_)) {}
34   friend void swap(Mutex& first, Mutex& second)
35   {
36     using std::swap;
37     swap(first.mutex_, second.mutex_);
38   }
39   Mutex& operator=(Mutex mutex)
40   {
41     swap(*this, mutex);
42     return *this;
43   }
44   Mutex(Mutex&& mutex) : mutex_(nullptr)
45   {
46     swap(*this, mutex);
47   }
48
49   bool valid() const
50   {
51     return mutex_ != nullptr;
52   }
53
54 public:
55   void lock();
56   void unlock();
57   bool try_lock();
58
59 private:
60   simgrid::simix::Mutex* mutex_;
61 };
62
63 }} // namespace simgrid::s4u
64
65 #endif /* SIMGRID_S4U_MUTEX_HPP */