]>
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
)
135 while (!AcquireSpinLockOrFail (SpinLock
)) {
138 Current
= GetPerformanceCounter();
139 Delta
= (INT64
) (Current
- Previous
);
147 ASSERT (Total
< Timeout
);
154 Attempts to place a spin lock in the acquired state.
156 This function checks the state of the spin lock specified by SpinLock. If
157 SpinLock is in the released state, then this function places SpinLock in the
158 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
159 transitions of SpinLock must be performed using MP safe mechanisms.
161 If SpinLock is NULL, then ASSERT().
162 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
164 @param SpinLock A pointer to the spin lock to place in the acquired state.
166 @retval TRUE SpinLock was placed in the acquired state.
167 @retval FALSE SpinLock could not be acquired.
172 AcquireSpinLockOrFail (
173 IN OUT SPIN_LOCK
*SpinLock
178 ASSERT (SpinLock
!= NULL
);
180 LockValue
= *SpinLock
;
181 ASSERT (SPIN_LOCK_ACQUIRED
== LockValue
|| SPIN_LOCK_RELEASED
== LockValue
);
184 InterlockedCompareExchangePointer (
186 (VOID
*)SPIN_LOCK_RELEASED
,
187 (VOID
*)SPIN_LOCK_ACQUIRED
188 ) == (VOID
*)SPIN_LOCK_RELEASED
193 Releases a spin lock.
195 This function places the spin lock specified by SpinLock in the release state
196 and returns SpinLock.
198 If SpinLock is NULL, then ASSERT().
199 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
201 @param SpinLock A pointer to the spin lock to release.
203 @return SpinLock released lock.
209 IN OUT SPIN_LOCK
*SpinLock
214 ASSERT (SpinLock
!= NULL
);
216 LockValue
= *SpinLock
;
217 ASSERT (SPIN_LOCK_ACQUIRED
== LockValue
|| SPIN_LOCK_RELEASED
== LockValue
);
219 *SpinLock
= SPIN_LOCK_RELEASED
;
224 Performs an atomic increment of an 32-bit unsigned integer.
226 Performs an atomic increment of the 32-bit unsigned integer specified by
227 Value and returns the incremented value. The increment operation must be
228 performed using MP safe mechanisms.
230 If Value is NULL, then ASSERT().
232 @param Value A pointer to the 32-bit value to increment.
234 @return The incremented value.
239 InterlockedIncrement (
240 IN
volatile UINT32
*Value
243 ASSERT (Value
!= NULL
);
244 return InternalSyncIncrement (Value
);
248 Performs an atomic decrement of an 32-bit unsigned integer.
250 Performs an atomic decrement of the 32-bit unsigned integer specified by
251 Value and returns the decremented value. The decrement operation must be
252 performed using MP safe mechanisms.
254 If Value is NULL, then ASSERT().
256 @param Value A pointer to the 32-bit value to decrement.
258 @return The decremented value.
263 InterlockedDecrement (
264 IN
volatile UINT32
*Value
267 ASSERT (Value
!= NULL
);
268 return InternalSyncDecrement (Value
);
272 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
274 Performs an atomic compare exchange operation on the 16-bit unsigned integer
275 specified by Value. If Value is equal to CompareValue, then Value is set to
276 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
277 then Value is returned. The compare exchange operation must be performed using
280 If Value is NULL, then ASSERT().
282 @param Value A pointer to the 16-bit value for the compare exchange
284 @param CompareValue 16-bit value used in compare operation.
285 @param ExchangeValue 16-bit value used in exchange operation.
287 @return The original *Value before exchange.
292 InterlockedCompareExchange16 (
293 IN OUT
volatile UINT16
*Value
,
294 IN UINT16 CompareValue
,
295 IN UINT16 ExchangeValue
298 ASSERT (Value
!= NULL
);
299 return InternalSyncCompareExchange16 (Value
, CompareValue
, ExchangeValue
);
303 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
305 Performs an atomic compare exchange operation on the 32-bit unsigned integer
306 specified by Value. If Value is equal to CompareValue, then Value is set to
307 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
308 then Value is returned. The compare exchange operation must be performed using
311 If Value is NULL, then ASSERT().
313 @param Value A pointer to the 32-bit value for the compare exchange
315 @param CompareValue 32-bit value used in compare operation.
316 @param ExchangeValue 32-bit value used in exchange operation.
318 @return The original *Value before exchange.
323 InterlockedCompareExchange32 (
324 IN OUT
volatile UINT32
*Value
,
325 IN UINT32 CompareValue
,
326 IN UINT32 ExchangeValue
329 ASSERT (Value
!= NULL
);
330 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
334 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
336 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
337 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
338 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
339 The compare exchange operation must be performed using MP safe mechanisms.
341 If Value is NULL, then ASSERT().
343 @param Value A pointer to the 64-bit value for the compare exchange
345 @param CompareValue 64-bit value used in compare operation.
346 @param ExchangeValue 64-bit value used in exchange operation.
348 @return The original *Value before exchange.
353 InterlockedCompareExchange64 (
354 IN OUT
volatile UINT64
*Value
,
355 IN UINT64 CompareValue
,
356 IN UINT64 ExchangeValue
359 ASSERT (Value
!= NULL
);
360 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
364 Performs an atomic compare exchange operation on a pointer value.
366 Performs an atomic compare exchange operation on the pointer value specified
367 by Value. If Value is equal to CompareValue, then Value is set to
368 ExchangeValue and CompareValue is returned. If Value is not equal to
369 CompareValue, then Value is returned. The compare exchange operation must be
370 performed using MP safe mechanisms.
372 If Value is NULL, then ASSERT().
374 @param Value A pointer to the pointer value for the compare exchange
376 @param CompareValue Pointer value used in compare operation.
377 @param ExchangeValue Pointer value used in exchange operation.
379 @return The original *Value before exchange.
383 InterlockedCompareExchangePointer (
384 IN OUT VOID
* volatile *Value
,
385 IN VOID
*CompareValue
,
386 IN VOID
*ExchangeValue
391 SizeOfValue
= sizeof (*Value
);
393 switch (SizeOfValue
) {
394 case sizeof (UINT32
):
395 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
396 (volatile UINT32
*)Value
,
397 (UINT32
)(UINTN
)CompareValue
,
398 (UINT32
)(UINTN
)ExchangeValue
400 case sizeof (UINT64
):
401 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
402 (volatile UINT64
*)Value
,
403 (UINT64
)(UINTN
)CompareValue
,
404 (UINT64
)(UINTN
)ExchangeValue