]> 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 bd81aad6c243622d3e88782d3d66ba45068a8624..d82e0205f553fc95a0507df06bac016a34bcb54f 100644 (file)
@@ -1,7 +1,7 @@
 /** @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
@@ -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,18 +71,19 @@ 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
@@ -163,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
@@ -198,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
@@ -210,6 +210,6 @@ InternalSyncCompareExchange64 (
     : "memory",\r
       "cc"\r
     );\r
-  \r
+\r
   return CompareValue;\r
 }\r