AnimatLab  2
Test
StdCriticalSectionInternal.h
1 
7 // CTryEnterCS
9 //
10 // A ::TryEnterCriticalSection type thing that works on 9x
11 //
12 // Written by Olan Patrick Barnes (patrick@mfcfree.com)
13 // Copyright (c) 2001 Olan Patrick Barnes
14 //
15 // This code may be used in compiled form in any way you desire. This
16 // file may be redistributed by any means PROVIDING it is
17 // not sold for profit without the authors written consent, and
18 // providing that this notice and the authors name is included.
19 //
20 // This file is provided "as is" with no expressed or implied warranty.
21 // The author accepts no liability if it causes any damage to you or your
22 // computer whatsoever.
23 //
24 // Description:
25 //
26 // ::TryEnterCriticalSection() is only available on NT platforms, and you
27 // may need to support 9x. This is a custom critical section class that
28 // allows for "try-enter" logic. It operates 100% in user mode (this
29 // class does not use expensive kernel objects), making use of the
30 // ::InterlockedExchange() and ::GetCurrentThreadId() API's.
31 //
33 
34 #pragma once
35 
36 namespace StdUtils
37 {
38 
39 typedef enum {Locked, Unlocked} LockState;
40 
51 {
52 protected:
53 
55 #ifdef WIN32
56  boost::atomic<LockState> m_iBusy;
57 #else
58  std::atomic<int> m_iBusy;
59 #endif
60 
61  boost::thread::id m_dwOwner;
62 
63  bool m_bOwned;
64 
66  ULONG m_ulRefCnt;
67 
68 #pragma region InternalLocker
69 
79  {
80  protected:
81  //Do not allow copy constructor or copy operator
82 
90 
99  InternalLocker(const InternalLocker& src);
100 
111  const InternalLocker& operator=(const InternalLocker& src);
112 
114 #ifdef WIN32
115  boost::atomic<LockState> &m_iBusy;
116 #else
117  std::atomic<int> &m_iBusy;
118 #endif
119 
120  public:
121 
130 #ifdef WIN32
131  explicit InternalLocker(boost::atomic<LockState> &iBusy);
132 #else
133  explicit InternalLocker(std::atomic<int> &iBusy);
134 #endif
135 
141  ~InternalLocker();
142  };
143  friend class InternalLocker;
144 
145 #pragma endregion
146 
147 public:
148 
151 
152  bool TryEnter();
153  bool Enter(long lMilliTimeout = -1);
154  bool Leave();
155 };
156 
157 } //StdUtils
ULONG m_ulRefCnt
The number of reference counts to this critical section.
const InternalLocker & operator=(const InternalLocker &src)
Assignment operator.
std::atomic< int > & m_iBusy
Tells if this is busy.
bool Leave()
Leaves this critical section.
std::atomic< int > m_iBusy
Tells if this critical section is currently being used.
Namespace for the standard utility objects.
Definition: MarkupSTL.cpp:19
Standard critical section.
bool Enter(long lMilliTimeout=-1)
Try's to enter the critical section. Waits until it can get in, or until timeout. ...