X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FBaseLib%2FSynchronization.c;h=503313322424a30c97b24feeba6ba17c72448727;hb=c9708dddc1a2d10725860d5e9ac1b4929d0b43f4;hp=df395869a295a1b8f0675caaf5624e3b55152ef0;hpb=878ddf1fc3540a715f63594ed22b6929e881afb4;p=mirror_edk2.git diff --git a/MdePkg/Library/BaseLib/Synchronization.c b/MdePkg/Library/BaseLib/Synchronization.c index df395869a2..5033133224 100644 --- a/MdePkg/Library/BaseLib/Synchronization.c +++ b/MdePkg/Library/BaseLib/Synchronization.c @@ -14,21 +14,64 @@ **/ -#define SPIN_LOCK_RELEASED ((SPIN_LOCK)0) -#define SPIN_LOCK_ACQUIRED ((SPIN_LOCK)-1) +#define SPIN_LOCK_RELEASED ((SPIN_LOCK)1) +#define SPIN_LOCK_ACQUIRED ((SPIN_LOCK)2) +/** + 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. + + @param Value A pointer to the 32-bit value to increment. + + @return The incremented value. + +**/ UINT32 EFIAPI InternalSyncIncrement ( IN volatile UINT32 *Value ); +/** + Performs an atomic decrement of an 32-bit unsigned integer. + + Performs an atomic decrement of the 32-bit unsigned integer specified by + Value and returns the decrement value. The decrement operation must be + performed using MP safe mechanisms. The state of the return value is not + guaranteed to be MP safe. + + @param Value A pointer to the 32-bit value to decrement. + + @return The decrement value. + +**/ UINT32 EFIAPI InternalSyncDecrement ( IN volatile UINT32 *Value ); +/** + Performs an atomic compare exchange operation on a 32-bit unsigned integer. + + Performs an atomic compare exchange operation on the 32-bit unsigned integer + specified by Value. If Value is equal to CompareValue, then Value is set to + ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue, + then Value is returned. The compare exchange operation must be performed using + MP safe mechanisms. + + @param Value A pointer to the 32-bit value for the compare exchange + operation. + @param CompareValue 32-bit value used in compare operation. + @param ExchangeValue 32-bit value used in exchange operation. + + @return The original *Value before exchange. + +**/ UINT32 EFIAPI InternalSyncCompareExchange32 ( @@ -37,6 +80,22 @@ InternalSyncCompareExchange32 ( IN UINT32 ExchangeValue ); +/** + Performs an atomic compare exchange operation on a 64-bit unsigned integer. + + Performs an atomic compare exchange operation on the 64-bit unsigned integer specified + by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and + CompareValue is returned. If Value is not equal to CompareValue, then Value is returned. + The compare exchange operation must be performed using MP safe mechanisms. + + @param Value A pointer to the 64-bit value for the compare exchange + operation. + @param CompareValue 64-bit value used in compare operation. + @param ExchangeValue 64-bit value used in exchange operation. + + @return The original *Value before exchange. + +**/ UINT64 EFIAPI InternalSyncCompareExchange64 ( @@ -94,7 +153,7 @@ InitializeSpinLock ( ) { ASSERT (SpinLock != NULL); - *SpinLock = 0; + *SpinLock = SPIN_LOCK_RELEASED; return SpinLock; } @@ -131,12 +190,12 @@ AcquireSpinLock ( Tick = 0; Start = 0; End = 0; - if (FixedPcdGet32 (PcdSpinLockTimeout) > 0) { + if (PcdGet32 (PcdSpinLockTimeout) > 0) { Tick = GetPerformanceCounter (); Timeout = DivU64x32 ( MultU64x32 ( GetPerformanceCounterProperties (&Start, &End), - FixedPcdGet32 (PcdSpinLockTimeout) + PcdGet32 (PcdSpinLockTimeout) ), 1000000 ); @@ -177,7 +236,13 @@ AcquireSpinLockOrFail ( IN OUT SPIN_LOCK *SpinLock ) { + SPIN_LOCK LockValue; + ASSERT (SpinLock != NULL); + + LockValue = *SpinLock; + ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED); + return (BOOLEAN)( InterlockedCompareExchangePointer ( (VOID**)SpinLock, @@ -207,8 +272,14 @@ ReleaseSpinLock ( IN OUT SPIN_LOCK *SpinLock ) { + SPIN_LOCK LockValue; + ASSERT (SpinLock != NULL); - *SpinLock = 0; + + LockValue = *SpinLock; + ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED); + + *SpinLock = SPIN_LOCK_RELEASED; return SpinLock; } @@ -265,6 +336,14 @@ InterlockedDecrement ( /** Performs an atomic compare exchange operation on a 32-bit unsigned integer. + Performs an atomic compare exchange operation on the 32-bit unsigned integer + specified by Value. If Value is equal to CompareValue, then Value is set to + ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue, + then Value is returned. The compare exchange operation must be performed using + MP safe mechanisms. + + If Value is NULL, then ASSERT(). + @param Value A pointer to the 32-bit value for the compare exchange operation. @param CompareValue 32-bit value used in compare operation. @@ -276,7 +355,7 @@ InterlockedDecrement ( UINT32 EFIAPI InterlockedCompareExchange32 ( - IN UINT32 *Value, + IN OUT UINT32 *Value, IN UINT32 CompareValue, IN UINT32 ExchangeValue ) @@ -288,6 +367,13 @@ InterlockedCompareExchange32 ( /** Performs an atomic compare exchange operation on a 64-bit unsigned integer. + Performs an atomic compare exchange operation on the 64-bit unsigned integer specified + by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and + CompareValue is returned. If Value is not equal to CompareValue, then Value is returned. + The compare exchange operation must be performed using MP safe mechanisms. + + If Value is NULL, then ASSERT(). + @param Value A pointer to the 64-bit value for the compare exchange operation. @param CompareValue 64-bit value used in compare operation. @@ -299,7 +385,7 @@ InterlockedCompareExchange32 ( UINT64 EFIAPI InterlockedCompareExchange64 ( - IN UINT64 *Value, + IN OUT UINT64 *Value, IN UINT64 CompareValue, IN UINT64 ExchangeValue ) @@ -328,7 +414,7 @@ InterlockedCompareExchange64 ( VOID * EFIAPI InterlockedCompareExchangePointer ( - IN VOID **Value, + IN OUT VOID **Value, IN VOID *CompareValue, IN VOID *ExchangeValue )