]>
git.proxmox.com Git - mirror_edk2.git/blob - OldMdePkg/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.
13 Module Name: Synchronization.c
17 #include "BaseLibInternals.h"
19 #define SPIN_LOCK_RELEASED ((UINTN) 1)
20 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
23 Retrieves the architecture specific spin lock alignment requirements for
24 optimal spin lock performance.
26 This function retrieves the spin lock alignment requirements for optimal
27 performance on a given CPU architecture. The spin lock alignment must be a
28 power of two and is returned by this function. If there are no alignment
29 requirements, then 1 must be returned. The spin lock synchronization
30 functions must function correctly if the spin lock size and alignment values
31 returned by this function are not used at all. These values are hints to the
32 consumers of the spin lock synchronization functions to obtain optimal spin
35 @return The architecture specific spin lock alignment.
40 GetSpinLockProperties (
44 // @bug May use a PCD entry to determine this alignment.
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
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.
98 IN OUT SPIN_LOCK
*SpinLock
108 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
109 Tick
= GetPerformanceCounter ();
110 Timeout
= DivU64x32 (
112 GetPerformanceCounterProperties (&Start
, &End
),
113 PcdGet32 (PcdSpinLockTimeout
)
124 while (!AcquireSpinLockOrFail (SpinLock
)) {
126 ASSERT ((Start
< End
) ^ (Tick
<= GetPerformanceCounter ()));
132 Attempts to place a spin lock in the acquired state.
134 This function checks the state of the spin lock specified by SpinLock. If
135 SpinLock is in the released state, then this function places SpinLock in the
136 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
137 transitions of SpinLock must be performed using MP safe mechanisms.
139 If SpinLock is NULL, then ASSERT().
140 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
142 @param SpinLock A pointer to the spin lock to place in the acquired state.
144 @retval TRUE SpinLock was placed in the acquired state.
145 @retval FALSE SpinLock could not be acquired.
150 AcquireSpinLockOrFail (
151 IN OUT SPIN_LOCK
*SpinLock
156 ASSERT (SpinLock
!= NULL
);
158 LockValue
= *SpinLock
;
159 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
162 InterlockedCompareExchangePointer (
164 (VOID
*)SPIN_LOCK_RELEASED
,
165 (VOID
*)SPIN_LOCK_ACQUIRED
166 ) == (VOID
*)SPIN_LOCK_RELEASED
171 Releases a spin lock.
173 This function places the spin lock specified by SpinLock in the release state
174 and returns SpinLock.
176 If SpinLock is NULL, then ASSERT().
177 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
179 @param SpinLock A pointer to the spin lock to release.
187 IN OUT SPIN_LOCK
*SpinLock
192 ASSERT (SpinLock
!= NULL
);
194 LockValue
= *SpinLock
;
195 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
197 *SpinLock
= SPIN_LOCK_RELEASED
;
202 Performs an atomic increment of an 32-bit unsigned integer.
204 Performs an atomic increment of the 32-bit unsigned integer specified by
205 Value and returns the incremented value. The increment operation must be
206 performed using MP safe mechanisms. The state of the return value is not
207 guaranteed to be MP safe.
209 If Value is NULL, then ASSERT().
211 @param Value A pointer to the 32-bit value to increment.
213 @return The incremented value.
218 InterlockedIncrement (
222 ASSERT (Value
!= NULL
);
223 return InternalSyncIncrement (Value
);
227 Performs an atomic decrement of an 32-bit unsigned integer.
229 Performs an atomic decrement of the 32-bit unsigned integer specified by
230 Value and returns the decremented value. The decrement operation must be
231 performed using MP safe mechanisms. The state of the return value is not
232 guaranteed to be MP safe.
234 If Value is NULL, then ASSERT().
236 @param Value A pointer to the 32-bit value to decrement.
238 @return The decremented value.
243 InterlockedDecrement (
247 ASSERT (Value
!= NULL
);
248 return InternalSyncDecrement (Value
);
252 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
254 Performs an atomic compare exchange operation on the 32-bit unsigned integer
255 specified by Value. If Value is equal to CompareValue, then Value is set to
256 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
257 then Value is returned. The compare exchange operation must be performed using
260 If Value is NULL, then ASSERT().
262 @param Value A pointer to the 32-bit value for the compare exchange
264 @param CompareValue 32-bit value used in compare operation.
265 @param ExchangeValue 32-bit value used in exchange operation.
267 @return The original *Value before exchange.
272 InterlockedCompareExchange32 (
273 IN OUT UINT32
*Value
,
274 IN UINT32 CompareValue
,
275 IN UINT32 ExchangeValue
278 ASSERT (Value
!= NULL
);
279 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
283 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
285 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
286 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
287 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
288 The compare exchange operation must be performed using MP safe mechanisms.
290 If Value is NULL, then ASSERT().
292 @param Value A pointer to the 64-bit value for the compare exchange
294 @param CompareValue 64-bit value used in compare operation.
295 @param ExchangeValue 64-bit value used in exchange operation.
297 @return The original *Value before exchange.
302 InterlockedCompareExchange64 (
303 IN OUT UINT64
*Value
,
304 IN UINT64 CompareValue
,
305 IN UINT64 ExchangeValue
308 ASSERT (Value
!= NULL
);
309 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
313 Performs an atomic compare exchange operation on a pointer value.
315 Performs an atomic compare exchange operation on the pointer value specified
316 by Value. If Value is equal to CompareValue, then Value is set to
317 ExchangeValue and CompareValue is returned. If Value is not equal to
318 CompareValue, then Value is returned. The compare exchange operation must be
319 performed using MP safe mechanisms.
321 If Value is NULL, then ASSERT().
323 @param Value A pointer to the pointer value for the compare exchange
325 @param CompareValue Pointer value used in compare operation.
326 @param ExchangeValue Pointer value used in exchange operation.
331 InterlockedCompareExchangePointer (
333 IN VOID
*CompareValue
,
334 IN VOID
*ExchangeValue
339 SizeOfValue
= sizeof (*Value
);
341 switch (SizeOfValue
) {
342 case sizeof (UINT32
):
343 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
345 (UINT32
)(UINTN
)CompareValue
,
346 (UINT32
)(UINTN
)ExchangeValue
348 case sizeof (UINT64
):
349 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
351 (UINT64
)(UINTN
)CompareValue
,
352 (UINT64
)(UINTN
)ExchangeValue