]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/SynchronizationMsc.c
Update copyright for files modified in this year
[mirror_edk2.git] / MdePkg / Library / BaseLib / SynchronizationMsc.c
index fb10b5d01b59fceff5e0377e8a26c3653c3aa290..591d97e35437c44f68d645b8349db3e963967590 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implementation of synchronization functions.\r
 \r
-  Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
+  Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
   All rights reserved. This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-  Module Name:  SynchronizationMsc.c\r
-\r
 **/\r
 \r
+\r
+\r
+\r
 #include "BaseLibInternals.h"\r
 \r
-//\r
-// Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics\r
-//\r
+/**\r
+  Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics.\r
+**/\r
+\r
 void    _ReadWriteBarrier (void);\r
 #pragma intrinsic(_ReadWriteBarrier)\r
 \r
@@ -48,7 +50,6 @@ GetSpinLockProperties (
   VOID\r
   )\r
 {\r
-  // @bug May use a PCD entry to determine this alignment.\r
   return 32;\r
 }\r
 \r
@@ -65,13 +66,13 @@ GetSpinLockProperties (
   @param  SpinLock  A pointer to the spin lock to initialize to the released\r
                     state.\r
 \r
-  @return SpinLock\r
+  @return SpinLock in release state.\r
 \r
 **/\r
 SPIN_LOCK *\r
 EFIAPI\r
 InitializeSpinLock (\r
-  OUT     SPIN_LOCK                 *SpinLock\r
+  OUT      SPIN_LOCK                 *SpinLock\r
   )\r
 {\r
   ASSERT (SpinLock != NULL);\r
@@ -100,7 +101,7 @@ InitializeSpinLock (
 \r
   @param  SpinLock  A pointer to the spin lock to place in the acquired state.\r
 \r
-  @return SpinLock\r
+  @return SpinLock acquired lock.\r
 \r
 **/\r
 SPIN_LOCK *\r
@@ -109,15 +110,32 @@ AcquireSpinLock (
   IN OUT  SPIN_LOCK                 *SpinLock\r
   )\r
 {\r
-  UINT64                            Tick;\r
-  UINT64                            Start, End;\r
-  UINT64                            Timeout;\r
+  UINT64  Current;\r
+  UINT64  Previous;\r
+  UINT64  Total;\r
+  UINT64  Start;\r
+  UINT64  End;\r
+  UINT64  Timeout;\r
+  INT64   Cycle;\r
+  INT64   Delta;\r
 \r
-  Tick = 0;\r
-  Start = 0;\r
-  End = 0;\r
   if (PcdGet32 (PcdSpinLockTimeout) > 0) {\r
-    Tick = GetPerformanceCounter ();\r
+    //\r
+    // Get the current timer value\r
+    //\r
+    Current = GetPerformanceCounter();\r
+\r
+    //\r
+    // Initialize local variables\r
+    //\r
+    Start = 0;\r
+    End   = 0;\r
+    Total = 0;\r
+\r
+    //\r
+    // Retrieve the performance counter properties and compute the number of performance\r
+    // counter ticks required to reach the timeout\r
+    //\r
     Timeout = DivU64x32 (\r
                 MultU64x32 (\r
                   GetPerformanceCounterProperties (&Start, &End),\r
@@ -125,16 +143,30 @@ AcquireSpinLock (
                   ),\r
                 1000000\r
                 );\r
-    if (Start < End) {\r
-      Tick += Timeout;\r
-    } else {\r
-      Tick -= Timeout;\r
+    Cycle = End - Start;\r
+    if (Cycle < 0) {\r
+      Cycle = -Cycle;\r
+    }\r
+    Cycle++;\r
+\r
+    while (!AcquireSpinLockOrFail (SpinLock)) {\r
+      CpuPause ();\r
+      Previous = Current;\r
+      Current  = GetPerformanceCounter();\r
+      Delta = (INT64) (Current - Previous);\r
+      if (Start > End) {\r
+        Delta = -Delta;\r
+      }\r
+      if (Delta < 0) {\r
+        Delta += Cycle;\r
+      }\r
+      Total += Delta;\r
+      ASSERT (Total < Timeout);\r
+    }\r
+  } else {\r
+    while (!AcquireSpinLockOrFail (SpinLock)) {\r
+      CpuPause ();\r
     }\r
-  }\r
-\r
-  while (!AcquireSpinLockOrFail (SpinLock)) {\r
-    CpuPause ();\r
-    ASSERT ((Start < End) ^ (Tick <= GetPerformanceCounter ()));\r
   }\r
   return SpinLock;\r
 }\r
@@ -162,10 +194,13 @@ AcquireSpinLockOrFail (
   IN OUT  SPIN_LOCK                 *SpinLock\r
   )\r
 {\r
-  VOID  *Result;\r
+  SPIN_LOCK   LockValue;\r
+  VOID        *Result;\r
   \r
   ASSERT (SpinLock != NULL);\r
-  ASSERT (*SpinLock == SPIN_LOCK_ACQUIRED || *SpinLock == SPIN_LOCK_RELEASED);\r
+\r
+  LockValue = *SpinLock;\r
+  ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r
 \r
   _ReadWriteBarrier ();\r
   Result = InterlockedCompareExchangePointer (\r
@@ -189,7 +224,7 @@ AcquireSpinLockOrFail (
 \r
   @param  SpinLock  A pointer to the spin lock to release.\r
 \r
-  @return SpinLock\r
+  @return SpinLock released lock.\r
 \r
 **/\r
 SPIN_LOCK *\r
@@ -198,8 +233,12 @@ ReleaseSpinLock (
   IN OUT  SPIN_LOCK                 *SpinLock\r
   )\r
 {\r
+  SPIN_LOCK    LockValue;\r
+\r
   ASSERT (SpinLock != NULL);\r
-  ASSERT (*SpinLock == SPIN_LOCK_ACQUIRED || *SpinLock == SPIN_LOCK_RELEASED);\r
+\r
+  LockValue = *SpinLock;\r
+  ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r
 \r
   _ReadWriteBarrier ();\r
   *SpinLock = SPIN_LOCK_RELEASED;\r
@@ -262,9 +301,9 @@ InterlockedDecrement (
   Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
 \r
   Performs an atomic compare exchange operation on the 32-bit unsigned integer\r
-  specified by Value.  If Value is equal to CompareValue, then Value is set to \r
+  specified by Value.  If Value is equal to CompareValue, then Value is set to\r
   ExchangeValue and CompareValue is returned.  If Value is not equal to CompareValue,\r
-  then Value is returned.  The compare exchange operation must be performed using \r
+  then Value is returned.  The compare exchange operation must be performed using\r
   MP safe mechanisms.\r
 \r
   If Value is NULL, then ASSERT().\r
@@ -292,9 +331,9 @@ InterlockedCompareExchange32 (
 /**\r
   Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r
 \r
-  Performs an atomic compare exchange operation on the 64-bit unsigned integer specified \r
-  by Value.  If Value is equal to CompareValue, then Value is set to ExchangeValue and \r
-  CompareValue is returned.  If Value is not equal to CompareValue, then Value is returned. \r
+  Performs an atomic compare exchange operation on the 64-bit unsigned integer specified\r
+  by Value.  If Value is equal to CompareValue, then Value is set to ExchangeValue and\r
+  CompareValue is returned.  If Value is not equal to CompareValue, then Value is returned.\r
   The compare exchange operation must be performed using MP safe mechanisms.\r
 \r
   If Value is NULL, then ASSERT().\r
@@ -335,6 +374,7 @@ InterlockedCompareExchange64 (
   @param  CompareValue  Pointer value used in compare operation.\r
   @param  ExchangeValue Pointer value used in exchange operation.\r
 \r
+  @return The original *Value before exchange.\r
 **/\r
 VOID *\r
 EFIAPI\r