]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/SynchronizationGcc.c
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.
16 // Include common header file for this module.
20 #include "BaseLibInternals.h"
23 // GCC inline assembly for Read Write Barrier
25 #define _ReadWriteBarrier() do { asm volatile ("": : : "memory"); } while(0)
27 #define SPIN_LOCK_RELEASED ((UINTN) 1)
28 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
31 Retrieves the architecture specific spin lock alignment requirements for
32 optimal spin lock performance.
34 This function retrieves the spin lock alignment requirements for optimal
35 performance on a given CPU architecture. The spin lock alignment must be a
36 power of two and is returned by this function. If there are no alignment
37 requirements, then 1 must be returned. The spin lock synchronization
38 functions must function correctly if the spin lock size and alignment values
39 returned by this function are not used at all. These values are hints to the
40 consumers of the spin lock synchronization functions to obtain optimal spin
43 @return The architecture specific spin lock alignment.
48 GetSpinLockProperties (
52 // @bug May use a PCD entry to determine this alignment.
57 Initializes a spin lock to the released state and returns the spin lock.
59 This function initializes the spin lock specified by SpinLock to the released
60 state, and returns SpinLock. Optimal performance can be achieved by calling
61 GetSpinLockProperties() to determine the size and alignment requirements for
64 If SpinLock is NULL, then ASSERT().
66 @param SpinLock A pointer to the spin lock to initialize to the released
75 OUT SPIN_LOCK
*SpinLock
78 ASSERT (SpinLock
!= NULL
);
81 *SpinLock
= SPIN_LOCK_RELEASED
;
88 Waits until a spin lock can be placed in the acquired state.
90 This function checks the state of the spin lock specified by SpinLock. If
91 SpinLock is in the released state, then this function places SpinLock in the
92 acquired state and returns SpinLock. Otherwise, this function waits
93 indefinitely for the spin lock to be released, and then places it in the
94 acquired state and returns SpinLock. All state transitions of SpinLock must
95 be performed using MP safe mechanisms.
97 If SpinLock is NULL, then ASSERT().
98 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
99 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
100 PcdSpinLockTimeout microseconds, then ASSERT().
102 @param SpinLock A pointer to the spin lock to place in the acquired state.
110 IN OUT SPIN_LOCK
*SpinLock
122 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
124 // Get the current timer value
126 Current
= GetPerformanceCounter();
129 // Initialize local variables
136 // Retrieve the performance counter properties and compute the number of performance
137 // counter ticks required to reach the timeout
139 Timeout
= DivU64x32 (
141 GetPerformanceCounterProperties (&Start
, &End
),
142 PcdGet32 (PcdSpinLockTimeout
)
152 while (!AcquireSpinLockOrFail (SpinLock
)) {
155 Current
= GetPerformanceCounter();
156 Delta
= (INT64
) (Current
- Previous
);
164 ASSERT (Total
< Timeout
);
167 while (!AcquireSpinLockOrFail (SpinLock
)) {
175 Attempts to place a spin lock in the acquired state.
177 This function checks the state of the spin lock specified by SpinLock. If
178 SpinLock is in the released state, then this function places SpinLock in the
179 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
180 transitions of SpinLock must be performed using MP safe mechanisms.
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 place in the acquired state.
187 @retval TRUE SpinLock was placed in the acquired state.
188 @retval FALSE SpinLock could not be acquired.
193 AcquireSpinLockOrFail (
194 IN OUT SPIN_LOCK
*SpinLock
200 ASSERT (SpinLock
!= NULL
);
202 LockValue
= *SpinLock
;
203 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
205 _ReadWriteBarrier ();
206 Result
= InterlockedCompareExchangePointer (
208 (VOID
*)SPIN_LOCK_RELEASED
,
209 (VOID
*)SPIN_LOCK_ACQUIRED
212 _ReadWriteBarrier ();
213 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
217 Releases a spin lock.
219 This function places the spin lock specified by SpinLock in the release state
220 and returns SpinLock.
222 If SpinLock is NULL, then ASSERT().
223 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
225 @param SpinLock A pointer to the spin lock to release.
233 IN OUT SPIN_LOCK
*SpinLock
238 ASSERT (SpinLock
!= NULL
);
240 LockValue
= *SpinLock
;
241 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
243 _ReadWriteBarrier ();
244 *SpinLock
= SPIN_LOCK_RELEASED
;
245 _ReadWriteBarrier ();
251 Performs an atomic increment of an 32-bit unsigned integer.
253 Performs an atomic increment of the 32-bit unsigned integer specified by
254 Value and returns the incremented value. The increment operation must be
255 performed using MP safe mechanisms. The state of the return value is not
256 guaranteed to be MP safe.
258 If Value is NULL, then ASSERT().
260 @param Value A pointer to the 32-bit value to increment.
262 @return The incremented value.
267 InterlockedIncrement (
271 ASSERT (Value
!= NULL
);
272 return InternalSyncIncrement (Value
);
276 Performs an atomic decrement of an 32-bit unsigned integer.
278 Performs an atomic decrement of the 32-bit unsigned integer specified by
279 Value and returns the decremented value. The decrement operation must be
280 performed using MP safe mechanisms. The state of the return value is not
281 guaranteed to be MP safe.
283 If Value is NULL, then ASSERT().
285 @param Value A pointer to the 32-bit value to decrement.
287 @return The decremented value.
292 InterlockedDecrement (
296 ASSERT (Value
!= NULL
);
297 return InternalSyncDecrement (Value
);
301 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
303 Performs an atomic compare exchange operation on the 32-bit unsigned integer
304 specified by Value. If Value is equal to CompareValue, then Value is set to
305 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
306 then Value is returned. The compare exchange operation must be performed using
309 If Value is NULL, then ASSERT().
311 @param Value A pointer to the 32-bit value for the compare exchange
313 @param CompareValue 32-bit value used in compare operation.
314 @param ExchangeValue 32-bit value used in exchange operation.
316 @return The original *Value before exchange.
321 InterlockedCompareExchange32 (
322 IN OUT UINT32
*Value
,
323 IN UINT32 CompareValue
,
324 IN UINT32 ExchangeValue
327 ASSERT (Value
!= NULL
);
328 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
332 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
334 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
335 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
336 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
337 The compare exchange operation must be performed using MP safe mechanisms.
339 If Value is NULL, then ASSERT().
341 @param Value A pointer to the 64-bit value for the compare exchange
343 @param CompareValue 64-bit value used in compare operation.
344 @param ExchangeValue 64-bit value used in exchange operation.
346 @return The original *Value before exchange.
351 InterlockedCompareExchange64 (
352 IN OUT UINT64
*Value
,
353 IN UINT64 CompareValue
,
354 IN UINT64 ExchangeValue
357 ASSERT (Value
!= NULL
);
358 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
362 Performs an atomic compare exchange operation on a pointer value.
364 Performs an atomic compare exchange operation on the pointer value specified
365 by Value. If Value is equal to CompareValue, then Value is set to
366 ExchangeValue and CompareValue is returned. If Value is not equal to
367 CompareValue, then Value is returned. The compare exchange operation must be
368 performed using MP safe mechanisms.
370 If Value is NULL, then ASSERT().
372 @param Value A pointer to the pointer value for the compare exchange
374 @param CompareValue Pointer value used in compare operation.
375 @param ExchangeValue Pointer value used in exchange operation.
380 InterlockedCompareExchangePointer (
382 IN VOID
*CompareValue
,
383 IN VOID
*ExchangeValue
388 SizeOfValue
= sizeof (*Value
);
390 switch (SizeOfValue
) {
391 case sizeof (UINT32
):
392 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
394 (UINT32
)(UINTN
)CompareValue
,
395 (UINT32
)(UINTN
)ExchangeValue
397 case sizeof (UINT64
):
398 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
400 (UINT64
)(UINTN
)CompareValue
,
401 (UINT64
)(UINTN
)ExchangeValue