]>
git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/BaseLib/Synchronization.c
3 Copyright (c) 2004 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Implementation of synchronization functions.
23 #include "BaseLibInternal.h"
25 #define SPIN_LOCK_RELEASED ((SPIN_LOCK)1)
26 #define SPIN_LOCK_ACQUIRED ((SPIN_LOCK)2)
29 Retrieves the architecture specific spin lock alignment requirements for
30 optimal spin lock performance.
32 This function retrieves the spin lock alignment requirements for optimal
33 performance on a given CPU architecture. The spin lock alignment must be a
34 power of two and is returned by this function. If there are no alignment
35 requirements, then 1 must be returned. The spin lock synchronization
36 functions must function correctly if the spin lock size and alignment values
37 returned by this function are not used at all. These values are hints to the
38 consumers of the spin lock synchronization functions to obtain optimal spin
41 @return The architecture specific spin lock alignment.
46 GetSpinLockProperties (
50 // @bug May use a PCD entry to determine this alignment.
55 Initializes a spin lock to the released state and returns the spin lock.
57 This function initializes the spin lock specified by SpinLock to the released
58 state, and returns SpinLock. Optimal performance can be achieved by calling
59 GetSpinLockProperties() to determine the size and alignment requirements for
62 If SpinLock is NULL, then ASSERT().
64 @param SpinLock A pointer to the spin lock to initialize to the released
73 OUT SPIN_LOCK
*SpinLock
76 ASSERT (SpinLock
!= NULL
);
77 *SpinLock
= SPIN_LOCK_RELEASED
;
82 Waits until a spin lock can be placed in the acquired state.
84 This function checks the state of the spin lock specified by SpinLock. If
85 SpinLock is in the released state, then this function places SpinLock in the
86 acquired state and returns SpinLock. Otherwise, this function waits
87 indefinitely for the spin lock to be released, and then places it in the
88 acquired state and returns SpinLock. All state transitions of SpinLock must
89 be performed using MP safe mechanisms.
91 If SpinLock is NULL, then ASSERT().
92 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
93 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
94 PcdSpinLockTimeout microseconds, then ASSERT().
96 @param SpinLock A pointer to the spin lock to place in the acquired state.
104 IN OUT SPIN_LOCK
*SpinLock
114 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
115 Tick
= GetPerformanceCounter ();
116 Timeout
= DivU64x32 (
118 GetPerformanceCounterProperties (&Start
, &End
),
119 PcdGet32 (PcdSpinLockTimeout
)
130 while (!AcquireSpinLockOrFail (SpinLock
)) {
132 ASSERT ((Start
< End
) ^ (Tick
<= GetPerformanceCounter ()));
138 Attempts to place a spin lock in the acquired state.
140 This function checks the state of the spin lock specified by SpinLock. If
141 SpinLock is in the released state, then this function places SpinLock in the
142 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
143 transitions of SpinLock must be performed using MP safe mechanisms.
145 If SpinLock is NULL, then ASSERT().
146 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
148 @param SpinLock A pointer to the spin lock to place in the acquired state.
150 @retval TRUE SpinLock was placed in the acquired state.
151 @retval FALSE SpinLock could not be acquired.
156 AcquireSpinLockOrFail (
157 IN OUT SPIN_LOCK
*SpinLock
160 volatile SPIN_LOCK LockValue
;
162 ASSERT (SpinLock
!= NULL
);
164 LockValue
= *SpinLock
;
165 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
168 InterlockedCompareExchangePointer (
170 (VOID
*)SPIN_LOCK_RELEASED
,
171 (VOID
*)SPIN_LOCK_ACQUIRED
172 ) == (VOID
*)SPIN_LOCK_RELEASED
177 Releases a spin lock.
179 This function places the spin lock specified by SpinLock in the release state
180 and returns SpinLock.
182 If SpinLock is NULL, then ASSERT().
183 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
185 @param SpinLock A pointer to the spin lock to release.
193 IN OUT SPIN_LOCK
*SpinLock
196 volatile SPIN_LOCK LockValue
;
198 ASSERT (SpinLock
!= NULL
);
200 LockValue
= *SpinLock
;
201 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
203 *SpinLock
= SPIN_LOCK_RELEASED
;
208 Performs an atomic increment of an 32-bit unsigned integer.
210 Performs an atomic increment of the 32-bit unsigned integer specified by
211 Value and returns the incremented value. The increment operation must be
212 performed using MP safe mechanisms. The state of the return value is not
213 guaranteed to be MP safe.
215 If Value is NULL, then ASSERT().
217 @param Value A pointer to the 32-bit value to increment.
219 @return The incremented value.
224 InterlockedIncrement (
228 ASSERT (Value
!= NULL
);
229 return InternalSyncIncrement (Value
);
233 Performs an atomic decrement of an 32-bit unsigned integer.
235 Performs an atomic decrement of the 32-bit unsigned integer specified by
236 Value and returns the decremented value. The decrement operation must be
237 performed using MP safe mechanisms. The state of the return value is not
238 guaranteed to be MP safe.
240 If Value is NULL, then ASSERT().
242 @param Value A pointer to the 32-bit value to decrement.
244 @return The decremented value.
249 InterlockedDecrement (
253 ASSERT (Value
!= NULL
);
254 return InternalSyncDecrement (Value
);
258 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
260 Performs an atomic compare exchange operation on the 32-bit unsigned integer
261 specified by Value. If Value is equal to CompareValue, then Value is set to
262 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
263 then Value is returned. The compare exchange operation must be performed using
266 If Value is NULL, then ASSERT().
268 @param Value A pointer to the 32-bit value for the compare exchange
270 @param CompareValue 32-bit value used in compare operation.
271 @param ExchangeValue 32-bit value used in exchange operation.
273 @return The original *Value before exchange.
278 InterlockedCompareExchange32 (
279 IN OUT UINT32
*Value
,
280 IN UINT32 CompareValue
,
281 IN UINT32 ExchangeValue
284 ASSERT (Value
!= NULL
);
285 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
289 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
291 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
292 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
293 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
294 The compare exchange operation must be performed using MP safe mechanisms.
296 If Value is NULL, then ASSERT().
298 @param Value A pointer to the 64-bit value for the compare exchange
300 @param CompareValue 64-bit value used in compare operation.
301 @param ExchangeValue 64-bit value used in exchange operation.
303 @return The original *Value before exchange.
308 InterlockedCompareExchange64 (
309 IN OUT UINT64
*Value
,
310 IN UINT64 CompareValue
,
311 IN UINT64 ExchangeValue
314 ASSERT (Value
!= NULL
);
315 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
319 Performs an atomic compare exchange operation on a pointer value.
321 Performs an atomic compare exchange operation on the pointer value specified
322 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
324 CompareValue, then Value is returned. The compare exchange operation must be
325 performed using MP safe mechanisms.
327 If Value is NULL, then ASSERT().
329 @param Value A pointer to the pointer value for the compare exchange
331 @param CompareValue Pointer value used in compare operation.
332 @param ExchangeValue Pointer value used in exchange operation.
337 InterlockedCompareExchangePointer (
339 IN VOID
*CompareValue
,
340 IN VOID
*ExchangeValue
345 SizeOfValue
= sizeof (*Value
);
347 switch (SizeOfValue
) {
348 case sizeof (UINT32
):
349 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
351 (UINT32
)(UINTN
)CompareValue
,
352 (UINT32
)(UINTN
)ExchangeValue
354 case sizeof (UINT64
):
355 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
357 (UINT64
)(UINTN
)CompareValue
,
358 (UINT64
)(UINTN
)ExchangeValue