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.

353 lines
8.3 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. mutntobj.c
  5. Abstract:
  6. This module implements the kernel mutant object. Functions are
  7. provided to initialize, read, and release mutant objects.
  8. N.B. Kernel mutex objects have been subsumed by mutant objects.
  9. Author:
  10. David N. Cutler (davec) 16-Oct-1989
  11. Environment:
  12. Kernel mode only.
  13. Revision History:
  14. --*/
  15. #include "ki.h"
  16. //
  17. // The following assert macro is used to check that an input mutant is
  18. // really a kmutant and not something else, like deallocated pool.
  19. //
  20. #define ASSERT_MUTANT(E) { \
  21. ASSERT((E)->Header.Type == MutantObject); \
  22. }
  23. VOID
  24. KeInitializeMutant (
  25. IN PRKMUTANT Mutant,
  26. IN BOOLEAN InitialOwner
  27. )
  28. /*++
  29. Routine Description:
  30. This function initializes a kernel mutant object.
  31. Arguments:
  32. Mutant - Supplies a pointer to a dispatcher object of type mutant.
  33. InitialOwner - Supplies a boolean value that determines whether the
  34. current thread is to be the initial owner of the mutant object.
  35. Return Value:
  36. None.
  37. --*/
  38. {
  39. PLIST_ENTRY ListEntry;
  40. KIRQL OldIrql;
  41. PRKTHREAD Thread;
  42. //
  43. // Initialize standard dispatcher object header, set the owner thread to
  44. // NULL, set the abandoned state to FALSE, and set the APC disable count
  45. // to zero (this is the only thing that distinguishes a mutex from a mutant).
  46. //
  47. Mutant->Header.Type = MutantObject;
  48. Mutant->Header.Size = sizeof(KMUTANT) / sizeof(LONG);
  49. if (InitialOwner == TRUE) {
  50. Thread = KeGetCurrentThread();
  51. Mutant->Header.SignalState = 0;
  52. Mutant->OwnerThread = Thread;
  53. KiLockDispatcherDatabase(&OldIrql);
  54. ListEntry = Thread->MutantListHead.Blink;
  55. InsertHeadList(ListEntry, &Mutant->MutantListEntry);
  56. KiUnlockDispatcherDatabase(OldIrql);
  57. } else {
  58. Mutant->Header.SignalState = 1;
  59. Mutant->OwnerThread = (PKTHREAD)NULL;
  60. }
  61. InitializeListHead(&Mutant->Header.WaitListHead);
  62. Mutant->Abandoned = FALSE;
  63. Mutant->ApcDisable = 0;
  64. return;
  65. }
  66. VOID
  67. KeInitializeMutex (
  68. IN PRKMUTANT Mutant,
  69. IN ULONG Level
  70. )
  71. /*++
  72. Routine Description:
  73. This function initializes a kernel mutex object. The level number
  74. is ignored.
  75. N.B. Kernel mutex objects have been subsumed by mutant objects.
  76. Arguments:
  77. Mutex - Supplies a pointer to a dispatcher object of type mutex.
  78. Level - Ignored.
  79. Return Value:
  80. None.
  81. --*/
  82. {
  83. UNREFERENCED_PARAMETER(Level);
  84. //
  85. // Initialize standard dispatcher object header, set the owner thread to
  86. // NULL, set the abandoned state to FALSE, adn set the APC disable count
  87. // to one (this is the only thing that distinguishes a mutex from a mutant).
  88. //
  89. Mutant->Header.Type = MutantObject;
  90. Mutant->Header.Size = sizeof(KMUTANT) / sizeof(LONG);
  91. Mutant->Header.SignalState = 1;
  92. InitializeListHead(&Mutant->Header.WaitListHead);
  93. Mutant->OwnerThread = (PKTHREAD)NULL;
  94. Mutant->Abandoned = FALSE;
  95. Mutant->ApcDisable = 1;
  96. return;
  97. }
  98. LONG
  99. KeReadStateMutant (
  100. IN PRKMUTANT Mutant
  101. )
  102. /*++
  103. Routine Description:
  104. This function reads the current signal state of a mutant object.
  105. Arguments:
  106. Mutant - Supplies a pointer to a dispatcher object of type mutant.
  107. Return Value:
  108. The current signal state of the mutant object.
  109. --*/
  110. {
  111. ASSERT_MUTANT(Mutant);
  112. //
  113. // Return current signal state of mutant object.
  114. //
  115. return Mutant->Header.SignalState;
  116. }
  117. LONG
  118. KeReleaseMutant (
  119. IN PRKMUTANT Mutant,
  120. IN KPRIORITY Increment,
  121. IN BOOLEAN Abandoned,
  122. IN BOOLEAN Wait
  123. )
  124. /*++
  125. Routine Description:
  126. This function releases a mutant object by incrementing the mutant
  127. count. If the resultant value is one, then an attempt is made to
  128. satisfy as many Waits as possible. The previous signal state of
  129. the mutant is returned as the function value. If the Abandoned
  130. parameter is TRUE, then the mutant object is released by settings
  131. the signal state to one.
  132. Arguments:
  133. Mutant - Supplies a pointer to a dispatcher object of type mutant.
  134. Increment - Supplies the priority increment that is to be applied
  135. if setting the event causes a Wait to be satisfied.
  136. Abandoned - Supplies a boolean value that signifies whether the
  137. mutant object is being abandoned.
  138. Wait - Supplies a boolean value that signifies whether the call to
  139. KeReleaseMutant will be immediately followed by a call to one
  140. of the kernel Wait functions.
  141. Return Value:
  142. The previous signal state of the mutant object.
  143. --*/
  144. {
  145. KIRQL OldIrql;
  146. LONG OldState;
  147. PRKTHREAD Thread;
  148. ULONG LeaveCriticalRegion;
  149. ASSERT_MUTANT(Mutant);
  150. ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
  151. ASSERT((Mutant->ApcDisable == 0) || (Mutant->ApcDisable == 1));
  152. //
  153. // Raise IRQL to dispatcher level and lock dispatcher database.
  154. //
  155. LeaveCriticalRegion = 0;
  156. Thread = KeGetCurrentThread();
  157. KiLockDispatcherDatabase(&OldIrql);
  158. //
  159. // Capture the current signal state of the mutant object.
  160. //
  161. OldState = Mutant->Header.SignalState;
  162. //
  163. // If the Abandoned parameter is TRUE, then force the release of the
  164. // mutant object by setting its ownership count to one and setting its
  165. // abandoned state to TRUE. Otherwise increment mutant ownership count.
  166. // If the result count is one, then remove the mutant object from the
  167. // thread's owned mutant list, set the owner thread to NULL, and attempt
  168. // to satisfy a Wait for the mutant object if the mutant object wait
  169. // list is not empty.
  170. //
  171. if (Abandoned != FALSE) {
  172. Mutant->Header.SignalState = 1;
  173. Mutant->Abandoned = TRUE;
  174. } else {
  175. //
  176. // If the Mutant object is not owned by the current thread, then
  177. // unlock the dispatcher data base and raise an exception. Otherwise
  178. // increment the ownership count.
  179. //
  180. if (Mutant->OwnerThread != Thread) {
  181. KiUnlockDispatcherDatabase(OldIrql);
  182. ExRaiseStatus(Mutant->Abandoned ?
  183. STATUS_ABANDONED : STATUS_MUTANT_NOT_OWNED);
  184. }
  185. Mutant->Header.SignalState += 1;
  186. }
  187. if (Mutant->Header.SignalState == 1) {
  188. if (OldState <= 0) {
  189. RemoveEntryList(&Mutant->MutantListEntry);
  190. LeaveCriticalRegion = Mutant->ApcDisable;
  191. }
  192. Mutant->OwnerThread = (PKTHREAD)NULL;
  193. if (IsListEmpty(&Mutant->Header.WaitListHead) == FALSE) {
  194. KiWaitTest(Mutant, Increment);
  195. }
  196. }
  197. //
  198. // If the value of the Wait argument is TRUE, then return to
  199. // caller with IRQL raised and the dispatcher database locked.
  200. // Else release the dispatcher database lock and lower IRQL to
  201. // its previous value.
  202. //
  203. if (Wait != FALSE) {
  204. Thread->WaitNext = Wait;
  205. Thread->WaitIrql = OldIrql;
  206. } else {
  207. KiUnlockDispatcherDatabase(OldIrql);
  208. }
  209. //
  210. // If kernel APC disable was biased when the mutant was acquired (mutex
  211. // variant), then leave critical region.
  212. //
  213. if (LeaveCriticalRegion != 0) {
  214. KeLeaveCriticalRegionThread(Thread);
  215. }
  216. //
  217. // Return previous signal state of mutant object.
  218. //
  219. return OldState;
  220. }
  221. LONG
  222. KeReleaseMutex (
  223. IN PRKMUTANT Mutex,
  224. IN BOOLEAN Wait
  225. )
  226. /*++
  227. Routine Description:
  228. This function releases a mutex object.
  229. N.B. Kernel mutex objects have been subsumed by mutant objects.
  230. Arguments:
  231. Mutex - Supplies a pointer to a dispatcher object of type mutex.
  232. Wait - Supplies a boolean value that signifies whether the call to
  233. KeReleaseMutex will be immediately followed by a call to one
  234. of the kernel Wait functions.
  235. Return Value:
  236. The previous signal state of the mutex object.
  237. --*/
  238. {
  239. ASSERT_MUTANT(Mutex);
  240. //
  241. // Release the specified mutex object with defaults for increment
  242. // and abandoned parameters.
  243. //
  244. return KeReleaseMutant(Mutex, 1, FALSE, Wait);
  245. }