]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/SynchronizationMsc.c
2 Implementation of synchronization functions.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "BaseSynchronizationLibInternals.h"
12 Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics.
20 #pragma intrinsic(_ReadWriteBarrier)
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 is byte alignment.
31 It must be a 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 (
47 return InternalGetSpinLockProperties ();
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 is 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 the lock.
104 IN OUT SPIN_LOCK
*SpinLock
116 if (PcdGet32 (PcdSpinLockTimeout
) == 0) {
117 while (!AcquireSpinLockOrFail (SpinLock
)) {
120 } else if (!AcquireSpinLockOrFail (SpinLock
)) {
122 // Get the current timer value
124 Current
= GetPerformanceCounter ();
127 // Initialize local variables
134 // Retrieve the performance counter properties and compute the number of performance
135 // counter ticks required to reach the timeout
137 Timeout
= DivU64x32 (
139 GetPerformanceCounterProperties (&Start
, &End
),
140 PcdGet32 (PcdSpinLockTimeout
)
151 while (!AcquireSpinLockOrFail (SpinLock
)) {
154 Current
= GetPerformanceCounter ();
155 Delta
= (INT64
)(Current
- Previous
);
165 ASSERT (Total
< Timeout
);
173 Attempts to place a spin lock in the acquired state.
175 This function checks the state of the spin lock specified by SpinLock. If
176 SpinLock is in the released state, then this function places SpinLock in the
177 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
178 transitions of SpinLock must be performed using MP safe mechanisms.
180 If SpinLock is NULL, then ASSERT().
181 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
183 @param SpinLock A pointer to the spin lock to place in the acquired state.
185 @retval TRUE SpinLock was placed in the acquired state.
186 @retval FALSE SpinLock could not be acquired.
191 AcquireSpinLockOrFail (
192 IN OUT SPIN_LOCK
*SpinLock
198 ASSERT (SpinLock
!= NULL
);
200 LockValue
= *SpinLock
;
201 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
203 _ReadWriteBarrier ();
204 Result
= InterlockedCompareExchangePointer (
206 (VOID
*)SPIN_LOCK_RELEASED
,
207 (VOID
*)SPIN_LOCK_ACQUIRED
210 _ReadWriteBarrier ();
211 return (BOOLEAN
)(Result
== (VOID
*)SPIN_LOCK_RELEASED
);
215 Releases a spin lock.
217 This function places the spin lock specified by SpinLock in the release state
218 and returns SpinLock.
220 If SpinLock is NULL, then ASSERT().
221 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
223 @param SpinLock A pointer to the spin lock to release.
225 @return SpinLock released the lock.
231 IN OUT SPIN_LOCK
*SpinLock
236 ASSERT (SpinLock
!= NULL
);
238 LockValue
= *SpinLock
;
239 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
241 _ReadWriteBarrier ();
242 *SpinLock
= SPIN_LOCK_RELEASED
;
243 _ReadWriteBarrier ();
249 Performs an atomic increment of an 32-bit unsigned integer.
251 Performs an atomic increment of the 32-bit unsigned integer specified by
252 Value and returns the incremented value. The increment operation must be
253 performed using MP safe mechanisms.
255 If Value is NULL, then ASSERT().
257 @param Value A pointer to the 32-bit value to increment.
259 @return The incremented value.
264 InterlockedIncrement (
265 IN
volatile UINT32
*Value
268 ASSERT (Value
!= NULL
);
269 return InternalSyncIncrement (Value
);
273 Performs an atomic decrement of an 32-bit unsigned integer.
275 Performs an atomic decrement of the 32-bit unsigned integer specified by
276 Value and returns the decremented value. The decrement operation must be
277 performed using MP safe mechanisms.
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 (
289 IN
volatile UINT32
*Value
292 ASSERT (Value
!= NULL
);
293 return InternalSyncDecrement (Value
);
297 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
299 Performs an atomic compare exchange operation on the 16-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 16-bit value for the compare exchange
309 @param CompareValue A 16-bit value used in a compare operation.
310 @param ExchangeValue A 16-bit value used in an exchange operation.
312 @return The original *Value before exchange.
317 InterlockedCompareExchange16 (
318 IN OUT
volatile UINT16
*Value
,
319 IN UINT16 CompareValue
,
320 IN UINT16 ExchangeValue
323 ASSERT (Value
!= NULL
);
324 return InternalSyncCompareExchange16 (Value
, CompareValue
, ExchangeValue
);
328 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
330 Performs an atomic compare exchange operation on the 32-bit unsigned integer
331 specified by Value. If Value is equal to CompareValue, then Value is set to
332 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
333 then Value is returned. The compare exchange operation must be performed using
336 If Value is NULL, then ASSERT().
338 @param Value A pointer to the 32-bit value for the compare exchange
340 @param CompareValue A 32-bit value used in a compare operation.
341 @param ExchangeValue A 32-bit value used in an exchange operation.
343 @return The original *Value before exchange.
348 InterlockedCompareExchange32 (
349 IN OUT
volatile UINT32
*Value
,
350 IN UINT32 CompareValue
,
351 IN UINT32 ExchangeValue
354 ASSERT (Value
!= NULL
);
355 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
359 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
361 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
362 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
363 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
364 The compare exchange operation must be performed using MP safe mechanisms.
366 If Value is NULL, then ASSERT().
368 @param Value A pointer to the 64-bit value for the compare exchange
370 @param CompareValue A 64-bit value used in a compare operation.
371 @param ExchangeValue A 64-bit value used in an exchange operation.
373 @return The original *Value before exchange.
378 InterlockedCompareExchange64 (
379 IN OUT
volatile UINT64
*Value
,
380 IN UINT64 CompareValue
,
381 IN UINT64 ExchangeValue
384 ASSERT (Value
!= NULL
);
385 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
389 Performs an atomic compare exchange operation on a pointer value.
391 Performs an atomic compare exchange operation on the pointer value specified
392 by Value. If Value is equal to CompareValue, then Value is set to
393 ExchangeValue and CompareValue is returned. If Value is not equal to
394 CompareValue, then Value is returned. The compare exchange operation must be
395 performed using MP safe mechanisms.
397 If Value is NULL, then ASSERT().
399 @param Value A pointer to the pointer value for the compare exchange
401 @param CompareValue A pointer value used in a compare operation.
402 @param ExchangeValue A pointer value used in an exchange operation.
404 @return The original *Value before exchange.
408 InterlockedCompareExchangePointer (
409 IN OUT VOID
*volatile *Value
,
410 IN VOID
*CompareValue
,
411 IN VOID
*ExchangeValue
416 SizeOfValue
= (UINT8
)sizeof (*Value
);
418 switch (SizeOfValue
) {
419 case sizeof (UINT32
):
420 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
421 (volatile UINT32
*)Value
,
422 (UINT32
)(UINTN
)CompareValue
,
423 (UINT32
)(UINTN
)ExchangeValue
425 case sizeof (UINT64
):
426 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
427 (volatile UINT64
*)Value
,
428 (UINT64
)(UINTN
)CompareValue
,
429 (UINT64
)(UINTN
)ExchangeValue