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 NTSTATUS 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(Thread
, NextThread
);
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 this is a user-mode wait of 0 seconds, yield execution */
289 if (!(Interval
->QuadPart
) && (WaitMode
!= KernelMode
))
291 /* Make sure the wait isn't alertable or interrupting an APC */
292 if (!(Alertable
) && !(Thread
->ApcState
.UserApcPending
))
294 /* Yield execution */
299 /* Setup the original time and timer/wait blocks */
300 OriginalDueTime
= Interval
;
301 Timer
= &Thread
->Timer
;
302 TimerBlock
= &Thread
->WaitBlock
[TIMER_WAIT_BLOCK
];
304 /* Check if the lock is already held */
305 if (!Thread
->WaitNext
) goto WaitStart
;
307 /* Otherwise, we already have the lock, so initialize the wait */
308 Thread
->WaitNext
= FALSE
;
311 /* Start wait loop */
314 /* Disable pre-emption */
315 Thread
->Preempted
= FALSE
;
317 /* Check if a kernel APC is pending and we're below APC_LEVEL */
318 if ((Thread
->ApcState
.KernelApcPending
) && !(Thread
->SpecialApcDisable
) &&
319 (Thread
->WaitIrql
< APC_LEVEL
))
321 /* Unlock the dispatcher */
322 KiReleaseDispatcherLock(Thread
->WaitIrql
);
326 /* Check if we have to bail out due to an alerted state */
327 WaitStatus
= KiCheckAlertability(Thread
, Alertable
, WaitMode
);
328 if (WaitStatus
!= STATUS_WAIT_0
) break;
330 /* Check if the timer expired */
331 InterruptTime
.QuadPart
= KeQueryInterruptTime();
332 if ((ULONGLONG
)InterruptTime
.QuadPart
>= Timer
->DueTime
.QuadPart
)
334 /* It did, so we don't need to wait */
338 /* It didn't, so activate it */
339 Timer
->Header
.Inserted
= TRUE
;
341 /* Handle Kernel Queues */
342 if (Thread
->Queue
) KiActivateWaiterQueue(Thread
->Queue
);
344 /* Setup the wait information */
345 Thread
->State
= Waiting
;
347 /* Add the thread to the wait list */
348 KiAddThreadToWaitList(Thread
, Swappable
);
350 /* Insert the timer and swap the thread */
351 ASSERT(Thread
->WaitIrql
<= DISPATCH_LEVEL
);
352 KiSetThreadSwapBusy(Thread
);
353 KxInsertTimer(Timer
, Hand
);
354 WaitStatus
= KiSwapThread(Thread
, KeGetCurrentPrcb());
356 /* Check if were swapped ok */
357 if (WaitStatus
!= STATUS_KERNEL_APC
)
359 /* This is a good thing */
360 if (WaitStatus
== STATUS_TIMEOUT
) WaitStatus
= STATUS_SUCCESS
;
366 /* Recalculate due times */
367 Interval
= KiRecalculateDueTime(OriginalDueTime
,
373 /* Setup a new wait */
374 Thread
->WaitIrql
= KeRaiseIrqlToSynchLevel();
376 KiAcquireDispatcherLockAtDpcLevel();
380 KiReleaseDispatcherLock(Thread
->WaitIrql
);
384 /* There was nothing to wait for. Did we have a wait interval? */
385 if (!Interval
->QuadPart
)
387 /* Unlock the dispatcher and do a yield */
388 KiReleaseDispatcherLock(Thread
->WaitIrql
);
389 return NtYieldExecution();
392 /* Unlock the dispatcher and adjust the quantum for a no-wait */
393 KiReleaseDispatcherLockFromDpcLevel();
394 KiAdjustQuantumThread(Thread
);
395 return STATUS_SUCCESS
;
403 KeWaitForSingleObject(IN PVOID Object
,
404 IN KWAIT_REASON WaitReason
,
405 IN KPROCESSOR_MODE WaitMode
,
406 IN BOOLEAN Alertable
,
407 IN PLARGE_INTEGER Timeout OPTIONAL
)
409 PKTHREAD Thread
= KeGetCurrentThread();
410 PKMUTANT CurrentObject
= (PKMUTANT
)Object
;
411 PKWAIT_BLOCK WaitBlock
= &Thread
->WaitBlock
[0];
412 PKWAIT_BLOCK TimerBlock
= &Thread
->WaitBlock
[TIMER_WAIT_BLOCK
];
413 PKTIMER Timer
= &Thread
->Timer
;
416 LARGE_INTEGER DueTime
, NewDueTime
, InterruptTime
;
417 PLARGE_INTEGER OriginalDueTime
= Timeout
;
420 /* Check if the lock is already held */
421 if (!Thread
->WaitNext
) goto WaitStart
;
423 /* Otherwise, we already have the lock, so initialize the wait */
424 Thread
->WaitNext
= FALSE
;
425 KxSingleThreadWait();
427 /* Start wait loop */
430 /* Disable pre-emption */
431 Thread
->Preempted
= FALSE
;
433 /* Check if a kernel APC is pending and we're below APC_LEVEL */
434 if ((Thread
->ApcState
.KernelApcPending
) && !(Thread
->SpecialApcDisable
) &&
435 (Thread
->WaitIrql
< APC_LEVEL
))
437 /* Unlock the dispatcher */
438 KiReleaseDispatcherLock(Thread
->WaitIrql
);
443 ASSERT(CurrentObject
->Header
.Type
!= QueueObject
);
445 /* Check if it's a mutant */
446 if (CurrentObject
->Header
.Type
== MutantObject
)
448 /* Check its signal state or if we own it */
449 if ((CurrentObject
->Header
.SignalState
> 0) ||
450 (Thread
== CurrentObject
->OwnerThread
))
452 /* Just unwait this guy and exit */
453 if (CurrentObject
->Header
.SignalState
!= (LONG
)MINLONG
)
455 /* It has a normal signal state. Unwait and return */
456 KiSatisfyMutantWait(CurrentObject
, Thread
);
457 WaitStatus
= Thread
->WaitStatus
;
462 /* Raise an exception */
463 KiReleaseDispatcherLock(Thread
->WaitIrql
);
464 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED
);
468 else if (CurrentObject
->Header
.SignalState
> 0)
470 /* Another satisfied object */
471 KiSatisfyNonMutantWait(CurrentObject
);
472 WaitStatus
= STATUS_WAIT_0
;
476 /* Make sure we can satisfy the Alertable request */
477 WaitStatus
= KiCheckAlertability(Thread
, Alertable
, WaitMode
);
478 if (WaitStatus
!= STATUS_WAIT_0
) break;
480 /* Enable the Timeout Timer if there was any specified */
483 /* Check if the timer expired */
484 InterruptTime
.QuadPart
= KeQueryInterruptTime();
485 if ((ULONGLONG
)InterruptTime
.QuadPart
>=
486 Timer
->DueTime
.QuadPart
)
488 /* It did, so we don't need to wait */
489 WaitStatus
= STATUS_TIMEOUT
;
493 /* It didn't, so activate it */
494 Timer
->Header
.Inserted
= TRUE
;
497 /* Link the Object to this Wait Block */
498 InsertTailList(&CurrentObject
->Header
.WaitListHead
,
499 &WaitBlock
->WaitListEntry
);
501 /* Handle Kernel Queues */
502 if (Thread
->Queue
) KiActivateWaiterQueue(Thread
->Queue
);
504 /* Setup the wait information */
505 Thread
->State
= Waiting
;
507 /* Add the thread to the wait list */
508 KiAddThreadToWaitList(Thread
, Swappable
);
510 /* Activate thread swap */
511 ASSERT(Thread
->WaitIrql
<= DISPATCH_LEVEL
);
512 KiSetThreadSwapBusy(Thread
);
514 /* Check if we have a timer */
518 KxInsertTimer(Timer
, Hand
);
522 /* Otherwise, unlock the dispatcher */
523 KiReleaseDispatcherLockFromDpcLevel();
526 /* Do the actual swap */
527 WaitStatus
= KiSwapThread(Thread
, KeGetCurrentPrcb());
529 /* Check if we were executing an APC */
530 if (WaitStatus
!= STATUS_KERNEL_APC
) return WaitStatus
;
532 /* Check if we had a timeout */
535 /* Recalculate due times */
536 Timeout
= KiRecalculateDueTime(OriginalDueTime
,
542 /* Setup a new wait */
543 Thread
->WaitIrql
= KeRaiseIrqlToSynchLevel();
544 KxSingleThreadWait();
545 KiAcquireDispatcherLockAtDpcLevel();
549 KiReleaseDispatcherLock(Thread
->WaitIrql
);
553 /* Release dispatcher lock but maintain high IRQL */
554 KiReleaseDispatcherLockFromDpcLevel();
556 /* Adjust the Quantum and return the wait status */
557 KiAdjustQuantumThread(Thread
);
566 KeWaitForMultipleObjects(IN ULONG Count
,
568 IN WAIT_TYPE WaitType
,
569 IN KWAIT_REASON WaitReason
,
570 IN KPROCESSOR_MODE WaitMode
,
571 IN BOOLEAN Alertable
,
572 IN PLARGE_INTEGER Timeout OPTIONAL
,
573 OUT PKWAIT_BLOCK WaitBlockArray OPTIONAL
)
575 PKMUTANT CurrentObject
;
576 PKWAIT_BLOCK WaitBlock
;
577 PKTHREAD Thread
= KeGetCurrentThread();
578 PKWAIT_BLOCK TimerBlock
= &Thread
->WaitBlock
[TIMER_WAIT_BLOCK
];
579 PKTIMER Timer
= &Thread
->Timer
;
580 NTSTATUS WaitStatus
= STATUS_SUCCESS
;
582 PLARGE_INTEGER OriginalDueTime
= Timeout
;
583 LARGE_INTEGER DueTime
, NewDueTime
, InterruptTime
;
584 ULONG Index
, Hand
= 0;
586 /* Make sure the Wait Count is valid */
589 /* Check in regards to the Thread Object Limit */
590 if (Count
> THREAD_WAIT_OBJECTS
)
593 KeBugCheck(MAXIMUM_WAIT_OBJECTS_EXCEEDED
);
596 /* Use the Thread's Wait Block */
597 WaitBlockArray
= &Thread
->WaitBlock
[0];
601 /* Using our own Block Array, so check with the System Object Limit */
602 if (Count
> MAXIMUM_WAIT_OBJECTS
)
605 KeBugCheck(MAXIMUM_WAIT_OBJECTS_EXCEEDED
);
612 /* Check if the lock is already held */
613 if (!Thread
->WaitNext
) goto WaitStart
;
615 /* Otherwise, we already have the lock, so initialize the wait */
616 Thread
->WaitNext
= FALSE
;
617 /* Note that KxMultiThreadWait is a macro, defined in ke_x.h, that */
618 /* uses (and modifies some of) the following local */
620 /* Thread, Index, WaitBlock, Timer, Timeout, Hand and Swappable. */
621 /* If it looks like this code doesn't actually wait for any objects */
622 /* at all, it's because the setup is done by that macro. */
625 /* Start wait loop */
628 /* Disable pre-emption */
629 Thread
->Preempted
= FALSE
;
631 /* Check if a kernel APC is pending and we're below APC_LEVEL */
632 if ((Thread
->ApcState
.KernelApcPending
) && !(Thread
->SpecialApcDisable
) &&
633 (Thread
->WaitIrql
< APC_LEVEL
))
635 /* Unlock the dispatcher */
636 KiReleaseDispatcherLock(Thread
->WaitIrql
);
640 /* Check what kind of wait this is */
642 if (WaitType
== WaitAny
)
647 /* Get the Current Object */
648 CurrentObject
= (PKMUTANT
)Object
[Index
];
649 ASSERT(CurrentObject
->Header
.Type
!= QueueObject
);
651 /* Check if the Object is a mutant */
652 if (CurrentObject
->Header
.Type
== MutantObject
)
654 /* Check if it's signaled */
655 if ((CurrentObject
->Header
.SignalState
> 0) ||
656 (Thread
== CurrentObject
->OwnerThread
))
658 /* This is a Wait Any, so unwait this and exit */
659 if (CurrentObject
->Header
.SignalState
!=
662 /* Normal signal state, unwait it and return */
663 KiSatisfyMutantWait(CurrentObject
, Thread
);
664 WaitStatus
= Thread
->WaitStatus
| Index
;
669 /* Raise an exception (see wasm.ru) */
670 KiReleaseDispatcherLock(Thread
->WaitIrql
);
671 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED
);
675 else if (CurrentObject
->Header
.SignalState
> 0)
677 /* Another signaled object, unwait and return */
678 KiSatisfyNonMutantWait(CurrentObject
);
683 /* Go to the next block */
685 } while (Index
< Count
);
692 /* Get the Current Object */
693 CurrentObject
= (PKMUTANT
)Object
[Index
];
694 ASSERT(CurrentObject
->Header
.Type
!= QueueObject
);
696 /* Check if we're dealing with a mutant again */
697 if (CurrentObject
->Header
.Type
== MutantObject
)
699 /* Check if it has an invalid count */
700 if ((Thread
== CurrentObject
->OwnerThread
) &&
701 (CurrentObject
->Header
.SignalState
== (LONG
)MINLONG
))
703 /* Raise an exception */
704 KiReleaseDispatcherLock(Thread
->WaitIrql
);
705 ExRaiseStatus(STATUS_MUTANT_LIMIT_EXCEEDED
);
707 else if ((CurrentObject
->Header
.SignalState
<= 0) &&
708 (Thread
!= CurrentObject
->OwnerThread
))
710 /* We don't own it, can't satisfy the wait */
714 else if (CurrentObject
->Header
.SignalState
<= 0)
716 /* Not signaled, can't satisfy */
720 /* Go to the next block */
722 } while (Index
< Count
);
724 /* Check if we've went through all the objects */
727 /* Loop wait blocks */
728 WaitBlock
= WaitBlockArray
;
731 /* Get the object and satisfy it */
732 CurrentObject
= (PKMUTANT
)WaitBlock
->Object
;
733 KiSatisfyObjectWait(CurrentObject
, Thread
);
735 /* Go to the next block */
736 WaitBlock
= WaitBlock
->NextWaitBlock
;
737 } while(WaitBlock
!= WaitBlockArray
);
739 /* Set the wait status and get out */
740 WaitStatus
= Thread
->WaitStatus
;
745 /* Make sure we can satisfy the Alertable request */
746 WaitStatus
= KiCheckAlertability(Thread
, Alertable
, WaitMode
);
747 if (WaitStatus
!= STATUS_WAIT_0
) break;
749 /* Enable the Timeout Timer if there was any specified */
752 /* Check if the timer expired */
753 InterruptTime
.QuadPart
= KeQueryInterruptTime();
754 if ((ULONGLONG
)InterruptTime
.QuadPart
>=
755 Timer
->DueTime
.QuadPart
)
757 /* It did, so we don't need to wait */
758 WaitStatus
= STATUS_TIMEOUT
;
762 /* It didn't, so activate it */
763 Timer
->Header
.Inserted
= TRUE
;
765 /* Link the wait blocks */
766 WaitBlock
->NextWaitBlock
= TimerBlock
;
769 /* Insert into Object's Wait List*/
770 WaitBlock
= WaitBlockArray
;
773 /* Get the Current Object */
774 CurrentObject
= WaitBlock
->Object
;
776 /* Link the Object to this Wait Block */
777 InsertTailList(&CurrentObject
->Header
.WaitListHead
,
778 &WaitBlock
->WaitListEntry
);
780 /* Move to the next Wait Block */
781 WaitBlock
= WaitBlock
->NextWaitBlock
;
782 } while (WaitBlock
!= WaitBlockArray
);
784 /* Handle Kernel Queues */
785 if (Thread
->Queue
) KiActivateWaiterQueue(Thread
->Queue
);
787 /* Setup the wait information */
788 Thread
->State
= Waiting
;
790 /* Add the thread to the wait list */
791 KiAddThreadToWaitList(Thread
, Swappable
);
793 /* Activate thread swap */
794 ASSERT(Thread
->WaitIrql
<= DISPATCH_LEVEL
);
795 KiSetThreadSwapBusy(Thread
);
797 /* Check if we have a timer */
801 KxInsertTimer(Timer
, Hand
);
805 /* Otherwise, unlock the dispatcher */
806 KiReleaseDispatcherLockFromDpcLevel();
809 /* Swap the thread */
810 WaitStatus
= KiSwapThread(Thread
, KeGetCurrentPrcb());
812 /* Check if we were executing an APC */
813 if (WaitStatus
!= STATUS_KERNEL_APC
) return WaitStatus
;
815 /* Check if we had a timeout */
818 /* Recalculate due times */
819 Timeout
= KiRecalculateDueTime(OriginalDueTime
,
826 /* Setup a new wait */
827 Thread
->WaitIrql
= KeRaiseIrqlToSynchLevel();
829 KiAcquireDispatcherLockAtDpcLevel();
833 KiReleaseDispatcherLock(Thread
->WaitIrql
);
837 /* Release dispatcher lock but maintain high IRQL */
838 KiReleaseDispatcherLockFromDpcLevel();
840 /* Adjust the Quantum and return the wait status */
841 KiAdjustQuantumThread(Thread
);
847 NtDelayExecution(IN BOOLEAN Alertable
,
848 IN PLARGE_INTEGER DelayInterval
)
850 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
851 LARGE_INTEGER SafeInterval
;
854 /* Check the previous mode */
855 if (PreviousMode
!= KernelMode
)
857 /* Enter SEH for probing */
860 /* Probe and capture the time out */
861 SafeInterval
= ProbeForReadLargeInteger(DelayInterval
);
862 DelayInterval
= &SafeInterval
;
864 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
866 /* Return the exception code */
867 _SEH2_YIELD(return _SEH2_GetExceptionCode());
872 /* Call the Kernel Function */
873 Status
= KeDelayExecutionThread(PreviousMode
,