fb10b5d01b59fceff5e0377e8a26c3653c3aa290
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.
13 Module Name: SynchronizationMsc.c
17 #include "BaseLibInternals.h"
20 // Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics
22 void _ReadWriteBarrier (void);
23 #pragma intrinsic(_ReadWriteBarrier)
26 #define SPIN_LOCK_RELEASED ((UINTN) 1)
27 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
30 Retrieves the architecture specific spin lock alignment requirements for
31 optimal spin lock performance.
33 This function retrieves the spin lock alignment requirements for optimal
34 performance on a given CPU architecture. The spin lock alignment must be a
35 power of two and is returned by this function. If there are no alignment
36 requirements, then 1 must be returned. The spin lock synchronization
37 functions must function correctly if the spin lock size and alignment values
38 returned by this function are not used at all. These values are hints to the
39 consumers of the spin lock synchronization functions to obtain optimal spin
42 @return The architecture specific spin lock alignment.
47 GetSpinLockProperties (
51 // @bug May use a PCD entry to determine this alignment.
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
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.
109 IN OUT SPIN_LOCK
*SpinLock
119 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
120 Tick
= GetPerformanceCounter ();
121 Timeout
= DivU64x32 (
123 GetPerformanceCounterProperties (&Start
, &End
),
124 PcdGet32 (PcdSpinLockTimeout
)
135 while (!AcquireSpinLockOrFail (SpinLock
)) {
137 ASSERT ((Start
< End
) ^ (Tick
<= GetPerformanceCounter ()));
143 Attempts to place a spin lock in the acquired state.
145 This function checks the state of the spin lock specified by SpinLock. If
146 SpinLock is in the released state, then this function places SpinLock in the
147 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
148 transitions of SpinLock must be performed using MP safe mechanisms.
150 If SpinLock is NULL, then ASSERT().
151 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
153 @param SpinLock A pointer to the spin lock to place in the acquired state.
155 @retval TRUE SpinLock was placed in the acquired state.
156 @retval FALSE SpinLock could not be acquired.
161 AcquireSpinLockOrFail (
162 IN OUT SPIN_LOCK
*SpinLock
167 ASSERT (SpinLock
!= NULL
);
168 ASSERT (*SpinLock
== SPIN_LOCK_ACQUIRED
|| *SpinLock
== SPIN_LOCK_RELEASED
);
170 _ReadWriteBarrier ();
171 Result
= InterlockedCompareExchangePointer (
173 (VOID
*)SPIN_LOCK_RELEASED
,
174 (VOID
*)SPIN_LOCK_ACQUIRED
177 _ReadWriteBarrier ();
178 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
182 Releases a spin lock.
184 This function places the spin lock specified by SpinLock in the release state
185 and returns SpinLock.
187 If SpinLock is NULL, then ASSERT().
188 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
190 @param SpinLock A pointer to the spin lock to release.
198 IN OUT SPIN_LOCK
*SpinLock
201 ASSERT (SpinLock
!= NULL
);
202 ASSERT (*SpinLock
== SPIN_LOCK_ACQUIRED
|| *SpinLock
== SPIN_LOCK_RELEASED
);
204 _ReadWriteBarrier ();
205 *SpinLock
= SPIN_LOCK_RELEASED
;
206 _ReadWriteBarrier ();
212 Performs an atomic increment of an 32-bit unsigned integer.
214 Performs an atomic increment of the 32-bit unsigned integer specified by
215 Value and returns the incremented value. The increment operation must be
216 performed using MP safe mechanisms. The state of the return value is not
217 guaranteed to be MP safe.
219 If Value is NULL, then ASSERT().
221 @param Value A pointer to the 32-bit value to increment.
223 @return The incremented value.
228 InterlockedIncrement (
232 ASSERT (Value
!= NULL
);
233 return InternalSyncIncrement (Value
);
237 Performs an atomic decrement of an 32-bit unsigned integer.
239 Performs an atomic decrement of the 32-bit unsigned integer specified by
240 Value and returns the decremented value. The decrement operation must be
241 performed using MP safe mechanisms. The state of the return value is not
242 guaranteed to be MP safe.
244 If Value is NULL, then ASSERT().
246 @param Value A pointer to the 32-bit value to decrement.
248 @return The decremented value.
253 InterlockedDecrement (
257 ASSERT (Value
!= NULL
);
258 return InternalSyncDecrement (Value
);
262 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
264 Performs an atomic compare exchange operation on the 32-bit unsigned integer
265 specified by Value. If Value is equal to CompareValue, then Value is set to
266 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
267 then Value is returned. The compare exchange operation must be performed using
270 If Value is NULL, then ASSERT().
272 @param Value A pointer to the 32-bit value for the compare exchange
274 @param CompareValue 32-bit value used in compare operation.
275 @param ExchangeValue 32-bit value used in exchange operation.
277 @return The original *Value before exchange.
282 InterlockedCompareExchange32 (
283 IN OUT UINT32
*Value
,
284 IN UINT32 CompareValue
,
285 IN UINT32 ExchangeValue
288 ASSERT (Value
!= NULL
);
289 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
293 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
295 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
296 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
297 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
298 The compare exchange operation must be performed using MP safe mechanisms.
300 If Value is NULL, then ASSERT().
302 @param Value A pointer to the 64-bit value for the compare exchange
304 @param CompareValue 64-bit value used in compare operation.
305 @param ExchangeValue 64-bit value used in exchange operation.
307 @return The original *Value before exchange.
312 InterlockedCompareExchange64 (
313 IN OUT UINT64
*Value
,
314 IN UINT64 CompareValue
,
315 IN UINT64 ExchangeValue
318 ASSERT (Value
!= NULL
);
319 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
323 Performs an atomic compare exchange operation on a pointer value.
325 Performs an atomic compare exchange operation on the pointer value specified
326 by Value. If Value is equal to CompareValue, then Value is set to
327 ExchangeValue and CompareValue is returned. If Value is not equal to
328 CompareValue, then Value is returned. The compare exchange operation must be
329 performed using MP safe mechanisms.
331 If Value is NULL, then ASSERT().
333 @param Value A pointer to the pointer value for the compare exchange
335 @param CompareValue Pointer value used in compare operation.
336 @param ExchangeValue Pointer value used in exchange operation.
341 InterlockedCompareExchangePointer (
343 IN VOID
*CompareValue
,
344 IN VOID
*ExchangeValue
349 SizeOfValue
= sizeof (*Value
);
351 switch (SizeOfValue
) {
352 case sizeof (UINT32
):
353 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
355 (UINT32
)(UINTN
)CompareValue
,
356 (UINT32
)(UINTN
)ExchangeValue
358 case sizeof (UINT64
):
359 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
361 (UINT64
)(UINTN
)CompareValue
,
362 (UINT64
)(UINTN
)ExchangeValue