Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.0 KiB

  1. #ifndef __LOCKST_H__
  2. #define __LOCKST_H__
  3. #include "locks.h"
  4. #include <assert.h>
  5. # pragma once
  6. #define CS_GUARD(name_of_lock , source_of_lock ) \
  7. LockGuard<CriticalSection> name_of_lock(source_of_lock);
  8. #define CS_GUARD_RETURN(name_of_lock , source_of_lock, return_code_of_lock ) \
  9. LockGuard<CriticalSection> name_of_lock(source_of_lock); \
  10. if (name_of_lock.locked() == false) \
  11. return (return_code_of_lock);
  12. #define CS_GUARD_RETURN_VOID(name_of_lock , source_of_lock) \
  13. LockGuard<CriticalSection> name_of_lock(source_of_lock); \
  14. if (name_of_lock.locked() == false) \
  15. return ;
  16. template <class LOCK, class EXCEPTION_STRATEGY = wminothrow_t>
  17. class LockGuard
  18. {
  19. // It performs automatic aquisition and release of
  20. // a parameterized synchronization object <LOCK>.
  21. public:
  22. // = Initialization and termination methods.
  23. LockGuard (LOCK &l);
  24. LockGuard (LOCK &l, bool block);
  25. // Implicitly and automatically acquire (or try to acquire) the
  26. // lock.
  27. ~LockGuard (void);
  28. // Implicitly release the lock.
  29. // = Lock accessors.
  30. bool acquire (void);
  31. // Explicitly acquire the lock.
  32. bool tryacquire (void);
  33. // Conditionally acquire the lock (i.e., won't block).
  34. bool release (void);
  35. // Explicitly release the lock, but only if it is held!
  36. bool locked (void);
  37. // true if locked, false if couldn't acquire the lock
  38. bool valid() { return lock_->valid();};
  39. void dump (void) const;
  40. // Dump the state of an object.
  41. protected:
  42. bool raise_exception(void)
  43. {
  44. if (!owner_)
  45. EXCEPTION_STRATEGY::raise_lock_failure();
  46. return owner_;
  47. }
  48. LockGuard (LOCK *lock): lock_ (lock) { }
  49. // Helper, meant for subclass only.
  50. LOCK *lock_;
  51. // Pointer to the LOCK we're LockGuarding.
  52. bool owner_;
  53. // Keeps track of whether we acquired the lock or failed.
  54. private:
  55. // = Prevent assignment and initialization.
  56. void operator= (const LockGuard<LOCK,EXCEPTION_STRATEGY> &);
  57. LockGuard (const LockGuard<LOCK,EXCEPTION_STRATEGY> &);
  58. };
  59. template <class LOCK, class EXCEPTION_STRATEGY = wminothrow_t>
  60. class ReadLockGuard : public LockGuard<LOCK,EXCEPTION_STRATEGY>
  61. {
  62. public:
  63. ReadLockGuard(LOCK& lock):LockGuard(&lock){ aquire();}
  64. ReadLockGuard(LOCK& lock, bool block);
  65. ~ReadLockGuard(){ release() }
  66. bool acquire (void)
  67. {
  68. assert(owner_==false);
  69. owner_ = lock_->acquire_read ();
  70. return raise_exception();
  71. };
  72. bool tryacquire (void)
  73. {
  74. assert(owner_==false);
  75. owner_ = lock_->tryacquire_read ();
  76. return raise_exception();
  77. }
  78. bool release (void)
  79. { if (owner_)
  80. {
  81. owner_ = false;
  82. lock_->release();
  83. }else
  84. return false;
  85. }
  86. };
  87. template <class LOCK, class EXCEPTION_STRATEGY = wminothrow_t>
  88. class WriteLockGuard : public LockGuard<LOCK,EXCEPTION_STRATEGY>
  89. {
  90. public:
  91. WriteLockGuard(LOCK& lock):LockGuard(&lock){ aquire();}
  92. WriteLockGuard(LOCK& lock, bool block);
  93. ~WriteLockGuard(){ release(); }
  94. bool acquire (void)
  95. {
  96. assert(owner_==false);
  97. owner_ = lock_->acquire_write ();
  98. return raise_exception();
  99. };
  100. bool tryacquire (void)
  101. {
  102. assert(owner_==false);
  103. owner_ = lock_->tryacquire_write ();
  104. return raise_exception();
  105. }
  106. bool release (void)
  107. { if (owner_)
  108. {
  109. owner_ = false;
  110. lock_->release ();
  111. }else
  112. return false;
  113. }
  114. };
  115. template <class LOCK, class EXCEPTION_STRATEGY> inline bool
  116. LockGuard<LOCK,EXCEPTION_STRATEGY>::acquire (void)
  117. {
  118. assert(owner_==false);
  119. owner_ = lock_->acquire ();
  120. return raise_exception();
  121. }
  122. template <class LOCK, class EXCEPTION_STRATEGY> inline bool
  123. LockGuard<LOCK,EXCEPTION_STRATEGY>::tryacquire (void)
  124. {
  125. assert(owner_==false);
  126. owner_ = lock_->tryacquire ();
  127. return raise_exception();
  128. }
  129. template <class LOCK, class EXCEPTION_STRATEGY> inline bool
  130. LockGuard<LOCK,EXCEPTION_STRATEGY>::release (void)
  131. {
  132. if (owner_)
  133. {
  134. owner_ = false;
  135. return lock_->release ();
  136. }
  137. else
  138. return 0;
  139. }
  140. template <class LOCK, class EXCEPTION_STRATEGY> inline
  141. LockGuard<LOCK,EXCEPTION_STRATEGY>::LockGuard (LOCK &l)
  142. : lock_ (&l),
  143. owner_ (false)
  144. {
  145. acquire ();
  146. }
  147. template <class LOCK, class EXCEPTION_STRATEGY> inline
  148. LockGuard<LOCK,EXCEPTION_STRATEGY>::LockGuard (LOCK &l, bool block)
  149. : lock_ (&l), owner_ (false)
  150. {
  151. if (block)
  152. acquire ();
  153. else
  154. tryacquire ();
  155. }
  156. template <class LOCK, class EXCEPTION_STRATEGY> inline
  157. LockGuard<LOCK,EXCEPTION_STRATEGY>::~LockGuard (void)
  158. {
  159. release ();
  160. }
  161. template <class LOCK, class EXCEPTION_STRATEGY> inline bool
  162. LockGuard<LOCK,EXCEPTION_STRATEGY>::locked (void)
  163. {
  164. return owner_;
  165. }
  166. template <class LOCK, class EXCEPTION_STRATEGY> inline
  167. ReadLockGuard<LOCK,EXCEPTION_STRATEGY>::ReadLockGuard (LOCK &l, bool block)
  168. : LockGuard (&l), owner_ (false)
  169. {
  170. if (block)
  171. acquire ();
  172. else
  173. tryacquire ();
  174. }
  175. template <class LOCK, class EXCEPTION_STRATEGY> inline
  176. WriteLockGuard<LOCK,EXCEPTION_STRATEGY>::WriteLockGuard (LOCK &l, bool block)
  177. : LockGuard (&l), owner_ (false)
  178. {
  179. if (block)
  180. acquire ();
  181. else
  182. tryacquire ();
  183. }
  184. #endif