]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseSynchronizationLib/Ia32/GccInline.c
MdePkg/SynchronizationLib: fix Interlocked[De|In]crement return value
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / Ia32 / GccInline.c
index d357e918170cddfcbc5431606809d752f01f1770..d82e0205f553fc95a0507df06bac016a34bcb54f 100644 (file)
@@ -1,12 +1,12 @@
 /** @file\r
   GCC inline implementation of BaseSynchronizationLib processor specific functions.\r
-  \r
-  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+\r
+  Copyright (c) 2006 - 2018, 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
@@ -20,8 +20,7 @@
 \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
   @param  Value A pointer to the 32-bit value to increment.\r
 \r
@@ -37,17 +36,18 @@ InternalSyncIncrement (
   UINT32  Result;\r
 \r
   __asm__ __volatile__ (\r
+    "movl    $1, %%eax  \n\t"\r
     "lock               \n\t"\r
-    "incl    %2         \n\t"\r
-    "movl    %2, %%eax      "\r
+    "xadd    %%eax, %2  \n\t"\r
+    "inc     %%eax          "\r
     : "=a" (Result),          // %0\r
       "=m" (*Value)           // %1\r
-    : "m"  (*Value)           // %2 \r
+    : "m"  (*Value)           // %2\r
     : "memory",\r
       "cc"\r
     );\r
-    \r
-  return Result;    \r
+\r
+  return Result;\r
 \r
 }\r
 \r
@@ -57,8 +57,7 @@ InternalSyncIncrement (
 \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
   @param  Value A pointer to the 32-bit value to decrement.\r
 \r
@@ -72,21 +71,64 @@ InternalSyncDecrement (
   )\r
 {\r
    UINT32  Result;\r
-  \r
+\r
   __asm__ __volatile__ (\r
-    "lock               \n\t"\r
-    "decl    %2         \n\t"\r
-    "movl    %2, %%eax      "\r
+    "movl    $-1, %%eax  \n\t"\r
+    "lock                \n\t"\r
+    "xadd    %%eax, %2   \n\t"\r
+    "dec     %%eax                  "\r
     : "=a" (Result),          // %0\r
       "=m" (*Value)           // %1\r
-    : "m"  (*Value)           // %2 \r
+    : "m"  (*Value)           // %2\r
     : "memory",\r
       "cc"\r
     );\r
-    \r
+\r
   return Result;\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
+  __asm__ __volatile__ (\r
+    "                     \n\t"\r
+    "lock                 \n\t"\r
+    "cmpxchgw    %1, %2   \n\t"\r
+    : "=a" (CompareValue)\r
+    : "q"  (ExchangeValue),\r
+      "m"  (*Value),\r
+      "0"  (CompareValue)\r
+    : "memory",\r
+      "cc"\r
+    );\r
+\r
+  return CompareValue;\r
+}\r
+\r
 /**\r
   Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
 \r
@@ -121,7 +163,7 @@ InternalSyncCompareExchange32 (
     : "=a" (CompareValue)     // %0\r
     : "q"  (ExchangeValue),   // %1\r
       "m"  (*Value),          // %2\r
-      "0"  (CompareValue)     // %4 \r
+      "0"  (CompareValue)     // %4\r
     : "memory",\r
       "cc"\r
     );\r
@@ -156,8 +198,8 @@ InternalSyncCompareExchange64 (
 {\r
   __asm__ __volatile__ (\r
     "                       \n\t"\r
-    "push        %%ebx      \n\t" \r
-    "movl        %2,%%ebx   \n\t"   \r
+    "push        %%ebx      \n\t"\r
+    "movl        %2,%%ebx   \n\t"\r
     "lock                   \n\t"\r
     "cmpxchg8b   (%1)       \n\t"\r
     "pop         %%ebx      \n\t"\r
@@ -168,6 +210,6 @@ InternalSyncCompareExchange64 (
     : "memory",\r
       "cc"\r
     );\r
-  \r
+\r
   return CompareValue;\r
 }\r