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.

222 lines
5.9 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1999.
  5. //
  6. // File: CiDllSem.Hxx
  7. //
  8. // Contents: Semaphore classes
  9. //
  10. // Classes: CDLLStaticMutexSem - Mutex semaphore class For Dlls
  11. // CDLLStaticLock - Safe Wrapper for lock...
  12. //
  13. // History: 11-Sep-92 Kurte Hacked up from AlexT's sources
  14. //
  15. // Notes: This file contains a hacked up version of the CMutexSem
  16. // named CDllStataticMutexSem, which does not delete its
  17. // critical section, when the DLL is unloaded, such that other
  18. // dlls can still use the semaphore during their unload
  19. // processing. This is to get around the NT less than
  20. // optimal DLL Exit list processing...
  21. //
  22. //----------------------------------------------------------------------------
  23. #pragma once
  24. //+---------------------------------------------------------------------------
  25. //
  26. // Class: CDLLStaticMutexSem (dmxs)
  27. //
  28. // Purpose: Mutex Semaphore services
  29. //
  30. // Interface: Init - initializer (two-step)
  31. // Request - acquire semaphore
  32. // Release - release semaphore
  33. //
  34. // History: 14-Jun-91 AlexT Created.
  35. // 30-oct-91 SethuR 32 bit implementation
  36. //
  37. // Notes: This class wraps a mutex semaphore. Mutex semaphores protect
  38. // access to resources by only allowing one client through at a
  39. // time. The client Requests the semaphore before accessing the
  40. // resource and Releases the semaphore when it is done. The
  41. // same client can Request the semaphore multiple times (a nest
  42. // count is maintained).
  43. //
  44. //----------------------------------------------------------------------------
  45. class CDLLStaticMutexSem
  46. {
  47. public:
  48. CDLLStaticMutexSem( BOOL fInit = FALSE );
  49. inline BOOL Init();
  50. ~CDLLStaticMutexSem();
  51. void Delete()
  52. {
  53. if ( _fInited )
  54. {
  55. DeleteCriticalSection( &_cs );
  56. _fInited = FALSE;
  57. }
  58. }
  59. void Request();
  60. void Release();
  61. private:
  62. CRITICAL_SECTION _cs;
  63. BOOL _fInited;
  64. };
  65. //+---------------------------------------------------------------------------
  66. //
  67. // Class: CDLLStaticLock (dlck)
  68. //
  69. // Purpose: Lock using a Mutex Semaphore
  70. //
  71. // History: 02-Oct-91 BartoszM Created.
  72. //
  73. // Notes: Simple lock object to be created on the stack.
  74. // The constructor acquires the semaphore, the destructor
  75. // (called when lock is going out of scope) releases it.
  76. //
  77. //----------------------------------------------------------------------------
  78. class CDLLStaticLock
  79. {
  80. public:
  81. CDLLStaticLock ( CDLLStaticMutexSem& dmxs );
  82. ~CDLLStaticLock ();
  83. private:
  84. CDLLStaticMutexSem& _dmxs;
  85. };
  86. //+---------------------------------------------------------------------------
  87. //
  88. // Member: CDLLStaticMutexSem::CDLLStaticMutexSem, public
  89. //
  90. // Synopsis: Mutex semaphore constructor
  91. //
  92. // Effects: Initializes the semaphores data
  93. //
  94. // History: 14-Jun-91 AlexT Created.
  95. //
  96. //----------------------------------------------------------------------------
  97. inline CDLLStaticMutexSem::CDLLStaticMutexSem( BOOL fInit ) :
  98. _fInited( FALSE )
  99. {
  100. if (fInit)
  101. Init();
  102. else
  103. memset( &_cs, 0, sizeof _cs );
  104. }
  105. inline CDLLStaticMutexSem::Init()
  106. {
  107. #if DBG
  108. if (_fInited)
  109. DbgPrint("CDLLStaticMutexSem already initialized!\n");
  110. else
  111. #endif // DBG
  112. InitializeCriticalSection(&_cs);
  113. _fInited = TRUE;
  114. return TRUE;
  115. };
  116. //+---------------------------------------------------------------------------
  117. //
  118. // Member: CDLLStaticMutexSem::~CDLLStaticMutexSem, public
  119. //
  120. // Synopsis: Mutex semaphore destructor
  121. //
  122. // Effects: Releases semaphore data
  123. //
  124. // History: 14-Jun-91 AlexT Created.
  125. //
  126. //----------------------------------------------------------------------------
  127. inline CDLLStaticMutexSem::~CDLLStaticMutexSem()
  128. {
  129. // We cannot delete the Critical Section as it may still
  130. // be called by someother DLL.
  131. }
  132. //+---------------------------------------------------------------------------
  133. //
  134. // Member: CDLLStaticMutexSem::Request, public
  135. //
  136. // Synopsis: Acquire semaphore
  137. //
  138. // Effects: Asserts correct owner
  139. //
  140. // Arguments: [dwMilliseconds] -- Timeout value
  141. //
  142. // History: 14-Jun-91 AlexT Created.
  143. //
  144. // Notes:
  145. //
  146. //----------------------------------------------------------------------------
  147. inline void CDLLStaticMutexSem::Request()
  148. {
  149. if ( ! _fInited )
  150. {
  151. Init();
  152. }
  153. EnterCriticalSection(&_cs);
  154. }
  155. //+---------------------------------------------------------------------------
  156. //
  157. // Member: CDLLStaticMutexSem::Release, public
  158. //
  159. // Synopsis: Release semaphore
  160. //
  161. // Effects: Asserts correct owner
  162. //
  163. // History: 14-Jun-91 AlexT Created.
  164. //
  165. // Notes:
  166. //
  167. //----------------------------------------------------------------------------
  168. inline void CDLLStaticMutexSem::Release()
  169. {
  170. LeaveCriticalSection(&_cs);
  171. }
  172. //+---------------------------------------------------------------------------
  173. //
  174. // Member: CDLLStaticLock::CDLLStaticLock
  175. //
  176. // Synopsis: Acquire semaphore
  177. //
  178. // History: 02-Oct-91 BartoszM Created.
  179. //
  180. //----------------------------------------------------------------------------
  181. inline CDLLStaticLock::CDLLStaticLock ( CDLLStaticMutexSem& dmxs )
  182. : _dmxs ( dmxs )
  183. {
  184. _dmxs.Request();
  185. }
  186. //+---------------------------------------------------------------------------
  187. //
  188. // Member: CDLLStaticLock::~CDLLStaticLock
  189. //
  190. // Synopsis: Release semaphore
  191. //
  192. // History: 02-Oct-91 BartoszM Created.
  193. //
  194. //----------------------------------------------------------------------------
  195. inline CDLLStaticLock::~CDLLStaticLock ()
  196. {
  197. _dmxs.Release();
  198. }