X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FBaseLib%2FSynchronization.c;h=b84188e61869acdd12aa7f785e38c36350e1dc9d;hb=9aa049d9717f1260426918eab2d5986e838a8754;hp=30dabfbc20edc35e54e31f37b0a9fd83a45a7e69;hpb=e1f414b6a7d8a0424e0e01f655b09a4612b4d0e8;p=mirror_edk2.git diff --git a/MdePkg/Library/BaseLib/Synchronization.c b/MdePkg/Library/BaseLib/Synchronization.c index 30dabfbc20..b84188e618 100644 --- a/MdePkg/Library/BaseLib/Synchronization.c +++ b/MdePkg/Library/BaseLib/Synchronization.c @@ -10,15 +10,8 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - Module Name: Synchronization.c - **/ -// -// Include common header file for this module. -// -#include "CommonHeader.h" - #include "BaseLibInternals.h" #define SPIN_LOCK_RELEASED ((UINTN) 1) @@ -46,7 +39,6 @@ GetSpinLockProperties ( VOID ) { - // @bug May use a PCD entry to determine this alignment. return 32; } @@ -63,7 +55,7 @@ GetSpinLockProperties ( @param SpinLock A pointer to the spin lock to initialize to the released state. - @return SpinLock + @return SpinLock in release state. **/ SPIN_LOCK * @@ -94,7 +86,7 @@ InitializeSpinLock ( @param SpinLock A pointer to the spin lock to place in the acquired state. - @return SpinLock + @return SpinLock acquired lock. **/ SPIN_LOCK * @@ -103,15 +95,32 @@ AcquireSpinLock ( IN OUT SPIN_LOCK *SpinLock ) { - UINT64 Tick; - UINT64 Start, End; - UINT64 Timeout; + UINT64 Current; + UINT64 Previous; + UINT64 Total; + UINT64 Start; + UINT64 End; + UINT64 Timeout; + INT64 Cycle; + INT64 Delta; - Tick = 0; - Start = 0; - End = 0; if (PcdGet32 (PcdSpinLockTimeout) > 0) { - Tick = GetPerformanceCounter (); + // + // Get the current timer value + // + Current = GetPerformanceCounter(); + + // + // Initialize local variables + // + Start = 0; + End = 0; + Total = 0; + + // + // Retrieve the performance counter properties and compute the number of performance + // counter ticks required to reach the timeout + // Timeout = DivU64x32 ( MultU64x32 ( GetPerformanceCounterProperties (&Start, &End), @@ -119,16 +128,30 @@ AcquireSpinLock ( ), 1000000 ); - if (Start < End) { - Tick += Timeout; - } else { - Tick -= Timeout; + Cycle = End - Start; + if (Cycle < 0) { + Cycle = -Cycle; + } + Cycle++; + + while (!AcquireSpinLockOrFail (SpinLock)) { + CpuPause (); + Previous = Current; + Current = GetPerformanceCounter(); + Delta = (INT64) (Current - Previous); + if (Start > End) { + Delta = -Delta; + } + if (Delta < 0) { + Delta += Cycle; + } + Total += Delta; + ASSERT (Total < Timeout); + } + } else { + while (!AcquireSpinLockOrFail (SpinLock)) { + CpuPause (); } - } - - while (!AcquireSpinLockOrFail (SpinLock)) { - CpuPause (); - ASSERT ((Start < End) ^ (Tick <= GetPerformanceCounter ())); } return SpinLock; } @@ -161,7 +184,7 @@ AcquireSpinLockOrFail ( ASSERT (SpinLock != NULL); LockValue = *SpinLock; - ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED); + ASSERT (SPIN_LOCK_ACQUIRED == LockValue || SPIN_LOCK_RELEASED == LockValue); return (BOOLEAN)( InterlockedCompareExchangePointer ( @@ -183,7 +206,7 @@ AcquireSpinLockOrFail ( @param SpinLock A pointer to the spin lock to release. - @return SpinLock + @return SpinLock released lock. **/ SPIN_LOCK * @@ -197,7 +220,7 @@ ReleaseSpinLock ( ASSERT (SpinLock != NULL); LockValue = *SpinLock; - ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED); + ASSERT (SPIN_LOCK_ACQUIRED == LockValue || SPIN_LOCK_RELEASED == LockValue); *SpinLock = SPIN_LOCK_RELEASED; return SpinLock; @@ -257,9 +280,9 @@ 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 + 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 + then Value is returned. The compare exchange operation must be performed using MP safe mechanisms. If Value is NULL, then ASSERT(). @@ -287,9 +310,9 @@ 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. + 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(). @@ -329,6 +352,8 @@ InterlockedCompareExchange64 ( operation. @param CompareValue Pointer value used in compare operation. @param ExchangeValue Pointer value used in exchange operation. + + @return The original *Value before exchange. **/ VOID *