]>
git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/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 "BaseLibInternals.h"
19 // GCC inline assembly for Read Write Barrier
21 #define _ReadWriteBarrier() do { asm volatile ("": : : "memory"); } while(0)
23 #define SPIN_LOCK_RELEASED ((UINTN) 1)
24 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
27 Retrieves the architecture specific spin lock alignment requirements for
28 optimal spin lock performance.
30 This function retrieves the spin lock alignment requirements for optimal
31 performance on a given CPU architecture. The spin lock alignment must be a
32 power of two and is returned by this function. If there are no alignment
33 requirements, then 1 must be returned. The spin lock synchronization
34 functions must function correctly if the spin lock size and alignment values
35 returned by this function are not used at all. These values are hints to the
36 consumers of the spin lock synchronization functions to obtain optimal spin
39 @return The architecture specific spin lock alignment.
44 GetSpinLockProperties (
48 // @bug May use a PCD entry to determine this alignment.
53 Initializes a spin lock to the released state and returns the spin lock.
55 This function initializes the spin lock specified by SpinLock to the released
56 state, and returns SpinLock. Optimal performance can be achieved by calling
57 GetSpinLockProperties() to determine the size and alignment requirements for
60 If SpinLock is NULL, then ASSERT().
62 @param SpinLock A pointer to the spin lock to initialize to the released
71 OUT SPIN_LOCK
*SpinLock
74 ASSERT (SpinLock
!= NULL
);
77 *SpinLock
= SPIN_LOCK_RELEASED
;
84 Waits until a spin lock can be placed in the acquired state.
86 This function checks the state of the spin lock specified by SpinLock. If
87 SpinLock is in the released state, then this function places SpinLock in the
88 acquired state and returns SpinLock. Otherwise, this function waits
89 indefinitely for the spin lock to be released, and then places it in the
90 acquired state and returns SpinLock. All state transitions of SpinLock must
91 be performed using MP safe mechanisms.
93 If SpinLock is NULL, then ASSERT().
94 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
95 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
96 PcdSpinLockTimeout microseconds, then ASSERT().
98 @param SpinLock A pointer to the spin lock to place in the acquired state.
106 IN OUT SPIN_LOCK
*SpinLock
118 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
120 // Get the current timer value
122 Current
= GetPerformanceCounter();
125 // Initialize local variables
132 // Retrieve the performance counter properties and compute the number of performance
133 // counter ticks required to reach the timeout
135 Timeout
= DivU64x32 (
137 GetPerformanceCounterProperties (&Start
, &End
),
138 PcdGet32 (PcdSpinLockTimeout
)
148 while (!AcquireSpinLockOrFail (SpinLock
)) {
151 Current
= GetPerformanceCounter();
152 Delta
= (INT64
) (Current
- Previous
);
160 ASSERT (Total
< Timeout
);
163 while (!AcquireSpinLockOrFail (SpinLock
)) {
171 Attempts to place a spin lock in the acquired state.
173 This function checks the state of the spin lock specified by SpinLock. If
174 SpinLock is in the released state, then this function places SpinLock in the
175 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
176 transitions of SpinLock must be performed using MP safe mechanisms.
178 If SpinLock is NULL, then ASSERT().
179 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
181 @param SpinLock A pointer to the spin lock to place in the acquired state.
183 @retval TRUE SpinLock was placed in the acquired state.
184 @retval FALSE SpinLock could not be acquired.
189 AcquireSpinLockOrFail (
190 IN OUT SPIN_LOCK
*SpinLock
196 ASSERT (SpinLock
!= NULL
);
198 LockValue
= *SpinLock
;
199 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
201 _ReadWriteBarrier ();
202 Result
= InterlockedCompareExchangePointer (
204 (VOID
*)SPIN_LOCK_RELEASED
,
205 (VOID
*)SPIN_LOCK_ACQUIRED
208 _ReadWriteBarrier ();
209 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
213 Releases a spin lock.
215 This function places the spin lock specified by SpinLock in the release state
216 and returns SpinLock.
218 If SpinLock is NULL, then ASSERT().
219 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
221 @param SpinLock A pointer to the spin lock to release.
229 IN OUT SPIN_LOCK
*SpinLock
234 ASSERT (SpinLock
!= NULL
);
236 LockValue
= *SpinLock
;
237 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
239 _ReadWriteBarrier ();
240 *SpinLock
= SPIN_LOCK_RELEASED
;
241 _ReadWriteBarrier ();
247 Performs an atomic increment of an 32-bit unsigned integer.
249 Performs an atomic increment of the 32-bit unsigned integer specified by
250 Value and returns the incremented value. The increment operation must be
251 performed using MP safe mechanisms. The state of the return value is not
252 guaranteed to be MP safe.
254 If Value is NULL, then ASSERT().
256 @param Value A pointer to the 32-bit value to increment.
258 @return The incremented value.
263 InterlockedIncrement (
267 ASSERT (Value
!= NULL
);
268 return InternalSyncIncrement (Value
);
272 Performs an atomic decrement of an 32-bit unsigned integer.
274 Performs an atomic decrement of the 32-bit unsigned integer specified by
275 Value and returns the decremented value. The decrement operation must be
276 performed using MP safe mechanisms. The state of the return value is not
277 guaranteed to be MP safe.
279 If Value is NULL, then ASSERT().
281 @param Value A pointer to the 32-bit value to decrement.
283 @return The decremented value.
288 InterlockedDecrement (
292 ASSERT (Value
!= NULL
);
293 return InternalSyncDecrement (Value
);
297 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
299 Performs an atomic compare exchange operation on the 32-bit unsigned integer
300 specified by Value. If Value is equal to CompareValue, then Value is set to
301 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
302 then Value is returned. The compare exchange operation must be performed using
305 If Value is NULL, then ASSERT().
307 @param Value A pointer to the 32-bit value for the compare exchange
309 @param CompareValue 32-bit value used in compare operation.
310 @param ExchangeValue 32-bit value used in exchange operation.
312 @return The original *Value before exchange.
317 InterlockedCompareExchange32 (
318 IN OUT UINT32
*Value
,
319 IN UINT32 CompareValue
,
320 IN UINT32 ExchangeValue
323 ASSERT (Value
!= NULL
);
324 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
328 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
330 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
331 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
332 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
333 The compare exchange operation must be performed using MP safe mechanisms.
335 If Value is NULL, then ASSERT().
337 @param Value A pointer to the 64-bit value for the compare exchange
339 @param CompareValue 64-bit value used in compare operation.
340 @param ExchangeValue 64-bit value used in exchange operation.
342 @return The original *Value before exchange.
347 InterlockedCompareExchange64 (
348 IN OUT UINT64
*Value
,
349 IN UINT64 CompareValue
,
350 IN UINT64 ExchangeValue
353 ASSERT (Value
!= NULL
);
354 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
358 Performs an atomic compare exchange operation on a pointer value.
360 Performs an atomic compare exchange operation on the pointer value specified
361 by Value. If Value is equal to CompareValue, then Value is set to
362 ExchangeValue and CompareValue is returned. If Value is not equal to
363 CompareValue, then Value is returned. The compare exchange operation must be
364 performed using MP safe mechanisms.
366 If Value is NULL, then ASSERT().
368 @param Value A pointer to the pointer value for the compare exchange
370 @param CompareValue Pointer value used in compare operation.
371 @param ExchangeValue Pointer value used in exchange operation.
376 InterlockedCompareExchangePointer (
378 IN VOID
*CompareValue
,
379 IN VOID
*ExchangeValue
384 SizeOfValue
= sizeof (*Value
);
386 switch (SizeOfValue
) {
387 case sizeof (UINT32
):
388 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
390 (UINT32
)(UINTN
)CompareValue
,
391 (UINT32
)(UINTN
)ExchangeValue
393 case sizeof (UINT64
):
394 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
396 (UINT64
)(UINTN
)CompareValue
,
397 (UINT64
)(UINTN
)ExchangeValue