]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/SynchronizationGcc.c
2 Implementation of synchronization functions.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "BaseSynchronizationLibInternals.h"
19 // GCC inline assembly for Read Write Barrier
21 #define _ReadWriteBarrier() do { __asm__ __volatile__ ("": : : "memory"); } while(0)
23 #define SPIN_LOCK_RELEASED ((UINTN) 1)
24 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
27 Retrieves the architecture specific spin lock alignment requirements for
28 optimal spin lock performance.
30 This function retrieves the spin lock alignment requirements for optimal
31 performance on a given CPU architecture. The spin lock alignment must be a
32 power of two and is returned by this function. If there are no alignment
33 requirements, then 1 must be returned. The spin lock synchronization
34 functions must function correctly if the spin lock size and alignment values
35 returned by this function are not used at all. These values are hints to the
36 consumers of the spin lock synchronization functions to obtain optimal spin
39 @return The architecture specific spin lock alignment.
44 GetSpinLockProperties (
52 Initializes a spin lock to the released state and returns the spin lock.
54 This function initializes the spin lock specified by SpinLock to the released
55 state, and returns SpinLock. Optimal performance can be achieved by calling
56 GetSpinLockProperties() to determine the size and alignment requirements for
59 If SpinLock is NULL, then ASSERT().
61 @param SpinLock A pointer to the spin lock to initialize to the released
64 @return SpinLock is in release state.
70 OUT SPIN_LOCK
*SpinLock
73 ASSERT (SpinLock
!= NULL
);
76 *SpinLock
= SPIN_LOCK_RELEASED
;
83 Waits until a spin lock can be placed in the acquired state.
85 This function checks the state of the spin lock specified by SpinLock. If
86 SpinLock is in the released state, then this function places SpinLock in the
87 acquired state and returns SpinLock. Otherwise, this function waits
88 indefinitely for the spin lock to be released, and then places it in the
89 acquired state and returns SpinLock. All state transitions of SpinLock must
90 be performed using MP safe mechanisms.
92 If SpinLock is NULL, then ASSERT().
93 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
94 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
95 PcdSpinLockTimeout microseconds, then ASSERT().
97 @param SpinLock A pointer to the spin lock to place in the acquired state.
99 @return SpinLock acquired the lock.
105 IN OUT SPIN_LOCK
*SpinLock
117 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
119 // Get the current timer value
121 Current
= GetPerformanceCounter();
124 // Initialize local variables
131 // Retrieve the performance counter properties and compute the number of performance
132 // counter ticks required to reach the timeout
134 Timeout
= DivU64x32 (
136 GetPerformanceCounterProperties (&Start
, &End
),
137 PcdGet32 (PcdSpinLockTimeout
)
147 while (!AcquireSpinLockOrFail (SpinLock
)) {
150 Current
= GetPerformanceCounter();
151 Delta
= (INT64
) (Current
- Previous
);
159 ASSERT (Total
< Timeout
);
162 while (!AcquireSpinLockOrFail (SpinLock
)) {
170 Attempts to place a spin lock in the acquired state.
172 This function checks the state of the spin lock specified by SpinLock. If
173 SpinLock is in the released state, then this function places SpinLock in the
174 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
175 transitions of SpinLock must be performed using MP safe mechanisms.
177 If SpinLock is NULL, then ASSERT().
178 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
180 @param SpinLock A pointer to the spin lock to place in the acquired state.
182 @retval TRUE SpinLock was placed in the acquired state.
183 @retval FALSE SpinLock could not be acquired.
188 AcquireSpinLockOrFail (
189 IN OUT SPIN_LOCK
*SpinLock
195 ASSERT (SpinLock
!= NULL
);
197 LockValue
= *SpinLock
;
198 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
200 _ReadWriteBarrier ();
201 Result
= InterlockedCompareExchangePointer (
203 (VOID
*)SPIN_LOCK_RELEASED
,
204 (VOID
*)SPIN_LOCK_ACQUIRED
207 _ReadWriteBarrier ();
208 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
212 Releases a spin lock.
214 This function places the spin lock specified by SpinLock in the release state
215 and returns SpinLock.
217 If SpinLock is NULL, then ASSERT().
218 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
220 @param SpinLock A pointer to the spin lock to release.
222 @return SpinLock released the lock.
228 IN OUT SPIN_LOCK
*SpinLock
233 ASSERT (SpinLock
!= NULL
);
235 LockValue
= *SpinLock
;
236 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
238 _ReadWriteBarrier ();
239 *SpinLock
= SPIN_LOCK_RELEASED
;
240 _ReadWriteBarrier ();
246 Performs an atomic increment of an 32-bit unsigned integer.
248 Performs an atomic increment of the 32-bit unsigned integer specified by
249 Value and returns the incremented value. The increment operation must be
250 performed using MP safe mechanisms. The state of the return value is not
251 guaranteed to be MP safe.
253 If Value is NULL, then ASSERT().
255 @param Value A pointer to the 32-bit value to increment.
257 @return The incremented value.
262 InterlockedIncrement (
266 ASSERT (Value
!= NULL
);
267 return InternalSyncIncrement (Value
);
271 Performs an atomic decrement of an 32-bit unsigned integer.
273 Performs an atomic decrement of the 32-bit unsigned integer specified by
274 Value and returns the decremented value. The decrement operation must be
275 performed using MP safe mechanisms. The state of the return value is not
276 guaranteed to be MP safe.
278 If Value is NULL, then ASSERT().
280 @param Value A pointer to the 32-bit value to decrement.
282 @return The decremented value.
287 InterlockedDecrement (
291 ASSERT (Value
!= NULL
);
292 return InternalSyncDecrement (Value
);
296 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
298 Performs an atomic compare exchange operation on the 32-bit unsigned integer
299 specified by Value. If Value is equal to CompareValue, then Value is set to
300 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
301 then Value is returned. The compare exchange operation must be performed using
304 If Value is NULL, then ASSERT().
306 @param Value A pointer to the 32-bit value for the compare exchange
308 @param CompareValue A 32-bit value used in compare operation.
309 @param ExchangeValue A 32-bit value used in exchange operation.
311 @return The original *Value before exchange.
316 InterlockedCompareExchange32 (
317 IN OUT UINT32
*Value
,
318 IN UINT32 CompareValue
,
319 IN UINT32 ExchangeValue
322 ASSERT (Value
!= NULL
);
323 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
327 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
329 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
330 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
331 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
332 The compare exchange operation must be performed using MP safe mechanisms.
334 If Value is NULL, then ASSERT().
336 @param Value A pointer to the 64-bit value for the compare exchange
338 @param CompareValue A 64-bit value used in a compare operation.
339 @param ExchangeValue A 64-bit value used in an exchange operation.
341 @return The original *Value before exchange.
346 InterlockedCompareExchange64 (
347 IN OUT UINT64
*Value
,
348 IN UINT64 CompareValue
,
349 IN UINT64 ExchangeValue
352 ASSERT (Value
!= NULL
);
353 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
357 Performs an atomic compare exchange operation on a pointer value.
359 Performs an atomic compare exchange operation on the pointer value specified
360 by Value. If Value is equal to CompareValue, then Value is set to
361 ExchangeValue and CompareValue is returned. If Value is not equal to
362 CompareValue, then Value is returned. The compare exchange operation must be
363 performed using MP safe mechanisms.
365 If Value is NULL, then ASSERT().
367 @param Value A pointer to the pointer value for the compare exchange
369 @param CompareValue A pointer value used in a compare operation.
370 @param ExchangeValue A pointer value used in an exchange operation.
372 @return The original *Value before exchange.
376 InterlockedCompareExchangePointer (
378 IN VOID
*CompareValue
,
379 IN VOID
*ExchangeValue
384 SizeOfValue
= sizeof (*Value
);
386 switch (SizeOfValue
) {
387 case sizeof (UINT32
):
388 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
390 (UINT32
)(UINTN
)CompareValue
,
391 (UINT32
)(UINTN
)ExchangeValue
393 case sizeof (UINT64
):
394 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
396 (UINT64
)(UINTN
)CompareValue
,
397 (UINT64
)(UINTN
)ExchangeValue