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.

214 lines
6.1 KiB

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