]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseSynchronizationLib/X64/GccInline.c
MdePkg/BaseSynchronizationLib: Add volatile Interlocked*() APIs
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / X64 / GccInline.c
index f2f72f80a46c040a581eba67a4622c4e1f276c45..6347073fee510c2fb9f14ee9440ac34dad9bfcc0 100644 (file)
@@ -1,12 +1,12 @@
 /** @file\r
   GCC inline implementation of BaseSynchronizationLib processor specific functions.\r
   \r
-  Copyright (c) 2006 - 2009, 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
+  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Portions copyright (c) 2008 - 2009, Apple Inc. 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
-  http://opensource.org/licenses/bsd-license.php\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
@@ -49,7 +49,6 @@ InternalSyncIncrement (
     );\r
     \r
   return Result;    \r
-\r
 }\r
 \r
 \r
@@ -89,6 +88,50 @@ InternalSyncDecrement (
 }\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
+\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
+InternalSyncCompareExchange16 (\r
+  IN OUT volatile  UINT16           *Value,\r
+  IN      UINT16                    CompareValue,\r
+  IN      UINT16                    ExchangeValue\r
+  )\r
+{\r
+\r
+\r
+  __asm__ __volatile__ (\r
+    "lock                 \n\t"\r
+    "cmpxchgw    %3, %1       "\r
+    : "=a" (CompareValue),\r
+      "=m" (*Value)\r
+    : "a"  (CompareValue),\r
+      "r"  (ExchangeValue),\r
+      "m"  (*Value)\r
+    : "memory",\r
+      "cc"\r
+    );\r
+\r
+  return CompareValue;\r
+}\r
+\r
+\r
 /**\r
   Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
 \r
@@ -115,12 +158,7 @@ InternalSyncCompareExchange32 (
   IN      UINT32                    ExchangeValue\r
   )\r
 {\r
-// GCC 4.1 and forward supports atomic builtins  \r
-#if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 1)))\r
 \r
-  return __sync_val_compare_and_swap (Value, CompareValue, ExchangeValue);\r
-\r
-#else\r
 \r
   __asm__ __volatile__ (\r
     "lock                 \n\t"\r
@@ -134,9 +172,7 @@ InternalSyncCompareExchange32 (
       "cc"\r
     );\r
     \r
-    return CompareValue;\r
-\r
-#endif\r
+  return CompareValue;\r
 }\r
 \r
 \r
@@ -165,12 +201,6 @@ InternalSyncCompareExchange64 (
   IN      UINT64                    ExchangeValue\r
   )\r
 {\r
-// GCC 4.1 and forward supports atomic builtins  \r
-#if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 1)))\r
-\r
-  return __sync_val_compare_and_swap (Value, CompareValue, ExchangeValue);\r
-\r
-#else\r
 \r
   __asm__ __volatile__ (\r
     "lock                 \n\t"\r
@@ -185,8 +215,6 @@ InternalSyncCompareExchange64 (
     );\r
   \r
   return CompareValue;\r
-\r
-#endif\r
 }\r
 \r
 \r