]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseSynchronizationLib/SynchronizationGcc.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / SynchronizationGcc.c
index ea28ce0dafba6f7d499d3dd5a285b3832d26defc..466775e6021ee701ed7cf267848d4aab9790cd57 100644 (file)
@@ -1,22 +1,16 @@
 /** @file\r
   Implementation of synchronization functions.\r
 \r
-  Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
-  Portions copyright (c) 2008-2009 Apple Inc. All rights reserved.<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
-  http://opensource.org/licenses/bsd-license.php\r
-\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
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "BaseSynchronizationLibInternals.h"\r
 \r
 //\r
-// GCC inline assembly for Read Write Barrier  \r
+// GCC inline assembly for Read Write Barrier\r
 //\r
 #define _ReadWriteBarrier() do { __asm__ __volatile__ ("": : : "memory"); } while(0)\r
 \r
@@ -28,8 +22,8 @@
   optimal spin lock performance.\r
 \r
   This function retrieves the spin lock alignment requirements for optimal\r
-  performance on a given CPU architecture. The spin lock alignment must be a\r
-  power of two and is returned by this function. If there are no alignment\r
+  performance on a given CPU architecture. The spin lock alignment is byte alignment.\r
+  It must be a power of two and is returned by this function. If there are no alignment\r
   requirements, then 1 must be returned. The spin lock synchronization\r
   functions must function correctly if the spin lock size and alignment values\r
   returned by this function are not used at all. These values are hints to the\r
@@ -45,7 +39,7 @@ GetSpinLockProperties (
   VOID\r
   )\r
 {\r
-  return 32;\r
+  return InternalGetSpinLockProperties ();\r
 }\r
 \r
 /**\r
@@ -61,7 +55,7 @@ GetSpinLockProperties (
   @param  SpinLock  A pointer to the spin lock to initialize to the released\r
                     state.\r
 \r
-  @return SpinLock in release state.\r
+  @return SpinLock is in release state.\r
 \r
 **/\r
 SPIN_LOCK *\r
@@ -96,7 +90,7 @@ InitializeSpinLock (
 \r
   @param  SpinLock  A pointer to the spin lock to place in the acquired state.\r
 \r
-  @return SpinLock acquired lock.\r
+  @return SpinLock acquired the lock.\r
 \r
 **/\r
 SPIN_LOCK *\r
@@ -114,7 +108,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
@@ -158,10 +156,6 @@ AcquireSpinLock (
       Total += Delta;\r
       ASSERT (Total < Timeout);\r
     }\r
-  } else {\r
-    while (!AcquireSpinLockOrFail (SpinLock)) {\r
-      CpuPause ();\r
-    }\r
   }\r
   return SpinLock;\r
 }\r
@@ -191,7 +185,7 @@ AcquireSpinLockOrFail (
 {\r
   SPIN_LOCK   LockValue;\r
   VOID        *Result;\r
-  \r
+\r
   ASSERT (SpinLock != NULL);\r
 \r
   LockValue = *SpinLock;\r
@@ -219,7 +213,7 @@ AcquireSpinLockOrFail (
 \r
   @param  SpinLock  A pointer to the spin lock to release.\r
 \r
-  @return SpinLock released lock.\r
+  @return SpinLock released the lock.\r
 \r
 **/\r
 SPIN_LOCK *\r
@@ -247,8 +241,7 @@ ReleaseSpinLock (
 \r
   Performs an atomic increment of the 32-bit unsigned integer specified by\r
   Value and returns the incremented value. The increment operation must be\r
-  performed using MP safe mechanisms. The state of the return value is not\r
-  guaranteed to be MP safe.\r
+  performed using MP safe mechanisms.\r
 \r
   If Value is NULL, then ASSERT().\r
 \r
@@ -260,7 +253,7 @@ ReleaseSpinLock (
 UINT32\r
 EFIAPI\r
 InterlockedIncrement (\r
-  IN      UINT32                    *Value\r
+  IN      volatile UINT32           *Value\r
   )\r
 {\r
   ASSERT (Value != NULL);\r
@@ -272,8 +265,7 @@ InterlockedIncrement (
 \r
   Performs an atomic decrement of the 32-bit unsigned integer specified by\r
   Value and returns the decremented value. The decrement operation must be\r
-  performed using MP safe mechanisms. The state of the return value is not\r
-  guaranteed to be MP safe.\r
+  performed using MP safe mechanisms.\r
 \r
   If Value is NULL, then ASSERT().\r
 \r
@@ -285,13 +277,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  A 16-bit value used in compare operation.\r
+  @param  ExchangeValue A 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
@@ -305,8 +328,8 @@ InterlockedDecrement (
 \r
   @param  Value         A pointer to the 32-bit value for the compare exchange\r
                         operation.\r
-  @param  CompareValue  32-bit value used in compare operation.\r
-  @param  ExchangeValue 32-bit value used in exchange operation.\r
+  @param  CompareValue  32-bit value used in compare operation.\r
+  @param  ExchangeValue 32-bit value used in exchange operation.\r
 \r
   @return The original *Value before exchange.\r
 \r
@@ -314,7 +337,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
@@ -335,8 +358,8 @@ InterlockedCompareExchange32 (
 \r
   @param  Value         A pointer to the 64-bit value for the compare exchange\r
                         operation.\r
-  @param  CompareValue  64-bit value used in compare operation.\r
-  @param  ExchangeValue 64-bit value used in exchange operation.\r
+  @param  CompareValue  A 64-bit value used in a compare operation.\r
+  @param  ExchangeValue A 64-bit value used in an exchange operation.\r
 \r
   @return The original *Value before exchange.\r
 \r
@@ -344,7 +367,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
@@ -366,15 +389,15 @@ InterlockedCompareExchange64 (
 \r
   @param  Value         A pointer to the pointer value for the compare exchange\r
                         operation.\r
-  @param  CompareValue  Pointer value used in compare operation.\r
-  @param  ExchangeValue Pointer value used in exchange operation.\r
+  @param  CompareValue  A pointer value used in a compare operation.\r
+  @param  ExchangeValue A pointer value used in an exchange operation.\r
 \r
   @return The original *Value before exchange.\r
 **/\r
 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
@@ -386,13 +409,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