]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/Synchronization.c
2 Implementation of synchronization functions.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "BaseSynchronizationLibInternals.h"
11 #define SPIN_LOCK_RELEASED ((UINTN) 1)
12 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
15 Retrieves the architecture specific spin lock alignment requirements for
16 optimal spin lock performance.
18 This function retrieves the spin lock alignment requirements for optimal
19 performance on a given CPU architecture. The spin lock alignment is byte alignment.
20 It must be a power of two and is returned by this function. If there are no alignment
21 requirements, then 1 must be returned. The spin lock synchronization
22 functions must function correctly if the spin lock size and alignment values
23 returned by this function are not used at all. These values are hints to the
24 consumers of the spin lock synchronization functions to obtain optimal spin
27 @return The architecture specific spin lock alignment.
32 GetSpinLockProperties (
40 Initializes a spin lock to the released state and returns the spin lock.
42 This function initializes the spin lock specified by SpinLock to the released
43 state, and returns SpinLock. Optimal performance can be achieved by calling
44 GetSpinLockProperties() to determine the size and alignment requirements for
47 If SpinLock is NULL, then ASSERT().
49 @param SpinLock A pointer to the spin lock to initialize to the released
52 @return SpinLock is in release state.
58 OUT SPIN_LOCK
*SpinLock
61 ASSERT (SpinLock
!= NULL
);
62 *SpinLock
= SPIN_LOCK_RELEASED
;
67 Waits until a spin lock can be placed in the acquired state.
69 This function checks the state of the spin lock specified by SpinLock. If
70 SpinLock is in the released state, then this function places SpinLock in the
71 acquired state and returns SpinLock. Otherwise, this function waits
72 indefinitely for the spin lock to be released, and then places it in the
73 acquired state and returns SpinLock. All state transitions of SpinLock must
74 be performed using MP safe mechanisms.
76 If SpinLock is NULL, then ASSERT().
77 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
78 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
79 PcdSpinLockTimeout microseconds, then ASSERT().
81 @param SpinLock A pointer to the spin lock to place in the acquired state.
83 @return SpinLock acquired the lock.
89 IN OUT SPIN_LOCK
*SpinLock
101 if (PcdGet32 (PcdSpinLockTimeout
) == 0) {
102 while (!AcquireSpinLockOrFail (SpinLock
)) {
105 } else if (!AcquireSpinLockOrFail (SpinLock
)) {
107 // Get the current timer value
109 Current
= GetPerformanceCounter ();
112 // Initialize local variables
119 // Retrieve the performance counter properties and compute the number of performance
120 // counter ticks required to reach the timeout
122 Timeout
= DivU64x32 (
124 GetPerformanceCounterProperties (&Start
, &End
),
125 PcdGet32 (PcdSpinLockTimeout
)
136 while (!AcquireSpinLockOrFail (SpinLock
)) {
139 Current
= GetPerformanceCounter ();
140 Delta
= (INT64
)(Current
- Previous
);
150 ASSERT (Total
< Timeout
);
158 Attempts to place a spin lock in the acquired state.
160 This function checks the state of the spin lock specified by SpinLock. If
161 SpinLock is in the released state, then this function places SpinLock in the
162 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
163 transitions of SpinLock must be performed using MP safe mechanisms.
165 If SpinLock is NULL, then ASSERT().
166 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
168 @param SpinLock A pointer to the spin lock to place in the acquired state.
170 @retval TRUE SpinLock was placed in the acquired state.
171 @retval FALSE SpinLock could not be acquired.
176 AcquireSpinLockOrFail (
177 IN OUT SPIN_LOCK
*SpinLock
182 ASSERT (SpinLock
!= NULL
);
184 LockValue
= *SpinLock
;
185 ASSERT (SPIN_LOCK_ACQUIRED
== LockValue
|| SPIN_LOCK_RELEASED
== LockValue
);
188 InterlockedCompareExchangePointer (
190 (VOID
*)SPIN_LOCK_RELEASED
,
191 (VOID
*)SPIN_LOCK_ACQUIRED
192 ) == (VOID
*)SPIN_LOCK_RELEASED
197 Releases a spin lock.
199 This function places the spin lock specified by SpinLock in the release state
200 and returns SpinLock.
202 If SpinLock is NULL, then ASSERT().
203 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
205 @param SpinLock A pointer to the spin lock to release.
207 @return SpinLock released lock.
213 IN OUT SPIN_LOCK
*SpinLock
218 ASSERT (SpinLock
!= NULL
);
220 LockValue
= *SpinLock
;
221 ASSERT (SPIN_LOCK_ACQUIRED
== LockValue
|| SPIN_LOCK_RELEASED
== LockValue
);
223 *SpinLock
= SPIN_LOCK_RELEASED
;
228 Performs an atomic increment of an 32-bit unsigned integer.
230 Performs an atomic increment of the 32-bit unsigned integer specified by
231 Value and returns the incremented value. The increment operation must be
232 performed using MP safe mechanisms.
234 If Value is NULL, then ASSERT().
236 @param Value A pointer to the 32-bit value to increment.
238 @return The incremented value.
243 InterlockedIncrement (
244 IN
volatile UINT32
*Value
247 ASSERT (Value
!= NULL
);
248 return InternalSyncIncrement (Value
);
252 Performs an atomic decrement of an 32-bit unsigned integer.
254 Performs an atomic decrement of the 32-bit unsigned integer specified by
255 Value and returns the decremented value. The decrement operation must be
256 performed using MP safe mechanisms.
258 If Value is NULL, then ASSERT().
260 @param Value A pointer to the 32-bit value to decrement.
262 @return The decremented value.
267 InterlockedDecrement (
268 IN
volatile UINT32
*Value
271 ASSERT (Value
!= NULL
);
272 return InternalSyncDecrement (Value
);
276 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
278 Performs an atomic compare exchange operation on the 16-bit unsigned integer
279 specified by Value. If Value is equal to CompareValue, then Value is set to
280 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
281 then Value is returned. The compare exchange operation must be performed using
284 If Value is NULL, then ASSERT().
286 @param Value A pointer to the 16-bit value for the compare exchange
288 @param CompareValue 16-bit value used in compare operation.
289 @param ExchangeValue 16-bit value used in exchange operation.
291 @return The original *Value before exchange.
296 InterlockedCompareExchange16 (
297 IN OUT
volatile UINT16
*Value
,
298 IN UINT16 CompareValue
,
299 IN UINT16 ExchangeValue
302 ASSERT (Value
!= NULL
);
303 return InternalSyncCompareExchange16 (Value
, CompareValue
, ExchangeValue
);
307 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
309 Performs an atomic compare exchange operation on the 32-bit unsigned integer
310 specified by Value. If Value is equal to CompareValue, then Value is set to
311 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
312 then Value is returned. The compare exchange operation must be performed using
315 If Value is NULL, then ASSERT().
317 @param Value A pointer to the 32-bit value for the compare exchange
319 @param CompareValue 32-bit value used in compare operation.
320 @param ExchangeValue 32-bit value used in exchange operation.
322 @return The original *Value before exchange.
327 InterlockedCompareExchange32 (
328 IN OUT
volatile UINT32
*Value
,
329 IN UINT32 CompareValue
,
330 IN UINT32 ExchangeValue
333 ASSERT (Value
!= NULL
);
334 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
338 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
340 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
341 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
342 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
343 The compare exchange operation must be performed using MP safe mechanisms.
345 If Value is NULL, then ASSERT().
347 @param Value A pointer to the 64-bit value for the compare exchange
349 @param CompareValue 64-bit value used in compare operation.
350 @param ExchangeValue 64-bit value used in exchange operation.
352 @return The original *Value before exchange.
357 InterlockedCompareExchange64 (
358 IN OUT
volatile UINT64
*Value
,
359 IN UINT64 CompareValue
,
360 IN UINT64 ExchangeValue
363 ASSERT (Value
!= NULL
);
364 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
368 Performs an atomic compare exchange operation on a pointer value.
370 Performs an atomic compare exchange operation on the pointer value specified
371 by Value. If Value is equal to CompareValue, then Value is set to
372 ExchangeValue and CompareValue is returned. If Value is not equal to
373 CompareValue, then Value is returned. The compare exchange operation must be
374 performed using MP safe mechanisms.
376 If Value is NULL, then ASSERT().
378 @param Value A pointer to the pointer value for the compare exchange
380 @param CompareValue Pointer value used in compare operation.
381 @param ExchangeValue Pointer value used in exchange operation.
383 @return The original *Value before exchange.
387 InterlockedCompareExchangePointer (
388 IN OUT VOID
*volatile *Value
,
389 IN VOID
*CompareValue
,
390 IN VOID
*ExchangeValue
395 SizeOfValue
= sizeof (*Value
);
397 switch (SizeOfValue
) {
398 case sizeof (UINT32
):
399 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
400 (volatile UINT32
*)Value
,
401 (UINT32
)(UINTN
)CompareValue
,
402 (UINT32
)(UINTN
)ExchangeValue
404 case sizeof (UINT64
):
405 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
406 (volatile UINT64
*)Value
,
407 (UINT64
)(UINTN
)CompareValue
,
408 (UINT64
)(UINTN
)ExchangeValue