2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/include/ke_x.h
5 * PURPOSE: Internal Inlined Functions for the Kernel
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
10 // Thread Dispatcher Header DebugActive Mask
12 #define DR_MASK(x) 1 << x
13 #define DR_ACTIVE_MASK 0x10
14 #define DR_REG_MASK 0x4F
18 // Sanitizes a selector
22 Ke386SanitizeSeg(IN ULONG Cs
,
23 IN KPROCESSOR_MODE Mode
)
26 // Check if we're in kernel-mode, and force CPL 0 if so.
27 // Otherwise, force CPL 3.
29 return ((Mode
== KernelMode
) ?
30 (Cs
& (0xFFFF & ~RPL_MASK
)) :
31 (RPL_MASK
| (Cs
& 0xFFFF)));
39 Ke386SanitizeFlags(IN ULONG Eflags
,
40 IN KPROCESSOR_MODE Mode
)
43 // Check if we're in kernel-mode, and sanitize EFLAGS if so.
44 // Otherwise, also force interrupt mask on.
46 return ((Mode
== KernelMode
) ?
47 (Eflags
& (EFLAGS_USER_SANITIZE
| EFLAGS_INTERRUPT_MASK
)) :
48 (EFLAGS_INTERRUPT_MASK
| (Eflags
& EFLAGS_USER_SANITIZE
)));
52 // Gets a DR register from a CONTEXT structure
56 KiDrFromContext(IN ULONG Dr
,
59 return *(PVOID
*)((ULONG_PTR
)Context
+ KiDebugRegisterContextOffsets
[Dr
]);
63 // Gets a DR register from a KTRAP_FRAME structure
67 KiDrFromTrapFrame(IN ULONG Dr
,
68 IN PKTRAP_FRAME TrapFrame
)
70 return (PVOID
*)((ULONG_PTR
)TrapFrame
+ KiDebugRegisterTrapOffsets
[Dr
]);
78 Ke386SanitizeDr(IN PVOID DrAddress
,
79 IN KPROCESSOR_MODE Mode
)
82 // Check if we're in kernel-mode, and return the address directly if so.
83 // Otherwise, make sure it's not inside the kernel-mode address space.
84 // If it is, then clear the address.
86 return ((Mode
== KernelMode
) ? DrAddress
:
87 (DrAddress
<= MM_HIGHEST_USER_ADDRESS
) ? DrAddress
: 0);
94 KeGetCurrentThread(VOID
)
97 /* Return the current thread */
98 return ((PKIPCR
)KeGetPcr())->PrcbData
.CurrentThread
;
100 PKPRCB Prcb
= KeGetCurrentPrcb();
101 return Prcb
->CurrentThread
;
107 KeGetPreviousMode(VOID
)
109 /* Return the current mode */
110 return KeGetCurrentThread()->PreviousMode
;
116 KeFlushProcessTb(VOID
)
118 /* Flush the TLB by resetting CR3 */
120 __asm__("sync\n\tisync\n\t");
123 // We need to implement this!
125 ASSERTMSG("Need ARM flush routine\n", FALSE
);
127 __writecr3(__readcr3());
132 // Enters a Guarded Region
134 #define KeEnterGuardedRegion() \
136 PKTHREAD _Thread = KeGetCurrentThread(); \
138 /* Sanity checks */ \
139 ASSERT(KeGetCurrentIrql() <= APC_LEVEL); \
140 ASSERT(_Thread == KeGetCurrentThread()); \
141 ASSERT((_Thread->SpecialApcDisable <= 0) && \
142 (_Thread->SpecialApcDisable != -32768)); \
144 /* Disable Special APCs */ \
145 _Thread->SpecialApcDisable--; \
149 // Leaves a Guarded Region
151 #define KeLeaveGuardedRegion() \
153 PKTHREAD _Thread = KeGetCurrentThread(); \
155 /* Sanity checks */ \
156 ASSERT(KeGetCurrentIrql() <= APC_LEVEL); \
157 ASSERT(_Thread == KeGetCurrentThread()); \
158 ASSERT(_Thread->SpecialApcDisable < 0); \
160 /* Leave region and check if APCs are OK now */ \
161 if (!(++_Thread->SpecialApcDisable)) \
163 /* Check for Kernel APCs on the list */ \
164 if (!IsListEmpty(&_Thread->ApcState. \
165 ApcListHead[KernelMode])) \
167 /* Check for APC Delivery */ \
168 KiCheckForKernelApcDelivery(); \
174 // Enters a Critical Region
176 #define KeEnterCriticalRegion() \
178 PKTHREAD _Thread = KeGetCurrentThread(); \
180 /* Sanity checks */ \
181 ASSERT(_Thread == KeGetCurrentThread()); \
182 ASSERT((_Thread->KernelApcDisable <= 0) && \
183 (_Thread->KernelApcDisable != -32768)); \
185 /* Disable Kernel APCs */ \
186 _Thread->KernelApcDisable--; \
190 // Leaves a Critical Region
192 #define KeLeaveCriticalRegion() \
194 PKTHREAD _Thread = KeGetCurrentThread(); \
196 /* Sanity checks */ \
197 ASSERT(_Thread == KeGetCurrentThread()); \
198 ASSERT(_Thread->KernelApcDisable < 0); \
200 /* Enable Kernel APCs */ \
201 _Thread->KernelApcDisable++; \
203 /* Check if Kernel APCs are now enabled */ \
204 if (!(_Thread->KernelApcDisable)) \
206 /* Check if we need to request an APC Delivery */ \
207 if (!(IsListEmpty(&_Thread->ApcState.ApcListHead[KernelMode])) && \
208 !(_Thread->SpecialApcDisable)) \
210 /* Check for the right environment */ \
211 KiCheckForKernelApcDelivery(); \
218 // Spinlock Acquire at IRQL >= DISPATCH_LEVEL
222 KxAcquireSpinLock(IN PKSPIN_LOCK SpinLock
)
224 /* On UP builds, spinlocks don't exist at IRQL >= DISPATCH */
225 UNREFERENCED_PARAMETER(SpinLock
);
229 // Spinlock Release at IRQL >= DISPATCH_LEVEL
233 KxReleaseSpinLock(IN PKSPIN_LOCK SpinLock
)
235 /* On UP builds, spinlocks don't exist at IRQL >= DISPATCH */
236 UNREFERENCED_PARAMETER(SpinLock
);
240 // This routine protects against multiple CPU acquires, it's meaningless on UP.
244 KiAcquireDispatcherObject(IN DISPATCHER_HEADER
* Object
)
246 UNREFERENCED_PARAMETER(Object
);
250 // This routine protects against multiple CPU acquires, it's meaningless on UP.
254 KiReleaseDispatcherObject(IN DISPATCHER_HEADER
* Object
)
256 UNREFERENCED_PARAMETER(Object
);
261 KiAcquireDispatcherLock(VOID
)
263 /* Raise to DPC level */
264 return KeRaiseIrqlToDpcLevel();
269 KiReleaseDispatcherLock(IN KIRQL OldIrql
)
271 /* Just exit the dispatcher */
272 KiExitDispatcher(OldIrql
);
277 KiAcquireDispatcherLockAtDpcLevel(VOID
)
279 /* This is a no-op at DPC Level for UP systems */
285 KiReleaseDispatcherLockFromDpcLevel(VOID
)
287 /* This is a no-op at DPC Level for UP systems */
292 // This routine makes the thread deferred ready on the boot CPU.
296 KiInsertDeferredReadyList(IN PKTHREAD Thread
)
298 /* Set the thread to deferred state and boot CPU */
299 Thread
->State
= DeferredReady
;
300 Thread
->DeferredProcessor
= 0;
302 /* Make the thread ready immediately */
303 KiDeferredReadyThread(Thread
);
308 KiRescheduleThread(IN BOOLEAN NewThread
,
311 /* This is meaningless on UP systems */
312 UNREFERENCED_PARAMETER(NewThread
);
313 UNREFERENCED_PARAMETER(Cpu
);
317 // This routine protects against multiple CPU acquires, it's meaningless on UP.
321 KiSetThreadSwapBusy(IN PKTHREAD Thread
)
323 UNREFERENCED_PARAMETER(Thread
);
327 // This routine protects against multiple CPU acquires, it's meaningless on UP.
331 KiAcquirePrcbLock(IN PKPRCB Prcb
)
333 UNREFERENCED_PARAMETER(Prcb
);
337 // This routine protects against multiple CPU acquires, it's meaningless on UP.
341 KiReleasePrcbLock(IN PKPRCB Prcb
)
343 UNREFERENCED_PARAMETER(Prcb
);
347 // This routine protects against multiple CPU acquires, it's meaningless on UP.
351 KiAcquireThreadLock(IN PKTHREAD Thread
)
353 UNREFERENCED_PARAMETER(Thread
);
357 // This routine protects against multiple CPU acquires, it's meaningless on UP.
361 KiReleaseThreadLock(IN PKTHREAD Thread
)
363 UNREFERENCED_PARAMETER(Thread
);
367 // This routine protects against multiple CPU acquires, it's meaningless on UP.
371 KiTryThreadLock(IN PKTHREAD Thread
)
373 UNREFERENCED_PARAMETER(Thread
);
379 KiCheckDeferredReadyList(IN PKPRCB Prcb
)
381 /* There are no deferred ready lists on UP systems */
382 UNREFERENCED_PARAMETER(Prcb
);
387 KiRundownThread(IN PKTHREAD Thread
)
389 #if defined(_M_IX86) || defined(_M_AMD64)
390 /* Check if this is the NPX Thread */
391 if (KeGetCurrentPrcb()->NpxThread
== Thread
)
394 KeGetCurrentPrcb()->NpxThread
= NULL
;
402 KiRequestApcInterrupt(IN BOOLEAN NeedApc
,
405 /* We deliver instantly on UP */
406 UNREFERENCED_PARAMETER(NeedApc
);
407 UNREFERENCED_PARAMETER(Processor
);
412 KiAcquireTimerLock(IN ULONG Hand
)
414 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
416 /* Nothing to do on UP */
417 UNREFERENCED_PARAMETER(Hand
);
423 KiReleaseTimerLock(IN PKSPIN_LOCK_QUEUE LockQueue
)
425 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
427 /* Nothing to do on UP */
428 UNREFERENCED_PARAMETER(LockQueue
);
434 // Spinlock Acquisition at IRQL >= DISPATCH_LEVEL
438 KxAcquireSpinLock(IN PKSPIN_LOCK SpinLock
)
442 /* Try to acquire it */
443 if (InterlockedBitTestAndSet((PLONG
)SpinLock
, 0))
445 /* Value changed... wait until it's locked */
446 while (*(volatile KSPIN_LOCK
*)SpinLock
== 1)
449 /* On debug builds, we use a much slower but useful routine */
450 //Kii386SpinOnSpinLock(SpinLock, 5);
452 /* FIXME: Do normal yield for now */
455 /* Otherwise, just yield and keep looping */
463 /* On debug builds, we OR in the KTHREAD */
464 *SpinLock
= (KSPIN_LOCK
)KeGetCurrentThread() | 1;
466 /* All is well, break out */
473 // Spinlock Release at IRQL >= DISPATCH_LEVEL
477 KxReleaseSpinLock(IN PKSPIN_LOCK SpinLock
)
480 /* Make sure that the threads match */
481 if (((KSPIN_LOCK
)KeGetCurrentThread() | 1) != *SpinLock
)
483 /* They don't, bugcheck */
484 KeBugCheckEx(SPIN_LOCK_NOT_OWNED
, (ULONG_PTR
)SpinLock
, 0, 0, 0);
488 InterlockedAnd((PLONG
)SpinLock
, 0);
493 KiAcquireDispatcherObject(IN DISPATCHER_HEADER
* Object
)
497 /* Make sure we're at a safe level to touch the lock */
498 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
500 /* Start acquire loop */
503 /* Loop until the other CPU releases it */
506 /* Check if it got released */
507 OldValue
= Object
->Lock
;
508 if ((OldValue
& KOBJECT_LOCK_BIT
) == 0) break;
510 /* Let the CPU know that this is a loop */
514 /* Try acquiring the lock now */
515 } while (InterlockedCompareExchange(&Object
->Lock
,
516 OldValue
| KOBJECT_LOCK_BIT
,
517 OldValue
) != OldValue
);
522 KiReleaseDispatcherObject(IN DISPATCHER_HEADER
* Object
)
524 /* Make sure we're at a safe level to touch the lock */
525 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
528 InterlockedAnd(&Object
->Lock
, ~KOBJECT_LOCK_BIT
);
533 KiAcquireDispatcherLock(VOID
)
535 /* Raise to synchronization level and acquire the dispatcher lock */
536 return KeAcquireQueuedSpinLockRaiseToSynch(LockQueueDispatcherLock
);
541 KiReleaseDispatcherLock(IN KIRQL OldIrql
)
543 /* First release the lock */
544 KeReleaseQueuedSpinLockFromDpcLevel(&KeGetCurrentPrcb()->
545 LockQueue
[LockQueueDispatcherLock
]);
547 /* Then exit the dispatcher */
548 KiExitDispatcher(OldIrql
);
553 KiAcquireDispatcherLockAtDpcLevel(VOID
)
555 /* Acquire the dispatcher lock */
556 KeAcquireQueuedSpinLockAtDpcLevel(LockQueueDispatcherLock
);
561 KiReleaseDispatcherLockFromDpcLevel(VOID
)
563 /* Release the dispatcher lock */
564 KeReleaseQueuedSpinLockFromDpcLevel(LockQueueDispatcherLock
);
568 // This routine inserts a thread into the deferred ready list of the given CPU
572 KiInsertDeferredReadyList(IN PKTHREAD Thread
)
574 PKPRCB Prcb
= KeGetCurrentPrcb();
576 /* Set the thread to deferred state and CPU */
577 Thread
->State
= DeferredReady
;
578 Thread
->DeferredProcessor
= Prcb
->Number
;
580 /* Add it on the list */
581 PushEntryList(&Prcb
->DeferredReadyListHead
, &Thread
->SwapListEntry
);
586 KiRescheduleThread(IN BOOLEAN NewThread
,
589 /* Check if a new thread needs to be scheduled on a different CPU */
590 if ((NewThread
) && !(KeGetPcr()->Number
== Cpu
))
592 /* Send an IPI to request delivery */
593 KiIpiSend(AFFINITY_MASK(Cpu
), IPI_DPC
);
598 // This routine sets the current thread in a swap busy state, which ensure that
599 // nobody else tries to swap it concurrently.
603 KiSetThreadSwapBusy(IN PKTHREAD Thread
)
605 /* Make sure nobody already set it */
606 ASSERT(Thread
->SwapBusy
== FALSE
);
608 /* Set it ourselves */
609 Thread
->SwapBusy
= TRUE
;
613 // This routine acquires the PRCB lock so that only one caller can touch
614 // volatile PRCB data.
616 // Since this is a simple optimized spin-lock, it must be be only acquired
617 // at dispatcher level or higher!
621 KiAcquirePrcbLock(IN PKPRCB Prcb
)
623 /* Make sure we're at a safe level to touch the PRCB lock */
624 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
626 /* Start acquire loop */
629 /* Acquire the lock and break out if we acquired it first */
630 if (!InterlockedExchange((PLONG
)&Prcb
->PrcbLock
, 1)) break;
632 /* Loop until the other CPU releases it */
635 /* Let the CPU know that this is a loop */
637 } while (Prcb
->PrcbLock
);
642 // This routine releases the PRCB lock so that other callers can touch
643 // volatile PRCB data.
645 // Since this is a simple optimized spin-lock, it must be be only acquired
646 // at dispatcher level or higher!
650 KiReleasePrcbLock(IN PKPRCB Prcb
)
652 /* Make sure it's acquired! */
653 ASSERT(Prcb
->PrcbLock
!= 0);
656 InterlockedAnd((PLONG
)&Prcb
->PrcbLock
, 0);
660 // This routine acquires the thread lock so that only one caller can touch
661 // volatile thread data.
663 // Since this is a simple optimized spin-lock, it must be be only acquired
664 // at dispatcher level or higher!
668 KiAcquireThreadLock(IN PKTHREAD Thread
)
670 /* Make sure we're at a safe level to touch the thread lock */
671 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
673 /* Start acquire loop */
676 /* Acquire the lock and break out if we acquired it first */
677 if (!InterlockedExchange((PLONG
)&Thread
->ThreadLock
, 1)) break;
679 /* Loop until the other CPU releases it */
682 /* Let the CPU know that this is a loop */
684 } while (Thread
->ThreadLock
);
689 // This routine releases the thread lock so that other callers can touch
690 // volatile thread data.
692 // Since this is a simple optimized spin-lock, it must be be only acquired
693 // at dispatcher level or higher!
697 KiReleaseThreadLock(IN PKTHREAD Thread
)
700 InterlockedAnd((PLONG
)&Thread
->ThreadLock
, 0);
705 KiTryThreadLock(IN PKTHREAD Thread
)
709 /* If the lock isn't acquired, return false */
710 if (!Thread
->ThreadLock
) return FALSE
;
712 /* Otherwise, try to acquire it and check the result */
714 Value
= InterlockedExchange((PLONG
)&Thread
->ThreadLock
, Value
);
716 /* Return the lock state */
717 return (Value
== TRUE
);
722 KiCheckDeferredReadyList(IN PKPRCB Prcb
)
724 /* Scan the deferred ready lists if required */
725 if (Prcb
->DeferredReadyListHead
.Next
) KiProcessDeferredReadyList(Prcb
);
730 KiRundownThread(IN PKTHREAD Thread
)
732 #if defined(_M_IX86) || defined(_M_AMD64)
734 ASSERTMSG("Not yet implemented\n", FALSE
);
740 KiRequestApcInterrupt(IN BOOLEAN NeedApc
,
743 /* Check if we need to request APC delivery */
746 /* Check if it's on another CPU */
747 if (KeGetPcr()->Number
!= Processor
)
749 /* Send an IPI to request delivery */
750 KiIpiSend(AFFINITY_MASK(Processor
), IPI_APC
);
754 /* Request a software interrupt */
755 HalRequestSoftwareInterrupt(APC_LEVEL
);
762 KiAcquireTimerLock(IN ULONG Hand
)
764 PKSPIN_LOCK_QUEUE LockQueue
;
766 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
768 /* Get the lock index */
769 LockIndex
= Hand
>> LOCK_QUEUE_TIMER_LOCK_SHIFT
;
770 LockIndex
&= (LOCK_QUEUE_TIMER_TABLE_LOCKS
- 1);
772 /* Now get the lock */
773 LockQueue
= &KeGetCurrentPrcb()->LockQueue
[LockQueueTimerTableLock
+ LockIndex
];
775 /* Acquire it and return */
776 KeAcquireQueuedSpinLockAtDpcLevel(LockQueue
);
782 KiReleaseTimerLock(IN PKSPIN_LOCK_QUEUE LockQueue
)
784 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
786 /* Release the lock */
787 KeReleaseQueuedSpinLockFromDpcLevel(LockQueue
);
794 KiAcquireApcLock(IN PKTHREAD Thread
,
795 IN PKLOCK_QUEUE_HANDLE Handle
)
797 /* Acquire the lock and raise to synchronization level */
798 KeAcquireInStackQueuedSpinLockRaiseToSynch(&Thread
->ApcQueueLock
, Handle
);
803 KiAcquireApcLockAtDpcLevel(IN PKTHREAD Thread
,
804 IN PKLOCK_QUEUE_HANDLE Handle
)
806 /* Acquire the lock */
807 KeAcquireInStackQueuedSpinLockAtDpcLevel(&Thread
->ApcQueueLock
, Handle
);
812 KiAcquireApcLockAtApcLevel(IN PKTHREAD Thread
,
813 IN PKLOCK_QUEUE_HANDLE Handle
)
815 /* Acquire the lock */
816 KeAcquireInStackQueuedSpinLock(&Thread
->ApcQueueLock
, Handle
);
821 KiReleaseApcLock(IN PKLOCK_QUEUE_HANDLE Handle
)
823 /* Release the lock */
824 KeReleaseInStackQueuedSpinLock(Handle
);
829 KiReleaseApcLockFromDpcLevel(IN PKLOCK_QUEUE_HANDLE Handle
)
831 /* Release the lock */
832 KeReleaseInStackQueuedSpinLockFromDpcLevel(Handle
);
837 KiAcquireProcessLock(IN PKPROCESS Process
,
838 IN PKLOCK_QUEUE_HANDLE Handle
)
840 /* Acquire the lock and raise to synchronization level */
841 KeAcquireInStackQueuedSpinLockRaiseToSynch(&Process
->ProcessLock
, Handle
);
846 KiReleaseProcessLock(IN PKLOCK_QUEUE_HANDLE Handle
)
848 /* Release the lock */
849 KeReleaseInStackQueuedSpinLock(Handle
);
854 KiReleaseProcessLockFromDpcLevel(IN PKLOCK_QUEUE_HANDLE Handle
)
856 /* Release the lock */
857 KeReleaseInStackQueuedSpinLockFromDpcLevel(Handle
);
862 KiAcquireDeviceQueueLock(IN PKDEVICE_QUEUE DeviceQueue
,
863 IN PKLOCK_QUEUE_HANDLE DeviceLock
)
865 /* Check if we were called from a threaded DPC */
866 if (KeGetCurrentPrcb()->DpcThreadActive
)
868 /* Lock the Queue, we're not at DPC level */
869 KeAcquireInStackQueuedSpinLock(&DeviceQueue
->Lock
, DeviceLock
);
873 /* We must be at DPC level, acquire the lock safely */
874 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
875 KeAcquireInStackQueuedSpinLockAtDpcLevel(&DeviceQueue
->Lock
,
882 KiReleaseDeviceQueueLock(IN PKLOCK_QUEUE_HANDLE DeviceLock
)
884 /* Check if we were called from a threaded DPC */
885 if (KeGetCurrentPrcb()->DpcThreadActive
)
887 /* Unlock the Queue, we're not at DPC level */
888 KeReleaseInStackQueuedSpinLock(DeviceLock
);
892 /* We must be at DPC level, release the lock safely */
893 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
894 KeReleaseInStackQueuedSpinLockFromDpcLevel(DeviceLock
);
899 // Satisfies the wait of any dispatcher object
901 #define KiSatisfyObjectWait(Object, Thread) \
903 /* Special case for Mutants */ \
904 if ((Object)->Header.Type == MutantObject) \
906 /* Decrease the Signal State */ \
907 (Object)->Header.SignalState--; \
909 /* Check if it's now non-signaled */ \
910 if (!(Object)->Header.SignalState) \
912 /* Set the Owner Thread */ \
913 (Object)->OwnerThread = Thread; \
915 /* Disable APCs if needed */ \
916 Thread->KernelApcDisable = Thread->KernelApcDisable - \
917 (Object)->ApcDisable; \
919 /* Check if it's abandoned */ \
920 if ((Object)->Abandoned) \
923 (Object)->Abandoned = FALSE; \
925 /* Return Status */ \
926 Thread->WaitStatus = STATUS_ABANDONED; \
929 /* Insert it into the Mutant List */ \
930 InsertHeadList(Thread->MutantListHead.Blink, \
931 &(Object)->MutantListEntry); \
934 else if (((Object)->Header.Type & TIMER_OR_EVENT_TYPE) == \
935 EventSynchronizationObject) \
937 /* Synchronization Timers and Events just get un-signaled */ \
938 (Object)->Header.SignalState = 0; \
940 else if ((Object)->Header.Type == SemaphoreObject) \
942 /* These ones can have multiple states, so we only decrease it */ \
943 (Object)->Header.SignalState--; \
948 // Satisfies the wait of a mutant dispatcher object
950 #define KiSatisfyMutantWait(Object, Thread) \
952 /* Decrease the Signal State */ \
953 (Object)->Header.SignalState--; \
955 /* Check if it's now non-signaled */ \
956 if (!(Object)->Header.SignalState) \
958 /* Set the Owner Thread */ \
959 (Object)->OwnerThread = Thread; \
961 /* Disable APCs if needed */ \
962 Thread->KernelApcDisable = Thread->KernelApcDisable - \
963 (Object)->ApcDisable; \
965 /* Check if it's abandoned */ \
966 if ((Object)->Abandoned) \
969 (Object)->Abandoned = FALSE; \
971 /* Return Status */ \
972 Thread->WaitStatus = STATUS_ABANDONED; \
975 /* Insert it into the Mutant List */ \
976 InsertHeadList(Thread->MutantListHead.Blink, \
977 &(Object)->MutantListEntry); \
982 // Satisfies the wait of any nonmutant dispatcher object
984 #define KiSatisfyNonMutantWait(Object) \
986 if (((Object)->Header.Type & TIMER_OR_EVENT_TYPE) == \
987 EventSynchronizationObject) \
989 /* Synchronization Timers and Events just get un-signaled */ \
990 (Object)->Header.SignalState = 0; \
992 else if ((Object)->Header.Type == SemaphoreObject) \
994 /* These ones can have multiple states, so we only decrease it */ \
995 (Object)->Header.SignalState--; \
1000 // Recalculates the due time
1004 KiRecalculateDueTime(IN PLARGE_INTEGER OriginalDueTime
,
1005 IN PLARGE_INTEGER DueTime
,
1006 IN OUT PLARGE_INTEGER NewDueTime
)
1008 /* Don't do anything for absolute waits */
1009 if (OriginalDueTime
->QuadPart
>= 0) return OriginalDueTime
;
1011 /* Otherwise, query the interrupt time and recalculate */
1012 NewDueTime
->QuadPart
= KeQueryInterruptTime();
1013 NewDueTime
->QuadPart
-= DueTime
->QuadPart
;
1018 // Determines whether a thread should be added to the wait list
1022 KiCheckThreadStackSwap(IN PKTHREAD Thread
,
1023 IN KPROCESSOR_MODE WaitMode
)
1025 /* Check the required conditions */
1026 if ((WaitMode
!= KernelMode
) &&
1027 (Thread
->EnableStackSwap
) &&
1028 (Thread
->Priority
>= (LOW_REALTIME_PRIORITY
+ 9)))
1030 /* We are go for swap */
1035 /* Don't swap the thread */
1041 // Adds a thread to the wait list
1043 #define KiAddThreadToWaitList(Thread, Swappable) \
1045 /* Make sure it's swappable */ \
1048 /* Insert it into the PRCB's List */ \
1049 InsertTailList(&KeGetCurrentPrcb()->WaitListHead, \
1050 &Thread->WaitListEntry); \
1055 // Checks if a wait in progress should be interrupted by APCs or an alertable
1060 KiCheckAlertability(IN PKTHREAD Thread
,
1061 IN BOOLEAN Alertable
,
1062 IN KPROCESSOR_MODE WaitMode
)
1064 /* Check if the wait is alertable */
1067 /* It is, first check if the thread is alerted in this mode */
1068 if (Thread
->Alerted
[WaitMode
])
1070 /* It is, so bail out of the wait */
1071 Thread
->Alerted
[WaitMode
] = FALSE
;
1072 return STATUS_ALERTED
;
1074 else if ((WaitMode
!= KernelMode
) &&
1075 (!IsListEmpty(&Thread
->ApcState
.ApcListHead
[UserMode
])))
1077 /* It's isn't, but this is a user wait with queued user APCs */
1078 Thread
->ApcState
.UserApcPending
= TRUE
;
1079 return STATUS_USER_APC
;
1081 else if (Thread
->Alerted
[KernelMode
])
1083 /* It isn't that either, but we're alered in kernel mode */
1084 Thread
->Alerted
[KernelMode
] = FALSE
;
1085 return STATUS_ALERTED
;
1088 else if ((WaitMode
!= KernelMode
) && (Thread
->ApcState
.UserApcPending
))
1090 /* Not alertable, but this is a user wait with pending user APCs */
1091 return STATUS_USER_APC
;
1094 /* Otherwise, we're fine */
1095 return STATUS_WAIT_0
;
1099 // Called from KiCompleteTimer, KiInsertTreeTimer, KeSetSystemTime
1100 // to remove timer entries
1101 // See Windows HPI blog for more information.
1104 KiRemoveEntryTimer(IN PKTIMER Timer
)
1107 PKTIMER_TABLE_ENTRY TableEntry
;
1109 /* Remove the timer from the timer list and check if it's empty */
1110 Hand
= Timer
->Header
.Hand
;
1111 if (RemoveEntryList(&Timer
->TimerListEntry
))
1113 /* Get the respective timer table entry */
1114 TableEntry
= &KiTimerTableListHead
[Hand
];
1115 if (&TableEntry
->Entry
== TableEntry
->Entry
.Flink
)
1117 /* Set the entry to an infinite absolute time */
1118 TableEntry
->Time
.HighPart
= 0xFFFFFFFF;
1122 /* Clear the list entries on dbg builds so we can tell the timer is gone */
1124 Timer
->TimerListEntry
.Flink
= NULL
;
1125 Timer
->TimerListEntry
.Blink
= NULL
;
1130 // Called by Wait and Queue code to insert a timer for dispatching.
1131 // Also called by KeSetTimerEx to insert a timer from the caller.
1135 KxInsertTimer(IN PKTIMER Timer
,
1138 PKSPIN_LOCK_QUEUE LockQueue
;
1140 /* Acquire the lock and release the dispatcher lock */
1141 LockQueue
= KiAcquireTimerLock(Hand
);
1142 KiReleaseDispatcherLockFromDpcLevel();
1144 /* Try to insert the timer */
1145 if (KiInsertTimerTable(Timer
, Hand
))
1148 KiCompleteTimer(Timer
, LockQueue
);
1152 /* Do nothing, just release the lock */
1153 KiReleaseTimerLock(LockQueue
);
1158 // Called by KeSetTimerEx and KiInsertTreeTimer to calculate Due Time
1159 // See the Windows HPI Blog for more information
1163 KiComputeDueTime(IN PKTIMER Timer
,
1164 IN LARGE_INTEGER DueTime
,
1167 LARGE_INTEGER InterruptTime
, SystemTime
, DifferenceTime
;
1169 /* Convert to relative time if needed */
1170 Timer
->Header
.Absolute
= FALSE
;
1171 if (DueTime
.HighPart
>= 0)
1173 /* Get System Time */
1174 KeQuerySystemTime(&SystemTime
);
1176 /* Do the conversion */
1177 DifferenceTime
.QuadPart
= SystemTime
.QuadPart
- DueTime
.QuadPart
;
1179 /* Make sure it hasn't already expired */
1180 Timer
->Header
.Absolute
= TRUE
;
1181 if (DifferenceTime
.HighPart
>= 0)
1183 /* Cancel everything */
1184 Timer
->Header
.SignalState
= TRUE
;
1185 Timer
->Header
.Hand
= 0;
1186 Timer
->DueTime
.QuadPart
= 0;
1191 /* Set the time as Absolute */
1192 DueTime
= DifferenceTime
;
1195 /* Get the Interrupt Time */
1196 InterruptTime
.QuadPart
= KeQueryInterruptTime();
1198 /* Recalculate due time */
1199 Timer
->DueTime
.QuadPart
= InterruptTime
.QuadPart
- DueTime
.QuadPart
;
1201 /* Get the handle */
1202 *Hand
= KiComputeTimerTableIndex(Timer
->DueTime
.QuadPart
);
1203 Timer
->Header
.Hand
= (UCHAR
)*Hand
;
1204 Timer
->Header
.Inserted
= TRUE
;
1209 // Called from Unlink and Queue Insert Code.
1210 // Also called by timer code when canceling an inserted timer.
1211 // Removes a timer from it's tree.
1215 KxRemoveTreeTimer(IN PKTIMER Timer
)
1217 ULONG Hand
= Timer
->Header
.Hand
;
1218 PKSPIN_LOCK_QUEUE LockQueue
;
1219 PKTIMER_TABLE_ENTRY TimerEntry
;
1221 /* Acquire timer lock */
1222 LockQueue
= KiAcquireTimerLock(Hand
);
1224 /* Set the timer as non-inserted */
1225 Timer
->Header
.Inserted
= FALSE
;
1227 /* Remove it from the timer list */
1228 if (RemoveEntryList(&Timer
->TimerListEntry
))
1230 /* Get the entry and check if it's empty */
1231 TimerEntry
= &KiTimerTableListHead
[Hand
];
1232 if (IsListEmpty(&TimerEntry
->Entry
))
1234 /* Clear the time then */
1235 TimerEntry
->Time
.HighPart
= 0xFFFFFFFF;
1239 /* Release the timer lock */
1240 KiReleaseTimerLock(LockQueue
);
1245 KxSetTimerForThreadWait(IN PKTIMER Timer
,
1246 IN LARGE_INTEGER Interval
,
1250 LARGE_INTEGER InterruptTime
, SystemTime
, TimeDifference
;
1252 /* Check the timer's interval to see if it's absolute */
1253 Timer
->Header
.Absolute
= FALSE
;
1254 if (Interval
.HighPart
>= 0)
1256 /* Get the system time and calculate the relative time */
1257 KeQuerySystemTime(&SystemTime
);
1258 TimeDifference
.QuadPart
= SystemTime
.QuadPart
- Interval
.QuadPart
;
1259 Timer
->Header
.Absolute
= TRUE
;
1261 /* Check if we've already expired */
1262 if (TimeDifference
.HighPart
>= 0)
1264 /* Reset everything */
1265 Timer
->DueTime
.QuadPart
= 0;
1267 Timer
->Header
.Hand
= 0;
1272 /* Update the interval */
1273 Interval
= TimeDifference
;
1277 /* Calculate the due time */
1278 InterruptTime
.QuadPart
= KeQueryInterruptTime();
1279 DueTime
= InterruptTime
.QuadPart
- Interval
.QuadPart
;
1280 Timer
->DueTime
.QuadPart
= DueTime
;
1282 /* Calculate the timer handle */
1283 *Hand
= KiComputeTimerTableIndex(DueTime
);
1284 Timer
->Header
.Hand
= (UCHAR
)*Hand
;
1287 #define KxDelayThreadWait() \
1289 /* Setup the Wait Block */ \
1290 Thread->WaitBlockList = TimerBlock; \
1292 /* Setup the timer */ \
1293 KxSetTimerForThreadWait(Timer, *Interval, &Hand); \
1295 /* Save the due time for the caller */ \
1296 DueTime.QuadPart = Timer->DueTime.QuadPart; \
1298 /* Link the timer to this Wait Block */ \
1299 TimerBlock->NextWaitBlock = TimerBlock; \
1300 Timer->Header.WaitListHead.Flink = &TimerBlock->WaitListEntry; \
1301 Timer->Header.WaitListHead.Blink = &TimerBlock->WaitListEntry; \
1303 /* Clear wait status */ \
1304 Thread->WaitStatus = STATUS_SUCCESS; \
1306 /* Setup wait fields */ \
1307 Thread->Alertable = Alertable; \
1308 Thread->WaitReason = DelayExecution; \
1309 Thread->WaitMode = WaitMode; \
1311 /* Check if we can swap the thread's stack */ \
1312 Thread->WaitListEntry.Flink = NULL; \
1313 Swappable = KiCheckThreadStackSwap(Thread, WaitMode); \
1315 /* Set the wait time */ \
1316 Thread->WaitTime = KeTickCount.LowPart;
1318 #define KxMultiThreadWait() \
1319 /* Link wait block array to the thread */ \
1320 Thread->WaitBlockList = WaitBlockArray; \
1322 /* Reset the index */ \
1325 /* Loop wait blocks */ \
1328 /* Fill out the wait block */ \
1329 WaitBlock = &WaitBlockArray[Index]; \
1330 WaitBlock->Object = Object[Index]; \
1331 WaitBlock->WaitKey = (USHORT)Index; \
1332 WaitBlock->WaitType = WaitType; \
1333 WaitBlock->Thread = Thread; \
1335 /* Link to next block */ \
1336 WaitBlock->NextWaitBlock = &WaitBlockArray[Index + 1]; \
1338 } while (Index < Count); \
1340 /* Link the last block */ \
1341 WaitBlock->NextWaitBlock = WaitBlockArray; \
1343 /* Set default wait status */ \
1344 Thread->WaitStatus = STATUS_WAIT_0; \
1346 /* Check if we have a timer */ \
1349 /* Link to the block */ \
1350 TimerBlock->NextWaitBlock = WaitBlockArray; \
1352 /* Setup the timer */ \
1353 KxSetTimerForThreadWait(Timer, *Timeout, &Hand); \
1355 /* Save the due time for the caller */ \
1356 DueTime.QuadPart = Timer->DueTime.QuadPart; \
1358 /* Initialize the list */ \
1359 InitializeListHead(&Timer->Header.WaitListHead); \
1362 /* Set wait settings */ \
1363 Thread->Alertable = Alertable; \
1364 Thread->WaitMode = WaitMode; \
1365 Thread->WaitReason = WaitReason; \
1367 /* Check if we can swap the thread's stack */ \
1368 Thread->WaitListEntry.Flink = NULL; \
1369 Swappable = KiCheckThreadStackSwap(Thread, WaitMode); \
1371 /* Set the wait time */ \
1372 Thread->WaitTime = KeTickCount.LowPart;
1374 #define KxSingleThreadWait() \
1375 /* Setup the Wait Block */ \
1376 Thread->WaitBlockList = WaitBlock; \
1377 WaitBlock->WaitKey = STATUS_SUCCESS; \
1378 WaitBlock->Object = Object; \
1379 WaitBlock->WaitType = WaitAny; \
1381 /* Clear wait status */ \
1382 Thread->WaitStatus = STATUS_SUCCESS; \
1384 /* Check if we have a timer */ \
1387 /* Setup the timer */ \
1388 KxSetTimerForThreadWait(Timer, *Timeout, &Hand); \
1390 /* Save the due time for the caller */ \
1391 DueTime.QuadPart = Timer->DueTime.QuadPart; \
1393 /* Pointer to timer block */ \
1394 WaitBlock->NextWaitBlock = TimerBlock; \
1395 TimerBlock->NextWaitBlock = WaitBlock; \
1397 /* Link the timer to this Wait Block */ \
1398 Timer->Header.WaitListHead.Flink = &TimerBlock->WaitListEntry; \
1399 Timer->Header.WaitListHead.Blink = &TimerBlock->WaitListEntry; \
1403 /* No timer block, just ourselves */ \
1404 WaitBlock->NextWaitBlock = WaitBlock; \
1407 /* Set wait settings */ \
1408 Thread->Alertable = Alertable; \
1409 Thread->WaitMode = WaitMode; \
1410 Thread->WaitReason = WaitReason; \
1412 /* Check if we can swap the thread's stack */ \
1413 Thread->WaitListEntry.Flink = NULL; \
1414 Swappable = KiCheckThreadStackSwap(Thread, WaitMode); \
1416 /* Set the wait time */ \
1417 Thread->WaitTime = KeTickCount.LowPart;
1419 #define KxQueueThreadWait() \
1420 /* Setup the Wait Block */ \
1421 Thread->WaitBlockList = WaitBlock; \
1422 WaitBlock->WaitKey = STATUS_SUCCESS; \
1423 WaitBlock->Object = Queue; \
1424 WaitBlock->WaitType = WaitAny; \
1425 WaitBlock->Thread = Thread; \
1427 /* Clear wait status */ \
1428 Thread->WaitStatus = STATUS_SUCCESS; \
1430 /* Check if we have a timer */ \
1433 /* Setup the timer */ \
1434 KxSetTimerForThreadWait(Timer, *Timeout, &Hand); \
1436 /* Save the due time for the caller */ \
1437 DueTime.QuadPart = Timer->DueTime.QuadPart; \
1439 /* Pointer to timer block */ \
1440 WaitBlock->NextWaitBlock = TimerBlock; \
1441 TimerBlock->NextWaitBlock = WaitBlock; \
1443 /* Link the timer to this Wait Block */ \
1444 Timer->Header.WaitListHead.Flink = &TimerBlock->WaitListEntry; \
1445 Timer->Header.WaitListHead.Blink = &TimerBlock->WaitListEntry; \
1449 /* No timer block, just ourselves */ \
1450 WaitBlock->NextWaitBlock = WaitBlock; \
1453 /* Set wait settings */ \
1454 Thread->Alertable = FALSE; \
1455 Thread->WaitMode = WaitMode; \
1456 Thread->WaitReason = WrQueue; \
1458 /* Check if we can swap the thread's stack */ \
1459 Thread->WaitListEntry.Flink = NULL; \
1460 Swappable = KiCheckThreadStackSwap(Thread, WaitMode); \
1462 /* Set the wait time */ \
1463 Thread->WaitTime = KeTickCount.LowPart;
1470 KxUnwaitThread(IN DISPATCHER_HEADER
*Object
,
1471 IN KPRIORITY Increment
)
1473 PLIST_ENTRY WaitEntry
, WaitList
;
1474 PKWAIT_BLOCK WaitBlock
;
1475 PKTHREAD WaitThread
;
1478 /* Loop the Wait Entries */
1479 WaitList
= &Object
->WaitListHead
;
1480 ASSERT(IsListEmpty(&Object
->WaitListHead
) == FALSE
);
1481 WaitEntry
= WaitList
->Flink
;
1484 /* Get the current wait block */
1485 WaitBlock
= CONTAINING_RECORD(WaitEntry
, KWAIT_BLOCK
, WaitListEntry
);
1487 /* Get the waiting thread */
1488 WaitThread
= WaitBlock
->Thread
;
1490 /* Check the current Wait Mode */
1491 if (WaitBlock
->WaitType
== WaitAny
)
1493 /* Use the actual wait key */
1494 WaitKey
= WaitBlock
->WaitKey
;
1498 /* Otherwise, use STATUS_KERNEL_APC */
1499 WaitKey
= STATUS_KERNEL_APC
;
1502 /* Unwait the thread */
1503 KiUnwaitThread(WaitThread
, WaitKey
, Increment
);
1506 WaitEntry
= WaitList
->Flink
;
1507 } while (WaitEntry
!= WaitList
);
1511 // Unwaits a Thread waiting on an event
1515 KxUnwaitThreadForEvent(IN PKEVENT Event
,
1516 IN KPRIORITY Increment
)
1518 PLIST_ENTRY WaitEntry
, WaitList
;
1519 PKWAIT_BLOCK WaitBlock
;
1520 PKTHREAD WaitThread
;
1522 /* Loop the Wait Entries */
1523 WaitList
= &Event
->Header
.WaitListHead
;
1524 ASSERT(IsListEmpty(&Event
->Header
.WaitListHead
) == FALSE
);
1525 WaitEntry
= WaitList
->Flink
;
1528 /* Get the current wait block */
1529 WaitBlock
= CONTAINING_RECORD(WaitEntry
, KWAIT_BLOCK
, WaitListEntry
);
1531 /* Get the waiting thread */
1532 WaitThread
= WaitBlock
->Thread
;
1534 /* Check the current Wait Mode */
1535 if (WaitBlock
->WaitType
== WaitAny
)
1538 Event
->Header
.SignalState
= 0;
1540 /* Un-signal the event and unwait the thread */
1541 KiUnwaitThread(WaitThread
, WaitBlock
->WaitKey
, Increment
);
1545 /* Unwait the thread with STATUS_KERNEL_APC */
1546 KiUnwaitThread(WaitThread
, STATUS_KERNEL_APC
, Increment
);
1549 WaitEntry
= WaitList
->Flink
;
1550 } while (WaitEntry
!= WaitList
);
1554 // This routine queues a thread that is ready on the PRCB's ready lists.
1555 // If this thread cannot currently run on this CPU, then the thread is
1556 // added to the deferred ready list instead.
1558 // This routine must be entered with the PRCB lock held and it will exit
1559 // with the PRCB lock released!
1563 KxQueueReadyThread(IN PKTHREAD Thread
,
1570 ASSERT(Prcb
== KeGetCurrentPrcb());
1571 ASSERT(Thread
->State
== Running
);
1572 ASSERT(Thread
->NextProcessor
== Prcb
->Number
);
1574 /* Check if this thread is allowed to run in this CPU */
1576 if ((Thread
->Affinity
) & (Prcb
->SetMember
))
1581 /* Set thread ready for execution */
1582 Thread
->State
= Ready
;
1584 /* Save current priority and if someone had pre-empted it */
1585 Priority
= Thread
->Priority
;
1586 Preempted
= Thread
->Preempted
;
1588 /* We're not pre-empting now, and set the wait time */
1589 Thread
->Preempted
= FALSE
;
1590 Thread
->WaitTime
= KeTickCount
.LowPart
;
1593 ASSERT((Priority
>= 0) && (Priority
<= HIGH_PRIORITY
));
1595 /* Insert this thread in the appropriate order */
1596 Preempted
? InsertHeadList(&Prcb
->DispatcherReadyListHead
[Priority
],
1597 &Thread
->WaitListEntry
) :
1598 InsertTailList(&Prcb
->DispatcherReadyListHead
[Priority
],
1599 &Thread
->WaitListEntry
);
1601 /* Update the ready summary */
1602 Prcb
->ReadySummary
|= PRIORITY_MASK(Priority
);
1605 ASSERT(Priority
== Thread
->Priority
);
1607 /* Release the PRCB lock */
1608 KiReleasePrcbLock(Prcb
);
1612 /* Otherwise, prepare this thread to be deferred */
1613 Thread
->State
= DeferredReady
;
1614 Thread
->DeferredProcessor
= Prcb
->Number
;
1616 /* Release the lock and defer scheduling */
1617 KiReleasePrcbLock(Prcb
);
1618 KiDeferredReadyThread(Thread
);
1623 // This routine scans for an appropriate ready thread to select at the
1624 // given priority and for the given CPU.
1628 KiSelectReadyThread(IN KPRIORITY Priority
,
1633 PLIST_ENTRY ListEntry
;
1634 PKTHREAD Thread
= NULL
;
1636 /* Save the current mask and get the priority set for the CPU */
1637 PrioritySet
= Prcb
->ReadySummary
>> Priority
;
1638 if (!PrioritySet
) goto Quickie
;
1640 /* Get the highest priority possible */
1641 BitScanReverse((PULONG
)&HighPriority
, PrioritySet
);
1642 ASSERT((PrioritySet
& PRIORITY_MASK(HighPriority
)) != 0);
1643 HighPriority
+= Priority
;
1645 /* Make sure the list isn't empty at the highest priority */
1646 ASSERT(IsListEmpty(&Prcb
->DispatcherReadyListHead
[HighPriority
]) == FALSE
);
1648 /* Get the first thread on the list */
1649 ListEntry
= Prcb
->DispatcherReadyListHead
[HighPriority
].Flink
;
1650 Thread
= CONTAINING_RECORD(ListEntry
, KTHREAD
, WaitListEntry
);
1652 /* Make sure this thread is here for a reason */
1653 ASSERT(HighPriority
== Thread
->Priority
);
1654 ASSERT(Thread
->Affinity
& AFFINITY_MASK(Prcb
->Number
));
1655 ASSERT(Thread
->NextProcessor
== Prcb
->Number
);
1657 /* Remove it from the list */
1658 if (RemoveEntryList(&Thread
->WaitListEntry
))
1660 /* The list is empty now, reset the ready summary */
1661 Prcb
->ReadySummary
^= PRIORITY_MASK(HighPriority
);
1664 /* Sanity check and return the thread */
1666 ASSERT((Thread
== NULL
) ||
1667 (Thread
->BasePriority
== 0) ||
1668 (Thread
->Priority
!= 0));
1673 // This routine computes the new priority for a thread. It is only valid for
1674 // threads with priorities in the dynamic priority range.
1678 KiComputeNewPriority(IN PKTHREAD Thread
,
1679 IN SCHAR Adjustment
)
1683 /* Priority sanity checks */
1684 ASSERT((Thread
->PriorityDecrement
>= 0) &&
1685 (Thread
->PriorityDecrement
<= Thread
->Priority
));
1686 ASSERT((Thread
->Priority
< LOW_REALTIME_PRIORITY
) ?
1687 TRUE
: (Thread
->PriorityDecrement
== 0));
1689 /* Get the current priority */
1690 Priority
= Thread
->Priority
;
1691 if (Priority
< LOW_REALTIME_PRIORITY
)
1693 /* Decrease priority by the priority decrement */
1694 Priority
-= (Thread
->PriorityDecrement
+ Adjustment
);
1696 /* Don't go out of bounds */
1697 if (Priority
< Thread
->BasePriority
) Priority
= Thread
->BasePriority
;
1699 /* Reset the priority decrement */
1700 Thread
->PriorityDecrement
= 0;
1704 ASSERT((Thread
->BasePriority
== 0) || (Priority
!= 0));
1706 /* Return the new priority */
1711 // Guarded Mutex Routines
1715 _KeInitializeGuardedMutex(OUT PKGUARDED_MUTEX GuardedMutex
)
1717 /* Setup the Initial Data */
1718 GuardedMutex
->Count
= GM_LOCK_BIT
;
1719 GuardedMutex
->Owner
= NULL
;
1720 GuardedMutex
->Contention
= 0;
1722 /* Initialize the Wait Gate */
1723 KeInitializeGate(&GuardedMutex
->Gate
);
1728 _KeAcquireGuardedMutexUnsafe(IN OUT PKGUARDED_MUTEX GuardedMutex
)
1730 PKTHREAD Thread
= KeGetCurrentThread();
1733 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1734 (Thread
->SpecialApcDisable
< 0) ||
1735 (Thread
->Teb
== NULL
) ||
1736 (Thread
->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1737 ASSERT(GuardedMutex
->Owner
!= Thread
);
1739 /* Remove the lock */
1740 if (!InterlockedBitTestAndReset(&GuardedMutex
->Count
, GM_LOCK_BIT_V
))
1742 /* The Guarded Mutex was already locked, enter contented case */
1743 KiAcquireGuardedMutex(GuardedMutex
);
1747 GuardedMutex
->Owner
= Thread
;
1752 _KeReleaseGuardedMutexUnsafe(IN OUT PKGUARDED_MUTEX GuardedMutex
)
1754 LONG OldValue
, NewValue
;
1757 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1758 (KeGetCurrentThread()->SpecialApcDisable
< 0) ||
1759 (KeGetCurrentThread()->Teb
== NULL
) ||
1760 (KeGetCurrentThread()->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1761 ASSERT(GuardedMutex
->Owner
== KeGetCurrentThread());
1763 /* Destroy the Owner */
1764 GuardedMutex
->Owner
= NULL
;
1766 /* Add the Lock Bit */
1767 OldValue
= InterlockedExchangeAdd(&GuardedMutex
->Count
, GM_LOCK_BIT
);
1768 ASSERT((OldValue
& GM_LOCK_BIT
) == 0);
1770 /* Check if it was already locked, but not woken */
1771 if ((OldValue
) && !(OldValue
& GM_LOCK_WAITER_WOKEN
))
1773 /* Update the Oldvalue to what it should be now */
1774 OldValue
+= GM_LOCK_BIT
;
1776 /* The mutex will be woken, minus one waiter */
1777 NewValue
= OldValue
+ GM_LOCK_WAITER_WOKEN
-
1780 /* Remove the Woken bit */
1781 if (InterlockedCompareExchange(&GuardedMutex
->Count
,
1783 OldValue
) == OldValue
)
1785 /* Signal the Gate */
1786 KeSignalGateBoostPriority(&GuardedMutex
->Gate
);
1793 _KeAcquireGuardedMutex(IN PKGUARDED_MUTEX GuardedMutex
)
1795 PKTHREAD Thread
= KeGetCurrentThread();
1798 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1799 ASSERT(GuardedMutex
->Owner
!= Thread
);
1801 /* Disable Special APCs */
1802 KeEnterGuardedRegion();
1804 /* Remove the lock */
1805 if (!InterlockedBitTestAndReset(&GuardedMutex
->Count
, GM_LOCK_BIT_V
))
1807 /* The Guarded Mutex was already locked, enter contented case */
1808 KiAcquireGuardedMutex(GuardedMutex
);
1811 /* Set the Owner and Special APC Disable state */
1812 GuardedMutex
->Owner
= Thread
;
1813 GuardedMutex
->SpecialApcDisable
= Thread
->SpecialApcDisable
;
1818 _KeReleaseGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex
)
1820 LONG OldValue
, NewValue
;
1823 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1824 ASSERT(GuardedMutex
->Owner
== KeGetCurrentThread());
1825 ASSERT(KeGetCurrentThread()->SpecialApcDisable
==
1826 GuardedMutex
->SpecialApcDisable
);
1828 /* Destroy the Owner */
1829 GuardedMutex
->Owner
= NULL
;
1831 /* Add the Lock Bit */
1832 OldValue
= InterlockedExchangeAdd(&GuardedMutex
->Count
, GM_LOCK_BIT
);
1833 ASSERT((OldValue
& GM_LOCK_BIT
) == 0);
1835 /* Check if it was already locked, but not woken */
1836 if ((OldValue
) && !(OldValue
& GM_LOCK_WAITER_WOKEN
))
1838 /* Update the Oldvalue to what it should be now */
1839 OldValue
+= GM_LOCK_BIT
;
1841 /* The mutex will be woken, minus one waiter */
1842 NewValue
= OldValue
+ GM_LOCK_WAITER_WOKEN
-
1845 /* Remove the Woken bit */
1846 if (InterlockedCompareExchange(&GuardedMutex
->Count
,
1848 OldValue
) == OldValue
)
1850 /* Signal the Gate */
1851 KeSignalGateBoostPriority(&GuardedMutex
->Gate
);
1855 /* Re-enable APCs */
1856 KeLeaveGuardedRegion();
1861 _KeTryToAcquireGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex
)
1863 PKTHREAD Thread
= KeGetCurrentThread();
1866 KeEnterGuardedRegion();
1868 /* Remove the lock */
1869 if (!InterlockedBitTestAndReset(&GuardedMutex
->Count
, GM_LOCK_BIT_V
))
1871 /* Re-enable APCs */
1872 KeLeaveGuardedRegion();
1875 /* Return failure */
1879 /* Set the Owner and APC State */
1880 GuardedMutex
->Owner
= Thread
;
1881 GuardedMutex
->SpecialApcDisable
= Thread
->SpecialApcDisable
;