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.

572 lines
13 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. eventobj.c
  5. Abstract:
  6. This module implements the kernel event objects. Functions are
  7. provided to initialize, pulse, read, reset, and set event objects.
  8. Author:
  9. David N. Cutler (davec) 27-Feb-1989
  10. Environment:
  11. Kernel mode only.
  12. Revision History:
  13. --*/
  14. #include "ki.h"
  15. #pragma alloc_text (PAGE, KeInitializeEventPair)
  16. #undef KeClearEvent
  17. //
  18. // The following assert macro is used to check that an input event is
  19. // really a kernel event and not something else, like deallocated pool.
  20. //
  21. #define ASSERT_EVENT(E) { \
  22. ASSERT((E)->Header.Type == NotificationEvent || \
  23. (E)->Header.Type == SynchronizationEvent); \
  24. }
  25. //
  26. // The following assert macro is used to check that an input event is
  27. // really a kernel event pair and not something else, like deallocated
  28. // pool.
  29. //
  30. #define ASSERT_EVENT_PAIR(E) { \
  31. ASSERT((E)->Type == EventPairObject); \
  32. }
  33. #undef KeInitializeEvent
  34. VOID
  35. KeInitializeEvent (
  36. IN PRKEVENT Event,
  37. IN EVENT_TYPE Type,
  38. IN BOOLEAN State
  39. )
  40. /*++
  41. Routine Description:
  42. This function initializes a kernel event object. The initial signal
  43. state of the object is set to the specified value.
  44. Arguments:
  45. Event - Supplies a pointer to a dispatcher object of type event.
  46. Type - Supplies the type of event; NotificationEvent or
  47. SynchronizationEvent.
  48. State - Supplies the initial signal state of the event object.
  49. Return Value:
  50. None.
  51. --*/
  52. {
  53. //
  54. // Initialize standard dispatcher object header, set initial signal
  55. // state of event object, and set the type of event object.
  56. //
  57. Event->Header.Type = (UCHAR)Type;
  58. Event->Header.Size = sizeof(KEVENT) / sizeof(LONG);
  59. Event->Header.SignalState = State;
  60. InitializeListHead(&Event->Header.WaitListHead);
  61. return;
  62. }
  63. VOID
  64. KeInitializeEventPair (
  65. IN PKEVENT_PAIR EventPair
  66. )
  67. /*++
  68. Routine Description:
  69. This function initializes a kernel event pair object. A kernel event
  70. pair object contains two separate synchronization event objects that
  71. are used to provide a fast interprocess synchronization capability.
  72. Arguments:
  73. EventPair - Supplies a pointer to a control object of type event pair.
  74. Return Value:
  75. None.
  76. --*/
  77. {
  78. //
  79. // Initialize the type and size of the event pair object and initialize
  80. // the two event object as synchronization events with an initial state
  81. // of FALSE.
  82. //
  83. EventPair->Type = (USHORT)EventPairObject;
  84. EventPair->Size = sizeof(KEVENT_PAIR);
  85. KeInitializeEvent(&EventPair->EventLow, SynchronizationEvent, FALSE);
  86. KeInitializeEvent(&EventPair->EventHigh, SynchronizationEvent, FALSE);
  87. return;
  88. }
  89. VOID
  90. KeClearEvent (
  91. IN PRKEVENT Event
  92. )
  93. /*++
  94. Routine Description:
  95. This function clears the signal state of an event object.
  96. Arguments:
  97. Event - Supplies a pointer to a dispatcher object of type event.
  98. Return Value:
  99. None.
  100. --*/
  101. {
  102. ASSERT_EVENT(Event);
  103. //
  104. // Clear signal state of event object.
  105. //
  106. Event->Header.SignalState = 0;
  107. return;
  108. }
  109. LONG
  110. KePulseEvent (
  111. IN PRKEVENT Event,
  112. IN KPRIORITY Increment,
  113. IN BOOLEAN Wait
  114. )
  115. /*++
  116. Routine Description:
  117. This function atomically sets the signal state of an event object to
  118. signaled, attempts to satisfy as many waits as possible, and then resets
  119. the signal state of the event object to Not-Signaled. The previous signal
  120. state of the event object is returned as the function value.
  121. Arguments:
  122. Event - Supplies a pointer to a dispatcher object of type event.
  123. Increment - Supplies the priority increment that is to be applied
  124. if setting the event causes a Wait to be satisfied.
  125. Wait - Supplies a boolean value that signifies whether the call to
  126. KePulseEvent will be immediately followed by a call to one of the
  127. kernel Wait functions.
  128. Return Value:
  129. The previous signal state of the event object.
  130. --*/
  131. {
  132. KIRQL OldIrql;
  133. LONG OldState;
  134. PRKTHREAD Thread;
  135. ASSERT_EVENT(Event);
  136. ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
  137. //
  138. // Raise IRQL to dispatcher level and lock dispatcher database.
  139. //
  140. KiLockDispatcherDatabase(&OldIrql);
  141. //
  142. // If the current state of the event object is Not-Signaled and
  143. // the wait queue is not empty, then set the state of the event
  144. // to Signaled, satisfy as many Waits as possible, and then reset
  145. // the state of the event to Not-Signaled.
  146. //
  147. OldState = Event->Header.SignalState;
  148. if ((OldState == 0) &&
  149. (IsListEmpty(&Event->Header.WaitListHead) == FALSE)) {
  150. Event->Header.SignalState = 1;
  151. KiWaitTest(Event, Increment);
  152. }
  153. Event->Header.SignalState = 0;
  154. //
  155. // If the value of the Wait argument is TRUE, then return to the
  156. // caller with IRQL raised and the dispatcher database locked. Else
  157. // release the dispatcher database lock and lower IRQL to the
  158. // previous value.
  159. //
  160. if (Wait != FALSE) {
  161. Thread = KeGetCurrentThread();
  162. Thread->WaitIrql = OldIrql;
  163. Thread->WaitNext = Wait;
  164. } else {
  165. KiUnlockDispatcherDatabase(OldIrql);
  166. }
  167. //
  168. // Return previous signal state of event object.
  169. //
  170. return OldState;
  171. }
  172. LONG
  173. KeReadStateEvent (
  174. IN PRKEVENT Event
  175. )
  176. /*++
  177. Routine Description:
  178. This function reads the current signal state of an event object.
  179. Arguments:
  180. Event - Supplies a pointer to a dispatcher object of type event.
  181. Return Value:
  182. The current signal state of the event object.
  183. --*/
  184. {
  185. ASSERT_EVENT(Event);
  186. //
  187. // Return current signal state of event object.
  188. //
  189. return Event->Header.SignalState;
  190. }
  191. LONG
  192. KeResetEvent (
  193. IN PRKEVENT Event
  194. )
  195. /*++
  196. Routine Description:
  197. This function resets the signal state of an event object to
  198. Not-Signaled. The previous state of the event object is returned
  199. as the function value.
  200. Arguments:
  201. Event - Supplies a pointer to a dispatcher object of type event.
  202. Return Value:
  203. The previous signal state of the event object.
  204. --*/
  205. {
  206. KIRQL OldIrql;
  207. LONG OldState;
  208. ASSERT_EVENT(Event);
  209. ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
  210. //
  211. // Raise IRQL to dispatcher level and lock dispatcher database.
  212. //
  213. KiLockDispatcherDatabase(&OldIrql);
  214. //
  215. // Capture the current signal state of event object and then reset
  216. // the state of the event object to Not-Signaled.
  217. //
  218. OldState = Event->Header.SignalState;
  219. Event->Header.SignalState = 0;
  220. //
  221. // Unlock the dispatcher database and lower IRQL to its previous
  222. // value.
  223. KiUnlockDispatcherDatabase(OldIrql);
  224. //
  225. // Return previous signal state of event object.
  226. //
  227. return OldState;
  228. }
  229. LONG
  230. KeSetEvent (
  231. IN PRKEVENT Event,
  232. IN KPRIORITY Increment,
  233. IN BOOLEAN Wait
  234. )
  235. /*++
  236. Routine Description:
  237. This function sets the signal state of an event object to signaled
  238. and attempts to satisfy as many waits as possible. The previous
  239. signal state of the event object is returned as the function value.
  240. Arguments:
  241. Event - Supplies a pointer to a dispatcher object of type event.
  242. Increment - Supplies the priority increment that is to be applied
  243. if setting the event causes a Wait to be satisfied.
  244. Wait - Supplies a boolean value that signifies whether the call to
  245. KePulseEvent will be immediately followed by a call to one of the
  246. kernel Wait functions.
  247. Return Value:
  248. The previous signal state of the event object.
  249. --*/
  250. {
  251. KIRQL OldIrql;
  252. LONG OldState;
  253. PRKTHREAD Thread;
  254. ASSERT_EVENT(Event);
  255. ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
  256. //
  257. // Collect call data.
  258. //
  259. #if defined(_COLLECT_SET_EVENT_CALLDATA_)
  260. RECORD_CALL_DATA(&KiSetEventCallData);
  261. #endif
  262. //
  263. // Raise IRQL to dispatcher level and lock dispatcher database.
  264. //
  265. KiLockDispatcherDatabase(&OldIrql);
  266. //
  267. // Capture the old state and set the new state to signaled.
  268. //
  269. // If the old state is not-signaled and the wait list is not empty,
  270. // then satisfy as many waits as possible.
  271. //
  272. OldState = Event->Header.SignalState;
  273. Event->Header.SignalState = 1;
  274. if ((OldState == 0) &&
  275. (IsListEmpty(&Event->Header.WaitListHead) == FALSE)) {
  276. if (Event->Header.Type == EventNotificationObject) {
  277. KiWaitTestWithoutSideEffects(Event, Increment);
  278. } else {
  279. KiWaitTestSynchronizationObject(Event, Increment);
  280. }
  281. }
  282. //
  283. // If the value of the Wait argument is TRUE, then return to the
  284. // caller with IRQL raised and the dispatcher database locked. Else
  285. // release the dispatcher database lock and lower IRQL to its
  286. // previous value.
  287. //
  288. if (Wait != FALSE) {
  289. Thread = KeGetCurrentThread();
  290. Thread->WaitNext = Wait;
  291. Thread->WaitIrql = OldIrql;
  292. } else {
  293. KiUnlockDispatcherDatabase(OldIrql);
  294. }
  295. //
  296. // Return previous signal state of event object.
  297. //
  298. return OldState;
  299. }
  300. VOID
  301. KeSetEventBoostPriority (
  302. IN PRKEVENT Event,
  303. IN PRKTHREAD *Thread OPTIONAL
  304. )
  305. /*++
  306. Routine Description:
  307. This function conditionally sets the signal state of an event object
  308. to signaled, and attempts to unwait the first waiter, and optionally
  309. returns the thread address of the unwaited thread.
  310. N.B. This function can only be called with synchronization events.
  311. Arguments:
  312. Event - Supplies a pointer to a dispatcher object of type event.
  313. Thread - Supplies an optional pointer to a variable that receives
  314. the address of the thread that is awakened.
  315. Return Value:
  316. None.
  317. --*/
  318. {
  319. PKTHREAD CurrentThread;
  320. KIRQL OldIrql;
  321. PKWAIT_BLOCK WaitBlock;
  322. PRKTHREAD WaitThread;
  323. ASSERT(Event->Header.Type == SynchronizationEvent);
  324. ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
  325. //
  326. // Raise IRQL to dispatcher level and lock dispatcher database.
  327. //
  328. CurrentThread = KeGetCurrentThread();
  329. KiLockDispatcherDatabase(&OldIrql);
  330. //
  331. // If the the wait list is not empty, then satisfy the wait of the
  332. // first thread in the wait list. Otherwise, set the signal state
  333. // of the event object.
  334. //
  335. if (IsListEmpty(&Event->Header.WaitListHead) != FALSE) {
  336. Event->Header.SignalState = 1;
  337. } else {
  338. //
  339. // Get the address of the first wait block in the event list.
  340. // If the wait is a wait any, then set the state of the event
  341. // to signaled and attempt to satisfy as many waits as possible.
  342. // Otherwise, unwait the first thread and apply an appropriate
  343. // priority boost to help prevent lock convoys from forming.
  344. //
  345. // N.B. Internal calls to this function for resource and fast
  346. // mutex boosts NEVER call with a possibility of having
  347. // a wait type of WaitAll. Calls from the NT service to
  348. // set event and boost priority are restricted as to the
  349. // event type, but not the wait type.
  350. //
  351. WaitBlock = CONTAINING_RECORD(Event->Header.WaitListHead.Flink,
  352. KWAIT_BLOCK,
  353. WaitListEntry);
  354. if (WaitBlock->WaitType == WaitAll) {
  355. Event->Header.SignalState = 1;
  356. KiWaitTestSynchronizationObject(Event, EVENT_INCREMENT);
  357. } else {
  358. //
  359. // Get the address of the waiting thread and return the address
  360. // if requested.
  361. //
  362. WaitThread = WaitBlock->Thread;
  363. if (ARGUMENT_PRESENT(Thread)) {
  364. *Thread = WaitThread;
  365. }
  366. //
  367. // Compute the new thread priority.
  368. //
  369. CurrentThread->Priority = KiComputeNewPriority(CurrentThread, 0);
  370. //
  371. // Unlink the thread from the appropriate wait queues and set
  372. // the wait completion status.
  373. //
  374. KiUnlinkThread(WaitThread, STATUS_SUCCESS);
  375. //
  376. // Set unwait priority adjustment parameters.
  377. //
  378. WaitThread->AdjustIncrement = CurrentThread->Priority;
  379. WaitThread->AdjustReason = (UCHAR)AdjustBoost;
  380. //
  381. // Ready the thread for execution.
  382. //
  383. KiReadyThread(WaitThread);
  384. }
  385. }
  386. //
  387. // Unlock dispatcher database lock and lower IRQL to its previous
  388. // value.
  389. //
  390. KiUnlockDispatcherDatabase(OldIrql);
  391. return;
  392. }