X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdePkg%2FLibrary%2FBaseLib%2FSynchronizationMsc.c;h=591d97e35437c44f68d645b8349db3e963967590;hp=fb10b5d01b59fceff5e0377e8a26c3653c3aa290;hb=373ade0eb64a522e45b1b94c15b95fb5ab417c00;hpb=ef7baf58d031adb9aad451f761a1898190f482f5 diff --git a/MdePkg/Library/BaseLib/SynchronizationMsc.c b/MdePkg/Library/BaseLib/SynchronizationMsc.c index fb10b5d01b..591d97e354 100644 --- a/MdePkg/Library/BaseLib/SynchronizationMsc.c +++ b/MdePkg/Library/BaseLib/SynchronizationMsc.c @@ -1,7 +1,7 @@ /** @file Implementation of synchronization functions. - Copyright (c) 2006 - 2007, Intel Corporation
+ Copyright (c) 2006 - 2008, Intel Corporation
All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -10,15 +10,17 @@ 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: SynchronizationMsc.c - **/ + + + #include "BaseLibInternals.h" -// -// Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics -// +/** + Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics. +**/ + void _ReadWriteBarrier (void); #pragma intrinsic(_ReadWriteBarrier) @@ -48,7 +50,6 @@ GetSpinLockProperties ( VOID ) { - // @bug May use a PCD entry to determine this alignment. return 32; } @@ -65,13 +66,13 @@ GetSpinLockProperties ( @param SpinLock A pointer to the spin lock to initialize to the released state. - @return SpinLock + @return SpinLock in release state. **/ SPIN_LOCK * EFIAPI InitializeSpinLock ( - OUT SPIN_LOCK *SpinLock + OUT SPIN_LOCK *SpinLock ) { ASSERT (SpinLock != NULL); @@ -100,7 +101,7 @@ InitializeSpinLock ( @param SpinLock A pointer to the spin lock to place in the acquired state. - @return SpinLock + @return SpinLock acquired lock. **/ SPIN_LOCK * @@ -109,15 +110,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), @@ -125,16 +143,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; } @@ -162,10 +194,13 @@ AcquireSpinLockOrFail ( IN OUT SPIN_LOCK *SpinLock ) { - VOID *Result; + SPIN_LOCK LockValue; + VOID *Result; ASSERT (SpinLock != NULL); - ASSERT (*SpinLock == SPIN_LOCK_ACQUIRED || *SpinLock == SPIN_LOCK_RELEASED); + + LockValue = *SpinLock; + ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED); _ReadWriteBarrier (); Result = InterlockedCompareExchangePointer ( @@ -189,7 +224,7 @@ AcquireSpinLockOrFail ( @param SpinLock A pointer to the spin lock to release. - @return SpinLock + @return SpinLock released lock. **/ SPIN_LOCK * @@ -198,8 +233,12 @@ ReleaseSpinLock ( IN OUT SPIN_LOCK *SpinLock ) { + SPIN_LOCK LockValue; + ASSERT (SpinLock != NULL); - ASSERT (*SpinLock == SPIN_LOCK_ACQUIRED || *SpinLock == SPIN_LOCK_RELEASED); + + LockValue = *SpinLock; + ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED); _ReadWriteBarrier (); *SpinLock = SPIN_LOCK_RELEASED; @@ -262,9 +301,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(). @@ -292,9 +331,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(). @@ -335,6 +374,7 @@ InterlockedCompareExchange64 ( @param CompareValue Pointer value used in compare operation. @param ExchangeValue Pointer value used in exchange operation. + @return The original *Value before exchange. **/ VOID * EFIAPI