STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lock.h
Go to the documentation of this file.
1 /***
2 *lock.h
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose: lock class for automatically acquiring/releasing
7 * the monitor lock on managed types.
8 *
9 * [Public]
10 *
11 ****/
12 
13 #pragma once
14 
15 #if !defined (_INC_MSCLR_LOCK)
16 
17 #ifndef __cplusplus_cli
18 #error ERROR: msclr libraries are not compatible with /clr:oldSyntax
19 #endif /* __cplusplus_cli */
20 
21 #include <msclr/safebool.h>
22 #if !defined (_M_CEE_SAFE)
23 #include <vcclr.h>
24 #endif /* !defined (_M_CEE_SAFE) */
25 
26 namespace msclr
27 {
28 
29  // The lock_when enumeration is used to defer the lock object
30  // from taking the lock.
32 
33  ref class lock
34  {
35  private:
36  System::Object ^ m_object;
37  bool m_locked;
38 
39  template<class T,class U> value struct is_not { typedef int __dont_use_this_type__; };
40  template<class T> value struct is_not<T,T> { };
41 
42  public:
43  // By default, take the lock immediately with an infinite timeout.
44  // Accept an optional timespan (in milliseconds) and throw on failure or timeout.
45  template<class T> lock( T ^ _object)
46  : m_object( _object ),
47  m_locked( false )
48  {
49  // ensure that T is not a ReaderWriterLock.
51 
52  acquire(System::Threading::Timeout::Infinite);
53  }
54 
55  template<class T> lock( T ^ _object, int _timeout )
56  : m_object( _object ),
57  m_locked( false )
58  {
59  // ensure that T is not a ReaderWriterLock.
61 
62  acquire( _timeout );
63  }
64 
65  // By default, take the lock immediately within the specified timespan
66  // and throw on failure or timeout.
67  template<class T> lock( T ^ _object, System::TimeSpan _timeout )
68  : m_object( _object ),
69  m_locked( false )
70  {
71  // ensure that T is not a ReaderWriterLock.
73 
74  acquire( _timeout );
75  }
76 
77  // If you use the "lock_later" enumeration, it causes lock
78  // to defer taking the lock.
79  template<class T> lock( T ^ _object, lock_when )
80  : m_object( _object ),
81  m_locked( false )
82  {
83  // ensure that T is not a ReaderWriterLock.
85  }
86 
87 #if !defined (_M_CEE_SAFE)
88  template<class T> lock( gcroot<T ^> _object)
89  : m_object( _object ),
90  m_locked( false )
91  {
92  // ensure that T is not a ReaderWriterLock.
94 
95  acquire(System::Threading::Timeout::Infinite);
96  }
97 
98 
99  template<class T> lock( gcroot<T ^> _object, int _timeout )
100  : m_object( _object ),
101  m_locked( false )
102  {
103  // ensure that T is not a ReaderWriterLock.
105 
106  acquire( _timeout );
107  }
108 
109  // By default, take the lock immediately within the specified timespan
110  // and throw on failure or timeout.
111  template<class T> lock( gcroot<T ^> _object, System::TimeSpan _timeout )
112  : m_object( _object ),
113  m_locked( false )
114  {
115  // ensure that T is not a ReaderWriterLock.
117 
118  acquire( _timeout );
119  }
120 
121  // If you use the "lock_later" enumeration, it causes lock
122  // to defer taking the lock.
123  template<class T> lock( gcroot<T ^> _object, lock_when )
124  : m_object( _object ),
125  m_locked( false )
126  {
127  // ensure that T is not a ReaderWriterLock.
129  }
130 #endif /* !defined (_M_CEE_SAFE) */
131 
132  // release the lock if it is not currently held
134  {
135  release();
136  }
137 
138  // Check to see if this lock object is currently holding the lock
139  bool is_locked()
140  {
141  return m_locked;
142  }
143 
144  // Check to see if this lock object is currently holding the lock
146  {
148  }
149 
150  // disallow explicit comparisons to _safe_bool
151  template<class T> bool operator==( T t )
152  {
153  // ensure that T is not a _safe_bool.
155 
156  return m_object == t;
157  }
158 
159  template<class T> bool operator!=( T t )
160  {
161  // ensure that T is not a _safe_bool.
163 
164  return m_object != t;
165  }
166 
167  // Take the lock within the specified timespan, or with an infinite
168  // timespan if none is specified. Throw on timeout
169  void acquire( int _timeout )
170  {
171  if( ! m_locked )
172  {
173  System::Threading::Monitor::TryEnter( m_object, _timeout, m_locked );
174  if( ! m_locked )
175  {
176  static const long _hresult_wait_timeout = 0x80070102;
177  throw System::Runtime::InteropServices::Marshal::GetExceptionForHR( _hresult_wait_timeout );
178  }
179  }
180  }
181 
182  void acquire()
183  {
184  if( ! m_locked )
185  {
186  System::Threading::Monitor::TryEnter( m_object,
187  System::Threading::Timeout::Infinite, m_locked );
188  if( ! m_locked )
189  {
190  static const long _hresult_wait_timeout = 0x80070102;
191  throw System::Runtime::InteropServices::Marshal::GetExceptionForHR( _hresult_wait_timeout );
192  }
193  }
194  }
195 
196  // Take the lock within the specified timespan.
197  // throw on timeout
198  void acquire( System::TimeSpan _timeout )
199  {
200  if( ! m_locked )
201  {
202  System::Threading::Monitor::TryEnter( m_object, _timeout, m_locked );
203  if( ! m_locked )
204  {
205  static const long _hresult_wait_timeout = 0x80070102;
206  throw System::Runtime::InteropServices::Marshal::GetExceptionForHR( _hresult_wait_timeout );
207  }
208  }
209  }
210 
211  // Try to take the lock within the specified timespan. In the case
212  // of timeout, return false.
213  bool try_acquire( int _timeout ) // throw()
214  {
215  if( ! m_locked )
216  {
217  System::Threading::Monitor::TryEnter( m_object, _timeout, m_locked );
218  if( ! m_locked )
219  {
220  return false;
221  }
222  }
223  return true;
224  }
225 
226  // Try to take the lock within the specified timespan. In the case
227  // of timeout, return false.
228  bool try_acquire( System::TimeSpan _timeout ) // throw()
229  {
230  if( ! m_locked )
231  {
232  System::Threading::Monitor::TryEnter( m_object, _timeout, m_locked );
233  if( ! m_locked )
234  {
235  return false;
236  }
237  }
238  return true;
239  }
240 
241  // Release the lock if it is currently held
242  void release()
243  {
244  if( m_locked )
245  {
246  System::Threading::Monitor::Exit( m_object );
247  m_locked = false;
248  }
249  }
250  };
251 }
252 
253 #define _INC_MSCLR_LOCK
254 
255 #endif /* !defined (_INC_MSCLR_LOCK) */
bool operator==(T t)
Definition: lock.h:151
System::String _safe_bool
Definition: safebool.h:37
lock(T^_object, int _timeout)
Definition: lock.h:55
void release()
Definition: lock.h:242
bool m_locked
Definition: lock.h:37
Definition: gcroot.h:43
static _safe_bool const _safe_false
Definition: safebool.h:39
Definition: lock.h:39
Definition: lock.h:33
lock(T^_object, System::TimeSpan _timeout)
Definition: lock.h:67
void acquire(System::TimeSpan _timeout)
Definition: lock.h:198
static _safe_bool const _safe_true
Definition: safebool.h:38
bool try_acquire(int _timeout)
Definition: lock.h:213
bool operator!=(T t)
Definition: lock.h:159
lock_when
Definition: lock.h:31
Definition: lock.h:31
lock(gcroot< T^> _object)
Definition: lock.h:88
lock(gcroot< T^> _object, lock_when)
Definition: lock.h:123
bool is_locked()
Definition: lock.h:139
lock(T^_object)
Definition: lock.h:45
#define false
Definition: stdbool.h:16
~lock()
Definition: lock.h:133
System::Object m_object
Definition: lock.h:36
void acquire(int _timeout)
Definition: lock.h:169
Definition: gcroot.h:56
lock(gcroot< T^> _object, System::TimeSpan _timeout)
Definition: lock.h:111
void acquire()
Definition: lock.h:182
lock(gcroot< T^> _object, int _timeout)
Definition: lock.h:99
int __dont_use_this_type__
Definition: lock.h:39
bool try_acquire(System::TimeSpan _timeout)
Definition: lock.h:228
lock(T^_object, lock_when)
Definition: lock.h:79