MdePkg/SynchronizationLib: fix Interlocked[De|In]crement return value
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1197 Today's InterlockedIncrement()/InterlockedDecrement() guarantees to perform atomic increment/decrement but doesn't guarantee the return value equals to the new value. The patch fixes the behavior to use "XADD" instruction to guarantee the return value equals to the new value. The patch calls intrinsic functions for MSVC tool chain, calls the NASM implementation for INTEL tool chain and calls GCC inline assembly implementation (GccInline.c) for GCC tool chain. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com> Cc: Jiewen Yao <jiewen.yao@intel.com> Reviewed-by: Liming Gao <liming.gao@intel.com> Cc: Michael D Kinney <michael.d.kinney@intel.com>
This commit is contained in:
@@ -15,14 +15,12 @@
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Performs an atomic increment of an 32-bit unsigned integer.
|
||||
|
||||
Performs an atomic increment of the 32-bit unsigned integer specified by
|
||||
Value and returns the incremented value. The increment operation must be
|
||||
performed using MP safe mechanisms. The state of the return value is not
|
||||
guaranteed to be MP safe.
|
||||
performed using MP safe mechanisms.
|
||||
|
||||
@param Value A pointer to the 32-bit value to increment.
|
||||
|
||||
@@ -38,9 +36,10 @@ InternalSyncIncrement (
|
||||
UINT32 Result;
|
||||
|
||||
__asm__ __volatile__ (
|
||||
"movl $1, %%eax \n\t"
|
||||
"lock \n\t"
|
||||
"incl %2 \n\t"
|
||||
"mov %2, %%eax "
|
||||
"xadd %%eax, %2 \n\t"
|
||||
"inc %%eax "
|
||||
: "=a" (Result), // %0
|
||||
"=m" (*Value) // %1
|
||||
: "m" (*Value) // %2
|
||||
@@ -57,8 +56,7 @@ InternalSyncIncrement (
|
||||
|
||||
Performs an atomic decrement of the 32-bit unsigned integer specified by
|
||||
Value and returns the decremented value. The decrement operation must be
|
||||
performed using MP safe mechanisms. The state of the return value is not
|
||||
guaranteed to be MP safe.
|
||||
performed using MP safe mechanisms.
|
||||
|
||||
@param Value A pointer to the 32-bit value to decrement.
|
||||
|
||||
@@ -74,9 +72,10 @@ InternalSyncDecrement (
|
||||
UINT32 Result;
|
||||
|
||||
__asm__ __volatile__ (
|
||||
"lock \n\t"
|
||||
"decl %2 \n\t"
|
||||
"mov %2, %%eax "
|
||||
"movl $-1, %%eax \n\t"
|
||||
"lock \n\t"
|
||||
"xadd %%eax, %2 \n\t"
|
||||
"dec %%eax "
|
||||
: "=a" (Result), // %0
|
||||
"=m" (*Value) // %1
|
||||
: "m" (*Value) // %2
|
||||
|
Reference in New Issue
Block a user