]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/SynchronizationMsc.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.
18 #include "BaseLibInternals.h"
21 Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics.
23 void _ReadWriteBarrier (void);
24 #pragma intrinsic(_ReadWriteBarrier)
27 #define SPIN_LOCK_RELEASED ((UINTN) 1)
28 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
31 Retrieves the architecture specific spin lock alignment requirements for
32 optimal spin lock performance.
34 This function retrieves the spin lock alignment requirements for optimal
35 performance on a given CPU architecture. The spin lock alignment must be a
36 power of two and is returned by this function. If there are no alignment
37 requirements, then 1 must be returned. The spin lock synchronization
38 functions must function correctly if the spin lock size and alignment values
39 returned by this function are not used at all. These values are hints to the
40 consumers of the spin lock synchronization functions to obtain optimal spin
43 @return The architecture specific spin lock alignment.
48 GetSpinLockProperties (
56 Initializes a spin lock to the released state and returns the spin lock.
58 This function initializes the spin lock specified by SpinLock to the released
59 state, and returns SpinLock. Optimal performance can be achieved by calling
60 GetSpinLockProperties() to determine the size and alignment requirements for
63 If SpinLock is NULL, then ASSERT().
65 @param SpinLock A pointer to the spin lock to initialize to the released
68 @return SpinLock in released state.
74 OUT SPIN_LOCK
*SpinLock
77 ASSERT (SpinLock
!= NULL
);
80 *SpinLock
= SPIN_LOCK_RELEASED
;
87 Waits until a spin lock can be placed in the acquired state.
89 This function checks the state of the spin lock specified by SpinLock. If
90 SpinLock is in the released state, then this function places SpinLock in the
91 acquired state and returns SpinLock. Otherwise, this function waits
92 indefinitely for the spin lock to be released, and then places it in the
93 acquired state and returns SpinLock. All state transitions of SpinLock must
94 be performed using MP safe mechanisms.
96 If SpinLock is NULL, then ASSERT().
97 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
98 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
99 PcdSpinLockTimeout microseconds, then ASSERT().
101 @param SpinLock A pointer to the spin lock to place in the acquired state.
103 @return SpinLock aquiring lock.
109 IN OUT SPIN_LOCK
*SpinLock
121 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
123 // Get the current timer value
125 Current
= GetPerformanceCounter();
128 // Initialize local variables
135 // Retrieve the performance counter properties and compute the number of performance
136 // counter ticks required to reach the timeout
138 Timeout
= DivU64x32 (
140 GetPerformanceCounterProperties (&Start
, &End
),
141 PcdGet32 (PcdSpinLockTimeout
)
151 while (!AcquireSpinLockOrFail (SpinLock
)) {
154 Current
= GetPerformanceCounter();
155 Delta
= (INT64
) (Current
- Previous
);
163 ASSERT (Total
< Timeout
);
166 while (!AcquireSpinLockOrFail (SpinLock
)) {
174 Attempts to place a spin lock in the acquired state.
176 This function checks the state of the spin lock specified by SpinLock. If
177 SpinLock is in the released state, then this function places SpinLock in the
178 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
179 transitions of SpinLock must be performed using MP safe mechanisms.
181 If SpinLock is NULL, then ASSERT().
182 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
184 @param SpinLock A pointer to the spin lock to place in the acquired state.
186 @retval TRUE SpinLock was placed in the acquired state.
187 @retval FALSE SpinLock could not be acquired.
192 AcquireSpinLockOrFail (
193 IN OUT SPIN_LOCK
*SpinLock
199 ASSERT (SpinLock
!= NULL
);
201 LockValue
= *SpinLock
;
202 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
204 _ReadWriteBarrier ();
205 Result
= InterlockedCompareExchangePointer (
207 (VOID
*)SPIN_LOCK_RELEASED
,
208 (VOID
*)SPIN_LOCK_ACQUIRED
211 _ReadWriteBarrier ();
212 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
216 Releases a spin lock.
218 This function places the spin lock specified by SpinLock in the release state
219 and returns SpinLock.
221 If SpinLock is NULL, then ASSERT().
222 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
224 @param SpinLock A pointer to the spin lock to release.
226 @return SpinLock releasing lock.
232 IN OUT SPIN_LOCK
*SpinLock
237 ASSERT (SpinLock
!= NULL
);
239 LockValue
= *SpinLock
;
240 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
242 _ReadWriteBarrier ();
243 *SpinLock
= SPIN_LOCK_RELEASED
;
244 _ReadWriteBarrier ();
250 Performs an atomic increment of an 32-bit unsigned integer.
252 Performs an atomic increment of the 32-bit unsigned integer specified by
253 Value and returns the incremented value. The increment operation must be
254 performed using MP safe mechanisms. The state of the return value is not
255 guaranteed to be MP safe.
257 If Value is NULL, then ASSERT().
259 @param Value A pointer to the 32-bit value to increment.
261 @return The incremented value.
266 InterlockedIncrement (
270 ASSERT (Value
!= NULL
);
271 return InternalSyncIncrement (Value
);
275 Performs an atomic decrement of an 32-bit unsigned integer.
277 Performs an atomic decrement of the 32-bit unsigned integer specified by
278 Value and returns the decremented value. The decrement operation must be
279 performed using MP safe mechanisms. The state of the return value is not
280 guaranteed to be MP safe.
282 If Value is NULL, then ASSERT().
284 @param Value A pointer to the 32-bit value to decrement.
286 @return The decremented value.
291 InterlockedDecrement (
295 ASSERT (Value
!= NULL
);
296 return InternalSyncDecrement (Value
);
300 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
302 Performs an atomic compare exchange operation on the 32-bit unsigned integer
303 specified by Value. If Value is equal to CompareValue, then Value is set to
304 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
305 then Value is returned. The compare exchange operation must be performed using
308 If Value is NULL, then ASSERT().
310 @param Value A pointer to the 32-bit value for the compare exchange
312 @param CompareValue 32-bit value used in compare operation.
313 @param ExchangeValue 32-bit value used in exchange operation.
315 @return The original *Value before exchange.
320 InterlockedCompareExchange32 (
321 IN OUT UINT32
*Value
,
322 IN UINT32 CompareValue
,
323 IN UINT32 ExchangeValue
326 ASSERT (Value
!= NULL
);
327 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
331 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
333 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
334 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
335 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
336 The compare exchange operation must be performed using MP safe mechanisms.
338 If Value is NULL, then ASSERT().
340 @param Value A pointer to the 64-bit value for the compare exchange
342 @param CompareValue 64-bit value used in compare operation.
343 @param ExchangeValue 64-bit value used in exchange operation.
345 @return The original *Value before exchange.
350 InterlockedCompareExchange64 (
351 IN OUT UINT64
*Value
,
352 IN UINT64 CompareValue
,
353 IN UINT64 ExchangeValue
356 ASSERT (Value
!= NULL
);
357 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
361 Performs an atomic compare exchange operation on a pointer value.
363 Performs an atomic compare exchange operation on the pointer value specified
364 by Value. If Value is equal to CompareValue, then Value is set to
365 ExchangeValue and CompareValue is returned. If Value is not equal to
366 CompareValue, then Value is returned. The compare exchange operation must be
367 performed using MP safe mechanisms.
369 If Value is NULL, then ASSERT().
371 @param Value A pointer to the pointer value for the compare exchange
373 @param CompareValue Pointer value used in compare operation.
374 @param ExchangeValue Pointer value used in exchange operation.
376 @return The original *Value before exchange.
381 InterlockedCompareExchangePointer (
383 IN VOID
*CompareValue
,
384 IN VOID
*ExchangeValue
389 SizeOfValue
= sizeof (*Value
);
391 switch (SizeOfValue
) {
392 case sizeof (UINT32
):
393 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
395 (UINT32
)(UINTN
)CompareValue
,
396 (UINT32
)(UINTN
)ExchangeValue
398 case sizeof (UINT64
):
399 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
401 (UINT64
)(UINTN
)CompareValue
,
402 (UINT64
)(UINTN
)ExchangeValue