]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Synchronization.c
2 Implementation of synchronization functions.
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 // Include common header file for this module.
20 #include "BaseLibInternals.h"
22 #define SPIN_LOCK_RELEASED ((UINTN) 1)
23 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
26 Retrieves the architecture specific spin lock alignment requirements for
27 optimal spin lock performance.
29 This function retrieves the spin lock alignment requirements for optimal
30 performance on a given CPU architecture. The spin lock alignment must be a
31 power of two and is returned by this function. If there are no alignment
32 requirements, then 1 must be returned. The spin lock synchronization
33 functions must function correctly if the spin lock size and alignment values
34 returned by this function are not used at all. These values are hints to the
35 consumers of the spin lock synchronization functions to obtain optimal spin
38 @return The architecture specific spin lock alignment.
43 GetSpinLockProperties (
51 Initializes a spin lock to the released state and returns the spin lock.
53 This function initializes the spin lock specified by SpinLock to the released
54 state, and returns SpinLock. Optimal performance can be achieved by calling
55 GetSpinLockProperties() to determine the size and alignment requirements for
58 If SpinLock is NULL, then ASSERT().
60 @param SpinLock A pointer to the spin lock to initialize to the released
63 @return SpinLock initialized in release state.
69 OUT SPIN_LOCK
*SpinLock
72 ASSERT (SpinLock
!= NULL
);
73 *SpinLock
= SPIN_LOCK_RELEASED
;
78 Waits until a spin lock can be placed in the acquired state.
80 This function checks the state of the spin lock specified by SpinLock. If
81 SpinLock is in the released state, then this function places SpinLock in the
82 acquired state and returns SpinLock. Otherwise, this function waits
83 indefinitely for the spin lock to be released, and then places it in the
84 acquired state and returns SpinLock. All state transitions of SpinLock must
85 be performed using MP safe mechanisms.
87 If SpinLock is NULL, then ASSERT().
88 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
89 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
90 PcdSpinLockTimeout microseconds, then ASSERT().
92 @param SpinLock A pointer to the spin lock to place in the acquired state.
94 @return SpinLock aquired lock.
100 IN OUT SPIN_LOCK
*SpinLock
112 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
114 // Get the current timer value
116 Current
= GetPerformanceCounter();
119 // Initialize local variables
126 // Retrieve the performance counter properties and compute the number of performance
127 // counter ticks required to reach the timeout
129 Timeout
= DivU64x32 (
131 GetPerformanceCounterProperties (&Start
, &End
),
132 PcdGet32 (PcdSpinLockTimeout
)
142 while (!AcquireSpinLockOrFail (SpinLock
)) {
145 Current
= GetPerformanceCounter();
146 Delta
= (INT64
) (Current
- Previous
);
154 ASSERT (Total
< Timeout
);
157 while (!AcquireSpinLockOrFail (SpinLock
)) {
165 Attempts to place a spin lock in the acquired state.
167 This function checks the state of the spin lock specified by SpinLock. If
168 SpinLock is in the released state, then this function places SpinLock in the
169 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
170 transitions of SpinLock must be performed using MP safe mechanisms.
172 If SpinLock is NULL, then ASSERT().
173 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
175 @param SpinLock A pointer to the spin lock to place in the acquired state.
177 @retval TRUE SpinLock was placed in the acquired state.
178 @retval FALSE SpinLock could not be acquired.
183 AcquireSpinLockOrFail (
184 IN OUT SPIN_LOCK
*SpinLock
189 ASSERT (SpinLock
!= NULL
);
191 LockValue
= *SpinLock
;
192 ASSERT (SPIN_LOCK_ACQUIRED
== LockValue
|| SPIN_LOCK_RELEASED
== LockValue
);
195 InterlockedCompareExchangePointer (
197 (VOID
*)SPIN_LOCK_RELEASED
,
198 (VOID
*)SPIN_LOCK_ACQUIRED
199 ) == (VOID
*)SPIN_LOCK_RELEASED
204 Releases a spin lock.
206 This function places the spin lock specified by SpinLock in the release state
207 and returns SpinLock.
209 If SpinLock is NULL, then ASSERT().
210 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
212 @param SpinLock A pointer to the spin lock to release.
220 IN OUT SPIN_LOCK
*SpinLock
225 ASSERT (SpinLock
!= NULL
);
227 LockValue
= *SpinLock
;
228 ASSERT (SPIN_LOCK_ACQUIRED
== LockValue
|| SPIN_LOCK_RELEASED
== LockValue
);
230 *SpinLock
= SPIN_LOCK_RELEASED
;
235 Performs an atomic increment of an 32-bit unsigned integer.
237 Performs an atomic increment of the 32-bit unsigned integer specified by
238 Value and returns the incremented value. The increment operation must be
239 performed using MP safe mechanisms. The state of the return value is not
240 guaranteed to be MP safe.
242 If Value is NULL, then ASSERT().
244 @param Value A pointer to the 32-bit value to increment.
246 @return The incremented value.
251 InterlockedIncrement (
255 ASSERT (Value
!= NULL
);
256 return InternalSyncIncrement (Value
);
260 Performs an atomic decrement of an 32-bit unsigned integer.
262 Performs an atomic decrement of the 32-bit unsigned integer specified by
263 Value and returns the decremented value. The decrement operation must be
264 performed using MP safe mechanisms. The state of the return value is not
265 guaranteed to be MP safe.
267 If Value is NULL, then ASSERT().
269 @param Value A pointer to the 32-bit value to decrement.
271 @return The decremented value.
276 InterlockedDecrement (
280 ASSERT (Value
!= NULL
);
281 return InternalSyncDecrement (Value
);
285 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
287 Performs an atomic compare exchange operation on the 32-bit unsigned integer
288 specified by Value. If Value is equal to CompareValue, then Value is set to
289 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
290 then Value is returned. The compare exchange operation must be performed using
293 If Value is NULL, then ASSERT().
295 @param Value A pointer to the 32-bit value for the compare exchange
297 @param CompareValue 32-bit value used in compare operation.
298 @param ExchangeValue 32-bit value used in exchange operation.
300 @return The original *Value before exchange.
305 InterlockedCompareExchange32 (
306 IN OUT UINT32
*Value
,
307 IN UINT32 CompareValue
,
308 IN UINT32 ExchangeValue
311 ASSERT (Value
!= NULL
);
312 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
316 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
318 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
319 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
320 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
321 The compare exchange operation must be performed using MP safe mechanisms.
323 If Value is NULL, then ASSERT().
325 @param Value A pointer to the 64-bit value for the compare exchange
327 @param CompareValue 64-bit value used in compare operation.
328 @param ExchangeValue 64-bit value used in exchange operation.
330 @return The original *Value before exchange.
335 InterlockedCompareExchange64 (
336 IN OUT UINT64
*Value
,
337 IN UINT64 CompareValue
,
338 IN UINT64 ExchangeValue
341 ASSERT (Value
!= NULL
);
342 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
346 Performs an atomic compare exchange operation on a pointer value.
348 Performs an atomic compare exchange operation on the pointer value specified
349 by Value. If Value is equal to CompareValue, then Value is set to
350 ExchangeValue and CompareValue is returned. If Value is not equal to
351 CompareValue, then Value is returned. The compare exchange operation must be
352 performed using MP safe mechanisms.
354 If Value is NULL, then ASSERT().
356 @param Value A pointer to the pointer value for the compare exchange
358 @param CompareValue Pointer value used in compare operation.
359 @param ExchangeValue Pointer value used in exchange operation.
361 @return The original *Value before exchange.
366 InterlockedCompareExchangePointer (
368 IN VOID
*CompareValue
,
369 IN VOID
*ExchangeValue
374 SizeOfValue
= sizeof (*Value
);
376 switch (SizeOfValue
) {
377 case sizeof (UINT32
):
378 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
380 (UINT32
)(UINTN
)CompareValue
,
381 (UINT32
)(UINTN
)ExchangeValue
383 case sizeof (UINT64
):
384 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
386 (UINT64
)(UINTN
)CompareValue
,
387 (UINT64
)(UINTN
)ExchangeValue