My Project
Mutex.hpp
1 #pragma once
2 
3 // Use Window or Posix
4 #ifdef WIN32
5 #include <windows.h>
6 #else
7 #include <pthread.h>
8 #endif
9 
10 namespace ParaEngine
11 {
13  template <typename Mutex>
15  {
16  public:
17  // Constructor acquires the lock.
18  Scoped_Lock(Mutex& m)
19  : mutex_(m)
20  {
21  mutex_.lock();
22  locked_ = true;
23  }
24 
25  // Destructor releases the lock.
26  ~Scoped_Lock()
27  {
28  if (locked_)
29  mutex_.unlock();
30  }
31 
32  // Explicitly acquire the lock.
33  void lock()
34  {
35  if (!locked_)
36  {
37  mutex_.lock();
38  locked_ = true;
39  }
40  }
41 
42  // Explicitly release the lock.
43  void unlock()
44  {
45  if (locked_)
46  {
47  mutex_.unlock();
48  locked_ = false;
49  }
50  }
51 
52  // Test whether the lock is held.
53  bool locked() const
54  {
55  return locked_;
56  }
57 
58  // Get the underlying mutex.
59  Mutex& mutex()
60  {
61  return mutex_;
62  }
63 
64  private:
65  // The underlying mutex.
66  Mutex& mutex_;
67 
68  // Whether the mutex is currently locked or unlocked.
69  bool locked_;
70  };
71 
88  class Mutex
89  {
90  public:
92  private:
93 
94 #ifdef WIN32
95  CRITICAL_SECTION _mutex;
96 #else
97  pthread_mutex_t _mutex;
98 #endif
99 
100  bool _locked;
102  void init(){
103 #ifdef WIN32
104  InitializeCriticalSection(&_mutex);
105 #else
106  pthread_mutexattr_t attr;
107  pthread_mutexattr_init(&attr);
108  pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
109  pthread_mutex_init(&_mutex,&attr);
110  pthread_mutexattr_destroy(&attr);
111 #endif
112  _locked = false;
113  }
114 
115  public:
116 
121  Mutex(){
122  init();
123  }
129  Mutex( const Mutex &in_mutex ) {
130  init();
131 
132  if(in_mutex._locked && !_locked) lock();
133  else if(!in_mutex._locked && _locked) unlock();
134  }
135 
141  Mutex& operator=(const Mutex &in_mutex) {
142  if(in_mutex._locked && !_locked) lock();
143  else if(!in_mutex._locked && _locked) unlock();
144  return *this;
145  }
146 
150  virtual ~Mutex(){
151 #ifdef WIN32
152  DeleteCriticalSection(&_mutex);
153 #else
154  pthread_mutex_unlock(&_mutex);
155  pthread_mutex_destroy(&_mutex);
156 #endif
157  }
158 
164  bool lock(){
165  _locked = true;
166 #ifdef WIN32
167  EnterCriticalSection(&_mutex);
168  return true;
169 #else
170  return pthread_mutex_lock(&_mutex) == 0;
171 #endif
172  }
173 
178  bool tryLock(){
179  _locked = true;
180 #ifdef WIN32
181  return !!TryEnterCriticalSection(&_mutex);
182 #else
183  return pthread_mutex_trylock(&_mutex) == 0;
184 #endif
185  }
186 
192  bool unlock(){
193  _locked = false;
194 #ifdef WIN32
195  LeaveCriticalSection(&_mutex);
196  return true;
197 #else
198  return pthread_mutex_unlock(&_mutex) == 0;
199 #endif
200  }
201 
215  bool isLocked() const{
216  return _locked;
217  }
218 
219  };
220 }
bool lock()
lock a mutex
Definition: Mutex.hpp:164
virtual ~Mutex()
Destructor.
Definition: Mutex.hpp:150
Mutex & operator=(const Mutex &in_mutex)
Copy a mutex (copy the locked state only)
Definition: Mutex.hpp:141
different physics engine has different winding order.
Definition: EventBinding.h:32
bool unlock()
unlock a mutex
Definition: Mutex.hpp:192
Mutex(const Mutex &in_mutex)
Copy Constructor a mutex (copy the locked state only)
Definition: Mutex.hpp:129
simple scoped lock function
Definition: Mutex.hpp:14
Mutex()
Construct a Mutex.
Definition: Mutex.hpp:121
cross platform mutex
Definition: Mutex.hpp:88
bool tryLock()
lock a mutex
Definition: Mutex.hpp:178
bool isLocked() const
Fast locked look up.
Definition: Mutex.hpp:215
cross platform mutex
Definition: mutex.h:95