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
;
99 #elif defined (_M_AMD64)
100 return (PRKTHREAD
)__readgsqword(FIELD_OFFSET(KIPCR
, Prcb
.CurrentThread
));
102 PKPRCB Prcb
= KeGetCurrentPrcb();
103 return Prcb
->CurrentThread
;
109 KeGetPreviousMode(VOID
)
111 /* Return the current mode */
112 return KeGetCurrentThread()->PreviousMode
;
118 KeFlushProcessTb(VOID
)
120 /* Flush the TLB by resetting CR3 */
122 __asm__("sync\n\tisync\n\t");
125 // We need to implement this!
127 ASSERTMSG("Need ARM flush routine\n", FALSE
);
129 __writecr3(__readcr3());
134 // Enters a Guarded Region
136 #define KeEnterGuardedRegion() \
138 PKTHREAD _Thread = KeGetCurrentThread(); \
140 /* Sanity checks */ \
141 ASSERT(KeGetCurrentIrql() <= APC_LEVEL); \
142 ASSERT(_Thread == KeGetCurrentThread()); \
143 ASSERT((_Thread->SpecialApcDisable <= 0) && \
144 (_Thread->SpecialApcDisable != -32768)); \
146 /* Disable Special APCs */ \
147 _Thread->SpecialApcDisable--; \
151 // Leaves a Guarded Region
153 #define KeLeaveGuardedRegion() \
155 PKTHREAD _Thread = KeGetCurrentThread(); \
157 /* Sanity checks */ \
158 ASSERT(KeGetCurrentIrql() <= APC_LEVEL); \
159 ASSERT(_Thread == KeGetCurrentThread()); \
160 ASSERT(_Thread->SpecialApcDisable < 0); \
162 /* Leave region and check if APCs are OK now */ \
163 if (!(++_Thread->SpecialApcDisable)) \
165 /* Check for Kernel APCs on the list */ \
166 if (!IsListEmpty(&_Thread->ApcState. \
167 ApcListHead[KernelMode])) \
169 /* Check for APC Delivery */ \
170 KiCheckForKernelApcDelivery(); \
176 // Enters a Critical Region
178 #define KeEnterCriticalRegion() \
180 PKTHREAD _Thread = KeGetCurrentThread(); \
182 /* Sanity checks */ \
183 ASSERT(_Thread == KeGetCurrentThread()); \
184 ASSERT((_Thread->KernelApcDisable <= 0) && \
185 (_Thread->KernelApcDisable != -32768)); \
187 /* Disable Kernel APCs */ \
188 _Thread->KernelApcDisable--; \
192 // Leaves a Critical Region
194 #define KeLeaveCriticalRegion() \
196 PKTHREAD _Thread = KeGetCurrentThread(); \
198 /* Sanity checks */ \
199 ASSERT(_Thread == KeGetCurrentThread()); \
200 ASSERT(_Thread->KernelApcDisable < 0); \
202 /* Enable Kernel APCs */ \
203 _Thread->KernelApcDisable++; \
205 /* Check if Kernel APCs are now enabled */ \
206 if (!(_Thread->KernelApcDisable)) \
208 /* Check if we need to request an APC Delivery */ \
209 if (!(IsListEmpty(&_Thread->ApcState.ApcListHead[KernelMode])) && \
210 !(_Thread->SpecialApcDisable)) \
212 /* Check for the right environment */ \
213 KiCheckForKernelApcDelivery(); \
220 // Spinlock Acquire at IRQL >= DISPATCH_LEVEL
224 KxAcquireSpinLock(IN PKSPIN_LOCK SpinLock
)
226 /* On UP builds, spinlocks don't exist at IRQL >= DISPATCH */
227 UNREFERENCED_PARAMETER(SpinLock
);
231 // Spinlock Release at IRQL >= DISPATCH_LEVEL
235 KxReleaseSpinLock(IN PKSPIN_LOCK SpinLock
)
237 /* On UP builds, spinlocks don't exist at IRQL >= DISPATCH */
238 UNREFERENCED_PARAMETER(SpinLock
);
242 // This routine protects against multiple CPU acquires, it's meaningless on UP.
246 KiAcquireDispatcherObject(IN DISPATCHER_HEADER
* Object
)
248 UNREFERENCED_PARAMETER(Object
);
252 // This routine protects against multiple CPU acquires, it's meaningless on UP.
256 KiReleaseDispatcherObject(IN DISPATCHER_HEADER
* Object
)
258 UNREFERENCED_PARAMETER(Object
);
263 KiAcquireDispatcherLock(VOID
)
265 /* Raise to DPC level */
266 return KeRaiseIrqlToDpcLevel();
271 KiReleaseDispatcherLock(IN KIRQL OldIrql
)
273 /* Just exit the dispatcher */
274 KiExitDispatcher(OldIrql
);
279 KiAcquireDispatcherLockAtDpcLevel(VOID
)
281 /* This is a no-op at DPC Level for UP systems */
287 KiReleaseDispatcherLockFromDpcLevel(VOID
)
289 /* This is a no-op at DPC Level for UP systems */
294 // This routine makes the thread deferred ready on the boot CPU.
298 KiInsertDeferredReadyList(IN PKTHREAD Thread
)
300 /* Set the thread to deferred state and boot CPU */
301 Thread
->State
= DeferredReady
;
302 Thread
->DeferredProcessor
= 0;
304 /* Make the thread ready immediately */
305 KiDeferredReadyThread(Thread
);
310 KiRescheduleThread(IN BOOLEAN NewThread
,
313 /* This is meaningless on UP systems */
314 UNREFERENCED_PARAMETER(NewThread
);
315 UNREFERENCED_PARAMETER(Cpu
);
319 // This routine protects against multiple CPU acquires, it's meaningless on UP.
323 KiSetThreadSwapBusy(IN PKTHREAD Thread
)
325 UNREFERENCED_PARAMETER(Thread
);
329 // This routine protects against multiple CPU acquires, it's meaningless on UP.
333 KiAcquirePrcbLock(IN PKPRCB Prcb
)
335 UNREFERENCED_PARAMETER(Prcb
);
339 // This routine protects against multiple CPU acquires, it's meaningless on UP.
343 KiReleasePrcbLock(IN PKPRCB Prcb
)
345 UNREFERENCED_PARAMETER(Prcb
);
349 // This routine protects against multiple CPU acquires, it's meaningless on UP.
353 KiAcquireThreadLock(IN PKTHREAD Thread
)
355 UNREFERENCED_PARAMETER(Thread
);
359 // This routine protects against multiple CPU acquires, it's meaningless on UP.
363 KiReleaseThreadLock(IN PKTHREAD Thread
)
365 UNREFERENCED_PARAMETER(Thread
);
369 // This routine protects against multiple CPU acquires, it's meaningless on UP.
373 KiTryThreadLock(IN PKTHREAD Thread
)
375 UNREFERENCED_PARAMETER(Thread
);
381 KiCheckDeferredReadyList(IN PKPRCB Prcb
)
383 /* There are no deferred ready lists on UP systems */
384 UNREFERENCED_PARAMETER(Prcb
);
389 KiRundownThread(IN PKTHREAD Thread
)
392 /* Check if this is the NPX Thread */
393 if (KeGetCurrentPrcb()->NpxThread
== Thread
)
396 KeGetCurrentPrcb()->NpxThread
= NULL
;
404 KiRequestApcInterrupt(IN BOOLEAN NeedApc
,
407 /* We deliver instantly on UP */
408 UNREFERENCED_PARAMETER(NeedApc
);
409 UNREFERENCED_PARAMETER(Processor
);
414 KiAcquireTimerLock(IN ULONG Hand
)
416 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
418 /* Nothing to do on UP */
419 UNREFERENCED_PARAMETER(Hand
);
425 KiReleaseTimerLock(IN PKSPIN_LOCK_QUEUE LockQueue
)
427 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
429 /* Nothing to do on UP */
430 UNREFERENCED_PARAMETER(LockQueue
);
436 // Spinlock Acquisition at IRQL >= DISPATCH_LEVEL
440 KxAcquireSpinLock(IN PKSPIN_LOCK SpinLock
)
444 /* Try to acquire it */
445 if (InterlockedBitTestAndSet((PLONG
)SpinLock
, 0))
447 /* Value changed... wait until it's locked */
448 while (*(volatile KSPIN_LOCK
*)SpinLock
== 1)
451 /* On debug builds, we use a much slower but useful routine */
452 //Kii386SpinOnSpinLock(SpinLock, 5);
454 /* FIXME: Do normal yield for now */
457 /* Otherwise, just yield and keep looping */
465 /* On debug builds, we OR in the KTHREAD */
466 *SpinLock
= (KSPIN_LOCK
)KeGetCurrentThread() | 1;
468 /* All is well, break out */
475 // Spinlock Release at IRQL >= DISPATCH_LEVEL
479 KxReleaseSpinLock(IN PKSPIN_LOCK SpinLock
)
482 /* Make sure that the threads match */
483 if (((KSPIN_LOCK
)KeGetCurrentThread() | 1) != *SpinLock
)
485 /* They don't, bugcheck */
486 KeBugCheckEx(SPIN_LOCK_NOT_OWNED
, (ULONG_PTR
)SpinLock
, 0, 0, 0);
490 InterlockedAnd((PLONG
)SpinLock
, 0);
495 KiAcquireDispatcherObject(IN DISPATCHER_HEADER
* Object
)
499 /* Make sure we're at a safe level to touch the lock */
500 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
502 /* Start acquire loop */
505 /* Loop until the other CPU releases it */
508 /* Check if it got released */
509 OldValue
= Object
->Lock
;
510 if ((OldValue
& KOBJECT_LOCK_BIT
) == 0) break;
512 /* Let the CPU know that this is a loop */
516 /* Try acquiring the lock now */
517 } while (InterlockedCompareExchange(&Object
->Lock
,
518 OldValue
| KOBJECT_LOCK_BIT
,
519 OldValue
) != OldValue
);
524 KiReleaseDispatcherObject(IN DISPATCHER_HEADER
* Object
)
526 /* Make sure we're at a safe level to touch the lock */
527 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
530 InterlockedAnd(&Object
->Lock
, ~KOBJECT_LOCK_BIT
);
535 KiAcquireDispatcherLock(VOID
)
537 /* Raise to synchronization level and acquire the dispatcher lock */
538 return KeAcquireQueuedSpinLockRaiseToSynch(LockQueueDispatcherLock
);
543 KiReleaseDispatcherLock(IN KIRQL OldIrql
)
545 /* First release the lock */
546 KeReleaseQueuedSpinLockFromDpcLevel(&KeGetCurrentPrcb()->
547 LockQueue
[LockQueueDispatcherLock
]);
549 /* Then exit the dispatcher */
550 KiExitDispatcher(OldIrql
);
555 KiAcquireDispatcherLockAtDpcLevel(VOID
)
557 /* Acquire the dispatcher lock */
558 KeAcquireQueuedSpinLockAtDpcLevel(LockQueueDispatcherLock
);
563 KiReleaseDispatcherLockFromDpcLevel(VOID
)
565 /* Release the dispatcher lock */
566 KeReleaseQueuedSpinLockFromDpcLevel(LockQueueDispatcherLock
);
570 // This routine inserts a thread into the deferred ready list of the given CPU
574 KiInsertDeferredReadyList(IN PKTHREAD Thread
)
576 PKPRCB Prcb
= KeGetCurrentPrcb();
578 /* Set the thread to deferred state and CPU */
579 Thread
->State
= DeferredReady
;
580 Thread
->DeferredProcessor
= Prcb
->Number
;
582 /* Add it on the list */
583 PushEntryList(&Prcb
->DeferredReadyListHead
, &Thread
->SwapListEntry
);
588 KiRescheduleThread(IN BOOLEAN NewThread
,
591 /* Check if a new thread needs to be scheduled on a different CPU */
592 if ((NewThread
) && !(KeGetPcr()->Number
== Cpu
))
594 /* Send an IPI to request delivery */
595 KiIpiSend(AFFINITY_MASK(Cpu
), IPI_DPC
);
600 // This routine sets the current thread in a swap busy state, which ensure that
601 // nobody else tries to swap it concurrently.
605 KiSetThreadSwapBusy(IN PKTHREAD Thread
)
607 /* Make sure nobody already set it */
608 ASSERT(Thread
->SwapBusy
== FALSE
);
610 /* Set it ourselves */
611 Thread
->SwapBusy
= TRUE
;
615 // This routine acquires the PRCB lock so that only one caller can touch
616 // volatile PRCB data.
618 // Since this is a simple optimized spin-lock, it must be be only acquired
619 // at dispatcher level or higher!
623 KiAcquirePrcbLock(IN PKPRCB Prcb
)
625 /* Make sure we're at a safe level to touch the PRCB lock */
626 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
628 /* Start acquire loop */
631 /* Acquire the lock and break out if we acquired it first */
632 if (!InterlockedExchange((PLONG
)&Prcb
->PrcbLock
, 1)) break;
634 /* Loop until the other CPU releases it */
637 /* Let the CPU know that this is a loop */
639 } while (Prcb
->PrcbLock
);
644 // This routine releases the PRCB lock so that other callers can touch
645 // volatile PRCB data.
647 // Since this is a simple optimized spin-lock, it must be be only acquired
648 // at dispatcher level or higher!
652 KiReleasePrcbLock(IN PKPRCB Prcb
)
654 /* Make sure it's acquired! */
655 ASSERT(Prcb
->PrcbLock
!= 0);
658 InterlockedAnd((PLONG
)&Prcb
->PrcbLock
, 0);
662 // This routine acquires the thread lock so that only one caller can touch
663 // volatile thread data.
665 // Since this is a simple optimized spin-lock, it must be be only acquired
666 // at dispatcher level or higher!
670 KiAcquireThreadLock(IN PKTHREAD Thread
)
672 /* Make sure we're at a safe level to touch the thread lock */
673 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
675 /* Start acquire loop */
678 /* Acquire the lock and break out if we acquired it first */
679 if (!InterlockedExchange((PLONG
)&Thread
->ThreadLock
, 1)) break;
681 /* Loop until the other CPU releases it */
684 /* Let the CPU know that this is a loop */
686 } while (Thread
->ThreadLock
);
691 // This routine releases the thread lock so that other callers can touch
692 // volatile thread data.
694 // Since this is a simple optimized spin-lock, it must be be only acquired
695 // at dispatcher level or higher!
699 KiReleaseThreadLock(IN PKTHREAD Thread
)
702 InterlockedAnd((PLONG
)&Thread
->ThreadLock
, 0);
707 KiTryThreadLock(IN PKTHREAD Thread
)
711 /* If the lock isn't acquired, return false */
712 if (!Thread
->ThreadLock
) return FALSE
;
714 /* Otherwise, try to acquire it and check the result */
716 Value
= InterlockedExchange((PLONG
)&Thread
->ThreadLock
, Value
);
718 /* Return the lock state */
719 return (Value
== TRUE
);
724 KiCheckDeferredReadyList(IN PKPRCB Prcb
)
726 /* Scan the deferred ready lists if required */
727 if (Prcb
->DeferredReadyListHead
.Next
) KiProcessDeferredReadyList(Prcb
);
732 KiRundownThread(IN PKTHREAD Thread
)
734 #if defined(_M_IX86) || defined(_M_AMD64)
736 ASSERTMSG("Not yet implemented\n", FALSE
);
742 KiRequestApcInterrupt(IN BOOLEAN NeedApc
,
745 /* Check if we need to request APC delivery */
748 /* Check if it's on another CPU */
749 if (KeGetPcr()->Number
!= Processor
)
751 /* Send an IPI to request delivery */
752 KiIpiSend(AFFINITY_MASK(Processor
), IPI_APC
);
756 /* Request a software interrupt */
757 HalRequestSoftwareInterrupt(APC_LEVEL
);
764 KiAcquireTimerLock(IN ULONG Hand
)
766 PKSPIN_LOCK_QUEUE LockQueue
;
768 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
770 /* Get the lock index */
771 LockIndex
= Hand
>> LOCK_QUEUE_TIMER_LOCK_SHIFT
;
772 LockIndex
&= (LOCK_QUEUE_TIMER_TABLE_LOCKS
- 1);
774 /* Now get the lock */
775 LockQueue
= &KeGetCurrentPrcb()->LockQueue
[LockQueueTimerTableLock
+ LockIndex
];
777 /* Acquire it and return */
778 KeAcquireQueuedSpinLockAtDpcLevel(LockQueue
);
784 KiReleaseTimerLock(IN PKSPIN_LOCK_QUEUE LockQueue
)
786 ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL
);
788 /* Release the lock */
789 KeReleaseQueuedSpinLockFromDpcLevel(LockQueue
);
796 KiAcquireApcLock(IN PKTHREAD Thread
,
797 IN PKLOCK_QUEUE_HANDLE Handle
)
799 /* Acquire the lock and raise to synchronization level */
800 KeAcquireInStackQueuedSpinLockRaiseToSynch(&Thread
->ApcQueueLock
, Handle
);
805 KiAcquireApcLockAtDpcLevel(IN PKTHREAD Thread
,
806 IN PKLOCK_QUEUE_HANDLE Handle
)
808 /* Acquire the lock */
809 KeAcquireInStackQueuedSpinLockAtDpcLevel(&Thread
->ApcQueueLock
, Handle
);
814 KiAcquireApcLockAtApcLevel(IN PKTHREAD Thread
,
815 IN PKLOCK_QUEUE_HANDLE Handle
)
817 /* Acquire the lock */
818 KeAcquireInStackQueuedSpinLock(&Thread
->ApcQueueLock
, Handle
);
823 KiReleaseApcLock(IN PKLOCK_QUEUE_HANDLE Handle
)
825 /* Release the lock */
826 KeReleaseInStackQueuedSpinLock(Handle
);
831 KiReleaseApcLockFromDpcLevel(IN PKLOCK_QUEUE_HANDLE Handle
)
833 /* Release the lock */
834 KeReleaseInStackQueuedSpinLockFromDpcLevel(Handle
);
839 KiAcquireProcessLock(IN PKPROCESS Process
,
840 IN PKLOCK_QUEUE_HANDLE Handle
)
842 /* Acquire the lock and raise to synchronization level */
843 KeAcquireInStackQueuedSpinLockRaiseToSynch(&Process
->ProcessLock
, Handle
);
848 KiReleaseProcessLock(IN PKLOCK_QUEUE_HANDLE Handle
)
850 /* Release the lock */
851 KeReleaseInStackQueuedSpinLock(Handle
);
856 KiReleaseProcessLockFromDpcLevel(IN PKLOCK_QUEUE_HANDLE Handle
)
858 /* Release the lock */
859 KeReleaseInStackQueuedSpinLockFromDpcLevel(Handle
);
864 KiAcquireDeviceQueueLock(IN PKDEVICE_QUEUE DeviceQueue
,
865 IN PKLOCK_QUEUE_HANDLE DeviceLock
)
867 /* Check if we were called from a threaded DPC */
868 if (KeGetCurrentPrcb()->DpcThreadActive
)
870 /* Lock the Queue, we're not at DPC level */
871 KeAcquireInStackQueuedSpinLock(&DeviceQueue
->Lock
, DeviceLock
);
875 /* We must be at DPC level, acquire the lock safely */
876 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
877 KeAcquireInStackQueuedSpinLockAtDpcLevel(&DeviceQueue
->Lock
,
884 KiReleaseDeviceQueueLock(IN PKLOCK_QUEUE_HANDLE DeviceLock
)
886 /* Check if we were called from a threaded DPC */
887 if (KeGetCurrentPrcb()->DpcThreadActive
)
889 /* Unlock the Queue, we're not at DPC level */
890 KeReleaseInStackQueuedSpinLock(DeviceLock
);
894 /* We must be at DPC level, release the lock safely */
895 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
896 KeReleaseInStackQueuedSpinLockFromDpcLevel(DeviceLock
);
901 // Satisfies the wait of any dispatcher object
903 #define KiSatisfyObjectWait(Object, Thread) \
905 /* Special case for Mutants */ \
906 if ((Object)->Header.Type == MutantObject) \
908 /* Decrease the Signal State */ \
909 (Object)->Header.SignalState--; \
911 /* Check if it's now non-signaled */ \
912 if (!(Object)->Header.SignalState) \
914 /* Set the Owner Thread */ \
915 (Object)->OwnerThread = Thread; \
917 /* Disable APCs if needed */ \
918 Thread->KernelApcDisable = Thread->KernelApcDisable - \
919 (Object)->ApcDisable; \
921 /* Check if it's abandoned */ \
922 if ((Object)->Abandoned) \
925 (Object)->Abandoned = FALSE; \
927 /* Return Status */ \
928 Thread->WaitStatus = STATUS_ABANDONED; \
931 /* Insert it into the Mutant List */ \
932 InsertHeadList(Thread->MutantListHead.Blink, \
933 &(Object)->MutantListEntry); \
936 else if (((Object)->Header.Type & TIMER_OR_EVENT_TYPE) == \
937 EventSynchronizationObject) \
939 /* Synchronization Timers and Events just get un-signaled */ \
940 (Object)->Header.SignalState = 0; \
942 else if ((Object)->Header.Type == SemaphoreObject) \
944 /* These ones can have multiple states, so we only decrease it */ \
945 (Object)->Header.SignalState--; \
950 // Satisfies the wait of a mutant dispatcher object
952 #define KiSatisfyMutantWait(Object, Thread) \
954 /* Decrease the Signal State */ \
955 (Object)->Header.SignalState--; \
957 /* Check if it's now non-signaled */ \
958 if (!(Object)->Header.SignalState) \
960 /* Set the Owner Thread */ \
961 (Object)->OwnerThread = Thread; \
963 /* Disable APCs if needed */ \
964 Thread->KernelApcDisable = Thread->KernelApcDisable - \
965 (Object)->ApcDisable; \
967 /* Check if it's abandoned */ \
968 if ((Object)->Abandoned) \
971 (Object)->Abandoned = FALSE; \
973 /* Return Status */ \
974 Thread->WaitStatus = STATUS_ABANDONED; \
977 /* Insert it into the Mutant List */ \
978 InsertHeadList(Thread->MutantListHead.Blink, \
979 &(Object)->MutantListEntry); \
984 // Satisfies the wait of any nonmutant dispatcher object
986 #define KiSatisfyNonMutantWait(Object) \
988 if (((Object)->Header.Type & TIMER_OR_EVENT_TYPE) == \
989 EventSynchronizationObject) \
991 /* Synchronization Timers and Events just get un-signaled */ \
992 (Object)->Header.SignalState = 0; \
994 else if ((Object)->Header.Type == SemaphoreObject) \
996 /* These ones can have multiple states, so we only decrease it */ \
997 (Object)->Header.SignalState--; \
1002 // Recalculates the due time
1006 KiRecalculateDueTime(IN PLARGE_INTEGER OriginalDueTime
,
1007 IN PLARGE_INTEGER DueTime
,
1008 IN OUT PLARGE_INTEGER NewDueTime
)
1010 /* Don't do anything for absolute waits */
1011 if (OriginalDueTime
->QuadPart
>= 0) return OriginalDueTime
;
1013 /* Otherwise, query the interrupt time and recalculate */
1014 NewDueTime
->QuadPart
= KeQueryInterruptTime();
1015 NewDueTime
->QuadPart
-= DueTime
->QuadPart
;
1020 // Determines whether a thread should be added to the wait list
1024 KiCheckThreadStackSwap(IN PKTHREAD Thread
,
1025 IN KPROCESSOR_MODE WaitMode
)
1027 /* Check the required conditions */
1028 if ((WaitMode
!= KernelMode
) &&
1029 (Thread
->EnableStackSwap
) &&
1030 (Thread
->Priority
>= (LOW_REALTIME_PRIORITY
+ 9)))
1032 /* We are go for swap */
1037 /* Don't swap the thread */
1043 // Adds a thread to the wait list
1045 #define KiAddThreadToWaitList(Thread, Swappable) \
1047 /* Make sure it's swappable */ \
1050 /* Insert it into the PRCB's List */ \
1051 InsertTailList(&KeGetCurrentPrcb()->WaitListHead, \
1052 &Thread->WaitListEntry); \
1057 // Checks if a wait in progress should be interrupted by APCs or an alertable
1062 KiCheckAlertability(IN PKTHREAD Thread
,
1063 IN BOOLEAN Alertable
,
1064 IN KPROCESSOR_MODE WaitMode
)
1066 /* Check if the wait is alertable */
1069 /* It is, first check if the thread is alerted in this mode */
1070 if (Thread
->Alerted
[WaitMode
])
1072 /* It is, so bail out of the wait */
1073 Thread
->Alerted
[WaitMode
] = FALSE
;
1074 return STATUS_ALERTED
;
1076 else if ((WaitMode
!= KernelMode
) &&
1077 (!IsListEmpty(&Thread
->ApcState
.ApcListHead
[UserMode
])))
1079 /* It's isn't, but this is a user wait with queued user APCs */
1080 Thread
->ApcState
.UserApcPending
= TRUE
;
1081 return STATUS_USER_APC
;
1083 else if (Thread
->Alerted
[KernelMode
])
1085 /* It isn't that either, but we're alered in kernel mode */
1086 Thread
->Alerted
[KernelMode
] = FALSE
;
1087 return STATUS_ALERTED
;
1090 else if ((WaitMode
!= KernelMode
) && (Thread
->ApcState
.UserApcPending
))
1092 /* Not alertable, but this is a user wait with pending user APCs */
1093 return STATUS_USER_APC
;
1096 /* Otherwise, we're fine */
1097 return STATUS_WAIT_0
;
1101 // Called from KiCompleteTimer, KiInsertTreeTimer, KeSetSystemTime
1102 // to remove timer entries
1103 // See Windows HPI blog for more information.
1106 KiRemoveEntryTimer(IN PKTIMER Timer
)
1109 PKTIMER_TABLE_ENTRY TableEntry
;
1111 /* Remove the timer from the timer list and check if it's empty */
1112 Hand
= Timer
->Header
.Hand
;
1113 if (RemoveEntryList(&Timer
->TimerListEntry
))
1115 /* Get the respective timer table entry */
1116 TableEntry
= &KiTimerTableListHead
[Hand
];
1117 if (&TableEntry
->Entry
== TableEntry
->Entry
.Flink
)
1119 /* Set the entry to an infinite absolute time */
1120 TableEntry
->Time
.HighPart
= 0xFFFFFFFF;
1124 /* Clear the list entries on dbg builds so we can tell the timer is gone */
1126 Timer
->TimerListEntry
.Flink
= NULL
;
1127 Timer
->TimerListEntry
.Blink
= NULL
;
1132 // Called by Wait and Queue code to insert a timer for dispatching.
1133 // Also called by KeSetTimerEx to insert a timer from the caller.
1137 KxInsertTimer(IN PKTIMER Timer
,
1140 PKSPIN_LOCK_QUEUE LockQueue
;
1142 /* Acquire the lock and release the dispatcher lock */
1143 LockQueue
= KiAcquireTimerLock(Hand
);
1144 KiReleaseDispatcherLockFromDpcLevel();
1146 /* Try to insert the timer */
1147 if (KiInsertTimerTable(Timer
, Hand
))
1150 KiCompleteTimer(Timer
, LockQueue
);
1154 /* Do nothing, just release the lock */
1155 KiReleaseTimerLock(LockQueue
);
1160 // Called by KeSetTimerEx and KiInsertTreeTimer to calculate Due Time
1161 // See the Windows HPI Blog for more information
1165 KiComputeDueTime(IN PKTIMER Timer
,
1166 IN LARGE_INTEGER DueTime
,
1169 LARGE_INTEGER InterruptTime
, SystemTime
, DifferenceTime
;
1171 /* Convert to relative time if needed */
1172 Timer
->Header
.Absolute
= FALSE
;
1173 if (DueTime
.HighPart
>= 0)
1175 /* Get System Time */
1176 KeQuerySystemTime(&SystemTime
);
1178 /* Do the conversion */
1179 DifferenceTime
.QuadPart
= SystemTime
.QuadPart
- DueTime
.QuadPart
;
1181 /* Make sure it hasn't already expired */
1182 Timer
->Header
.Absolute
= TRUE
;
1183 if (DifferenceTime
.HighPart
>= 0)
1185 /* Cancel everything */
1186 Timer
->Header
.SignalState
= TRUE
;
1187 Timer
->Header
.Hand
= 0;
1188 Timer
->DueTime
.QuadPart
= 0;
1193 /* Set the time as Absolute */
1194 DueTime
= DifferenceTime
;
1197 /* Get the Interrupt Time */
1198 InterruptTime
.QuadPart
= KeQueryInterruptTime();
1200 /* Recalculate due time */
1201 Timer
->DueTime
.QuadPart
= InterruptTime
.QuadPart
- DueTime
.QuadPart
;
1203 /* Get the handle */
1204 *Hand
= KiComputeTimerTableIndex(Timer
->DueTime
.QuadPart
);
1205 Timer
->Header
.Hand
= (UCHAR
)*Hand
;
1206 Timer
->Header
.Inserted
= TRUE
;
1211 // Called from Unlink and Queue Insert Code.
1212 // Also called by timer code when canceling an inserted timer.
1213 // Removes a timer from it's tree.
1217 KxRemoveTreeTimer(IN PKTIMER Timer
)
1219 ULONG Hand
= Timer
->Header
.Hand
;
1220 PKSPIN_LOCK_QUEUE LockQueue
;
1221 PKTIMER_TABLE_ENTRY TimerEntry
;
1223 /* Acquire timer lock */
1224 LockQueue
= KiAcquireTimerLock(Hand
);
1226 /* Set the timer as non-inserted */
1227 Timer
->Header
.Inserted
= FALSE
;
1229 /* Remove it from the timer list */
1230 if (RemoveEntryList(&Timer
->TimerListEntry
))
1232 /* Get the entry and check if it's empty */
1233 TimerEntry
= &KiTimerTableListHead
[Hand
];
1234 if (IsListEmpty(&TimerEntry
->Entry
))
1236 /* Clear the time then */
1237 TimerEntry
->Time
.HighPart
= 0xFFFFFFFF;
1241 /* Release the timer lock */
1242 KiReleaseTimerLock(LockQueue
);
1247 KxSetTimerForThreadWait(IN PKTIMER Timer
,
1248 IN LARGE_INTEGER Interval
,
1252 LARGE_INTEGER InterruptTime
, SystemTime
, TimeDifference
;
1254 /* Check the timer's interval to see if it's absolute */
1255 Timer
->Header
.Absolute
= FALSE
;
1256 if (Interval
.HighPart
>= 0)
1258 /* Get the system time and calculate the relative time */
1259 KeQuerySystemTime(&SystemTime
);
1260 TimeDifference
.QuadPart
= SystemTime
.QuadPart
- Interval
.QuadPart
;
1261 Timer
->Header
.Absolute
= TRUE
;
1263 /* Check if we've already expired */
1264 if (TimeDifference
.HighPart
>= 0)
1266 /* Reset everything */
1267 Timer
->DueTime
.QuadPart
= 0;
1269 Timer
->Header
.Hand
= 0;
1274 /* Update the interval */
1275 Interval
= TimeDifference
;
1279 /* Calculate the due time */
1280 InterruptTime
.QuadPart
= KeQueryInterruptTime();
1281 DueTime
= InterruptTime
.QuadPart
- Interval
.QuadPart
;
1282 Timer
->DueTime
.QuadPart
= DueTime
;
1284 /* Calculate the timer handle */
1285 *Hand
= KiComputeTimerTableIndex(DueTime
);
1286 Timer
->Header
.Hand
= (UCHAR
)*Hand
;
1289 #define KxDelayThreadWait() \
1291 /* Setup the Wait Block */ \
1292 Thread->WaitBlockList = TimerBlock; \
1294 /* Setup the timer */ \
1295 KxSetTimerForThreadWait(Timer, *Interval, &Hand); \
1297 /* Save the due time for the caller */ \
1298 DueTime.QuadPart = Timer->DueTime.QuadPart; \
1300 /* Link the timer to this Wait Block */ \
1301 TimerBlock->NextWaitBlock = TimerBlock; \
1302 Timer->Header.WaitListHead.Flink = &TimerBlock->WaitListEntry; \
1303 Timer->Header.WaitListHead.Blink = &TimerBlock->WaitListEntry; \
1305 /* Clear wait status */ \
1306 Thread->WaitStatus = STATUS_SUCCESS; \
1308 /* Setup wait fields */ \
1309 Thread->Alertable = Alertable; \
1310 Thread->WaitReason = DelayExecution; \
1311 Thread->WaitMode = WaitMode; \
1313 /* Check if we can swap the thread's stack */ \
1314 Thread->WaitListEntry.Flink = NULL; \
1315 Swappable = KiCheckThreadStackSwap(Thread, WaitMode); \
1317 /* Set the wait time */ \
1318 Thread->WaitTime = KeTickCount.LowPart;
1320 #define KxMultiThreadWait() \
1321 /* Link wait block array to the thread */ \
1322 Thread->WaitBlockList = WaitBlockArray; \
1324 /* Reset the index */ \
1327 /* Loop wait blocks */ \
1330 /* Fill out the wait block */ \
1331 WaitBlock = &WaitBlockArray[Index]; \
1332 WaitBlock->Object = Object[Index]; \
1333 WaitBlock->WaitKey = (USHORT)Index; \
1334 WaitBlock->WaitType = WaitType; \
1335 WaitBlock->Thread = Thread; \
1337 /* Link to next block */ \
1338 WaitBlock->NextWaitBlock = &WaitBlockArray[Index + 1]; \
1340 } while (Index < Count); \
1342 /* Link the last block */ \
1343 WaitBlock->NextWaitBlock = WaitBlockArray; \
1345 /* Set default wait status */ \
1346 Thread->WaitStatus = STATUS_WAIT_0; \
1348 /* Check if we have a timer */ \
1351 /* Link to the block */ \
1352 TimerBlock->NextWaitBlock = WaitBlockArray; \
1354 /* Setup the timer */ \
1355 KxSetTimerForThreadWait(Timer, *Timeout, &Hand); \
1357 /* Save the due time for the caller */ \
1358 DueTime.QuadPart = Timer->DueTime.QuadPart; \
1360 /* Initialize the list */ \
1361 InitializeListHead(&Timer->Header.WaitListHead); \
1364 /* Set wait settings */ \
1365 Thread->Alertable = Alertable; \
1366 Thread->WaitMode = WaitMode; \
1367 Thread->WaitReason = WaitReason; \
1369 /* Check if we can swap the thread's stack */ \
1370 Thread->WaitListEntry.Flink = NULL; \
1371 Swappable = KiCheckThreadStackSwap(Thread, WaitMode); \
1373 /* Set the wait time */ \
1374 Thread->WaitTime = KeTickCount.LowPart;
1376 #define KxSingleThreadWait() \
1377 /* Setup the Wait Block */ \
1378 Thread->WaitBlockList = WaitBlock; \
1379 WaitBlock->WaitKey = STATUS_SUCCESS; \
1380 WaitBlock->Object = Object; \
1381 WaitBlock->WaitType = WaitAny; \
1383 /* Clear wait status */ \
1384 Thread->WaitStatus = STATUS_SUCCESS; \
1386 /* Check if we have a timer */ \
1389 /* Setup the timer */ \
1390 KxSetTimerForThreadWait(Timer, *Timeout, &Hand); \
1392 /* Save the due time for the caller */ \
1393 DueTime.QuadPart = Timer->DueTime.QuadPart; \
1395 /* Pointer to timer block */ \
1396 WaitBlock->NextWaitBlock = TimerBlock; \
1397 TimerBlock->NextWaitBlock = WaitBlock; \
1399 /* Link the timer to this Wait Block */ \
1400 Timer->Header.WaitListHead.Flink = &TimerBlock->WaitListEntry; \
1401 Timer->Header.WaitListHead.Blink = &TimerBlock->WaitListEntry; \
1405 /* No timer block, just ourselves */ \
1406 WaitBlock->NextWaitBlock = WaitBlock; \
1409 /* Set wait settings */ \
1410 Thread->Alertable = Alertable; \
1411 Thread->WaitMode = WaitMode; \
1412 Thread->WaitReason = WaitReason; \
1414 /* Check if we can swap the thread's stack */ \
1415 Thread->WaitListEntry.Flink = NULL; \
1416 Swappable = KiCheckThreadStackSwap(Thread, WaitMode); \
1418 /* Set the wait time */ \
1419 Thread->WaitTime = KeTickCount.LowPart;
1421 #define KxQueueThreadWait() \
1422 /* Setup the Wait Block */ \
1423 Thread->WaitBlockList = WaitBlock; \
1424 WaitBlock->WaitKey = STATUS_SUCCESS; \
1425 WaitBlock->Object = Queue; \
1426 WaitBlock->WaitType = WaitAny; \
1427 WaitBlock->Thread = Thread; \
1429 /* Clear wait status */ \
1430 Thread->WaitStatus = STATUS_SUCCESS; \
1432 /* Check if we have a timer */ \
1435 /* Setup the timer */ \
1436 KxSetTimerForThreadWait(Timer, *Timeout, &Hand); \
1438 /* Save the due time for the caller */ \
1439 DueTime.QuadPart = Timer->DueTime.QuadPart; \
1441 /* Pointer to timer block */ \
1442 WaitBlock->NextWaitBlock = TimerBlock; \
1443 TimerBlock->NextWaitBlock = WaitBlock; \
1445 /* Link the timer to this Wait Block */ \
1446 Timer->Header.WaitListHead.Flink = &TimerBlock->WaitListEntry; \
1447 Timer->Header.WaitListHead.Blink = &TimerBlock->WaitListEntry; \
1451 /* No timer block, just ourselves */ \
1452 WaitBlock->NextWaitBlock = WaitBlock; \
1455 /* Set wait settings */ \
1456 Thread->Alertable = FALSE; \
1457 Thread->WaitMode = WaitMode; \
1458 Thread->WaitReason = WrQueue; \
1460 /* Check if we can swap the thread's stack */ \
1461 Thread->WaitListEntry.Flink = NULL; \
1462 Swappable = KiCheckThreadStackSwap(Thread, WaitMode); \
1464 /* Set the wait time */ \
1465 Thread->WaitTime = KeTickCount.LowPart;
1472 KxUnwaitThread(IN DISPATCHER_HEADER
*Object
,
1473 IN KPRIORITY Increment
)
1475 PLIST_ENTRY WaitEntry
, WaitList
;
1476 PKWAIT_BLOCK WaitBlock
;
1477 PKTHREAD WaitThread
;
1480 /* Loop the Wait Entries */
1481 WaitList
= &Object
->WaitListHead
;
1482 ASSERT(IsListEmpty(&Object
->WaitListHead
) == FALSE
);
1483 WaitEntry
= WaitList
->Flink
;
1486 /* Get the current wait block */
1487 WaitBlock
= CONTAINING_RECORD(WaitEntry
, KWAIT_BLOCK
, WaitListEntry
);
1489 /* Get the waiting thread */
1490 WaitThread
= WaitBlock
->Thread
;
1492 /* Check the current Wait Mode */
1493 if (WaitBlock
->WaitType
== WaitAny
)
1495 /* Use the actual wait key */
1496 WaitKey
= WaitBlock
->WaitKey
;
1500 /* Otherwise, use STATUS_KERNEL_APC */
1501 WaitKey
= STATUS_KERNEL_APC
;
1504 /* Unwait the thread */
1505 KiUnwaitThread(WaitThread
, WaitKey
, Increment
);
1508 WaitEntry
= WaitList
->Flink
;
1509 } while (WaitEntry
!= WaitList
);
1513 // Unwaits a Thread waiting on an event
1517 KxUnwaitThreadForEvent(IN PKEVENT Event
,
1518 IN KPRIORITY Increment
)
1520 PLIST_ENTRY WaitEntry
, WaitList
;
1521 PKWAIT_BLOCK WaitBlock
;
1522 PKTHREAD WaitThread
;
1524 /* Loop the Wait Entries */
1525 WaitList
= &Event
->Header
.WaitListHead
;
1526 ASSERT(IsListEmpty(&Event
->Header
.WaitListHead
) == FALSE
);
1527 WaitEntry
= WaitList
->Flink
;
1530 /* Get the current wait block */
1531 WaitBlock
= CONTAINING_RECORD(WaitEntry
, KWAIT_BLOCK
, WaitListEntry
);
1533 /* Get the waiting thread */
1534 WaitThread
= WaitBlock
->Thread
;
1536 /* Check the current Wait Mode */
1537 if (WaitBlock
->WaitType
== WaitAny
)
1540 Event
->Header
.SignalState
= 0;
1542 /* Un-signal the event and unwait the thread */
1543 KiUnwaitThread(WaitThread
, WaitBlock
->WaitKey
, Increment
);
1547 /* Unwait the thread with STATUS_KERNEL_APC */
1548 KiUnwaitThread(WaitThread
, STATUS_KERNEL_APC
, Increment
);
1551 WaitEntry
= WaitList
->Flink
;
1552 } while (WaitEntry
!= WaitList
);
1556 // This routine queues a thread that is ready on the PRCB's ready lists.
1557 // If this thread cannot currently run on this CPU, then the thread is
1558 // added to the deferred ready list instead.
1560 // This routine must be entered with the PRCB lock held and it will exit
1561 // with the PRCB lock released!
1565 KxQueueReadyThread(IN PKTHREAD Thread
,
1572 ASSERT(Prcb
== KeGetCurrentPrcb());
1573 ASSERT(Thread
->State
== Running
);
1574 ASSERT(Thread
->NextProcessor
== Prcb
->Number
);
1576 /* Check if this thread is allowed to run in this CPU */
1578 if ((Thread
->Affinity
) & (Prcb
->SetMember
))
1583 /* Set thread ready for execution */
1584 Thread
->State
= Ready
;
1586 /* Save current priority and if someone had pre-empted it */
1587 Priority
= Thread
->Priority
;
1588 Preempted
= Thread
->Preempted
;
1590 /* We're not pre-empting now, and set the wait time */
1591 Thread
->Preempted
= FALSE
;
1592 Thread
->WaitTime
= KeTickCount
.LowPart
;
1595 ASSERT((Priority
>= 0) && (Priority
<= HIGH_PRIORITY
));
1597 /* Insert this thread in the appropriate order */
1598 Preempted
? InsertHeadList(&Prcb
->DispatcherReadyListHead
[Priority
],
1599 &Thread
->WaitListEntry
) :
1600 InsertTailList(&Prcb
->DispatcherReadyListHead
[Priority
],
1601 &Thread
->WaitListEntry
);
1603 /* Update the ready summary */
1604 Prcb
->ReadySummary
|= PRIORITY_MASK(Priority
);
1607 ASSERT(Priority
== Thread
->Priority
);
1609 /* Release the PRCB lock */
1610 KiReleasePrcbLock(Prcb
);
1614 /* Otherwise, prepare this thread to be deferred */
1615 Thread
->State
= DeferredReady
;
1616 Thread
->DeferredProcessor
= Prcb
->Number
;
1618 /* Release the lock and defer scheduling */
1619 KiReleasePrcbLock(Prcb
);
1620 KiDeferredReadyThread(Thread
);
1625 // This routine scans for an appropriate ready thread to select at the
1626 // given priority and for the given CPU.
1630 KiSelectReadyThread(IN KPRIORITY Priority
,
1635 PLIST_ENTRY ListEntry
;
1636 PKTHREAD Thread
= NULL
;
1638 /* Save the current mask and get the priority set for the CPU */
1639 PrioritySet
= Prcb
->ReadySummary
>> Priority
;
1640 if (!PrioritySet
) goto Quickie
;
1642 /* Get the highest priority possible */
1643 BitScanReverse((PULONG
)&HighPriority
, PrioritySet
);
1644 ASSERT((PrioritySet
& PRIORITY_MASK(HighPriority
)) != 0);
1645 HighPriority
+= Priority
;
1647 /* Make sure the list isn't empty at the highest priority */
1648 ASSERT(IsListEmpty(&Prcb
->DispatcherReadyListHead
[HighPriority
]) == FALSE
);
1650 /* Get the first thread on the list */
1651 ListEntry
= Prcb
->DispatcherReadyListHead
[HighPriority
].Flink
;
1652 Thread
= CONTAINING_RECORD(ListEntry
, KTHREAD
, WaitListEntry
);
1654 /* Make sure this thread is here for a reason */
1655 ASSERT(HighPriority
== Thread
->Priority
);
1656 ASSERT(Thread
->Affinity
& AFFINITY_MASK(Prcb
->Number
));
1657 ASSERT(Thread
->NextProcessor
== Prcb
->Number
);
1659 /* Remove it from the list */
1660 if (RemoveEntryList(&Thread
->WaitListEntry
))
1662 /* The list is empty now, reset the ready summary */
1663 Prcb
->ReadySummary
^= PRIORITY_MASK(HighPriority
);
1666 /* Sanity check and return the thread */
1668 ASSERT((Thread
== NULL
) ||
1669 (Thread
->BasePriority
== 0) ||
1670 (Thread
->Priority
!= 0));
1675 // This routine computes the new priority for a thread. It is only valid for
1676 // threads with priorities in the dynamic priority range.
1680 KiComputeNewPriority(IN PKTHREAD Thread
,
1681 IN SCHAR Adjustment
)
1685 /* Priority sanity checks */
1686 ASSERT((Thread
->PriorityDecrement
>= 0) &&
1687 (Thread
->PriorityDecrement
<= Thread
->Priority
));
1688 ASSERT((Thread
->Priority
< LOW_REALTIME_PRIORITY
) ?
1689 TRUE
: (Thread
->PriorityDecrement
== 0));
1691 /* Get the current priority */
1692 Priority
= Thread
->Priority
;
1693 if (Priority
< LOW_REALTIME_PRIORITY
)
1695 /* Decrease priority by the priority decrement */
1696 Priority
-= (Thread
->PriorityDecrement
+ Adjustment
);
1698 /* Don't go out of bounds */
1699 if (Priority
< Thread
->BasePriority
) Priority
= Thread
->BasePriority
;
1701 /* Reset the priority decrement */
1702 Thread
->PriorityDecrement
= 0;
1706 ASSERT((Thread
->BasePriority
== 0) || (Priority
!= 0));
1708 /* Return the new priority */
1713 // Guarded Mutex Routines
1717 _KeInitializeGuardedMutex(OUT PKGUARDED_MUTEX GuardedMutex
)
1719 /* Setup the Initial Data */
1720 GuardedMutex
->Count
= GM_LOCK_BIT
;
1721 GuardedMutex
->Owner
= NULL
;
1722 GuardedMutex
->Contention
= 0;
1724 /* Initialize the Wait Gate */
1725 KeInitializeGate(&GuardedMutex
->Gate
);
1730 _KeAcquireGuardedMutexUnsafe(IN OUT PKGUARDED_MUTEX GuardedMutex
)
1732 PKTHREAD Thread
= KeGetCurrentThread();
1735 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1736 (Thread
->SpecialApcDisable
< 0) ||
1737 (Thread
->Teb
== NULL
) ||
1738 (Thread
->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1739 ASSERT(GuardedMutex
->Owner
!= Thread
);
1741 /* Remove the lock */
1742 if (!InterlockedBitTestAndReset(&GuardedMutex
->Count
, GM_LOCK_BIT_V
))
1744 /* The Guarded Mutex was already locked, enter contented case */
1745 KiAcquireGuardedMutex(GuardedMutex
);
1749 GuardedMutex
->Owner
= Thread
;
1754 _KeReleaseGuardedMutexUnsafe(IN OUT PKGUARDED_MUTEX GuardedMutex
)
1756 LONG OldValue
, NewValue
;
1759 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1760 (KeGetCurrentThread()->SpecialApcDisable
< 0) ||
1761 (KeGetCurrentThread()->Teb
== NULL
) ||
1762 (KeGetCurrentThread()->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1763 ASSERT(GuardedMutex
->Owner
== KeGetCurrentThread());
1765 /* Destroy the Owner */
1766 GuardedMutex
->Owner
= NULL
;
1768 /* Add the Lock Bit */
1769 OldValue
= InterlockedExchangeAdd(&GuardedMutex
->Count
, GM_LOCK_BIT
);
1770 ASSERT((OldValue
& GM_LOCK_BIT
) == 0);
1772 /* Check if it was already locked, but not woken */
1773 if ((OldValue
) && !(OldValue
& GM_LOCK_WAITER_WOKEN
))
1775 /* Update the Oldvalue to what it should be now */
1776 OldValue
+= GM_LOCK_BIT
;
1778 /* The mutex will be woken, minus one waiter */
1779 NewValue
= OldValue
+ GM_LOCK_WAITER_WOKEN
-
1782 /* Remove the Woken bit */
1783 if (InterlockedCompareExchange(&GuardedMutex
->Count
,
1785 OldValue
) == OldValue
)
1787 /* Signal the Gate */
1788 KeSignalGateBoostPriority(&GuardedMutex
->Gate
);
1795 _KeAcquireGuardedMutex(IN PKGUARDED_MUTEX GuardedMutex
)
1797 PKTHREAD Thread
= KeGetCurrentThread();
1800 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1801 ASSERT(GuardedMutex
->Owner
!= Thread
);
1803 /* Disable Special APCs */
1804 KeEnterGuardedRegion();
1806 /* Remove the lock */
1807 if (!InterlockedBitTestAndReset(&GuardedMutex
->Count
, GM_LOCK_BIT_V
))
1809 /* The Guarded Mutex was already locked, enter contented case */
1810 KiAcquireGuardedMutex(GuardedMutex
);
1813 /* Set the Owner and Special APC Disable state */
1814 GuardedMutex
->Owner
= Thread
;
1815 GuardedMutex
->SpecialApcDisable
= Thread
->SpecialApcDisable
;
1820 _KeReleaseGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex
)
1822 LONG OldValue
, NewValue
;
1825 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1826 ASSERT(GuardedMutex
->Owner
== KeGetCurrentThread());
1827 ASSERT(KeGetCurrentThread()->SpecialApcDisable
==
1828 GuardedMutex
->SpecialApcDisable
);
1830 /* Destroy the Owner */
1831 GuardedMutex
->Owner
= NULL
;
1833 /* Add the Lock Bit */
1834 OldValue
= InterlockedExchangeAdd(&GuardedMutex
->Count
, GM_LOCK_BIT
);
1835 ASSERT((OldValue
& GM_LOCK_BIT
) == 0);
1837 /* Check if it was already locked, but not woken */
1838 if ((OldValue
) && !(OldValue
& GM_LOCK_WAITER_WOKEN
))
1840 /* Update the Oldvalue to what it should be now */
1841 OldValue
+= GM_LOCK_BIT
;
1843 /* The mutex will be woken, minus one waiter */
1844 NewValue
= OldValue
+ GM_LOCK_WAITER_WOKEN
-
1847 /* Remove the Woken bit */
1848 if (InterlockedCompareExchange(&GuardedMutex
->Count
,
1850 OldValue
) == OldValue
)
1852 /* Signal the Gate */
1853 KeSignalGateBoostPriority(&GuardedMutex
->Gate
);
1857 /* Re-enable APCs */
1858 KeLeaveGuardedRegion();
1863 _KeTryToAcquireGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex
)
1865 PKTHREAD Thread
= KeGetCurrentThread();
1868 KeEnterGuardedRegion();
1870 /* Remove the lock */
1871 if (!InterlockedBitTestAndReset(&GuardedMutex
->Count
, GM_LOCK_BIT_V
))
1873 /* Re-enable APCs */
1874 KeLeaveGuardedRegion();
1877 /* Return failure */
1881 /* Set the Owner and APC State */
1882 GuardedMutex
->Owner
= Thread
;
1883 GuardedMutex
->SpecialApcDisable
= Thread
->SpecialApcDisable
;