]>
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.
18 #include "BaseLibInternals.h"
20 #define SPIN_LOCK_RELEASED ((UINTN) 1)
21 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
24 Retrieves the architecture specific spin lock alignment requirements for
25 optimal spin lock performance.
27 This function retrieves the spin lock alignment requirements for optimal
28 performance on a given CPU architecture. The spin lock alignment must be a
29 power of two and is returned by this function. If there are no alignment
30 requirements, then 1 must be returned. The spin lock synchronization
31 functions must function correctly if the spin lock size and alignment values
32 returned by this function are not used at all. These values are hints to the
33 consumers of the spin lock synchronization functions to obtain optimal spin
36 @return The architecture specific spin lock alignment.
41 GetSpinLockProperties (
49 Initializes a spin lock to the released state and returns the spin lock.
51 This function initializes the spin lock specified by SpinLock to the released
52 state, and returns SpinLock. Optimal performance can be achieved by calling
53 GetSpinLockProperties() to determine the size and alignment requirements for
56 If SpinLock is NULL, then ASSERT().
58 @param SpinLock A pointer to the spin lock to initialize to the released
61 @return SpinLock initialized in release state.
67 OUT SPIN_LOCK
*SpinLock
70 ASSERT (SpinLock
!= NULL
);
71 *SpinLock
= SPIN_LOCK_RELEASED
;
76 Waits until a spin lock can be placed in the acquired state.
78 This function checks the state of the spin lock specified by SpinLock. If
79 SpinLock is in the released state, then this function places SpinLock in the
80 acquired state and returns SpinLock. Otherwise, this function waits
81 indefinitely for the spin lock to be released, and then places it in the
82 acquired state and returns SpinLock. All state transitions of SpinLock must
83 be performed using MP safe mechanisms.
85 If SpinLock is NULL, then ASSERT().
86 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
87 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
88 PcdSpinLockTimeout microseconds, then ASSERT().
90 @param SpinLock A pointer to the spin lock to place in the acquired state.
92 @return SpinLock aquired lock.
98 IN OUT SPIN_LOCK
*SpinLock
110 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
112 // Get the current timer value
114 Current
= GetPerformanceCounter();
117 // Initialize local variables
124 // Retrieve the performance counter properties and compute the number of performance
125 // counter ticks required to reach the timeout
127 Timeout
= DivU64x32 (
129 GetPerformanceCounterProperties (&Start
, &End
),
130 PcdGet32 (PcdSpinLockTimeout
)
140 while (!AcquireSpinLockOrFail (SpinLock
)) {
143 Current
= GetPerformanceCounter();
144 Delta
= (INT64
) (Current
- Previous
);
152 ASSERT (Total
< Timeout
);
155 while (!AcquireSpinLockOrFail (SpinLock
)) {
163 Attempts to place a spin lock in the acquired state.
165 This function checks the state of the spin lock specified by SpinLock. If
166 SpinLock is in the released state, then this function places SpinLock in the
167 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
168 transitions of SpinLock must be performed using MP safe mechanisms.
170 If SpinLock is NULL, then ASSERT().
171 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
173 @param SpinLock A pointer to the spin lock to place in the acquired state.
175 @retval TRUE SpinLock was placed in the acquired state.
176 @retval FALSE SpinLock could not be acquired.
181 AcquireSpinLockOrFail (
182 IN OUT SPIN_LOCK
*SpinLock
187 ASSERT (SpinLock
!= NULL
);
189 LockValue
= *SpinLock
;
190 ASSERT (SPIN_LOCK_ACQUIRED
== LockValue
|| SPIN_LOCK_RELEASED
== LockValue
);
193 InterlockedCompareExchangePointer (
195 (VOID
*)SPIN_LOCK_RELEASED
,
196 (VOID
*)SPIN_LOCK_ACQUIRED
197 ) == (VOID
*)SPIN_LOCK_RELEASED
202 Releases a spin lock.
204 This function places the spin lock specified by SpinLock in the release state
205 and returns SpinLock.
207 If SpinLock is NULL, then ASSERT().
208 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
210 @param SpinLock A pointer to the spin lock to release.
218 IN OUT SPIN_LOCK
*SpinLock
223 ASSERT (SpinLock
!= NULL
);
225 LockValue
= *SpinLock
;
226 ASSERT (SPIN_LOCK_ACQUIRED
== LockValue
|| SPIN_LOCK_RELEASED
== LockValue
);
228 *SpinLock
= SPIN_LOCK_RELEASED
;
233 Performs an atomic increment of an 32-bit unsigned integer.
235 Performs an atomic increment of the 32-bit unsigned integer specified by
236 Value and returns the incremented value. The increment 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 increment.
244 @return The incremented value.
249 InterlockedIncrement (
253 ASSERT (Value
!= NULL
);
254 return InternalSyncIncrement (Value
);
258 Performs an atomic decrement of an 32-bit unsigned integer.
260 Performs an atomic decrement of the 32-bit unsigned integer specified by
261 Value and returns the decremented value. The decrement operation must be
262 performed using MP safe mechanisms. The state of the return value is not
263 guaranteed to be MP safe.
265 If Value is NULL, then ASSERT().
267 @param Value A pointer to the 32-bit value to decrement.
269 @return The decremented value.
274 InterlockedDecrement (
278 ASSERT (Value
!= NULL
);
279 return InternalSyncDecrement (Value
);
283 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
285 Performs an atomic compare exchange operation on the 32-bit unsigned integer
286 specified by Value. If Value is equal to CompareValue, then Value is set to
287 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
288 then Value is returned. The compare exchange operation must be performed using
291 If Value is NULL, then ASSERT().
293 @param Value A pointer to the 32-bit value for the compare exchange
295 @param CompareValue 32-bit value used in compare operation.
296 @param ExchangeValue 32-bit value used in exchange operation.
298 @return The original *Value before exchange.
303 InterlockedCompareExchange32 (
304 IN OUT UINT32
*Value
,
305 IN UINT32 CompareValue
,
306 IN UINT32 ExchangeValue
309 ASSERT (Value
!= NULL
);
310 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
314 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
316 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
317 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
318 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
319 The compare exchange operation must be performed using MP safe mechanisms.
321 If Value is NULL, then ASSERT().
323 @param Value A pointer to the 64-bit value for the compare exchange
325 @param CompareValue 64-bit value used in compare operation.
326 @param ExchangeValue 64-bit value used in exchange operation.
328 @return The original *Value before exchange.
333 InterlockedCompareExchange64 (
334 IN OUT UINT64
*Value
,
335 IN UINT64 CompareValue
,
336 IN UINT64 ExchangeValue
339 ASSERT (Value
!= NULL
);
340 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
344 Performs an atomic compare exchange operation on a pointer value.
346 Performs an atomic compare exchange operation on the pointer value specified
347 by Value. If Value is equal to CompareValue, then Value is set to
348 ExchangeValue and CompareValue is returned. If Value is not equal to
349 CompareValue, then Value is returned. The compare exchange operation must be
350 performed using MP safe mechanisms.
352 If Value is NULL, then ASSERT().
354 @param Value A pointer to the pointer value for the compare exchange
356 @param CompareValue Pointer value used in compare operation.
357 @param ExchangeValue Pointer value used in exchange operation.
359 @return The original *Value before exchange.
364 InterlockedCompareExchangePointer (
366 IN VOID
*CompareValue
,
367 IN VOID
*ExchangeValue
372 SizeOfValue
= sizeof (*Value
);
374 switch (SizeOfValue
) {
375 case sizeof (UINT32
):
376 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
378 (UINT32
)(UINTN
)CompareValue
,
379 (UINT32
)(UINTN
)ExchangeValue
381 case sizeof (UINT64
):
382 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
384 (UINT64
)(UINTN
)CompareValue
,
385 (UINT64
)(UINTN
)ExchangeValue