]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/SynchronizationGcc.c
2 Implementation of synchronization functions.
4 Copyright (c) 2006 - 2008, 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.
15 #include "BaseSynchronizationLibInternals.h"
18 // GCC inline assembly for Read Write Barrier
20 #define _ReadWriteBarrier() do { asm volatile ("": : : "memory"); } while(0)
22 #define SPIN_LOCK_RELEASED ((UINTN) 1)
23 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
26 Retrieves the architecture specific spin lock alignment requirements for
27 optimal spin lock performance.
29 This function retrieves the spin lock alignment requirements for optimal
30 performance on a given CPU architecture. The spin lock alignment must be a
31 power of two and is returned by this function. If there are no alignment
32 requirements, then 1 must be returned. The spin lock synchronization
33 functions must function correctly if the spin lock size and alignment values
34 returned by this function are not used at all. These values are hints to the
35 consumers of the spin lock synchronization functions to obtain optimal spin
38 @return The architecture specific spin lock alignment.
43 GetSpinLockProperties (
51 Initializes a spin lock to the released state and returns the spin lock.
53 This function initializes the spin lock specified by SpinLock to the released
54 state, and returns SpinLock. Optimal performance can be achieved by calling
55 GetSpinLockProperties() to determine the size and alignment requirements for
58 If SpinLock is NULL, then ASSERT().
60 @param SpinLock A pointer to the spin lock to initialize to the released
63 @return SpinLock in release state.
69 OUT SPIN_LOCK
*SpinLock
72 ASSERT (SpinLock
!= NULL
);
75 *SpinLock
= SPIN_LOCK_RELEASED
;
82 Waits until a spin lock can be placed in the acquired state.
84 This function checks the state of the spin lock specified by SpinLock. If
85 SpinLock is in the released state, then this function places SpinLock in the
86 acquired state and returns SpinLock. Otherwise, this function waits
87 indefinitely for the spin lock to be released, and then places it in the
88 acquired state and returns SpinLock. All state transitions of SpinLock must
89 be performed using MP safe mechanisms.
91 If SpinLock is NULL, then ASSERT().
92 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
93 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
94 PcdSpinLockTimeout microseconds, then ASSERT().
96 @param SpinLock A pointer to the spin lock to place in the acquired state.
98 @return SpinLock acquired lock.
104 IN OUT SPIN_LOCK
*SpinLock
116 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
118 // Get the current timer value
120 Current
= GetPerformanceCounter();
123 // Initialize local variables
130 // Retrieve the performance counter properties and compute the number of performance
131 // counter ticks required to reach the timeout
133 Timeout
= DivU64x32 (
135 GetPerformanceCounterProperties (&Start
, &End
),
136 PcdGet32 (PcdSpinLockTimeout
)
146 while (!AcquireSpinLockOrFail (SpinLock
)) {
149 Current
= GetPerformanceCounter();
150 Delta
= (INT64
) (Current
- Previous
);
158 ASSERT (Total
< Timeout
);
161 while (!AcquireSpinLockOrFail (SpinLock
)) {
169 Attempts to place a spin lock in the acquired state.
171 This function checks the state of the spin lock specified by SpinLock. If
172 SpinLock is in the released state, then this function places SpinLock in the
173 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
174 transitions of SpinLock must be performed using MP safe mechanisms.
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 place in the acquired state.
181 @retval TRUE SpinLock was placed in the acquired state.
182 @retval FALSE SpinLock could not be acquired.
187 AcquireSpinLockOrFail (
188 IN OUT SPIN_LOCK
*SpinLock
194 ASSERT (SpinLock
!= NULL
);
196 LockValue
= *SpinLock
;
197 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
199 _ReadWriteBarrier ();
200 Result
= InterlockedCompareExchangePointer (
202 (VOID
*)SPIN_LOCK_RELEASED
,
203 (VOID
*)SPIN_LOCK_ACQUIRED
206 _ReadWriteBarrier ();
207 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
211 Releases a spin lock.
213 This function places the spin lock specified by SpinLock in the release state
214 and returns SpinLock.
216 If SpinLock is NULL, then ASSERT().
217 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
219 @param SpinLock A pointer to the spin lock to release.
221 @return SpinLock released lock.
227 IN OUT SPIN_LOCK
*SpinLock
232 ASSERT (SpinLock
!= NULL
);
234 LockValue
= *SpinLock
;
235 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
237 _ReadWriteBarrier ();
238 *SpinLock
= SPIN_LOCK_RELEASED
;
239 _ReadWriteBarrier ();
245 Performs an atomic increment of an 32-bit unsigned integer.
247 Performs an atomic increment of the 32-bit unsigned integer specified by
248 Value and returns the incremented value. The increment operation must be
249 performed using MP safe mechanisms. The state of the return value is not
250 guaranteed to be MP safe.
252 If Value is NULL, then ASSERT().
254 @param Value A pointer to the 32-bit value to increment.
256 @return The incremented value.
261 InterlockedIncrement (
265 ASSERT (Value
!= NULL
);
266 return InternalSyncIncrement (Value
);
270 Performs an atomic decrement of an 32-bit unsigned integer.
272 Performs an atomic decrement of the 32-bit unsigned integer specified by
273 Value and returns the decremented value. The decrement operation must be
274 performed using MP safe mechanisms. The state of the return value is not
275 guaranteed to be MP safe.
277 If Value is NULL, then ASSERT().
279 @param Value A pointer to the 32-bit value to decrement.
281 @return The decremented value.
286 InterlockedDecrement (
290 ASSERT (Value
!= NULL
);
291 return InternalSyncDecrement (Value
);
295 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
297 Performs an atomic compare exchange operation on the 32-bit unsigned integer
298 specified by Value. If Value is equal to CompareValue, then Value is set to
299 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
300 then Value is returned. The compare exchange operation must be performed using
303 If Value is NULL, then ASSERT().
305 @param Value A pointer to the 32-bit value for the compare exchange
307 @param CompareValue 32-bit value used in compare operation.
308 @param ExchangeValue 32-bit value used in exchange operation.
310 @return The original *Value before exchange.
315 InterlockedCompareExchange32 (
316 IN OUT UINT32
*Value
,
317 IN UINT32 CompareValue
,
318 IN UINT32 ExchangeValue
321 ASSERT (Value
!= NULL
);
322 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
326 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
328 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
329 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
330 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
331 The compare exchange operation must be performed using MP safe mechanisms.
333 If Value is NULL, then ASSERT().
335 @param Value A pointer to the 64-bit value for the compare exchange
337 @param CompareValue 64-bit value used in compare operation.
338 @param ExchangeValue 64-bit value used in exchange operation.
340 @return The original *Value before exchange.
345 InterlockedCompareExchange64 (
346 IN OUT UINT64
*Value
,
347 IN UINT64 CompareValue
,
348 IN UINT64 ExchangeValue
351 ASSERT (Value
!= NULL
);
352 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
356 Performs an atomic compare exchange operation on a pointer value.
358 Performs an atomic compare exchange operation on the pointer value specified
359 by Value. If Value is equal to CompareValue, then Value is set to
360 ExchangeValue and CompareValue is returned. If Value is not equal to
361 CompareValue, then Value is returned. The compare exchange operation must be
362 performed using MP safe mechanisms.
364 If Value is NULL, then ASSERT().
366 @param Value A pointer to the pointer value for the compare exchange
368 @param CompareValue Pointer value used in compare operation.
369 @param ExchangeValue Pointer value used in exchange operation.
371 @return The original *Value before exchange.
375 InterlockedCompareExchangePointer (
377 IN VOID
*CompareValue
,
378 IN VOID
*ExchangeValue
383 SizeOfValue
= sizeof (*Value
);
385 switch (SizeOfValue
) {
386 case sizeof (UINT32
):
387 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
389 (UINT32
)(UINTN
)CompareValue
,
390 (UINT32
)(UINTN
)ExchangeValue
392 case sizeof (UINT64
):
393 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
395 (UINT64
)(UINTN
)CompareValue
,
396 (UINT64
)(UINTN
)ExchangeValue