]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/SynchronizationGcc.c
2 Implementation of synchronization functions.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "BaseSynchronizationLibInternals.h"
13 // GCC inline assembly for Read Write Barrier
15 #define _ReadWriteBarrier() do { __asm__ __volatile__ ("": : : "memory"); } while(0)
17 #define SPIN_LOCK_RELEASED ((UINTN) 1)
18 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
21 Retrieves the architecture specific spin lock alignment requirements for
22 optimal spin lock performance.
24 This function retrieves the spin lock alignment requirements for optimal
25 performance on a given CPU architecture. The spin lock alignment is byte alignment.
26 It must be a power of two and is returned by this function. If there are no alignment
27 requirements, then 1 must be returned. The spin lock synchronization
28 functions must function correctly if the spin lock size and alignment values
29 returned by this function are not used at all. These values are hints to the
30 consumers of the spin lock synchronization functions to obtain optimal spin
33 @return The architecture specific spin lock alignment.
38 GetSpinLockProperties (
42 return InternalGetSpinLockProperties ();
46 Initializes a spin lock to the released state and returns the spin lock.
48 This function initializes the spin lock specified by SpinLock to the released
49 state, and returns SpinLock. Optimal performance can be achieved by calling
50 GetSpinLockProperties() to determine the size and alignment requirements for
53 If SpinLock is NULL, then ASSERT().
55 @param SpinLock A pointer to the spin lock to initialize to the released
58 @return SpinLock is in release state.
64 OUT SPIN_LOCK
*SpinLock
67 ASSERT (SpinLock
!= NULL
);
70 *SpinLock
= SPIN_LOCK_RELEASED
;
77 Waits until a spin lock can be placed in the acquired state.
79 This function checks the state of the spin lock specified by SpinLock. If
80 SpinLock is in the released state, then this function places SpinLock in the
81 acquired state and returns SpinLock. Otherwise, this function waits
82 indefinitely for the spin lock to be released, and then places it in the
83 acquired state and returns SpinLock. All state transitions of SpinLock must
84 be performed using MP safe mechanisms.
86 If SpinLock is NULL, then ASSERT().
87 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
88 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
89 PcdSpinLockTimeout microseconds, then ASSERT().
91 @param SpinLock A pointer to the spin lock to place in the acquired state.
93 @return SpinLock acquired the lock.
99 IN OUT SPIN_LOCK
*SpinLock
111 if (PcdGet32 (PcdSpinLockTimeout
) == 0) {
112 while (!AcquireSpinLockOrFail (SpinLock
)) {
115 } else if (!AcquireSpinLockOrFail (SpinLock
)) {
117 // Get the current timer value
119 Current
= GetPerformanceCounter();
122 // Initialize local variables
129 // Retrieve the performance counter properties and compute the number of performance
130 // counter ticks required to reach the timeout
132 Timeout
= DivU64x32 (
134 GetPerformanceCounterProperties (&Start
, &End
),
135 PcdGet32 (PcdSpinLockTimeout
)
145 while (!AcquireSpinLockOrFail (SpinLock
)) {
148 Current
= GetPerformanceCounter();
149 Delta
= (INT64
) (Current
- Previous
);
157 ASSERT (Total
< Timeout
);
164 Attempts to place a spin lock in the acquired state.
166 This function checks the state of the spin lock specified by SpinLock. If
167 SpinLock is in the released state, then this function places SpinLock in the
168 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
169 transitions of SpinLock must be performed using MP safe mechanisms.
171 If SpinLock is NULL, then ASSERT().
172 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
174 @param SpinLock A pointer to the spin lock to place in the acquired state.
176 @retval TRUE SpinLock was placed in the acquired state.
177 @retval FALSE SpinLock could not be acquired.
182 AcquireSpinLockOrFail (
183 IN OUT SPIN_LOCK
*SpinLock
189 ASSERT (SpinLock
!= NULL
);
191 LockValue
= *SpinLock
;
192 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
194 _ReadWriteBarrier ();
195 Result
= InterlockedCompareExchangePointer (
197 (VOID
*)SPIN_LOCK_RELEASED
,
198 (VOID
*)SPIN_LOCK_ACQUIRED
201 _ReadWriteBarrier ();
202 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
206 Releases a spin lock.
208 This function places the spin lock specified by SpinLock in the release state
209 and returns SpinLock.
211 If SpinLock is NULL, then ASSERT().
212 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
214 @param SpinLock A pointer to the spin lock to release.
216 @return SpinLock released the lock.
222 IN OUT SPIN_LOCK
*SpinLock
227 ASSERT (SpinLock
!= NULL
);
229 LockValue
= *SpinLock
;
230 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
232 _ReadWriteBarrier ();
233 *SpinLock
= SPIN_LOCK_RELEASED
;
234 _ReadWriteBarrier ();
240 Performs an atomic increment of an 32-bit unsigned integer.
242 Performs an atomic increment of the 32-bit unsigned integer specified by
243 Value and returns the incremented value. The increment operation must be
244 performed using MP safe mechanisms.
246 If Value is NULL, then ASSERT().
248 @param Value A pointer to the 32-bit value to increment.
250 @return The incremented value.
255 InterlockedIncrement (
256 IN
volatile UINT32
*Value
259 ASSERT (Value
!= NULL
);
260 return InternalSyncIncrement (Value
);
264 Performs an atomic decrement of an 32-bit unsigned integer.
266 Performs an atomic decrement of the 32-bit unsigned integer specified by
267 Value and returns the decremented value. The decrement operation must be
268 performed using MP safe mechanisms.
270 If Value is NULL, then ASSERT().
272 @param Value A pointer to the 32-bit value to decrement.
274 @return The decremented value.
279 InterlockedDecrement (
280 IN
volatile UINT32
*Value
283 ASSERT (Value
!= NULL
);
284 return InternalSyncDecrement (Value
);
288 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
290 Performs an atomic compare exchange operation on the 16-bit unsigned integer
291 specified by Value. If Value is equal to CompareValue, then Value is set to
292 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
293 then Value is returned. The compare exchange operation must be performed using
296 If Value is NULL, then ASSERT().
298 @param Value A pointer to the 16-bit value for the compare exchange
300 @param CompareValue A 16-bit value used in compare operation.
301 @param ExchangeValue A 16-bit value used in exchange operation.
303 @return The original *Value before exchange.
308 InterlockedCompareExchange16 (
309 IN OUT
volatile UINT16
*Value
,
310 IN UINT16 CompareValue
,
311 IN UINT16 ExchangeValue
314 ASSERT (Value
!= NULL
);
315 return InternalSyncCompareExchange16 (Value
, CompareValue
, ExchangeValue
);
319 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
321 Performs an atomic compare exchange operation on the 32-bit unsigned integer
322 specified by Value. If Value is equal to CompareValue, then Value is set to
323 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
324 then Value is returned. The compare exchange operation must be performed using
327 If Value is NULL, then ASSERT().
329 @param Value A pointer to the 32-bit value for the compare exchange
331 @param CompareValue A 32-bit value used in compare operation.
332 @param ExchangeValue A 32-bit value used in exchange operation.
334 @return The original *Value before exchange.
339 InterlockedCompareExchange32 (
340 IN OUT
volatile UINT32
*Value
,
341 IN UINT32 CompareValue
,
342 IN UINT32 ExchangeValue
345 ASSERT (Value
!= NULL
);
346 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
350 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
352 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
353 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
354 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
355 The compare exchange operation must be performed using MP safe mechanisms.
357 If Value is NULL, then ASSERT().
359 @param Value A pointer to the 64-bit value for the compare exchange
361 @param CompareValue A 64-bit value used in a compare operation.
362 @param ExchangeValue A 64-bit value used in an exchange operation.
364 @return The original *Value before exchange.
369 InterlockedCompareExchange64 (
370 IN OUT
volatile UINT64
*Value
,
371 IN UINT64 CompareValue
,
372 IN UINT64 ExchangeValue
375 ASSERT (Value
!= NULL
);
376 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
380 Performs an atomic compare exchange operation on a pointer value.
382 Performs an atomic compare exchange operation on the pointer value specified
383 by Value. If Value is equal to CompareValue, then Value is set to
384 ExchangeValue and CompareValue is returned. If Value is not equal to
385 CompareValue, then Value is returned. The compare exchange operation must be
386 performed using MP safe mechanisms.
388 If Value is NULL, then ASSERT().
390 @param Value A pointer to the pointer value for the compare exchange
392 @param CompareValue A pointer value used in a compare operation.
393 @param ExchangeValue A pointer value used in an exchange operation.
395 @return The original *Value before exchange.
399 InterlockedCompareExchangePointer (
400 IN OUT VOID
* volatile *Value
,
401 IN VOID
*CompareValue
,
402 IN VOID
*ExchangeValue
407 SizeOfValue
= sizeof (*Value
);
409 switch (SizeOfValue
) {
410 case sizeof (UINT32
):
411 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
412 (volatile UINT32
*)Value
,
413 (UINT32
)(UINTN
)CompareValue
,
414 (UINT32
)(UINTN
)ExchangeValue
416 case sizeof (UINT64
):
417 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
418 (volatile UINT64
*)Value
,
419 (UINT64
)(UINTN
)CompareValue
,
420 (UINT64
)(UINTN
)ExchangeValue