3 /* GLOBAL VARIABLES *********************************************************/
5 extern TIME_ZONE_INFORMATION ExpTimeZoneInfo
;
6 extern LARGE_INTEGER ExpTimeZoneBias
;
7 extern ULONG ExpTimeZoneId
;
8 extern ULONG ExpTickCountMultiplier
;
9 extern ULONG ExpLastTimeZoneBias
;
10 extern POBJECT_TYPE ExEventPairObjectType
;
11 extern POBJECT_TYPE _ExEventObjectType
, _ExSemaphoreObjectType
;
12 extern ULONG NtBuildNumber
;
13 extern ULONG NtMajorVersion
;
14 extern ULONG NtMinorVersion
;
15 extern FAST_MUTEX ExpEnvironmentLock
;
16 extern ERESOURCE ExpFirmwareTableResource
;
17 extern LIST_ENTRY ExpFirmwareTableProviderListHead
;
18 extern BOOLEAN ExpIsWinPEMode
;
19 extern LIST_ENTRY ExpSystemResourcesList
;
20 extern ULONG ExpAnsiCodePageDataOffset
, ExpOemCodePageDataOffset
;
21 extern ULONG ExpUnicodeCaseTableDataOffset
;
22 extern PVOID ExpNlsSectionPointer
;
23 extern CHAR NtBuildLab
[];
24 extern ULONG CmNtCSDVersion
;
25 extern ULONG NtGlobalFlag
;
26 extern ULONG ExpInitializationPhase
;
27 extern ULONG ExpAltTimeZoneBias
;
28 extern LIST_ENTRY ExSystemLookasideListHead
;
29 extern PCALLBACK_OBJECT PowerStateCallback
;
31 typedef struct _EXHANDLE
40 HANDLE GenericHandleOverlay
;
43 } EXHANDLE
, *PEXHANDLE
;
45 typedef struct _ETIMER
50 LIST_ENTRY ActiveTimerListEntry
;
53 BOOLEAN ApcAssociated
;
55 LIST_ENTRY WakeTimerListEntry
;
60 PCALLBACK_OBJECT
*CallbackObject
;
64 #define MAX_FAST_REFS 7
66 #define ExAcquireRundownProtection _ExAcquireRundownProtection
67 #define ExReleaseRundownProtection _ExReleaseRundownProtection
68 #define ExInitializeRundownProtection _ExInitializeRundownProtection
69 #define ExWaitForRundownProtectionRelease _ExWaitForRundownProtectionRelease
70 #define ExRundownCompleted _ExRundownCompleted
71 #define ExGetPreviousMode KeGetPreviousMode
75 // Various bits tagged on the handle or handle table
77 #define EXHANDLE_TABLE_ENTRY_LOCK_BIT 1
78 #define FREE_HANDLE_MASK -1
81 // Number of entries in each table level
83 #define LOW_LEVEL_ENTRIES (PAGE_SIZE / sizeof(HANDLE_TABLE_ENTRY))
84 #define MID_LEVEL_ENTRIES (PAGE_SIZE / sizeof(PHANDLE_TABLE_ENTRY))
85 #define HIGH_LEVEL_ENTRIES (16777216 / (LOW_LEVEL_ENTRIES * MID_LEVEL_ENTRIES))
88 // Maximum index in each table level before we need another table
90 #define MAX_LOW_INDEX LOW_LEVEL_ENTRIES
91 #define MAX_MID_INDEX (MID_LEVEL_ENTRIES * LOW_LEVEL_ENTRIES)
92 #define MAX_HIGH_INDEX (MID_LEVEL_ENTRIES * MID_LEVEL_ENTRIES * LOW_LEVEL_ENTRIES)
97 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40300) || \
98 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ == 40303)
100 #define DEFINE_WAIT_BLOCK(x) \
104 EX_PUSH_LOCK_WAIT_BLOCK UnalignedBlock; \
106 PEX_PUSH_LOCK_WAIT_BLOCK x = (PEX_PUSH_LOCK_WAIT_BLOCK) \
107 ((ULONG_PTR)&WaitBlockBuffer.UnalignedBlock &~ 0xF);
112 // This is only for compatibility; the compiler will optimize the extra
113 // local variable (the actual pointer) away, so we don't take any perf hit
116 #define DEFINE_WAIT_BLOCK(x) \
117 EX_PUSH_LOCK_WAIT_BLOCK WaitBlockBuffer; \
118 PEX_PUSH_LOCK_WAIT_BLOCK x = &WaitBlockBuffer;
122 #define ExpChangeRundown(x, y, z) (ULONG_PTR)InterlockedCompareExchangePointer(&x->Ptr, (PVOID)y, (PVOID)z)
123 #define ExpChangePushlock(x, y, z) InterlockedCompareExchangePointer((PVOID*)x, (PVOID)y, (PVOID)z)
124 #define ExpSetRundown(x, y) InterlockedExchangePointer(&x->Ptr, (PVOID)y)
126 /* INITIALIZATION FUNCTIONS *************************************************/
138 Phase1Initialization(
144 ExpInitializePushLocks(VOID
);
148 ExRefreshTimeZoneInformation(
149 IN PLARGE_INTEGER SystemBootTime
154 ExpInitializeWorkerThreads(VOID
);
158 ExSwapinWorkerThreads(IN BOOLEAN AllowSwap
);
162 ExpInitLookasideLists(VOID
);
166 ExInitializeSystemLookasideList(
167 IN PGENERAL_LOOKASIDE List
,
171 IN USHORT MaximumDepth
,
172 IN PLIST_ENTRY ListHead
177 ExpInitializeCallbacks(VOID
);
185 ExpInitializeExecutive(
187 IN PLOADER_PARAMETER_BLOCK LoaderBlock
192 ExpInitializeEventImplementation(VOID
);
196 ExpInitializeEventImplementation(VOID
);
200 ExpInitializeEventPairImplementation(VOID
);
204 ExpInitializeSemaphoreImplementation(VOID
);
208 ExpInitializeMutantImplementation(VOID
);
212 ExpInitializeTimerImplementation(VOID
);
216 ExpInitializeProfileImplementation(VOID
);
220 ExpResourceInitialization(VOID
);
224 ExInitPoolLookasidePointers(VOID
);
226 /* Callback Functions ********************************************************/
230 ExInitializeCallBack(
231 IN OUT PEX_CALLBACK Callback
234 PEX_CALLBACK_ROUTINE_BLOCK
237 IN PEX_CALLBACK_FUNCTION Function
,
244 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
249 ExCompareExchangeCallBack (
250 IN OUT PEX_CALLBACK CallBack
,
251 IN PEX_CALLBACK_ROUTINE_BLOCK NewBlock
,
252 IN PEX_CALLBACK_ROUTINE_BLOCK OldBlock
255 PEX_CALLBACK_ROUTINE_BLOCK
257 ExReferenceCallBackBlock(
258 IN OUT PEX_CALLBACK CallBack
263 ExDereferenceCallBackBlock(
264 IN OUT PEX_CALLBACK CallBack
,
265 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
268 PEX_CALLBACK_FUNCTION
270 ExGetCallBackBlockRoutine(
271 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
276 ExGetCallBackBlockContext(
277 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
283 IN PEX_CALLBACK_ROUTINE_BLOCK CallbackRoutineBlock
286 /* Rundown Functions ********************************************************/
290 ExfInitializeRundownProtection(
291 OUT PEX_RUNDOWN_REF RunRef
296 ExfReInitializeRundownProtection(
297 OUT PEX_RUNDOWN_REF RunRef
302 ExfAcquireRundownProtection(
303 IN OUT PEX_RUNDOWN_REF RunRef
308 ExfAcquireRundownProtectionEx(
309 IN OUT PEX_RUNDOWN_REF RunRef
,
315 ExfReleaseRundownProtection(
316 IN OUT PEX_RUNDOWN_REF RunRef
321 ExfReleaseRundownProtectionEx(
322 IN OUT PEX_RUNDOWN_REF RunRef
,
329 OUT PEX_RUNDOWN_REF RunRef
334 ExfWaitForRundownProtectionRelease(
335 IN OUT PEX_RUNDOWN_REF RunRef
338 /* HANDLE TABLE FUNCTIONS ***************************************************/
341 (NTAPI
*PEX_SWEEP_HANDLE_CALLBACK
)(
342 PHANDLE_TABLE_ENTRY HandleTableEntry
,
348 (NTAPI
*PEX_DUPLICATE_HANDLE_CALLBACK
)(
349 IN PEPROCESS Process
,
350 IN PHANDLE_TABLE HandleTable
,
351 IN PHANDLE_TABLE_ENTRY HandleTableEntry
,
352 IN PHANDLE_TABLE_ENTRY NewEntry
356 (NTAPI
*PEX_CHANGE_HANDLE_CALLBACK
)(
357 PHANDLE_TABLE_ENTRY HandleTableEntry
,
363 ExpInitializeHandleTables(
370 IN PEPROCESS Process OPTIONAL
375 ExUnlockHandleTableEntry(
376 IN PHANDLE_TABLE HandleTable
,
377 IN PHANDLE_TABLE_ENTRY HandleTableEntry
383 IN PHANDLE_TABLE HandleTable
,
384 IN PHANDLE_TABLE_ENTRY HandleTableEntry
389 ExDestroyHandleTable(
390 IN PHANDLE_TABLE HandleTable
,
391 IN PVOID DestroyHandleProcedure OPTIONAL
397 IN PHANDLE_TABLE HandleTable
,
399 IN PHANDLE_TABLE_ENTRY HandleTableEntry OPTIONAL
404 ExMapHandleToPointer(
405 IN PHANDLE_TABLE HandleTable
,
412 IN PEPROCESS Process
,
413 IN PHANDLE_TABLE HandleTable
,
414 IN PEX_DUPLICATE_HANDLE_CALLBACK DupHandleProcedure
,
421 IN PHANDLE_TABLE HandleTable
,
423 IN PEX_CHANGE_HANDLE_CALLBACK ChangeRoutine
,
430 IN PHANDLE_TABLE HandleTable
,
431 IN PEX_SWEEP_HANDLE_CALLBACK EnumHandleProcedure
,
435 /* PSEH EXCEPTION HANDLING **************************************************/
439 ExSystemExceptionFilter(VOID
);
441 /* CALLBACKS *****************************************************************/
445 ExDoCallBack(IN OUT PEX_CALLBACK Callback
,
450 PEX_CALLBACK_ROUTINE_BLOCK CallbackBlock
;
451 PEX_CALLBACK_FUNCTION Function
;
453 /* Reference the block */
454 CallbackBlock
= ExReferenceCallBackBlock(Callback
);
457 /* Get the function */
458 Function
= ExGetCallBackBlockRoutine(CallbackBlock
);
460 /* Do the callback */
461 Function(Context
, Argument1
, Argument2
);
463 /* Now dereference it */
464 ExDereferenceCallBackBlock(Callback
, CallbackBlock
);
468 /* FAST REFS ******************************************************************/
472 ExGetObjectFastReference(IN EX_FAST_REF FastRef
)
474 /* Return the unbiased pointer */
475 return (PVOID
)(FastRef
.Value
& ~MAX_FAST_REFS
);
480 ExGetCountFastReference(IN EX_FAST_REF FastRef
)
482 /* Return the reference count */
483 return FastRef
.RefCnt
;
488 ExInitializeFastReference(OUT PEX_FAST_REF FastRef
,
489 IN OPTIONAL PVOID Object
)
492 ASSERT((((ULONG_PTR
)Object
) & MAX_FAST_REFS
) == 0);
494 /* Check if an object is being set */
497 /* Clear the field */
498 FastRef
->Object
= NULL
;
502 /* Otherwise, we assume the object was referenced and is ready */
503 FastRef
->Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
509 ExAcquireFastReference(IN OUT PEX_FAST_REF FastRef
)
511 EX_FAST_REF OldValue
, NewValue
;
513 /* Start reference loop */
516 /* Get the current reference count */
520 /* Increase the reference count */
521 NewValue
.Value
= OldValue
.Value
- 1;
522 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
525 if (NewValue
.Object
!= OldValue
.Object
) continue;
532 /* Return the old value */
538 ExInsertFastReference(IN OUT PEX_FAST_REF FastRef
,
541 EX_FAST_REF OldValue
, NewValue
;
544 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
546 /* Start update loop */
549 /* Get the current reference count */
552 /* Check if the current count is too high or if the pointer changed */
553 if (((OldValue
.RefCnt
+ MAX_FAST_REFS
) > MAX_FAST_REFS
) ||
554 ((OldValue
.Value
&~ MAX_FAST_REFS
) != (ULONG_PTR
)Object
))
560 /* Update the reference count */
561 NewValue
.Value
= OldValue
.Value
+ MAX_FAST_REFS
;
562 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
565 if (NewValue
.Object
!= OldValue
.Object
) continue;
577 ExReleaseFastReference(IN PEX_FAST_REF FastRef
,
580 EX_FAST_REF OldValue
, NewValue
;
583 ASSERT(Object
!= NULL
);
584 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
586 /* Start reference loop */
589 /* Get the current reference count */
592 /* Check if we're full if if the pointer changed */
593 if ((OldValue
.Value
^ (ULONG_PTR
)Object
) >= MAX_FAST_REFS
) return FALSE
;
595 /* Decrease the reference count */
596 NewValue
.Value
= OldValue
.Value
+ 1;
597 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
600 if (NewValue
.Object
!= OldValue
.Object
) continue;
612 ExSwapFastReference(IN PEX_FAST_REF FastRef
,
615 EX_FAST_REF NewValue
, OldValue
;
618 ASSERT((((ULONG_PTR
)Object
) & MAX_FAST_REFS
) == 0);
620 /* Check if an object is being set */
623 /* Clear the field */
624 NewValue
.Object
= NULL
;
628 /* Otherwise, we assume the object was referenced and is ready */
629 NewValue
.Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
632 /* Update the object */
633 OldValue
.Object
= InterlockedExchangePointer(&FastRef
->Object
, NewValue
.Object
);
639 ExCompareSwapFastReference(IN PEX_FAST_REF FastRef
,
643 EX_FAST_REF OldValue
, NewValue
;
645 /* Sanity check and start swap loop */
646 ASSERT(!(((ULONG_PTR
)Object
) & MAX_FAST_REFS
));
649 /* Get the current value */
652 /* Make sure there's enough references to swap */
653 if (!((OldValue
.Value
^ (ULONG_PTR
)OldObject
) <= MAX_FAST_REFS
)) break;
655 /* Check if we have an object to swap */
658 /* Set up the value with maximum fast references */
659 NewValue
.Value
= (ULONG_PTR
)Object
| MAX_FAST_REFS
;
663 /* Write the object address itself (which is empty) */
664 NewValue
.Value
= (ULONG_PTR
)Object
;
667 /* Do the actual compare exchange */
668 NewValue
.Object
= ExpChangePushlock(&FastRef
->Object
,
671 if (NewValue
.Object
!= OldValue
.Object
) continue;
677 /* Return the old value */
681 /* RUNDOWN *******************************************************************/
684 * @name ExfAcquireRundownProtection
687 * The ExfAcquireRundownProtection routine acquires rundown protection for
688 * the specified descriptor.
691 * Pointer to a rundown reference descriptor.
693 * @return TRUE if access to the protected structure was granted, FALSE otherwise.
695 * @remarks This is the internal macro for system use only.In case the rundown
696 * was active, then the slow-path will be called through the exported
702 _ExAcquireRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
704 ULONG_PTR Value
, NewValue
;
706 /* Get the current value and mask the active bit */
707 Value
= RunRef
->Count
&~ EX_RUNDOWN_ACTIVE
;
709 /* Add a reference */
710 NewValue
= Value
+ EX_RUNDOWN_COUNT_INC
;
712 /* Change the value */
713 NewValue
= ExpChangeRundown(RunRef
, NewValue
, Value
);
714 if (NewValue
!= Value
)
716 /* Rundown was active, use long path */
717 return ExfAcquireRundownProtection(RunRef
);
725 * @name ExReleaseRundownProtection
728 * The ExReleaseRundownProtection routine releases rundown protection for
729 * the specified descriptor.
732 * Pointer to a rundown reference descriptor.
734 * @return TRUE if access to the protected structure was granted, FALSE otherwise.
736 * @remarks This is the internal macro for system use only.In case the rundown
737 * was active, then the slow-path will be called through the exported
743 _ExReleaseRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
745 ULONG_PTR Value
, NewValue
;
747 /* Get the current value and mask the active bit */
748 Value
= RunRef
->Count
&~ EX_RUNDOWN_ACTIVE
;
750 /* Remove a reference */
751 NewValue
= Value
- EX_RUNDOWN_COUNT_INC
;
753 /* Change the value */
754 NewValue
= ExpChangeRundown(RunRef
, NewValue
, Value
);
756 /* Check if the rundown was active */
757 if (NewValue
!= Value
)
759 /* Rundown was active, use long path */
760 ExfReleaseRundownProtection(RunRef
);
765 ASSERT((Value
>= EX_RUNDOWN_COUNT_INC
) || (KeNumberProcessors
> 1));
770 * @name ExInitializeRundownProtection
773 * The ExInitializeRundownProtection routine initializes a rundown
774 * protection descriptor.
777 * Pointer to a rundown reference descriptor.
781 * @remarks This is the internal macro for system use only.
786 _ExInitializeRundownProtection(IN PEX_RUNDOWN_REF RunRef
)
788 /* Set the count to zero */
793 * @name ExWaitForRundownProtectionRelease
796 * The ExWaitForRundownProtectionRelease routine waits until the specified
797 * rundown descriptor has been released.
800 * Pointer to a rundown reference descriptor.
804 * @remarks This is the internal macro for system use only. If a wait is actually
805 * necessary, then the slow path is taken through the exported function.
810 _ExWaitForRundownProtectionRelease(IN PEX_RUNDOWN_REF RunRef
)
814 /* Set the active bit */
815 Value
= ExpChangeRundown(RunRef
, EX_RUNDOWN_ACTIVE
, 0);
816 if ((Value
) && (Value
!= EX_RUNDOWN_ACTIVE
))
818 /* If the the rundown wasn't already active, then take the long path */
819 ExfWaitForRundownProtectionRelease(RunRef
);
824 * @name ExRundownCompleted
827 * The ExRundownCompleted routine completes the rundown of the specified
828 * descriptor by setting the active bit.
831 * Pointer to a rundown reference descriptor.
835 * @remarks This is the internal macro for system use only.
840 _ExRundownCompleted(IN PEX_RUNDOWN_REF RunRef
)
843 ASSERT((RunRef
->Count
& EX_RUNDOWN_ACTIVE
) != 0);
845 /* Mark the counter as active */
846 ExpSetRundown(RunRef
, EX_RUNDOWN_ACTIVE
);
849 /* PUSHLOCKS *****************************************************************/
851 /* FIXME: VERIFY THESE! */
856 IN PEX_PUSH_LOCK PushLock
,
863 IN PEX_PUSH_LOCK PushLock
,
864 IN PVOID CurrentWaitBlock
869 ExWaitForUnblockPushLock(
870 IN PEX_PUSH_LOCK PushLock
,
875 * @name _ExInitializePushLock
878 * The _ExInitializePushLock macro initializes a PushLock.
881 * Pointer to the pushlock which is to be initialized.
890 _ExInitializePushLock(OUT PEX_PUSH_LOCK PushLock
)
892 /* Set the value to 0 */
895 #define ExInitializePushLock _ExInitializePushLock
898 * @name ExAcquirePushLockExclusive
901 * The ExAcquirePushLockExclusive macro exclusively acquires a PushLock.
904 * Pointer to the pushlock which is to be acquired.
908 * @remarks The function attempts the quickest route to acquire the lock, which is
909 * to simply set the lock bit.
910 * However, if the pushlock is already shared, the slower path is taken.
912 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
913 * This macro should usually be paired up with KeAcquireCriticalRegion.
918 ExAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock
)
920 /* Try acquiring the lock */
921 if (InterlockedBitTestAndSet((PLONG
)PushLock
, EX_PUSH_LOCK_LOCK_V
))
923 /* Someone changed it, use the slow path */
924 ExfAcquirePushLockExclusive(PushLock
);
928 ASSERT(PushLock
->Locked
);
932 * @name ExTryToAcquirePushLockExclusive
935 * The ExAcquirePushLockExclusive macro exclusively acquires a PushLock.
938 * Pointer to the pushlock which is to be acquired.
942 * @remarks The function attempts the quickest route to acquire the lock, which is
943 * to simply set the lock bit.
944 * However, if the pushlock is already shared, the slower path is taken.
946 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
947 * This macro should usually be paired up with KeAcquireCriticalRegion.
952 ExTryToAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock
)
954 /* Try acquiring the lock */
955 if (InterlockedBitTestAndSet((PLONG
)PushLock
, EX_PUSH_LOCK_LOCK_V
))
962 ASSERT (PushLock
->Locked
);
967 * @name ExAcquirePushLockShared
970 * The ExAcquirePushLockShared macro acquires a shared PushLock.
973 * Pointer to the pushlock which is to be acquired.
977 * @remarks The function attempts the quickest route to acquire the lock, which is
978 * to simply set the lock bit and set the share count to one.
979 * However, if the pushlock is already shared, the slower path is taken.
981 * Callers of ExAcquirePushLockShared must be running at IRQL <= APC_LEVEL.
982 * This macro should usually be paired up with KeAcquireCriticalRegion.
987 ExAcquirePushLockShared(PEX_PUSH_LOCK PushLock
)
989 EX_PUSH_LOCK NewValue
;
991 /* Try acquiring the lock */
992 NewValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
993 if (ExpChangePushlock(PushLock
, NewValue
.Ptr
, 0))
995 /* Someone changed it, use the slow path */
996 ExfAcquirePushLockShared(PushLock
);
1000 ASSERT(PushLock
->Locked
);
1001 ASSERT(PushLock
->Waiting
|| PushLock
->Shared
> 0);
1005 * @name ExConvertPushLockSharedToExclusive
1008 * The ExConvertPushLockSharedToExclusive macro converts an exclusive
1009 * pushlock to a shared pushlock.
1012 * Pointer to the pushlock which is to be converted.
1014 * @return FALSE if conversion failed, TRUE otherwise.
1016 * @remarks The function attempts the quickest route to convert the lock, which is
1017 * to simply set the lock bit and remove any other bits.
1022 ExConvertPushLockSharedToExclusive(IN PEX_PUSH_LOCK PushLock
)
1024 EX_PUSH_LOCK OldValue
;
1026 /* Set the expected old value */
1027 OldValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1029 /* Try converting the lock */
1030 if (ExpChangePushlock(PushLock
, EX_PUSH_LOCK_LOCK
, OldValue
.Value
) !=
1033 /* Conversion failed */
1038 ASSERT(PushLock
->Locked
);
1043 * @name ExWaitOnPushLock
1046 * The ExWaitOnPushLock macro acquires and instantly releases a pushlock.
1049 * Pointer to a pushlock.
1053 * @remarks The function attempts to get any exclusive waiters out of their slow
1054 * path by forcing an instant acquire/release operation.
1056 * Callers of ExWaitOnPushLock must be running at IRQL <= APC_LEVEL.
1061 ExWaitOnPushLock(PEX_PUSH_LOCK PushLock
)
1063 /* Check if we're locked */
1064 if (PushLock
->Locked
)
1066 /* Acquire the lock */
1067 ExfAcquirePushLockExclusive(PushLock
);
1068 ASSERT(PushLock
->Locked
);
1071 ExfReleasePushLockExclusive(PushLock
);
1076 * @name ExReleasePushLockShared
1079 * The ExReleasePushLockShared macro releases a previously acquired PushLock.
1082 * Pointer to a previously acquired pushlock.
1086 * @remarks The function attempts the quickest route to release the lock, which is
1087 * to simply decrease the share count and remove the lock bit.
1088 * However, if the pushlock is being waited on then the long path is taken.
1090 * Callers of ExReleasePushLockShared must be running at IRQL <= APC_LEVEL.
1091 * This macro should usually be paired up with KeLeaveCriticalRegion.
1096 ExReleasePushLockShared(PEX_PUSH_LOCK PushLock
)
1098 EX_PUSH_LOCK OldValue
;
1101 ASSERT(PushLock
->Locked
);
1102 ASSERT(PushLock
->Waiting
|| PushLock
->Shared
> 0);
1104 /* Try to clear the pushlock */
1105 OldValue
.Value
= EX_PUSH_LOCK_LOCK
| EX_PUSH_LOCK_SHARE_INC
;
1106 if (ExpChangePushlock(PushLock
, 0, OldValue
.Ptr
) != OldValue
.Ptr
)
1108 /* There are still other people waiting on it */
1109 ExfReleasePushLockShared(PushLock
);
1114 * @name ExReleasePushLockExclusive
1117 * The ExReleasePushLockExclusive macro releases a previously
1118 * exclusively acquired PushLock.
1121 * Pointer to a previously acquired pushlock.
1125 * @remarks The function attempts the quickest route to release the lock, which is
1126 * to simply clear the locked bit.
1127 * However, if the pushlock is being waited on, the slow path is taken
1128 * in an attempt to wake up the lock.
1130 * Callers of ExReleasePushLockExclusive must be running at IRQL <= APC_LEVEL.
1131 * This macro should usually be paired up with KeLeaveCriticalRegion.
1136 ExReleasePushLockExclusive(PEX_PUSH_LOCK PushLock
)
1138 EX_PUSH_LOCK OldValue
;
1141 ASSERT(PushLock
->Locked
);
1142 ASSERT(PushLock
->Waiting
|| PushLock
->Shared
== 0);
1144 /* Unlock the pushlock */
1145 OldValue
.Value
= InterlockedExchangeAddSizeT((PSIZE_T
)PushLock
,
1146 -(SSIZE_T
)EX_PUSH_LOCK_LOCK
);
1149 ASSERT(OldValue
.Locked
);
1150 ASSERT(OldValue
.Waiting
|| OldValue
.Shared
== 0);
1152 /* Check if anyone is waiting on it and it's not already waking*/
1153 if ((OldValue
.Waiting
) && !(OldValue
.Waking
))
1156 ExfTryToWakePushLock(PushLock
);
1161 * @name ExReleasePushLock
1164 * The ExReleasePushLock macro releases a previously acquired PushLock.
1167 * Pointer to a previously acquired pushlock.
1171 * @remarks The function attempts the quickest route to release the lock, which is
1172 * to simply clear all the fields and decrease the share count if required.
1173 * However, if the pushlock is being waited on then the long path is taken.
1175 * Callers of ExReleasePushLock must be running at IRQL <= APC_LEVEL.
1176 * This macro should usually be paired up with KeLeaveCriticalRegion.
1181 ExReleasePushLock(PEX_PUSH_LOCK PushLock
)
1183 EX_PUSH_LOCK OldValue
= *PushLock
;
1184 EX_PUSH_LOCK NewValue
;
1187 ASSERT(OldValue
.Locked
);
1189 /* Check if the pushlock is shared */
1190 if (OldValue
.Shared
> 1)
1192 /* Decrease the share count */
1193 NewValue
.Value
= OldValue
.Value
- EX_PUSH_LOCK_SHARE_INC
;
1197 /* Clear the pushlock entirely */
1201 /* Check if nobody is waiting on us and try clearing the lock here */
1202 if ((OldValue
.Waiting
) ||
1203 (ExpChangePushlock(PushLock
, NewValue
.Ptr
, OldValue
.Ptr
) !=
1206 /* We have waiters, use the long path */
1207 ExfReleasePushLock(PushLock
);
1211 /* FAST MUTEX INLINES *********************************************************/
1215 _ExAcquireFastMutexUnsafe(IN PFAST_MUTEX FastMutex
)
1217 PKTHREAD Thread
= KeGetCurrentThread();
1220 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1221 (Thread
->CombinedApcDisable
!= 0) ||
1222 (Thread
->Teb
== NULL
) ||
1223 (Thread
->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1224 ASSERT(FastMutex
->Owner
!= Thread
);
1226 /* Decrease the count */
1227 if (InterlockedDecrement(&FastMutex
->Count
))
1229 /* Someone is still holding it, use slow path */
1230 KiAcquireFastMutex(FastMutex
);
1234 FastMutex
->Owner
= Thread
;
1239 _ExReleaseFastMutexUnsafe(IN OUT PFAST_MUTEX FastMutex
)
1241 ASSERT((KeGetCurrentIrql() == APC_LEVEL
) ||
1242 (KeGetCurrentThread()->CombinedApcDisable
!= 0) ||
1243 (KeGetCurrentThread()->Teb
== NULL
) ||
1244 (KeGetCurrentThread()->Teb
>= (PTEB
)MM_SYSTEM_RANGE_START
));
1245 ASSERT(FastMutex
->Owner
== KeGetCurrentThread());
1247 /* Erase the owner */
1248 FastMutex
->Owner
= NULL
;
1250 /* Increase the count */
1251 if (InterlockedIncrement(&FastMutex
->Count
) <= 0)
1253 /* Someone was waiting for it, signal the waiter */
1254 KeSetEventBoostPriority(&FastMutex
->Event
, NULL
);
1260 _ExAcquireFastMutex(IN PFAST_MUTEX FastMutex
)
1263 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1265 /* Raise IRQL to APC */
1266 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1268 /* Decrease the count */
1269 if (InterlockedDecrement(&FastMutex
->Count
))
1271 /* Someone is still holding it, use slow path */
1272 KiAcquireFastMutex(FastMutex
);
1275 /* Set the owner and IRQL */
1276 FastMutex
->Owner
= KeGetCurrentThread();
1277 FastMutex
->OldIrql
= OldIrql
;
1282 _ExReleaseFastMutex(IN OUT PFAST_MUTEX FastMutex
)
1285 ASSERT(KeGetCurrentIrql() == APC_LEVEL
);
1287 /* Erase the owner */
1288 FastMutex
->Owner
= NULL
;
1289 OldIrql
= (KIRQL
)FastMutex
->OldIrql
;
1291 /* Increase the count */
1292 if (InterlockedIncrement(&FastMutex
->Count
) <= 0)
1294 /* Someone was waiting for it, signal the waiter */
1295 KeSetEventBoostPriority(&FastMutex
->Event
, NULL
);
1298 /* Lower IRQL back */
1299 KeLowerIrql(OldIrql
);
1304 _ExTryToAcquireFastMutex(IN OUT PFAST_MUTEX FastMutex
)
1307 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1309 /* Raise to APC_LEVEL */
1310 KeRaiseIrql(APC_LEVEL
, &OldIrql
);
1312 /* Check if we can quickly acquire it */
1313 if (InterlockedCompareExchange(&FastMutex
->Count
, 0, 1) == 1)
1315 /* We have, set us as owners */
1316 FastMutex
->Owner
= KeGetCurrentThread();
1317 FastMutex
->OldIrql
= OldIrql
;
1322 /* Acquire attempt failed */
1323 KeLowerIrql(OldIrql
);
1331 _ExEnterCriticalRegionAndAcquireFastMutexUnsafe(IN OUT PFAST_MUTEX FastMutex
)
1333 /* Enter the Critical Region */
1334 KeEnterCriticalRegion();
1336 /* Acquire the mutex unsafely */
1337 _ExAcquireFastMutexUnsafe(FastMutex
);
1342 _ExReleaseFastMutexUnsafeAndLeaveCriticalRegion(IN OUT PFAST_MUTEX FastMutex
)
1344 /* Release the mutex unsafely */
1345 _ExReleaseFastMutexUnsafe(FastMutex
);
1347 /* Leave the critical region */
1348 KeLeaveCriticalRegion();
1351 /* OTHER FUNCTIONS **********************************************************/
1355 ExTryToAcquireResourceExclusiveLite(
1356 IN PERESOURCE Resource
1360 ExpSetTimeZoneInformation(PTIME_ZONE_INFORMATION TimeZoneInformation
);
1364 ExAcquireTimeRefreshLock(BOOLEAN Wait
);
1368 ExReleaseTimeRefreshLock(VOID
);
1372 ExUpdateSystemTimeFromCmos(IN BOOLEAN UpdateInterruptTime
,
1373 IN ULONG MaxSepInSeconds
);
1377 ExpAllocateLocallyUniqueId(OUT LUID
*LocallyUniqueId
);
1381 ExTimerRundown(VOID
);
1386 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1392 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1395 #define InterlockedDecrementUL(Addend) \
1396 (ULONG)InterlockedDecrement((PLONG)(Addend))
1398 #define InterlockedIncrementUL(Addend) \
1399 (ULONG)InterlockedIncrement((PLONG)(Addend))
1401 #define InterlockedExchangeUL(Target, Value) \
1402 (ULONG)InterlockedExchange((PLONG)(Target), (LONG)(Value))
1404 #define InterlockedExchangeAddUL(Addend, Value) \
1405 (ULONG)InterlockedExchangeAdd((PLONG)(Addend), (LONG)(Value))
1407 #define InterlockedCompareExchangeUL(Destination, Exchange, Comperand) \
1408 (ULONG)InterlockedCompareExchange((PLONG)(Destination), (LONG)(Exchange), (LONG)(Comperand))
1410 #define ExfInterlockedCompareExchange64UL(Destination, Exchange, Comperand) \
1411 (ULONGLONG)ExfInterlockedCompareExchange64((PLONGLONG)(Destination), (PLONGLONG)(Exchange), (PLONGLONG)(Comperand))