Windows NT 4.0 source code leak
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.

223 lines
4.3 KiB

4 years ago
  1. /* --------------------------------------------------------------------
  2. Microsoft OS/2 LAN Manager
  3. Copyright(c) Microsoft Corp., 1990
  4. -------------------------------------------------------------------- */
  5. /* --------------------------------------------------------------------
  6. File: mutex.hxx
  7. Description:
  8. This file contains the system independent mutex class. A mutex is an
  9. object which is used to serialize access to a resource. Besides
  10. construction and destruction, a mutex can have two operations performed
  11. on it: Clear and Request. Request is a request for exclusive access
  12. to the mutex; the method will not complete until the calling thread has
  13. exclusive access to the mutex. Clear indicates that the thread with
  14. exclusive access to the mutex is done.
  15. For example, suppose I have a shared counter which I want to protect.
  16. The counter will be accessed by several different threads at the same
  17. time.
  18. MUTEX CounterMutex;
  19. unsigned int Counter;
  20. :
  21. void
  22. IncrementCounter (
  23. )
  24. {
  25. CounterMutex.Request();
  26. Counter += 1;
  27. CounterMutex.Clear();
  28. }
  29. History:
  30. mikemon ??-??-?? The starting line.
  31. mikemon 12-31-90 Upgraded the comments.
  32. -------------------------------------------------------------------- */
  33. #ifndef __MUTEX_HXX__
  34. #define __MUTEX_HXX__
  35. class MUTEX
  36. {
  37. unsigned int SuccessfullyInitialized;
  38. RTL_CRITICAL_SECTION CriticalSection;
  39. #ifdef NO_RECURSIVE_MUTEXES
  40. unsigned int RecursionCount;
  41. #endif // NO_RECURSIVE_MUTEXES
  42. public:
  43. MUTEX (
  44. IN OUT RPC_STATUS PAPI * RpcStatus
  45. );
  46. ~MUTEX (
  47. );
  48. void
  49. Request ( // Request exclusive access to the mutex.
  50. )
  51. {
  52. #ifdef DOSWIN32RPC
  53. EnterCriticalSection(&CriticalSection);
  54. #else // DOSWIN32RPC
  55. NTSTATUS status;
  56. status = RtlEnterCriticalSection(&CriticalSection);
  57. ASSERT(NT_SUCCESS(status));
  58. #endif // DOSWIN32RPC
  59. #ifdef NO_RECURSIVE_MUTEXES
  60. ASSERT(RecursionCount == 0);
  61. RecursionCount += 1;
  62. #endif // NO_RECURSIVE_MUTEXES
  63. }
  64. void
  65. Clear ( // Clear exclusive access to the mutex.
  66. )
  67. {
  68. #ifdef NO_RECURSIVE_MUTEXES
  69. RecursionCount -= 1;
  70. #endif // NO_RECURSIVE_MUTEXES
  71. #ifdef DOSWIN32RPC
  72. LeaveCriticalSection(&CriticalSection);
  73. #else // DOSWIN32RPC
  74. NTSTATUS status;
  75. status = RtlLeaveCriticalSection(&CriticalSection);
  76. ASSERT(NT_SUCCESS(status));
  77. #endif // DOSWIN32RPC
  78. }
  79. inline void
  80. VerifyOwned()
  81. {
  82. ASSERT((unsigned long) CriticalSection.OwningThread == GetCurrentThreadId());
  83. }
  84. inline void
  85. VerifyNotOwned()
  86. {
  87. //
  88. // The first test is not needed by NT but might be needed
  89. // by Windows 95.
  90. //
  91. ASSERT(CriticalSection.RecursionCount == -1 ||
  92. (unsigned long) CriticalSection.OwningThread != GetCurrentThreadId());
  93. }
  94. };
  95. class EVENT
  96. {
  97. private:
  98. unsigned int SuccessfullyInitialized;
  99. public:
  100. HANDLE EventHandle;
  101. EVENT (
  102. IN OUT RPC_STATUS PAPI * RpcStatus,
  103. IN int ManualReset = 1
  104. );
  105. ~EVENT ( // Destructor.
  106. );
  107. void
  108. Raise ( // Raise the event.
  109. )
  110. #ifdef DOSWIN32RPC
  111. {
  112. BOOL status;
  113. status = SetEvent(EventHandle);
  114. ASSERT(status == TRUE);
  115. }
  116. #else // DOSWIN32RPC
  117. {
  118. NTSTATUS status;
  119. status = NtSetEvent(EventHandle,(PLONG) 0);
  120. ASSERT(NT_SUCCESS(status));
  121. }
  122. #endif // DOSWIN32RPC
  123. void
  124. Lower ( // Lower the event.
  125. )
  126. #ifdef DOSWIN32RPC
  127. {
  128. BOOL status;
  129. status = ResetEvent(EventHandle);
  130. ASSERT(status == TRUE);
  131. }
  132. #else // DOSWIN32RPC
  133. {
  134. NTSTATUS status;
  135. status = NtResetEvent(EventHandle,(PLONG) 0);
  136. ASSERT(NT_SUCCESS(status));
  137. }
  138. #endif // DOSWIN32RPC
  139. int
  140. Wait ( // Wait until the event is raised.
  141. IN long timeout = -1
  142. );
  143. void
  144. Request (
  145. ) {Wait(-1);}
  146. int
  147. RequestWithTimeout (
  148. IN long timeout = -1
  149. ) {return(Wait(timeout));}
  150. void
  151. Clear (
  152. ) {Raise();}
  153. };
  154. class CLAIM_MUTEX {
  155. private:
  156. MUTEX & Resource;
  157. public:
  158. CLAIM_MUTEX(
  159. MUTEX & ClaimedResource
  160. )
  161. : Resource(ClaimedResource)
  162. {
  163. Resource.Request();
  164. }
  165. ~CLAIM_MUTEX(
  166. )
  167. {
  168. Resource.Clear();
  169. }
  170. };
  171. #endif // __MUTEX_HXX__