]>
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 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 Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics.
21 void _ReadWriteBarrier (void);
22 #pragma intrinsic(_ReadWriteBarrier)
25 #define SPIN_LOCK_RELEASED ((UINTN) 1)
26 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
29 Retrieves the architecture specific spin lock alignment requirements for
30 optimal spin lock performance.
32 This function retrieves the spin lock alignment requirements for optimal
33 performance on a given CPU architecture. The spin lock alignment is byte alignment.
34 It must be a power of two and is returned by this function. If there are no alignment
35 requirements, then 1 must be returned. The spin lock synchronization
36 functions must function correctly if the spin lock size and alignment values
37 returned by this function are not used at all. These values are hints to the
38 consumers of the spin lock synchronization functions to obtain optimal spin
41 @return The architecture specific spin lock alignment.
46 GetSpinLockProperties (
50 return InternalGetSpinLockProperties ();
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
66 @return SpinLock is in release state.
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.
101 @return SpinLock acquired the lock.
107 IN OUT SPIN_LOCK
*SpinLock
119 if (PcdGet32 (PcdSpinLockTimeout
) == 0) {
120 while (!AcquireSpinLockOrFail (SpinLock
)) {
123 } else if (!AcquireSpinLockOrFail (SpinLock
)) {
125 // Get the current timer value
127 Current
= GetPerformanceCounter();
130 // Initialize local variables
137 // Retrieve the performance counter properties and compute the number of performance
138 // counter ticks required to reach the timeout
140 Timeout
= DivU64x32 (
142 GetPerformanceCounterProperties (&Start
, &End
),
143 PcdGet32 (PcdSpinLockTimeout
)
153 while (!AcquireSpinLockOrFail (SpinLock
)) {
156 Current
= GetPerformanceCounter();
157 Delta
= (INT64
) (Current
- Previous
);
165 ASSERT (Total
< Timeout
);
172 Attempts to place a spin lock in the acquired state.
174 This function checks the state of the spin lock specified by SpinLock. If
175 SpinLock is in the released state, then this function places SpinLock in the
176 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
177 transitions of SpinLock must be performed using MP safe mechanisms.
179 If SpinLock is NULL, then ASSERT().
180 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
182 @param SpinLock A pointer to the spin lock to place in the acquired state.
184 @retval TRUE SpinLock was placed in the acquired state.
185 @retval FALSE SpinLock could not be acquired.
190 AcquireSpinLockOrFail (
191 IN OUT SPIN_LOCK
*SpinLock
197 ASSERT (SpinLock
!= NULL
);
199 LockValue
= *SpinLock
;
200 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
202 _ReadWriteBarrier ();
203 Result
= InterlockedCompareExchangePointer (
205 (VOID
*)SPIN_LOCK_RELEASED
,
206 (VOID
*)SPIN_LOCK_ACQUIRED
209 _ReadWriteBarrier ();
210 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
214 Releases a spin lock.
216 This function places the spin lock specified by SpinLock in the release state
217 and returns SpinLock.
219 If SpinLock is NULL, then ASSERT().
220 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
222 @param SpinLock A pointer to the spin lock to release.
224 @return SpinLock released the lock.
230 IN OUT SPIN_LOCK
*SpinLock
235 ASSERT (SpinLock
!= NULL
);
237 LockValue
= *SpinLock
;
238 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
240 _ReadWriteBarrier ();
241 *SpinLock
= SPIN_LOCK_RELEASED
;
242 _ReadWriteBarrier ();
248 Performs an atomic increment of an 32-bit unsigned integer.
250 Performs an atomic increment of the 32-bit unsigned integer specified by
251 Value and returns the incremented value. The increment operation must be
252 performed using MP safe mechanisms.
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 (
264 IN
volatile UINT32
*Value
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.
278 If Value is NULL, then ASSERT().
280 @param Value A pointer to the 32-bit value to decrement.
282 @return The decremented value.
287 InterlockedDecrement (
288 IN
volatile UINT32
*Value
291 ASSERT (Value
!= NULL
);
292 return InternalSyncDecrement (Value
);
296 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
298 Performs an atomic compare exchange operation on the 16-bit unsigned integer
299 specified by Value. If Value is equal to CompareValue, then Value is set to
300 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
301 then Value is returned. The compare exchange operation must be performed using
304 If Value is NULL, then ASSERT().
306 @param Value A pointer to the 16-bit value for the compare exchange
308 @param CompareValue A 16-bit value used in a compare operation.
309 @param ExchangeValue A 16-bit value used in an exchange operation.
311 @return The original *Value before exchange.
316 InterlockedCompareExchange16 (
317 IN OUT
volatile UINT16
*Value
,
318 IN UINT16 CompareValue
,
319 IN UINT16 ExchangeValue
322 ASSERT (Value
!= NULL
);
323 return InternalSyncCompareExchange16 (Value
, CompareValue
, ExchangeValue
);
327 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
329 Performs an atomic compare exchange operation on the 32-bit unsigned integer
330 specified by Value. If Value is equal to CompareValue, then Value is set to
331 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
332 then Value is returned. The compare exchange operation must be performed using
335 If Value is NULL, then ASSERT().
337 @param Value A pointer to the 32-bit value for the compare exchange
339 @param CompareValue A 32-bit value used in a compare operation.
340 @param ExchangeValue A 32-bit value used in an exchange operation.
342 @return The original *Value before exchange.
347 InterlockedCompareExchange32 (
348 IN OUT
volatile UINT32
*Value
,
349 IN UINT32 CompareValue
,
350 IN UINT32 ExchangeValue
353 ASSERT (Value
!= NULL
);
354 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
358 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
360 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
361 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
362 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
363 The compare exchange operation must be performed using MP safe mechanisms.
365 If Value is NULL, then ASSERT().
367 @param Value A pointer to the 64-bit value for the compare exchange
369 @param CompareValue A 64-bit value used in a compare operation.
370 @param ExchangeValue A 64-bit value used in an exchange operation.
372 @return The original *Value before exchange.
377 InterlockedCompareExchange64 (
378 IN OUT
volatile UINT64
*Value
,
379 IN UINT64 CompareValue
,
380 IN UINT64 ExchangeValue
383 ASSERT (Value
!= NULL
);
384 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
388 Performs an atomic compare exchange operation on a pointer value.
390 Performs an atomic compare exchange operation on the pointer value specified
391 by Value. If Value is equal to CompareValue, then Value is set to
392 ExchangeValue and CompareValue is returned. If Value is not equal to
393 CompareValue, then Value is returned. The compare exchange operation must be
394 performed using MP safe mechanisms.
396 If Value is NULL, then ASSERT().
398 @param Value A pointer to the pointer value for the compare exchange
400 @param CompareValue A pointer value used in a compare operation.
401 @param ExchangeValue A pointer value used in an exchange operation.
403 @return The original *Value before exchange.
407 InterlockedCompareExchangePointer (
408 IN OUT VOID
* volatile *Value
,
409 IN VOID
*CompareValue
,
410 IN VOID
*ExchangeValue
415 SizeOfValue
= (UINT8
) sizeof (*Value
);
417 switch (SizeOfValue
) {
418 case sizeof (UINT32
):
419 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
420 (volatile UINT32
*)Value
,
421 (UINT32
)(UINTN
)CompareValue
,
422 (UINT32
)(UINTN
)ExchangeValue
424 case sizeof (UINT64
):
425 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
426 (volatile UINT64
*)Value
,
427 (UINT64
)(UINTN
)CompareValue
,
428 (UINT64
)(UINTN
)ExchangeValue