]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseSynchronizationLib/Synchronization.c
MdePkg/BaseSynchronizationLib: Add volatile Interlocked*() APIs
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / Synchronization.c
index 0eea40ba1622bb2dfa4a87f023b1cd2451375618..4f2e21bcb32110789ad57bf549a7cd2d3b274feb 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implementation of synchronization functions.\r
 \r
-  Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
   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
@@ -104,7 +104,11 @@ AcquireSpinLock (
   INT64   Cycle;\r
   INT64   Delta;\r
 \r
-  if (PcdGet32 (PcdSpinLockTimeout) > 0) {\r
+  if (PcdGet32 (PcdSpinLockTimeout) == 0) {\r
+    while (!AcquireSpinLockOrFail (SpinLock)) {\r
+      CpuPause ();\r
+    }\r
+  } else if (!AcquireSpinLockOrFail (SpinLock)) {\r
     //\r
     // Get the current timer value\r
     //\r
@@ -148,10 +152,6 @@ AcquireSpinLock (
       Total += Delta;\r
       ASSERT (Total < Timeout);\r
     }\r
-  } else {\r
-    while (!AcquireSpinLockOrFail (SpinLock)) {\r
-      CpuPause ();\r
-    }\r
   }\r
   return SpinLock;\r
 }\r
@@ -244,7 +244,7 @@ ReleaseSpinLock (
 UINT32\r
 EFIAPI\r
 InterlockedIncrement (\r
-  IN      UINT32                    *Value\r
+  IN      volatile UINT32           *Value\r
   )\r
 {\r
   ASSERT (Value != NULL);\r
@@ -269,13 +269,44 @@ InterlockedIncrement (
 UINT32\r
 EFIAPI\r
 InterlockedDecrement (\r
-  IN      UINT32                    *Value\r
+  IN      volatile UINT32           *Value\r
   )\r
 {\r
   ASSERT (Value != NULL);\r
   return InternalSyncDecrement (Value);\r
 }\r
 \r
+/**\r
+  Performs an atomic compare exchange operation on a 16-bit unsigned integer.\r
+\r
+  Performs an atomic compare exchange operation on the 16-bit unsigned integer\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
+  MP safe mechanisms.\r
+\r
+  If Value is NULL, then ASSERT().\r
+\r
+  @param  Value         A pointer to the 16-bit value for the compare exchange\r
+                        operation.\r
+  @param  CompareValue  16-bit value used in compare operation.\r
+  @param  ExchangeValue 16-bit value used in exchange operation.\r
+\r
+  @return The original *Value before exchange.\r
+\r
+**/\r
+UINT16\r
+EFIAPI\r
+InterlockedCompareExchange16 (\r
+  IN OUT  volatile UINT16           *Value,\r
+  IN      UINT16                    CompareValue,\r
+  IN      UINT16                    ExchangeValue\r
+  )\r
+{\r
+  ASSERT (Value != NULL);\r
+  return InternalSyncCompareExchange16 (Value, CompareValue, ExchangeValue);\r
+}\r
+\r
 /**\r
   Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
 \r
@@ -298,7 +329,7 @@ InterlockedDecrement (
 UINT32\r
 EFIAPI\r
 InterlockedCompareExchange32 (\r
-  IN OUT  UINT32                    *Value,\r
+  IN OUT  volatile UINT32           *Value,\r
   IN      UINT32                    CompareValue,\r
   IN      UINT32                    ExchangeValue\r
   )\r
@@ -328,7 +359,7 @@ InterlockedCompareExchange32 (
 UINT64\r
 EFIAPI\r
 InterlockedCompareExchange64 (\r
-  IN OUT  UINT64                    *Value,\r
+  IN OUT  volatile UINT64           *Value,\r
   IN      UINT64                    CompareValue,\r
   IN      UINT64                    ExchangeValue\r
   )\r
@@ -358,7 +389,7 @@ InterlockedCompareExchange64 (
 VOID *\r
 EFIAPI\r
 InterlockedCompareExchangePointer (\r
-  IN OUT  VOID                      **Value,\r
+  IN OUT  VOID                      * volatile *Value,\r
   IN      VOID                      *CompareValue,\r
   IN      VOID                      *ExchangeValue\r
   )\r
@@ -370,13 +401,13 @@ InterlockedCompareExchangePointer (
   switch (SizeOfValue) {\r
     case sizeof (UINT32):\r
       return (VOID*)(UINTN)InterlockedCompareExchange32 (\r
-                             (UINT32*)Value,\r
+                             (volatile UINT32 *)Value,\r
                              (UINT32)(UINTN)CompareValue,\r
                              (UINT32)(UINTN)ExchangeValue\r
                              );\r
     case sizeof (UINT64):\r
       return (VOID*)(UINTN)InterlockedCompareExchange64 (\r
-                             (UINT64*)Value,\r
+                             (volatile UINT64 *)Value,\r
                              (UINT64)(UINTN)CompareValue,\r
                              (UINT64)(UINTN)ExchangeValue\r
                              );\r