2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ke/wait.c
5 * PURPOSE: Manages waiting for Dispatcher Objects
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
10 /* INCLUDES ******************************************************************/
16 /* PRIVATE FUNCTIONS *********************************************************/
20 KiWaitTest(IN PVOID ObjectPointer
,
21 IN KPRIORITY Increment
)
23 PLIST_ENTRY WaitEntry
, WaitList
;
24 PKWAIT_BLOCK WaitBlock
;
26 PKMUTANT FirstObject
= ObjectPointer
;
29 /* Loop the Wait Entries */
30 WaitList
= &FirstObject
->Header
.WaitListHead
;
31 WaitEntry
= WaitList
->Flink
;
32 while ((FirstObject
->Header
.SignalState
> 0) && (WaitEntry
!= WaitList
))
34 /* Get the current wait block */
35 WaitBlock
= CONTAINING_RECORD(WaitEntry
, KWAIT_BLOCK
, WaitListEntry
);
36 WaitThread
= WaitBlock
->Thread
;
37 WaitStatus
= STATUS_KERNEL_APC
;
39 /* Check the current Wait Mode */
40 if (WaitBlock
->WaitType
== WaitAny
)
42 /* Easy case, satisfy only this wait */
43 WaitStatus
= (NTSTATUS
)WaitBlock
->WaitKey
;
44 KiSatisfyObjectWait(FirstObject
, WaitThread
);
47 /* Now do the rest of the unwait */
48 KiUnwaitThread(WaitThread
, WaitStatus
, Increment
);
49 WaitEntry
= WaitList
->Flink
;
55 KiUnlinkThread(IN PKTHREAD Thread
,
56 IN LONG_PTR WaitStatus
)
58 PKWAIT_BLOCK WaitBlock
;
61 /* Update wait status */
62 Thread
->WaitStatus
|= WaitStatus
;
64 /* Remove the Wait Blocks from the list */
65 WaitBlock
= Thread
->WaitBlockList
;
69 RemoveEntryList(&WaitBlock
->WaitListEntry
);
71 /* Go to the next one */
72 WaitBlock
= WaitBlock
->NextWaitBlock
;
73 } while (WaitBlock
!= Thread
->WaitBlockList
);
75 /* Remove the thread from the wait list! */
76 if (Thread
->WaitListEntry
.Flink
) RemoveEntryList(&Thread
->WaitListEntry
);
78 /* Check if there's a Thread Timer */
79 Timer
= &Thread
->Timer
;
80 if (Timer
->Header
.Inserted
) KxRemoveTreeTimer(Timer
);
82 /* Increment the Queue's active threads */
83 if (Thread
->Queue
) Thread
->Queue
->CurrentCount
++;
86 /* Must be called with the dispatcher lock held */
89 KiUnwaitThread(IN PKTHREAD Thread
,
90 IN LONG_PTR WaitStatus
,
91 IN KPRIORITY Increment
)
93 /* Unlink the thread */
94 KiUnlinkThread(Thread
, WaitStatus
);
96 /* Tell the scheduler do to the increment when it readies the thread */
97 ASSERT(Increment
>= 0);
98 Thread
->AdjustIncrement
= (SCHAR
)Increment
;
99 Thread
->AdjustReason
= AdjustUnwait
;
101 /* Reschedule the Thread */
102 KiReadyThread(Thread
);
107 KiAcquireFastMutex(IN PFAST_MUTEX FastMutex
)
109 /* Increase contention count */
110 FastMutex
->Contention
++;
112 /* Wait for the event */
113 KeWaitForSingleObject(&FastMutex
->Event
,
122 KiAcquireGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex
)
124 ULONG BitsToRemove
, BitsToAdd
;
125 LONG OldValue
, NewValue
;
127 /* We depend on these bits being just right */
128 C_ASSERT((GM_LOCK_WAITER_WOKEN
* 2) == GM_LOCK_WAITER_INC
);
130 /* Increase the contention count */
131 GuardedMutex
->Contention
++;
133 /* Start by unlocking the Guarded Mutex */
134 BitsToRemove
= GM_LOCK_BIT
;
135 BitsToAdd
= GM_LOCK_WAITER_INC
;
137 /* Start change loop */
140 /* Loop sanity checks */
141 ASSERT((BitsToRemove
== GM_LOCK_BIT
) ||
142 (BitsToRemove
== (GM_LOCK_BIT
| GM_LOCK_WAITER_WOKEN
)));
143 ASSERT((BitsToAdd
== GM_LOCK_WAITER_INC
) ||
144 (BitsToAdd
== GM_LOCK_WAITER_WOKEN
));
146 /* Get the Count Bits */
147 OldValue
= GuardedMutex
->Count
;
149 /* Start internal bit change loop */
152 /* Check if the Guarded Mutex is locked */
153 if (OldValue
& GM_LOCK_BIT
)
156 ASSERT((BitsToRemove
== GM_LOCK_BIT
) ||
157 ((OldValue
& GM_LOCK_WAITER_WOKEN
) != 0));
159 /* Unlock it by removing the Lock Bit */
160 NewValue
= OldValue
^ BitsToRemove
;
161 NewValue
= InterlockedCompareExchange(&GuardedMutex
->Count
,
164 if (NewValue
== OldValue
) return;
168 /* The Guarded Mutex isn't locked, so simply set the bits */
169 NewValue
= OldValue
+ BitsToAdd
;
170 NewValue
= InterlockedCompareExchange(&GuardedMutex
->Count
,
173 if (NewValue
== OldValue
) break;
176 /* Old value changed, loop again */
180 /* Now we have to wait for it */
181 KeWaitForGate(&GuardedMutex
->Gate
, WrGuardedMutex
, KernelMode
);
182 ASSERT((GuardedMutex
->Count
& GM_LOCK_WAITER_WOKEN
) != 0);
184 /* Ok, the wait is done, so set the new bits */
185 BitsToRemove
= GM_LOCK_BIT
| GM_LOCK_WAITER_WOKEN
;
186 BitsToAdd
= GM_LOCK_WAITER_WOKEN
;
191 // This routine exits the dispatcher after a compatible operation and
192 // swaps the context to the next scheduled thread on the current CPU if
195 // It does NOT attempt to scan for a new thread to schedule.
199 KiExitDispatcher(IN KIRQL OldIrql
)
201 PKPRCB Prcb
= KeGetCurrentPrcb();
202 PKTHREAD Thread
, NextThread
;
205 /* Make sure we're at synchronization level */
206 ASSERT(KeGetCurrentIrql() == SYNCH_LEVEL
);
208 /* Check if we have deferred threads */
209 KiCheckDeferredReadyList(Prcb
);
211 /* Check if we were called at dispatcher level or higher */
212 if (OldIrql
>= DISPATCH_LEVEL
)
214 /* Check if we have a thread to schedule, and that no DPC is active */
215 if ((Prcb
->NextThread
) && !(Prcb
->DpcRoutineActive
))
217 /* Request DPC interrupt */
218 HalRequestSoftwareInterrupt(DISPATCH_LEVEL
);
221 /* Lower IRQL and exit */
225 /* Make sure there's a new thread scheduled */
226 if (!Prcb
->NextThread
) goto Quickie
;
229 KiAcquirePrcbLock(Prcb
);
231 /* Get the next and current threads now */
232 NextThread
= Prcb
->NextThread
;
233 Thread
= Prcb
->CurrentThread
;
235 /* Set current thread's swap busy to true */
236 KiSetThreadSwapBusy(Thread
);
238 /* Switch threads in PRCB */
239 Prcb
->NextThread
= NULL
;
240 Prcb
->CurrentThread
= NextThread
;
242 /* Set thread to running */
243 NextThread
->State
= Running
;
245 /* Queue it on the ready lists */
246 KxQueueReadyThread(Thread
, Prcb
);
249 Thread
->WaitIrql
= OldIrql
;
251 /* Swap threads and check if APCs were pending */
252 PendingApc
= KiSwapContext(OldIrql
, Thread
);
255 /* Lower only to APC */
256 KeLowerIrql(APC_LEVEL
);
259 KiDeliverApc(KernelMode
, NULL
, NULL
);
260 ASSERT(OldIrql
== PASSIVE_LEVEL
);
263 /* Lower IRQl back */
265 KeLowerIrql(OldIrql
);
268 /* PUBLIC FUNCTIONS **********************************************************/
275 KeDelayExecutionThread(IN KPROCESSOR_MODE WaitMode
,
276 IN BOOLEAN Alertable
,
277 IN PLARGE_INTEGER Interval OPTIONAL
)
280 PKWAIT_BLOCK TimerBlock
;
281 PKTHREAD Thread
= KeGetCurrentThread();
284 PLARGE_INTEGER OriginalDueTime
;
285 LARGE_INTEGER DueTime
, NewDueTime
, InterruptTime
;
288 if (Thread
->WaitNext
)
289 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
291 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
293 /* If this is a user-mode wait of 0 seconds, yield execution */
294 if (!(Interval
->QuadPart
) && (WaitMode
!= KernelMode
))
296 /* Make sure the wait isn't alertable or interrupting an APC */
297 if (!(Alertable
) && !(Thread
->ApcState
.UserApcPending
))
299 /* Yield execution */
300 return NtYieldExecution();
304 /* Setup the original time and timer/wait blocks */
305 OriginalDueTime
= Interval
;
306 Timer
= &Thread
->Timer
;
307 TimerBlock
= &Thread
->WaitBlock
[TIMER_WAIT_BLOCK
];
309 /* Check if the lock is already held */
310 if (!Thread
->WaitNext
) goto WaitStart
;
312 /* Otherwise, we already have the lock, so initialize the wait */
313 Thread
->WaitNext
= FALSE
;
316 /* Start wait loop */
319 /* Disable pre-emption */
320 Thread
->Preempted
= FALSE
;
322 /* Check if a kernel APC is pending and we're below APC_LEVEL */
323 if ((Thread
->ApcState
.KernelApcPending
) && !(Thread
->SpecialApcDisable
) &&
324 (Thread
->WaitIrql
< APC_LEVEL
))
326 /* Unlock the dispatcher */
327 KiReleaseDispatcherLock(Thread
->WaitIrql
);
331 /* Check if we have to bail out due to an alerted state */
332 WaitStatus
= KiCheckAlertability(Thread
, Alertable
, WaitMode
);
333 if (WaitStatus
!= STATUS_WAIT_0
) break;
335 /* Check if the timer expired */
336 InterruptTime
.QuadPart
= KeQueryInterruptTime();
337 if ((ULONGLONG
)InterruptTime
.QuadPart
>= Timer
->DueTime
.QuadPart
)
339 /* It did, so we don't need to wait */
343 /* It didn't, so activate it */
344 Timer
->Header
.Inserted
= TRUE
;
346 /* Handle Kernel Queues */
347 if (Thread
->Queue
) KiActivateWaiterQueue(Thread
->Queue
);
349 /* Setup the wait information */
350 Thread
->State
= Waiting
;
352 /* Add the thread to the wait list */
353 KiAddThreadToWaitList(Thread
, Swappable
);
355 /* Insert the timer and swap the thread */
356 ASSERT(Thread
->WaitIrql
<= DISPATCH_LEVEL
);
357 KiSetThreadSwapBusy(Thread
);
358 KxInsertTimer(Timer
, Hand
);
359 WaitStatus
= (NTSTATUS
)KiSwapThread(Thread
, KeGetCurrentPrcb());
361 /* Check if were swapped ok */
362 if (WaitStatus
!= STATUS_KERNEL_APC
)
364 /* This is a good thing */
365 if (WaitStatus
== STATUS_TIMEOUT
) WaitStatus
= STATUS_SUCCESS
;
371 /* Recalculate due times */
372 Interval
= KiRecalculateDueTime(OriginalDueTime
,
378 /* Setup a new wait */
379 Thread
->WaitIrql
= KeRaiseIrqlToSynchLevel();
381 KiAcquireDispatcherLockAtDpcLevel();
385 KiReleaseDispatcherLock(Thread
->WaitIrql
);
389 /* There was nothing to wait for. Did we have a wait interval? */
390 if (!Interval
->QuadPart
)
392 /* Unlock the dispatcher and do a yield */
393 KiReleaseDispatcherLock(Thread
->WaitIrql
);
394 return NtYieldExecution();
397 /* Unlock the dispatcher and adjust the quantum for a no-wait */
398 KiReleaseDispatcherLockFromDpcLevel();
399 KiAdjustQuantumThread(Thread
);
400 return STATUS_SUCCESS
;
408 KeWaitForSingleObject(IN PVOID Object
,
409 IN KWAIT_REASON WaitReason
,
410 IN KPROCESSOR_MODE WaitMode
,
411 IN BOOLEAN Alertable
,
412 IN PLARGE_INTEGER Timeout OPTIONAL
)
414 PKTHREAD Thread
= KeGetCurrentThread();
415 PKMUTANT CurrentObject
= (PKMUTANT
)Object
;
416 PKWAIT_BLOCK WaitBlock
= &Thread
->WaitBlock
[0];
417 PKWAIT_BLOCK TimerBlock
= &Thread
->WaitBlock
[TIMER_WAIT_BLOCK
];
418 PKTIMER Timer
= &Thread
->Timer
;
421 LARGE_INTEGER DueTime
= {{0}}, NewDueTime
, InterruptTime
;
422 PLARGE_INTEGER OriginalDueTime
= Timeout
;
425 if (Thread
->WaitNext
)
426 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
428 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL
||
429 (KeGetCurrentIrql() == DISPATCH_LEVEL
&&
430 Timeout
&& Timeout
->QuadPart
== 0));
432 /* Check if the lock is already held */
433 if (!Thread
->WaitNext
) goto WaitStart
;
435 /* Otherwise, we already have the lock, so initialize the wait */
436 Thread
->WaitNext
= FALSE
;
437 KxSingleThreadWait();
439 /* Start wait loop */
442 /* Disable pre-emption */
443 Thread
->Preempted
= FALSE
;
445 /* Check if a kernel APC is pending and we're below APC_LEVEL */
446 if ((Thread
->ApcState
.KernelApcPending
) && !(Thread
->SpecialApcDisable
) &&
447 (Thread
->WaitIrql
< APC_LEVEL
))
449 /* Unlock the dispatcher */
450 KiReleaseDispatcherLock(Thread
->WaitIrql
);
455 ASSERT(CurrentObject
->Header
.Type
!= QueueObject
);
457 /* Check if it's a mutant */
458 if (CurrentObject
->Header
.Type
== MutantObject
)
460 /* Check its signal state or if we own it */
461 if ((CurrentObject
->Header
.SignalState
> 0) ||
462 (Thread
== CurrentObject
->OwnerThread
))
464 /* Just unwait this guy and exit */
465 if (CurrentObject
->Header
.SignalState
!= (LONG
)MINLONG
)
467 /* It has a normal signal state. Unwait and return */
468 KiSatisfyMutantWait(CurrentObject
, Thread
);
469 WaitStatus
= (NTSTATUS
)Thread
->WaitStatus
;
474 /* Raise an exception */
475 KiReleaseDispatcherLock(Thread
->WaitIrql
);
476 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED
);
480 else if (CurrentObject
->Header
.SignalState
> 0)
482 /* Another satisfied object */
483 KiSatisfyNonMutantWait(CurrentObject
);
484 WaitStatus
= STATUS_WAIT_0
;
488 /* Make sure we can satisfy the Alertable request */
489 WaitStatus
= KiCheckAlertability(Thread
, Alertable
, WaitMode
);
490 if (WaitStatus
!= STATUS_WAIT_0
) break;
492 /* Enable the Timeout Timer if there was any specified */
495 /* Check if the timer expired */
496 InterruptTime
.QuadPart
= KeQueryInterruptTime();
497 if ((ULONGLONG
)InterruptTime
.QuadPart
>=
498 Timer
->DueTime
.QuadPart
)
500 /* It did, so we don't need to wait */
501 WaitStatus
= STATUS_TIMEOUT
;
505 /* It didn't, so activate it */
506 Timer
->Header
.Inserted
= TRUE
;
509 /* Link the Object to this Wait Block */
510 InsertTailList(&CurrentObject
->Header
.WaitListHead
,
511 &WaitBlock
->WaitListEntry
);
513 /* Handle Kernel Queues */
514 if (Thread
->Queue
) KiActivateWaiterQueue(Thread
->Queue
);
516 /* Setup the wait information */
517 Thread
->State
= Waiting
;
519 /* Add the thread to the wait list */
520 KiAddThreadToWaitList(Thread
, Swappable
);
522 /* Activate thread swap */
523 ASSERT(Thread
->WaitIrql
<= DISPATCH_LEVEL
);
524 KiSetThreadSwapBusy(Thread
);
526 /* Check if we have a timer */
530 KxInsertTimer(Timer
, Hand
);
534 /* Otherwise, unlock the dispatcher */
535 KiReleaseDispatcherLockFromDpcLevel();
538 /* Do the actual swap */
539 WaitStatus
= (NTSTATUS
)KiSwapThread(Thread
, KeGetCurrentPrcb());
541 /* Check if we were executing an APC */
542 if (WaitStatus
!= STATUS_KERNEL_APC
) return WaitStatus
;
544 /* Check if we had a timeout */
547 /* Recalculate due times */
548 Timeout
= KiRecalculateDueTime(OriginalDueTime
,
554 /* Setup a new wait */
555 Thread
->WaitIrql
= KeRaiseIrqlToSynchLevel();
556 KxSingleThreadWait();
557 KiAcquireDispatcherLockAtDpcLevel();
561 KiReleaseDispatcherLock(Thread
->WaitIrql
);
565 /* Release dispatcher lock but maintain high IRQL */
566 KiReleaseDispatcherLockFromDpcLevel();
568 /* Adjust the Quantum and return the wait status */
569 KiAdjustQuantumThread(Thread
);
578 KeWaitForMultipleObjects(IN ULONG Count
,
580 IN WAIT_TYPE WaitType
,
581 IN KWAIT_REASON WaitReason
,
582 IN KPROCESSOR_MODE WaitMode
,
583 IN BOOLEAN Alertable
,
584 IN PLARGE_INTEGER Timeout OPTIONAL
,
585 OUT PKWAIT_BLOCK WaitBlockArray OPTIONAL
)
587 PKMUTANT CurrentObject
;
588 PKWAIT_BLOCK WaitBlock
;
589 PKTHREAD Thread
= KeGetCurrentThread();
590 PKWAIT_BLOCK TimerBlock
= &Thread
->WaitBlock
[TIMER_WAIT_BLOCK
];
591 PKTIMER Timer
= &Thread
->Timer
;
592 NTSTATUS WaitStatus
= STATUS_SUCCESS
;
594 PLARGE_INTEGER OriginalDueTime
= Timeout
;
595 LARGE_INTEGER DueTime
= {{0}}, NewDueTime
, InterruptTime
;
596 ULONG Index
, Hand
= 0;
598 if (Thread
->WaitNext
)
599 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
600 else if (KeGetCurrentIrql() == DISPATCH_LEVEL
&&
601 (!Timeout
|| Timeout
->QuadPart
!= 0))
603 /* HACK: tcpip is broken and waits with spinlocks acquired (bug #7129) */
604 DPRINT("%s called at DISPATCH_LEVEL with non-zero timeout!\n",
608 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
610 /* Make sure the Wait Count is valid */
613 /* Check in regards to the Thread Object Limit */
614 if (Count
> THREAD_WAIT_OBJECTS
)
617 KeBugCheck(MAXIMUM_WAIT_OBJECTS_EXCEEDED
);
620 /* Use the Thread's Wait Block */
621 WaitBlockArray
= &Thread
->WaitBlock
[0];
625 /* Using our own Block Array, so check with the System Object Limit */
626 if (Count
> MAXIMUM_WAIT_OBJECTS
)
629 KeBugCheck(MAXIMUM_WAIT_OBJECTS_EXCEEDED
);
636 /* Check if the lock is already held */
637 if (!Thread
->WaitNext
) goto WaitStart
;
639 /* Otherwise, we already have the lock, so initialize the wait */
640 Thread
->WaitNext
= FALSE
;
641 /* Note that KxMultiThreadWait is a macro, defined in ke_x.h, that */
642 /* uses (and modifies some of) the following local */
644 /* Thread, Index, WaitBlock, Timer, Timeout, Hand and Swappable. */
645 /* If it looks like this code doesn't actually wait for any objects */
646 /* at all, it's because the setup is done by that macro. */
649 /* Start wait loop */
652 /* Disable pre-emption */
653 Thread
->Preempted
= FALSE
;
655 /* Check if a kernel APC is pending and we're below APC_LEVEL */
656 if ((Thread
->ApcState
.KernelApcPending
) && !(Thread
->SpecialApcDisable
) &&
657 (Thread
->WaitIrql
< APC_LEVEL
))
659 /* Unlock the dispatcher */
660 KiReleaseDispatcherLock(Thread
->WaitIrql
);
664 /* Check what kind of wait this is */
666 if (WaitType
== WaitAny
)
671 /* Get the Current Object */
672 CurrentObject
= (PKMUTANT
)Object
[Index
];
673 ASSERT(CurrentObject
->Header
.Type
!= QueueObject
);
675 /* Check if the Object is a mutant */
676 if (CurrentObject
->Header
.Type
== MutantObject
)
678 /* Check if it's signaled */
679 if ((CurrentObject
->Header
.SignalState
> 0) ||
680 (Thread
== CurrentObject
->OwnerThread
))
682 /* This is a Wait Any, so unwait this and exit */
683 if (CurrentObject
->Header
.SignalState
!=
686 /* Normal signal state, unwait it and return */
687 KiSatisfyMutantWait(CurrentObject
, Thread
);
688 WaitStatus
= (NTSTATUS
)Thread
->WaitStatus
| Index
;
693 /* Raise an exception (see wasm.ru) */
694 KiReleaseDispatcherLock(Thread
->WaitIrql
);
695 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED
);
699 else if (CurrentObject
->Header
.SignalState
> 0)
701 /* Another signaled object, unwait and return */
702 KiSatisfyNonMutantWait(CurrentObject
);
707 /* Go to the next block */
709 } while (Index
< Count
);
716 /* Get the Current Object */
717 CurrentObject
= (PKMUTANT
)Object
[Index
];
718 ASSERT(CurrentObject
->Header
.Type
!= QueueObject
);
720 /* Check if we're dealing with a mutant again */
721 if (CurrentObject
->Header
.Type
== MutantObject
)
723 /* Check if it has an invalid count */
724 if ((Thread
== CurrentObject
->OwnerThread
) &&
725 (CurrentObject
->Header
.SignalState
== (LONG
)MINLONG
))
727 /* Raise an exception */
728 KiReleaseDispatcherLock(Thread
->WaitIrql
);
729 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED
);
731 else if ((CurrentObject
->Header
.SignalState
<= 0) &&
732 (Thread
!= CurrentObject
->OwnerThread
))
734 /* We don't own it, can't satisfy the wait */
738 else if (CurrentObject
->Header
.SignalState
<= 0)
740 /* Not signaled, can't satisfy */
744 /* Go to the next block */
746 } while (Index
< Count
);
748 /* Check if we've went through all the objects */
751 /* Loop wait blocks */
752 WaitBlock
= WaitBlockArray
;
755 /* Get the object and satisfy it */
756 CurrentObject
= (PKMUTANT
)WaitBlock
->Object
;
757 KiSatisfyObjectWait(CurrentObject
, Thread
);
759 /* Go to the next block */
760 WaitBlock
= WaitBlock
->NextWaitBlock
;
761 } while(WaitBlock
!= WaitBlockArray
);
763 /* Set the wait status and get out */
764 WaitStatus
= (NTSTATUS
)Thread
->WaitStatus
;
769 /* Make sure we can satisfy the Alertable request */
770 WaitStatus
= KiCheckAlertability(Thread
, Alertable
, WaitMode
);
771 if (WaitStatus
!= STATUS_WAIT_0
) break;
773 /* Enable the Timeout Timer if there was any specified */
776 /* Check if the timer expired */
777 InterruptTime
.QuadPart
= KeQueryInterruptTime();
778 if ((ULONGLONG
)InterruptTime
.QuadPart
>=
779 Timer
->DueTime
.QuadPart
)
781 /* It did, so we don't need to wait */
782 WaitStatus
= STATUS_TIMEOUT
;
786 /* It didn't, so activate it */
787 Timer
->Header
.Inserted
= TRUE
;
789 /* Link the wait blocks */
790 WaitBlock
->NextWaitBlock
= TimerBlock
;
793 /* Insert into Object's Wait List*/
794 WaitBlock
= WaitBlockArray
;
797 /* Get the Current Object */
798 CurrentObject
= WaitBlock
->Object
;
800 /* Link the Object to this Wait Block */
801 InsertTailList(&CurrentObject
->Header
.WaitListHead
,
802 &WaitBlock
->WaitListEntry
);
804 /* Move to the next Wait Block */
805 WaitBlock
= WaitBlock
->NextWaitBlock
;
806 } while (WaitBlock
!= WaitBlockArray
);
808 /* Handle Kernel Queues */
809 if (Thread
->Queue
) KiActivateWaiterQueue(Thread
->Queue
);
811 /* Setup the wait information */
812 Thread
->State
= Waiting
;
814 /* Add the thread to the wait list */
815 KiAddThreadToWaitList(Thread
, Swappable
);
817 /* Activate thread swap */
818 ASSERT(Thread
->WaitIrql
<= DISPATCH_LEVEL
);
819 KiSetThreadSwapBusy(Thread
);
821 /* Check if we have a timer */
825 KxInsertTimer(Timer
, Hand
);
829 /* Otherwise, unlock the dispatcher */
830 KiReleaseDispatcherLockFromDpcLevel();
833 /* Swap the thread */
834 WaitStatus
= (NTSTATUS
)KiSwapThread(Thread
, KeGetCurrentPrcb());
836 /* Check if we were executing an APC */
837 if (WaitStatus
!= STATUS_KERNEL_APC
) return WaitStatus
;
839 /* Check if we had a timeout */
842 /* Recalculate due times */
843 Timeout
= KiRecalculateDueTime(OriginalDueTime
,
850 /* Setup a new wait */
851 Thread
->WaitIrql
= KeRaiseIrqlToSynchLevel();
853 KiAcquireDispatcherLockAtDpcLevel();
857 KiReleaseDispatcherLock(Thread
->WaitIrql
);
861 /* Release dispatcher lock but maintain high IRQL */
862 KiReleaseDispatcherLockFromDpcLevel();
864 /* Adjust the Quantum and return the wait status */
865 KiAdjustQuantumThread(Thread
);
871 NtDelayExecution(IN BOOLEAN Alertable
,
872 IN PLARGE_INTEGER DelayInterval
)
874 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
875 LARGE_INTEGER SafeInterval
;
878 /* Check the previous mode */
879 if (PreviousMode
!= KernelMode
)
881 /* Enter SEH for probing */
884 /* Probe and capture the time out */
885 SafeInterval
= ProbeForReadLargeInteger(DelayInterval
);
886 DelayInterval
= &SafeInterval
;
888 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
890 /* Return the exception code */
891 _SEH2_YIELD(return _SEH2_GetExceptionCode());
896 /* Call the Kernel Function */
897 Status
= KeDelayExecutionThread(PreviousMode
,