commore  1.0.6-SNAPSHOT
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CriticalSection.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2014 Raphael David / CANTOR
3 //
4 
5 #ifndef CMR_CRITICAL_SECTION_INCLUDED
6 #define CMR_CRITICAL_SECTION_INCLUDED
7 
8 #include "Commore.h"
9 
10 namespace commore
11 {
16  {
17  public :
19  ~CriticalSection();
20 
21  public:
25  void enter();
29  void leave();
33  bool try_enter();
34 
35  private:
37  CriticalSection& operator=(const CriticalSection&);
38 
39  private:
40 #ifdef WIN32
41 #ifdef _M_X64
42  char cs_[64];
43  enum { CS_LAST_IDX = 63 };
44 #else
45  char cs_[32];
46  enum { CS_LAST_IDX = 31 };
47 #endif
48 #else
49  char cs_[64];
50  enum { CS_LAST_IDX = 63 };
51 #endif
52  };
53 
57  class CMREXD Mutex
58  {
59  public:
63  Mutex();
69  Mutex(const AChar* name, bool create);
70  ~Mutex();
71  public:
77  bool create(const AChar* name);
83  bool open(const AChar* name);
88  bool lock();
93  bool try_lock();
98  bool lock(const TimePeriod& timeout);
103  bool unlock();
107  bool is_ok();
108  private:
109  class Impl;
110  Impl* impl_;
111  };
112 
113 
119  class Lock
120  {
121  public:
126  Lock(CriticalSection& spcs) : spcs_(&spcs), mtx_(0)
127  {
128  if (spcs_)
129  {
130  spcs_->enter();
131  }
132  }
137  Lock(CriticalSection* spcs) : spcs_(spcs), mtx_(0)
138  {
139  if (spcs_)
140  {
141  spcs_->enter();
142  }
143  }
148  Lock(Mutex& mtx) : spcs_(0), mtx_(&mtx)
149  {
150  if (mtx_)
151  {
152  mtx_->lock();
153  }
154  }
159  Lock(Mutex* mtx) : spcs_(0), mtx_(mtx)
160  {
161  if (mtx_)
162  {
163  mtx_->lock();
164  }
165  }
170  {
171  if (spcs_)
172  {
173  spcs_->leave();
174  }
175  else if (mtx_)
176  {
177  mtx_->unlock();
178  }
179  }
180 
181  private:
182  Lock(const Lock&);
183  Lock& operator=(const Lock&);
184 
185  private:
186  CriticalSection* spcs_;
187  Mutex* mtx_;
188 
189  };
190 
197  class TryLock
198  {
199  public:
204  TryLock(CriticalSection& spcs) : spcs_(&spcs)
205  {
206  if (spcs_ && !spcs_->try_enter())
207  {
208  spcs_ = 0;
209  }
210  }
215  TryLock(CriticalSection* spcs) : spcs_(spcs)
216  {
217  if (spcs_ && !spcs_->try_enter())
218  {
219  spcs_ = 0;
220  }
221  }
226  TryLock(Mutex& mtx) : spcs_(0), mtx_(&mtx)
227  {
228  if (mtx_ && !mtx_->try_lock())
229  {
230  mtx_ = 0;
231  }
232  }
237  TryLock(Mutex* mtx) : spcs_(0), mtx_(mtx)
238  {
239  if (mtx_ && !mtx_->try_lock())
240  {
241  mtx_ = 0;
242  }
243  }
248  {
249  if (spcs_)
250  {
251  spcs_->leave();
252  }
253  }
254 
255  public:
259  bool entered() const
260  {
261  return spcs_ != 0 || mtx_ != 0;
262  }
266  operator bool () const
267  {
268  return entered();
269  }
270 
271  private:
272  TryLock(const Lock&); // Copy forbiden.
273  TryLock& operator=(const Lock&); // Copy forbiden
274 
275  private:
276  CriticalSection* spcs_;
277  Mutex* mtx_;
278 
279  }; // class TryLock
280 };
281 
282 #endif
void enter()
Definition: CriticalSection-linux.cpp:66
bool try_lock()
Definition: CriticalSection.cpp:70
Definition: MutexImpl.h:44
Lock(CriticalSection *spcs)
Definition: CriticalSection.h:137
TryLock(CriticalSection *spcs)
Definition: CriticalSection.h:215
bool entered() const
Definition: CriticalSection.h:259
Definition: Time.h:21
~TryLock()
Definition: CriticalSection.h:247
char AChar
Definition: Type.h:65
#define CMREXD
Definition: Compiler.h:22
bool unlock()
Definition: CriticalSection.cpp:96
TryLock(Mutex &mtx)
Definition: CriticalSection.h:226
TryLock(Mutex *mtx)
Definition: CriticalSection.h:237
bool lock()
Definition: CriticalSection.cpp:58
Lock(Mutex *mtx)
Definition: CriticalSection.h:159
Definition: CriticalSection.h:119
void leave()
Definition: CriticalSection-linux.cpp:76
TryLock(CriticalSection &spcs)
Definition: CriticalSection.h:204
Lock(CriticalSection &spcs)
Definition: CriticalSection.h:126
Definition: CriticalSection.h:15
Lock(Mutex &mtx)
Definition: CriticalSection.h:148
Definition: CriticalSection.h:57
~Lock()
Definition: CriticalSection.h:169
bool try_enter()
Definition: CriticalSection-linux.cpp:86
Definition: CriticalSection.h:197