Merge 25584, 25588.
[reactos.git] / reactos / ntoskrnl / include / internal / ex.h
index 39fa724..f730c7a 100644 (file)
@@ -6,13 +6,20 @@
 extern TIME_ZONE_INFORMATION ExpTimeZoneInfo;
 extern LARGE_INTEGER ExpTimeZoneBias;
 extern ULONG ExpTimeZoneId;
+extern ULONG ExpTickCountMultiplier;
+extern ULONG ExpLastTimeZoneBias;
 extern POBJECT_TYPE ExEventPairObjectType;
+extern POBJECT_TYPE _ExEventObjectType, _ExSemaphoreObjectType;
 extern ULONG NtBuildNumber;
 extern ULONG NtMajorVersion;
 extern ULONG NtMinorVersion;
 extern FAST_MUTEX ExpEnvironmentLock;
 extern ERESOURCE ExpFirmwareTableResource;
 extern LIST_ENTRY ExpFirmwareTableProviderListHead;
+extern BOOLEAN ExpIsWinPEMode;
+ULONG ExpAnsiCodePageDataOffset, ExpOemCodePageDataOffset;
+ULONG ExpUnicodeCaseTableDataOffset;
+PVOID ExpNlsSectionPointer;
 
 #define MAX_FAST_REFS           7
 
@@ -27,9 +34,9 @@ extern LIST_ENTRY ExpFirmwareTableProviderListHead;
 #ifndef CONFIG_SMP
 #define ExAcquireResourceLock(l, i) { \
     (void)i; \
-    Ke386DisableInterrupts(); \
+    _disable(); \
 }
-#define ExReleaseResourceLock(l, i) Ke386EnableInterrupts();
+#define ExReleaseResourceLock(l, i) _enable();
 #else
 #define ExAcquireResourceLock(l, i) KeAcquireSpinLock(l, i);
 #define ExReleaseResourceLock(l, i) KeReleaseSpinLock(l, i);
@@ -40,6 +47,37 @@ extern LIST_ENTRY ExpFirmwareTableProviderListHead;
 #define ExInitializeRundownProtection                   _ExInitializeRundownProtection
 #define ExWaitForRundownProtectionRelease               _ExWaitForRundownProtectionRelease
 #define ExRundownCompleted                              _ExRundownCompleted
+#define ExGetPreviousMode                               KeGetPreviousMode
+
+//
+// Detect GCC 4.1.2+
+//
+#if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40102
+
+//
+// Broken GCC with Alignment Bug. We'll do alignment ourselves at higher cost.
+//
+#define DEFINE_WAIT_BLOCK(x)                                \
+    struct _AlignHack                                       \
+    {                                                       \
+        UCHAR Hack[15];                                     \
+        EX_PUSH_LOCK_WAIT_BLOCK UnalignedBlock;             \
+    } WaitBlockBuffer;                                      \
+    PEX_PUSH_LOCK_WAIT_BLOCK x = (PEX_PUSH_LOCK_WAIT_BLOCK) \
+        ((ULONG_PTR)&WaitBlockBuffer.UnalignedBlock &~ 0xF);
+        
+#else
+
+//
+// This is only for compatibility; the compiler will optimize the extra
+// local variable (the actual pointer) away, so we don't take any perf hit
+// by doing this.
+//
+#define DEFINE_WAIT_BLOCK(x)                                \
+    EX_PUSH_LOCK_WAIT_BLOCK WaitBlockBuffer;                \
+    PEX_PUSH_LOCK_WAIT_BLOCK x = &WaitBlockBuffer;
+    
+#endif
 
 /* INITIALIZATION FUNCTIONS *************************************************/
 
