2 Implementation of synchronization functions.
4 Copyright (c) 2006 - 2007, 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: SynchronizationGcc.c
17 #include "BaseLibInternals.h"
20 // GCC inline assembly for Read Write Barrier
22 #define _ReadWriteBarrier() do { asm volatile ("": : : "memory"); } while(0)
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
);
78 *SpinLock
= SPIN_LOCK_RELEASED
;
85 Waits until a spin lock can be placed in the acquired state.
87 This function checks the state of the spin lock specified by SpinLock. If
88 SpinLock is in the released state, then this function places SpinLock in the
89 acquired state and returns SpinLock. Otherwise, this function waits
90 indefinitely for the spin lock to be released, and then places it in the
91 acquired state and returns SpinLock. All state transitions of SpinLock must
92 be performed using MP safe mechanisms.
94 If SpinLock is NULL, then ASSERT().
95 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
96 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
97 PcdSpinLockTimeout microseconds, then ASSERT().
99 @param SpinLock A pointer to the spin lock to place in the acquired state.
107 IN OUT SPIN_LOCK
*SpinLock
117 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
118 Tick
= GetPerformanceCounter ();
119 Timeout
= DivU64x32 (
121 GetPerformanceCounterProperties (&Start
, &End
),
122 PcdGet32 (PcdSpinLockTimeout
)
133 while (!AcquireSpinLockOrFail (SpinLock
)) {
135 ASSERT ((Start
< End
) ^ (Tick
<= GetPerformanceCounter ()));
141 Attempts to place a spin lock in the acquired state.
143 This function checks the state of the spin lock specified by SpinLock. If
144 SpinLock is in the released state, then this function places SpinLock in the
145 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
146 transitions of SpinLock must be performed using MP safe mechanisms.
148 If SpinLock is NULL, then ASSERT().
149 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
151 @param SpinLock A pointer to the spin lock to place in the acquired state.
153 @retval TRUE SpinLock was placed in the acquired state.
154 @retval FALSE SpinLock could not be acquired.
159 AcquireSpinLockOrFail (
160 IN OUT SPIN_LOCK
*SpinLock
165 ASSERT (SpinLock
!= NULL
);
166 ASSERT (*SpinLock
== SPIN_LOCK_ACQUIRED
|| *SpinLock
== SPIN_LOCK_RELEASED
);
168 _ReadWriteBarrier ();
169 Result
= InterlockedCompareExchangePointer (
171 (VOID
*)SPIN_LOCK_RELEASED
,
172 (VOID
*)SPIN_LOCK_ACQUIRED
175 _ReadWriteBarrier ();
176 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
180 Releases a spin lock.
182 This function places the spin lock specified by SpinLock in the release state
183 and returns SpinLock.
185 If SpinLock is NULL, then ASSERT().
186 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
188 @param SpinLock A pointer to the spin lock to release.
196 IN OUT SPIN_LOCK
*SpinLock
199 ASSERT (SpinLock
!= NULL
);
200 ASSERT (*SpinLock
== SPIN_LOCK_ACQUIRED
|| *SpinLock
== SPIN_LOCK_RELEASED
);
202 _ReadWriteBarrier ();
203 *SpinLock
= SPIN_LOCK_RELEASED
;
204 _ReadWriteBarrier ();
210 Performs an atomic increment of an 32-bit unsigned integer.
212 Performs an atomic increment of the 32-bit unsigned integer specified by
213 Value and returns the incremented value. The increment operation must be
214 performed using MP safe mechanisms. The state of the return value is not
215 guaranteed to be MP safe.
217 If Value is NULL, then ASSERT().
219 @param Value A pointer to the 32-bit value to increment.
221 @return The incremented value.
226 InterlockedIncrement (
230 ASSERT (Value
!= NULL
);
231 return InternalSyncIncrement (Value
);
235 Performs an atomic decrement of an 32-bit unsigned integer.
237 Performs an atomic decrement of the 32-bit unsigned integer specified by
238 Value and returns the decremented value. The decrement 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 decrement.
246 @return The decremented value.
251 InterlockedDecrement (
255 ASSERT (Value
!= NULL
);
256 return InternalSyncDecrement (Value
);
260 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
262 Performs an atomic compare exchange operation on the 32-bit unsigned integer
263 specified by Value. If Value is equal to CompareValue, then Value is set to
264 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
265 then Value is returned. The compare exchange operation must be performed using
268 If Value is NULL, then ASSERT().
270 @param Value A pointer to the 32-bit value for the compare exchange
272 @param CompareValue 32-bit value used in compare operation.
273 @param ExchangeValue 32-bit value used in exchange operation.
275 @return The original *Value before exchange.
280 InterlockedCompareExchange32 (
281 IN OUT UINT32
*Value
,
282 IN UINT32 CompareValue
,
283 IN UINT32 ExchangeValue
286 ASSERT (Value
!= NULL
);
287 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
291 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
293 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
294 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
295 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
296 The compare exchange operation must be performed using MP safe mechanisms.
298 If Value is NULL, then ASSERT().
300 @param Value A pointer to the 64-bit value for the compare exchange
302 @param CompareValue 64-bit value used in compare operation.
303 @param ExchangeValue 64-bit value used in exchange operation.
305 @return The original *Value before exchange.
310 InterlockedCompareExchange64 (
311 IN OUT UINT64
*Value
,
312 IN UINT64 CompareValue
,
313 IN UINT64 ExchangeValue
316 ASSERT (Value
!= NULL
);
317 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
321 Performs an atomic compare exchange operation on a pointer value.
323 Performs an atomic compare exchange operation on the pointer value specified
324 by Value. If Value is equal to CompareValue, then Value is set to
325 ExchangeValue and CompareValue is returned. If Value is not equal to
326 CompareValue, then Value is returned. The compare exchange operation must be
327 performed using MP safe mechanisms.
329 If Value is NULL, then ASSERT().
331 @param Value A pointer to the pointer value for the compare exchange
333 @param CompareValue Pointer value used in compare operation.
334 @param ExchangeValue Pointer value used in exchange operation.
339 InterlockedCompareExchangePointer (
341 IN VOID
*CompareValue
,
342 IN VOID
*ExchangeValue
347 SizeOfValue
= sizeof (*Value
);
349 switch (SizeOfValue
) {
350 case sizeof (UINT32
):
351 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
353 (UINT32
)(UINTN
)CompareValue
,
354 (UINT32
)(UINTN
)ExchangeValue
356 case sizeof (UINT64
):
357 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
359 (UINT64
)(UINTN
)CompareValue
,
360 (UINT64
)(UINTN
)ExchangeValue