My Project
mutex.h
1 #pragma once
2 
3 #include <boost/noncopyable.hpp>
4 #include <boost/thread/mutex.hpp>
5 // Note: if one wants to use nested mutex, use this one. However, it is slightly heavier than traditional mutex. On windows, mutex can be nested by default.
6 // #include <boost/thread/recursive_mutex.hpp>
7 
8 namespace ParaEngine
9 {
11  template <typename Mutex>
13  {
14  public:
15  // Constructor acquires the lock.
16  scoped_Lock(Mutex& m)
17  : mutex_(m)
18  {
19  mutex_.lock();
20  locked_ = true;
21  }
22 
23  // Destructor releases the lock.
24  ~scoped_Lock()
25  {
26  if (locked_)
27  mutex_.unlock();
28  }
29 
30  // Explicitly acquire the lock.
31  void lock()
32  {
33  if (!locked_)
34  {
35  mutex_.lock();
36  locked_ = true;
37  }
38  }
39 
40  // Explicitly release the lock.
41  void unlock()
42  {
43  if (locked_)
44  {
45  mutex_.unlock();
46  locked_ = false;
47  }
48  }
49 
50  // Test whether the lock is held.
51  bool locked() const
52  {
53  return locked_;
54  }
55 
56  // Get the underlying mutex.
57  Mutex& mutex()
58  {
59  return mutex_;
60  }
61 
62  private:
63  // The underlying mutex.
64  Mutex& mutex_;
65 
66  // Whether the mutex is currently locked or unlocked.
67  bool locked_;
68  };
69 
71  template <typename Mutex>
72  class no_lock{
73  public:
74  no_lock(Mutex& m){}
75  bool locked() const { return true; };
76  void unlock() {};
77  };
78 
95  class mutex
96  {
97  public:
99  private:
100 
101 #ifdef WIN32
102  CRITICAL_SECTION _mutex;
103 #else
104  pthread_mutex_t _mutex;
105 #endif
106 
107  bool _locked;
109  void init(){
110 #ifdef WIN32
111  InitializeCriticalSection(&_mutex);
112 #else
113  pthread_mutexattr_t attr;
114  pthread_mutexattr_init(&attr);
115  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
116  pthread_mutex_init(&_mutex, &attr);
117  pthread_mutexattr_destroy(&attr);
118 #endif
119  _locked = false;
120  }
121 
122  public:
123 
128  mutex(){
129  init();
130  }
136  mutex(const mutex &in_mutex) {
137  init();
138 
139  if (in_mutex._locked && !_locked) lock();
140  else if (!in_mutex._locked && _locked) unlock();
141  }
142 
148  mutex& operator=(const mutex &in_mutex) {
149  if (in_mutex._locked && !_locked) lock();
150  else if (!in_mutex._locked && _locked) unlock();
151  return *this;
152  }
153 
157  virtual ~mutex(){
158 #ifdef WIN32
159  DeleteCriticalSection(&_mutex);
160 #else
161  pthread_mutex_unlock(&_mutex);
162  pthread_mutex_destroy(&_mutex);
163 #endif
164  }
165 
171  bool lock(){
172  _locked = true;
173 #ifdef WIN32
174  EnterCriticalSection(&_mutex);
175  return true;
176 #else
177  return pthread_mutex_lock(&_mutex) == 0;
178 #endif
179  }
180 
185  bool tryLock(){
186  _locked = true;
187 #ifdef WIN32
188  return !!TryEnterCriticalSection(&_mutex);
189 #else
190  return pthread_mutex_trylock(&_mutex) == 0;
191 #endif
192  }
193 
199  bool unlock(){
200  _locked = false;
201 #ifdef WIN32
202  LeaveCriticalSection(&_mutex);
203  return true;
204 #else
205  return pthread_mutex_unlock(&_mutex) == 0;
206 #endif
207  }
208 
222  bool isLocked() const{
223  return _locked;
224  }
225 
226  };
227  typedef mutex::ScopedLock Lock;
228 }
229 
bool lock()
lock a mutex
Definition: Mutex.hpp:164
mutex()
Construct a mutex.
Definition: mutex.h:128
bool unlock()
unlock a mutex
Definition: mutex.h:199
bool lock()
lock a mutex
Definition: mutex.h:171
different physics engine has different winding order.
Definition: EventBinding.h:32
simple scoped lock function
Definition: mutex.h:12
bool unlock()
unlock a mutex
Definition: Mutex.hpp:192
bool tryLock()
lock a mutex
Definition: mutex.h:185
mutex(const mutex &in_mutex)
Copy Constructor a mutex (copy the locked state only)
Definition: mutex.h:136
mutex & operator=(const mutex &in_mutex)
Copy a mutex (copy the locked state only)
Definition: mutex.h:148
cross platform mutex
Definition: Mutex.hpp:88
a dummy lock object with all dummy interface functions
Definition: mutex.h:72
cross platform mutex
Definition: mutex.h:95
virtual ~mutex()
Destructor.
Definition: mutex.h:157
bool isLocked() const
Fast locked look up.
Definition: mutex.h:222