@@ -59,7 +97,13 @@ ExPhase2Init(
 
 VOID
 NTAPI
-ExpInitTimeZoneInfo(VOID);
+ExpInitializePushLocks(VOID);
+
+BOOLEAN
+NTAPI
+ExRefreshTimeZoneInformation(
+    IN PLARGE_INTEGER SystemBootTime
+);
 
 VOID
 NTAPI
@@ -131,6 +175,14 @@ VOID
 NTAPI
 ExInitPoolLookasidePointers(VOID);
 
+/* Callback Functions ********************************************************/
+
+VOID
+NTAPI
+ExInitializeCallBack(
+    IN PEX_CALLBACK Callback
+);
+
 /* Rundown Functions ********************************************************/
 
 VOID
@@ -304,6 +356,7 @@ static __inline _SEH_FILTER(_SEH_ExSystemExceptionFilter)
 #define ExpSetRundown(x, y) InterlockedExchange64((PLONGLONG)x, y)
 #else
 #define ExpChangeRundown(x, y, z) InterlockedCompareExchange((PLONG)x, PtrToLong(y), PtrToLong(z))
+#define ExpChangePushlock(x, y, z) LongToPtr(InterlockedCompareExchange((PLONG)x, PtrToLong(y), PtrToLong(z)))
 #define ExpSetRundown(x, y) InterlockedExchange((PLONG)x, y)
 #endif
 
@@ -328,7 +381,7 @@ BOOLEAN
 FORCEINLINE
 _ExAcquireRundownProtection(IN PEX_RUNDOWN_REF RunRef)
 {
-    ULONG_PTR Value, NewValue, OldValue;
+    ULONG_PTR Value, NewValue;
 
     /* Get the current value and mask the active bit */
     Value = RunRef->Count &~ EX_RUNDOWN_ACTIVE;
@@ -337,8 +390,8 @@ _ExAcquireRundownProtection(IN PEX_RUNDOWN_REF RunRef)
     NewValue = Value + EX_RUNDOWN_COUNT_INC;
 
     /* Change the value */
-    OldValue = ExpChangeRundown(RunRef, NewValue, Value);
-    if (OldValue != Value)
+    NewValue = ExpChangeRundown(RunRef, NewValue, Value);
+    if (NewValue != Value)
     {
         /* Rundown was active, use long path */
         return ExfAcquireRundownProtection(RunRef);
@@ -369,7 +422,7 @@ VOID
 FORCEINLINE
 _ExReleaseRundownProtection(IN PEX_RUNDOWN_REF RunRef)
 {
-    ULONG_PTR Value, NewValue, OldValue;
+    ULONG_PTR Value, NewValue;
 
     /* Get the current value and mask the active bit */
     Value = RunRef->Count &~ EX_RUNDOWN_ACTIVE;
@@ -378,10 +431,10 @@ _ExReleaseRundownProtection(IN PEX_RUNDOWN_REF RunRef)
     NewValue = Value - EX_RUNDOWN_COUNT_INC;
 
     /* Change the value */
-    OldValue = ExpChangeRundown(RunRef, NewValue, Value);
+    NewValue = ExpChangeRundown(RunRef, NewValue, Value);
 
     /* Check if the rundown was active */
-    if (OldValue != Value)
+    if (NewValue != Value)
     {
         /* Rundown was active, use long path */
         ExfReleaseRundownProtection(RunRef);
@@ -440,7 +493,7 @@ _ExWaitForRundownProtectionRelease(IN PEX_RUNDOWN_REF RunRef)
 
     /* Set the active bit */
     Value = ExpChangeRundown(RunRef, EX_RUNDOWN_ACTIVE, 0);
-    if ((Value) || (Value != EX_RUNDOWN_ACTIVE))
+    if ((Value) && (Value != EX_RUNDOWN_ACTIVE))
     {
         /* If the the rundown wasn't already active, then take the long path */
         ExfWaitForRundownProtectionRelease(RunRef);
@@ -475,6 +528,45 @@ _ExRundownCompleted(IN PEX_RUNDOWN_REF RunRef)
 
 /* PUSHLOCKS *****************************************************************/
 
+/* FIXME: VERIFY THESE! */
+
+VOID
+FASTCALL
+ExBlockPushLock(PEX_PUSH_LOCK PushLock,
+                PVOID WaitBlock);
+
+VOID
+FASTCALL
+ExfUnblockPushLock(PEX_PUSH_LOCK PushLock,
+                   PVOID CurrentWaitBlock);
+
+VOID
+FASTCALL
+ExWaitForUnblockPushLock(IN PEX_PUSH_LOCK PushLock,
+                         IN PEX_PUSH_LOCK_WAIT_BLOCK WaitBlock);
+
+/*++
+ * @name ExInitializePushLock
+ * INTERNAL MACRO
+ *
+ *     The ExInitializePushLock macro initializes a PushLock.
+ *
+ * @params PushLock
+ *         Pointer to the pushlock which is to be initialized.
+ *
+ * @return None.
+ *
+ * @remarks None.
+ *
+ *--*/
+VOID
+FORCEINLINE
+ExInitializePushLock(IN PULONG_PTR PushLock)
+{
+    /* Set the value to 0 */
+    *PushLock = 0;
+}
+
 /*++
  * @name ExAcquirePushLockExclusive
  * INTERNAL MACRO
@@ -537,7 +629,7 @@ ExAcquirePushLockShared(PEX_PUSH_LOCK PushLock)
 
     /* Try acquiring the lock */
     NewValue.Value = EX_PUSH_LOCK_LOCK | EX_PUSH_LOCK_SHARE_INC;
-    if (InterlockedCompareExchangePointer(PushLock, NewValue.Ptr, 0))
+    if (ExpChangePushlock(PushLock, NewValue.Ptr, 0))
     {
         /* Someone changed it, use the slow path */
         DbgPrint("%s - Contention!\n", __FUNCTION__);
@@ -549,6 +641,44 @@ ExAcquirePushLockShared(PEX_PUSH_LOCK PushLock)
     ASSERT(PushLock->Waiting || PushLock->Shared > 0);
 }
 
+/*++
+ * @name ExConvertPushLockSharedToExclusive
+ * INTERNAL MACRO
+ *
+ *     The ExConvertPushLockSharedToExclusive macro converts an exclusive
+ *     pushlock to a shared pushlock.
+ *
+ * @params PushLock
+ *         Pointer to the pushlock which is to be converted.
+ *
+ * @return FALSE if conversion failed, TRUE otherwise.
+ *
+ * @remarks The function attempts the quickest route to convert the lock, which is
+ *          to simply set the lock bit and remove any other bits.
+ *
+ *--*/
+BOOLEAN
+FORCEINLINE
+ExConvertPushLockSharedToExclusive(IN PEX_PUSH_LOCK PushLock)
+{
+    EX_PUSH_LOCK OldValue;
+
+    /* Set the expected old value */
+    OldValue.Value = EX_PUSH_LOCK_LOCK | EX_PUSH_LOCK_SHARE_INC;
+
+    /* Try converting the lock */
+    if (ExpChangePushlock(PushLock, EX_PUSH_LOCK_LOCK, OldValue.Value) !=
+        OldValue.Ptr)
+    {
+        /* Conversion failed */
+        return FALSE;
+    }
+
+    /* Sanity check */
+    ASSERT(PushLock->Locked);
+    return TRUE;
+}
+
 /*++
  * @name ExWaitOnPushLock
  * INTERNAL MACRO
@@ -570,12 +700,16 @@ VOID
 FORCEINLINE
 ExWaitOnPushLock(PEX_PUSH_LOCK PushLock)
 {
-    /* Acquire the lock */
-    ExfAcquirePushLockExclusive(PushLock);
-    ASSERT(PushLock->Locked);
+    /* Check if we're locked */
+    if (PushLock->Locked)
+    {
+        /* Acquire the lock */
+        ExfAcquirePushLockExclusive(PushLock);
+        ASSERT(PushLock->Locked);
 
-    /* Release it */
-    ExfReleasePushLockExclusive(PushLock);
+        /* Release it */
+        ExfReleasePushLockExclusive(PushLock);
+    }
 }
 
 /*++
@@ -609,8 +743,7 @@ ExReleasePushLockShared(PEX_PUSH_LOCK PushLock)
 
     /* Try to clear the pushlock */
     OldValue.Value = EX_PUSH_LOCK_LOCK | EX_PUSH_LOCK_SHARE_INC;
-    if (InterlockedCompareExchangePointer(PushLock, 0, OldValue.Ptr) !=
-        OldValue.Ptr)
+    if (ExpChangePushlock(PushLock, 0, OldValue.Ptr) != OldValue.Ptr)
     {
         /* There are still other people waiting on it */
         DbgPrint("%s - Contention!\n", __FUNCTION__);
@@ -650,7 +783,8 @@ ExReleasePushLockExclusive(PEX_PUSH_LOCK PushLock)
     ASSERT(PushLock->Waiting || PushLock->Shared == 0);
 
     /* Unlock the pushlock */
-    OldValue.Value = InterlockedExchangeAddSizeT((PLONG)PushLock, -1);
+    OldValue.Value = InterlockedExchangeAddSizeT((PLONG)PushLock,
+                                                 -EX_PUSH_LOCK_LOCK);
 
     /* Sanity checks */
     ASSERT(OldValue.Locked);
@@ -708,7 +842,7 @@ ExReleasePushLock(PEX_PUSH_LOCK PushLock)
 
     /* Check if nobody is waiting on us and try clearing the lock here */
     if ((OldValue.Waiting) ||
-        (InterlockedCompareExchangePointer(PushLock, NewValue.Ptr, OldValue.Ptr) ==
+        (ExpChangePushlock(PushLock, NewValue.Ptr, OldValue.Ptr) !=
          OldValue.Ptr))
     {
         /* We have waiters, use the long path */