]>
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.
15 void _ReadWriteBarrier (void);
16 #pragma intrinsic(_ReadWriteBarrier)
19 #define SPIN_LOCK_RELEASED ((UINTN) 1)
20 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
23 Retrieves the architecture specific spin lock alignment requirements for
24 optimal spin lock performance.
26 This function retrieves the spin lock alignment requirements for optimal
27 performance on a given CPU architecture. The spin lock alignment is byte alignment.
28 It must be a power of two and is returned by this function. If there are no alignment
29 requirements, then 1 must be returned. The spin lock synchronization
30 functions must function correctly if the spin lock size and alignment values
31 returned by this function are not used at all. These values are hints to the
32 consumers of the spin lock synchronization functions to obtain optimal spin
35 @return The architecture specific spin lock alignment.
40 GetSpinLockProperties (
44 return InternalGetSpinLockProperties ();
48 Initializes a spin lock to the released state and returns the spin lock.
50 This function initializes the spin lock specified by SpinLock to the released
51 state, and returns SpinLock. Optimal performance can be achieved by calling
52 GetSpinLockProperties() to determine the size and alignment requirements for
55 If SpinLock is NULL, then ASSERT().
57 @param SpinLock A pointer to the spin lock to initialize to the released
60 @return SpinLock is in release state.
66 OUT SPIN_LOCK
*SpinLock
69 ASSERT (SpinLock
!= NULL
);
72 *SpinLock
= SPIN_LOCK_RELEASED
;
79 Waits until a spin lock can be placed in the acquired state.
81 This function checks the state of the spin lock specified by SpinLock. If
82 SpinLock is in the released state, then this function places SpinLock in the
83 acquired state and returns SpinLock. Otherwise, this function waits
84 indefinitely for the spin lock to be released, and then places it in the
85 acquired state and returns SpinLock. All state transitions of SpinLock must
86 be performed using MP safe mechanisms.
88 If SpinLock is NULL, then ASSERT().
89 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
90 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
91 PcdSpinLockTimeout microseconds, then ASSERT().
93 @param SpinLock A pointer to the spin lock to place in the acquired state.
95 @return SpinLock acquired the lock.
101 IN OUT SPIN_LOCK
*SpinLock
113 if (PcdGet32 (PcdSpinLockTimeout
) == 0) {
114 while (!AcquireSpinLockOrFail (SpinLock
)) {
117 } else if (!AcquireSpinLockOrFail (SpinLock
)) {
119 // Get the current timer value
121 Current
= GetPerformanceCounter();
124 // Initialize local variables
131 // Retrieve the performance counter properties and compute the number of performance
132 // counter ticks required to reach the timeout
134 Timeout
= DivU64x32 (
136 GetPerformanceCounterProperties (&Start
, &End
),
137 PcdGet32 (PcdSpinLockTimeout
)
147 while (!AcquireSpinLockOrFail (SpinLock
)) {
150 Current
= GetPerformanceCounter();
151 Delta
= (INT64
) (Current
- Previous
);
159 ASSERT (Total
< Timeout
);
166 Attempts to place a spin lock in the acquired state.
168 This function checks the state of the spin lock specified by SpinLock. If
169 SpinLock is in the released state, then this function places SpinLock in the
170 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
171 transitions of SpinLock must be performed using MP safe mechanisms.
173 If SpinLock is NULL, then ASSERT().
174 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
176 @param SpinLock A pointer to the spin lock to place in the acquired state.
178 @retval TRUE SpinLock was placed in the acquired state.
179 @retval FALSE SpinLock could not be acquired.
184 AcquireSpinLockOrFail (
185 IN OUT SPIN_LOCK
*SpinLock
191 ASSERT (SpinLock
!= NULL
);
193 LockValue
= *SpinLock
;
194 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
196 _ReadWriteBarrier ();
197 Result
= InterlockedCompareExchangePointer (
199 (VOID
*)SPIN_LOCK_RELEASED
,
200 (VOID
*)SPIN_LOCK_ACQUIRED
203 _ReadWriteBarrier ();
204 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
208 Releases a spin lock.
210 This function places the spin lock specified by SpinLock in the release state
211 and returns SpinLock.
213 If SpinLock is NULL, then ASSERT().
214 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
216 @param SpinLock A pointer to the spin lock to release.
218 @return SpinLock released the lock.
224 IN OUT SPIN_LOCK
*SpinLock
229 ASSERT (SpinLock
!= NULL
);
231 LockValue
= *SpinLock
;
232 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
234 _ReadWriteBarrier ();
235 *SpinLock
= SPIN_LOCK_RELEASED
;
236 _ReadWriteBarrier ();
242 Performs an atomic increment of an 32-bit unsigned integer.
244 Performs an atomic increment of the 32-bit unsigned integer specified by
245 Value and returns the incremented value. The increment operation must be
246 performed using MP safe mechanisms.
248 If Value is NULL, then ASSERT().
250 @param Value A pointer to the 32-bit value to increment.
252 @return The incremented value.
257 InterlockedIncrement (
258 IN
volatile UINT32
*Value
261 ASSERT (Value
!= NULL
);
262 return InternalSyncIncrement (Value
);
266 Performs an atomic decrement of an 32-bit unsigned integer.
268 Performs an atomic decrement of the 32-bit unsigned integer specified by
269 Value and returns the decremented value. The decrement operation must be
270 performed using MP safe mechanisms.
272 If Value is NULL, then ASSERT().
274 @param Value A pointer to the 32-bit value to decrement.
276 @return The decremented value.
281 InterlockedDecrement (
282 IN
volatile UINT32
*Value
285 ASSERT (Value
!= NULL
);
286 return InternalSyncDecrement (Value
);
290 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
292 Performs an atomic compare exchange operation on the 16-bit unsigned integer
293 specified by Value. If Value is equal to CompareValue, then Value is set to
294 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
295 then Value is returned. The compare exchange operation must be performed using
298 If Value is NULL, then ASSERT().
300 @param Value A pointer to the 16-bit value for the compare exchange
302 @param CompareValue A 16-bit value used in a compare operation.
303 @param ExchangeValue A 16-bit value used in an exchange operation.
305 @return The original *Value before exchange.
310 InterlockedCompareExchange16 (
311 IN OUT
volatile UINT16
*Value
,
312 IN UINT16 CompareValue
,
313 IN UINT16 ExchangeValue
316 ASSERT (Value
!= NULL
);
317 return InternalSyncCompareExchange16 (Value
, CompareValue
, ExchangeValue
);
321 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
323 Performs an atomic compare exchange operation on the 32-bit unsigned integer
324 specified by Value. If Value is equal to CompareValue, then Value is set to
325 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
326 then Value is returned. The compare exchange operation must be performed using
329 If Value is NULL, then ASSERT().
331 @param Value A pointer to the 32-bit value for the compare exchange
333 @param CompareValue A 32-bit value used in a compare operation.
334 @param ExchangeValue A 32-bit value used in an exchange operation.
336 @return The original *Value before exchange.
341 InterlockedCompareExchange32 (
342 IN OUT
volatile UINT32
*Value
,
343 IN UINT32 CompareValue
,
344 IN UINT32 ExchangeValue
347 ASSERT (Value
!= NULL
);
348 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
352 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
354 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
355 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
356 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
357 The compare exchange operation must be performed using MP safe mechanisms.
359 If Value is NULL, then ASSERT().
361 @param Value A pointer to the 64-bit value for the compare exchange
363 @param CompareValue A 64-bit value used in a compare operation.
364 @param ExchangeValue A 64-bit value used in an exchange operation.
366 @return The original *Value before exchange.
371 InterlockedCompareExchange64 (
372 IN OUT
volatile UINT64
*Value
,
373 IN UINT64 CompareValue
,
374 IN UINT64 ExchangeValue
377 ASSERT (Value
!= NULL
);
378 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
382 Performs an atomic compare exchange operation on a pointer value.
384 Performs an atomic compare exchange operation on the pointer value specified
385 by Value. If Value is equal to CompareValue, then Value is set to
386 ExchangeValue and CompareValue is returned. If Value is not equal to
387 CompareValue, then Value is returned. The compare exchange operation must be
388 performed using MP safe mechanisms.
390 If Value is NULL, then ASSERT().
392 @param Value A pointer to the pointer value for the compare exchange
394 @param CompareValue A pointer value used in a compare operation.
395 @param ExchangeValue A pointer value used in an exchange operation.
397 @return The original *Value before exchange.
401 InterlockedCompareExchangePointer (
402 IN OUT VOID
* volatile *Value
,
403 IN VOID
*CompareValue
,
404 IN VOID
*ExchangeValue
409 SizeOfValue
= (UINT8
) sizeof (*Value
);
411 switch (SizeOfValue
) {
412 case sizeof (UINT32
):
413 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
414 (volatile UINT32
*)Value
,
415 (UINT32
)(UINTN
)CompareValue
,
416 (UINT32
)(UINTN
)ExchangeValue
418 case sizeof (UINT64
):
419 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
420 (volatile UINT64
*)Value
,
421 (UINT64
)(UINTN
)CompareValue
,
422 (UINT64
)(UINTN
)ExchangeValue