Source code of Windows XP (NT5)
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.

213 lines
5.1 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Copyright (C) Microsoft Corporation, 1993-1999
  4. // File: resource.hxx
  5. //
  6. // Contents: CResource, CReadLock, CWriteLock,
  7. // CSafeReadLock, CSafeWriteLock.
  8. //
  9. //
  10. // History: 8 Mar 93 WadeR Created.
  11. // 08-Jul-93 WadeR Moved from security to common
  12. // 18-Nov-93 WadeR Made inline, added Safe locks
  13. //
  14. //------------------------------------------------------------------------
  15. #ifndef _INC_RESOURCE_HXX_
  16. #define _INC_RESOURCE_HXX_
  17. #if _MSC_VER > 1000
  18. #pragma once
  19. #endif
  20. #include <nt.h>
  21. #include <ntrtl.h>
  22. #include <nturtl.h>
  23. #include <except.hxx>
  24. //+-----------------------------------------------------------------
  25. //
  26. // Class: CResource
  27. //
  28. // Purpose: Allows multiple readers or single writer at a resource.
  29. //
  30. // Interface: [CResource] --
  31. // [~CResource] --
  32. // [GetRead] --
  33. // [GetWrite] --
  34. // [ReadToWrite] --
  35. // [WriteToRead] --
  36. // [Release] --
  37. //
  38. // History: 24-Feb-93 WadeR Created.
  39. //
  40. // Notes: This wrapper will allow starving writers, since it just
  41. // passes everything through to the RTL Resource code, which
  42. // allows starving writers.
  43. //
  44. //------------------------------------------------------------------
  45. class CResource
  46. {
  47. RTL_RESOURCE _rResource;
  48. public:
  49. CResource();
  50. ~CResource();
  51. void Release();
  52. void GetRead();
  53. void GetWrite();
  54. void ReadToWrite();
  55. void WriteToRead();
  56. };
  57. inline
  58. CResource::CResource()
  59. {
  60. RtlInitializeResource( &_rResource );
  61. }
  62. inline
  63. CResource::~CResource()
  64. {
  65. RtlDeleteResource( &_rResource );
  66. }
  67. inline
  68. void CResource::Release()
  69. {
  70. RtlReleaseResource( &_rResource );
  71. }
  72. inline
  73. void CResource::GetRead()
  74. {
  75. RtlAcquireResourceShared( &_rResource, TRUE );
  76. }
  77. inline
  78. void CResource::GetWrite()
  79. {
  80. RtlAcquireResourceExclusive( &_rResource, TRUE );
  81. }
  82. inline
  83. void CResource::ReadToWrite()
  84. {
  85. RtlConvertSharedToExclusive( &_rResource );
  86. }
  87. inline
  88. void CResource::WriteToRead()
  89. {
  90. RtlConvertExclusiveToShared( &_rResource );
  91. }
  92. //+---------------------------------------------------------------------------
  93. //
  94. // Class: CReadLock
  95. //
  96. // Purpose: Lock using a Resource monitor
  97. //
  98. // History: 24-Feb-93 WadeR Created.
  99. //
  100. // Notes: Simple lock object to be created on the stack.
  101. // The constructor acquires the resource, the destructor
  102. // (called when lock is going out of scope) releases it.
  103. //
  104. //----------------------------------------------------------------------------
  105. class CReadLock
  106. {
  107. public:
  108. CReadLock ( CResource& r ) : _r(r)
  109. { _r.GetRead(); };
  110. ~CReadLock ()
  111. { _r.Release(); };
  112. private:
  113. CResource& _r;
  114. };
  115. //+---------------------------------------------------------------------------
  116. //
  117. // Class: CWriteLock
  118. //
  119. // Purpose: Lock using a Resource monitor
  120. //
  121. // History: 24-Feb-93 WadeR Created.
  122. //
  123. // Notes: Simple lock object to be created on the stack.
  124. // The constructor acquires the resource, the destructor
  125. // (called when lock is going out of scope) releases it.
  126. //
  127. //----------------------------------------------------------------------------
  128. class CWriteLock
  129. {
  130. public:
  131. CWriteLock ( CResource& r ) : _r(r)
  132. { _r.GetWrite(); };
  133. ~CWriteLock ()
  134. { _r.Release(); };
  135. private:
  136. CResource& _r;
  137. };
  138. //+---------------------------------------------------------------------------
  139. //
  140. // Class: CSafeReadLock
  141. //
  142. // Purpose: Exception safe lock using a Resource monitor
  143. //
  144. // History: 24-Feb-93 WadeR Created.
  145. //
  146. // Notes: Simple lock object to be created on the stack.
  147. // The constructor acquires the resource, the destructor
  148. // (called when lock is going out of scope) releases it.
  149. //
  150. //----------------------------------------------------------------------------
  151. class CSafeReadLock : INHERIT_UNWIND
  152. {
  153. INLINE_UNWIND(CSafeReadLock);
  154. public:
  155. CSafeReadLock ( CResource& r ) : _r(r)
  156. { _r.GetRead(); END_CONSTRUCTION (CSafeReadLock); };
  157. ~CSafeReadLock ()
  158. { _r.Release(); };
  159. private:
  160. CResource& _r;
  161. };
  162. //+---------------------------------------------------------------------------
  163. //
  164. // Class: CSafeWriteLock
  165. //
  166. // Purpose: Exception safe lock using a Resource monitor
  167. //
  168. // History: 24-Feb-93 WadeR Created.
  169. //
  170. // Notes: Simple lock object to be created on the stack.
  171. // The constructor acquires the resource, the destructor
  172. // (called when lock is going out of scope) releases it.
  173. //
  174. //----------------------------------------------------------------------------
  175. class CSafeWriteLock : INHERIT_UNWIND
  176. {
  177. INLINE_UNWIND(CSafeWriteLock);
  178. public:
  179. CSafeWriteLock ( CResource& r ) : _r(r)
  180. { _r.GetWrite(); END_CONSTRUCTION (CSafeWriteLock); };
  181. ~CSafeWriteLock ()
  182. { _r.Release(); };
  183. private:
  184. CResource& _r;
  185. };
  186. #endif // _INC_RESOURCE_HXX_