]>
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.
13 Module Name: Synchronization.c
18 // Include common header file for this module.
20 #include "CommonHeader.h"
22 #include "BaseLibInternals.h"
24 #define SPIN_LOCK_RELEASED ((UINTN) 1)
25 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
28 Retrieves the architecture specific spin lock alignment requirements for
29 optimal spin lock performance.
31 This function retrieves the spin lock alignment requirements for optimal
32 performance on a given CPU architecture. The spin lock alignment must be a
33 power of two and is returned by this function. If there are no alignment
34 requirements, then 1 must be returned. The spin lock synchronization
35 functions must function correctly if the spin lock size and alignment values
36 returned by this function are not used at all. These values are hints to the
37 consumers of the spin lock synchronization functions to obtain optimal spin
40 @return The architecture specific spin lock alignment.
45 GetSpinLockProperties (
49 // @bug May use a PCD entry to determine this alignment.
54 Initializes a spin lock to the released state and returns the spin lock.
56 This function initializes the spin lock specified by SpinLock to the released
57 state, and returns SpinLock. Optimal performance can be achieved by calling
58 GetSpinLockProperties() to determine the size and alignment requirements for
61 If SpinLock is NULL, then ASSERT().
63 @param SpinLock A pointer to the spin lock to initialize to the released
72 OUT SPIN_LOCK
*SpinLock
75 ASSERT (SpinLock
!= NULL
);
76 *SpinLock
= SPIN_LOCK_RELEASED
;
81 Waits until a spin lock can be placed in the acquired state.
83 This function checks the state of the spin lock specified by SpinLock. If
84 SpinLock is in the released state, then this function places SpinLock in the
85 acquired state and returns SpinLock. Otherwise, this function waits
86 indefinitely for the spin lock to be released, and then places it in the
87 acquired state and returns SpinLock. All state transitions of SpinLock must
88 be performed using MP safe mechanisms.
90 If SpinLock is NULL, then ASSERT().
91 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
92 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
93 PcdSpinLockTimeout microseconds, then ASSERT().
95 @param SpinLock A pointer to the spin lock to place in the acquired state.
103 IN OUT SPIN_LOCK
*SpinLock
113 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
114 Tick
= GetPerformanceCounter ();
115 Timeout
= DivU64x32 (
117 GetPerformanceCounterProperties (&Start
, &End
),
118 PcdGet32 (PcdSpinLockTimeout
)
129 while (!AcquireSpinLockOrFail (SpinLock
)) {
131 ASSERT ((Start
< End
) ^ (Tick
<= GetPerformanceCounter ()));
137 Attempts to place a spin lock in the acquired state.
139 This function checks the state of the spin lock specified by SpinLock. If
140 SpinLock is in the released state, then this function places SpinLock in the
141 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
142 transitions of SpinLock must be performed using MP safe mechanisms.
144 If SpinLock is NULL, then ASSERT().
145 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
147 @param SpinLock A pointer to the spin lock to place in the acquired state.
149 @retval TRUE SpinLock was placed in the acquired state.
150 @retval FALSE SpinLock could not be acquired.
155 AcquireSpinLockOrFail (
156 IN OUT SPIN_LOCK
*SpinLock
161 ASSERT (SpinLock
!= NULL
);
163 LockValue
= *SpinLock
;
164 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
167 InterlockedCompareExchangePointer (
169 (VOID
*)SPIN_LOCK_RELEASED
,
170 (VOID
*)SPIN_LOCK_ACQUIRED
171 ) == (VOID
*)SPIN_LOCK_RELEASED
176 Releases a spin lock.
178 This function places the spin lock specified by SpinLock in the release state
179 and returns SpinLock.
181 If SpinLock is NULL, then ASSERT().
182 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
184 @param SpinLock A pointer to the spin lock to release.
192 IN OUT SPIN_LOCK
*SpinLock
197 ASSERT (SpinLock
!= NULL
);
199 LockValue
= *SpinLock
;
200 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
202 *SpinLock
= SPIN_LOCK_RELEASED
;
207 Performs an atomic increment of an 32-bit unsigned integer.
209 Performs an atomic increment of the 32-bit unsigned integer specified by
210 Value and returns the incremented value. The increment operation must be
211 performed using MP safe mechanisms. The state of the return value is not
212 guaranteed to be MP safe.
214 If Value is NULL, then ASSERT().
216 @param Value A pointer to the 32-bit value to increment.
218 @return The incremented value.
223 InterlockedIncrement (
227 ASSERT (Value
!= NULL
);
228 return InternalSyncIncrement (Value
);
232 Performs an atomic decrement of an 32-bit unsigned integer.
234 Performs an atomic decrement of the 32-bit unsigned integer specified by
235 Value and returns the decremented value. The decrement operation must be
236 performed using MP safe mechanisms. The state of the return value is not
237 guaranteed to be MP safe.
239 If Value is NULL, then ASSERT().
241 @param Value A pointer to the 32-bit value to decrement.
243 @return The decremented value.
248 InterlockedDecrement (
252 ASSERT (Value
!= NULL
);
253 return InternalSyncDecrement (Value
);
257 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
259 Performs an atomic compare exchange operation on the 32-bit unsigned integer
260 specified by Value. If Value is equal to CompareValue, then Value is set to
261 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
262 then Value is returned. The compare exchange operation must be performed using
265 If Value is NULL, then ASSERT().
267 @param Value A pointer to the 32-bit value for the compare exchange
269 @param CompareValue 32-bit value used in compare operation.
270 @param ExchangeValue 32-bit value used in exchange operation.
272 @return The original *Value before exchange.
277 InterlockedCompareExchange32 (
278 IN OUT UINT32
*Value
,
279 IN UINT32 CompareValue
,
280 IN UINT32 ExchangeValue
283 ASSERT (Value
!= NULL
);
284 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
288 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
290 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
291 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
292 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
293 The compare exchange operation must be performed using MP safe mechanisms.
295 If Value is NULL, then ASSERT().
297 @param Value A pointer to the 64-bit value for the compare exchange
299 @param CompareValue 64-bit value used in compare operation.
300 @param ExchangeValue 64-bit value used in exchange operation.
302 @return The original *Value before exchange.
307 InterlockedCompareExchange64 (
308 IN OUT UINT64
*Value
,
309 IN UINT64 CompareValue
,
310 IN UINT64 ExchangeValue
313 ASSERT (Value
!= NULL
);
314 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
318 Performs an atomic compare exchange operation on a pointer value.
320 Performs an atomic compare exchange operation on the pointer value specified
321 by Value. If Value is equal to CompareValue, then Value is set to
322 ExchangeValue and CompareValue is returned. If Value is not equal to
323 CompareValue, then Value is returned. The compare exchange operation must be
324 performed using MP safe mechanisms.
326 If Value is NULL, then ASSERT().
328 @param Value A pointer to the pointer value for the compare exchange
330 @param CompareValue Pointer value used in compare operation.
331 @param ExchangeValue Pointer value used in exchange operation.
336 InterlockedCompareExchangePointer (
338 IN VOID
*CompareValue
,
339 IN VOID
*ExchangeValue
344 SizeOfValue
= sizeof (*Value
);
346 switch (SizeOfValue
) {
347 case sizeof (UINT32
):
348 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
350 (UINT32
)(UINTN
)CompareValue
,
351 (UINT32
)(UINTN
)ExchangeValue
353 case sizeof (UINT64
):
354 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
356 (UINT64
)(UINTN
)CompareValue
,
357 (UINT64
)(UINTN
)ExchangeValue