2 Implementation of synchronization functions.
4 Copyright (c) 2006, 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: Synchronization.c
17 #define SPIN_LOCK_RELEASED ((SPIN_LOCK)0)
18 #define SPIN_LOCK_ACQUIRED ((SPIN_LOCK)-1)
22 InternalSyncIncrement (
23 IN
volatile UINT32
*Value
28 InternalSyncDecrement (
29 IN
volatile UINT32
*Value
34 InternalSyncCompareExchange32 (
35 IN
volatile UINT32
*Value
,
36 IN UINT32 CompareValue
,
37 IN UINT32 ExchangeValue
42 InternalSyncCompareExchange64 (
43 IN
volatile UINT64
*Value
,
44 IN UINT64 CompareValue
,
45 IN UINT64 ExchangeValue
49 Retrieves the architecture specific spin lock alignment requirements for
50 optimal spin lock performance.
52 This function retrieves the spin lock alignment requirements for optimal
53 performance on a given CPU architecture. The spin lock alignment must be a
54 power of two and is returned by this function. If there are no alignment
55 requirements, then 1 must be returned. The spin lock synchronization
56 functions must function correctly if the spin lock size and alignment values
57 returned by this function are not used at all. These values are hints to the
58 consumers of the spin lock synchronization functions to obtain optimal spin
61 @return The architecture specific spin lock alignment.
66 GetSpinLockProperties (
70 // @bug May use a PCD entry to determine this alignment.
75 Initializes a spin lock to the released state and returns the spin lock.
77 This function initializes the spin lock specified by SpinLock to the released
78 state, and returns SpinLock. Optimal performance can be achieved by calling
79 GetSpinLockProperties() to determine the size and alignment requirements for
82 If SpinLock is NULL, then ASSERT().
84 @param SpinLock A pointer to the spin lock to initialize to the released
93 OUT SPIN_LOCK
*SpinLock
96 ASSERT (SpinLock
!= NULL
);
102 Waits until a spin lock can be placed in the acquired state.
104 This function checks the state of the spin lock specified by SpinLock. If
105 SpinLock is in the released state, then this function places SpinLock in the
106 acquired state and returns SpinLock. Otherwise, this function waits
107 indefinitely for the spin lock to be released, and then places it in the
108 acquired state and returns SpinLock. All state transitions of SpinLock must
109 be performed using MP safe mechanisms.
111 If SpinLock is NULL, then ASSERT().
112 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
113 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
114 PcdSpinLockTimeout microseconds, then ASSERT().
116 @param SpinLock A pointer to the spin lock to place in the acquired state.
124 IN OUT SPIN_LOCK
*SpinLock
134 if (FixedPcdGet32 (PcdSpinLockTimeout
) > 0) {
135 Tick
= GetPerformanceCounter ();
136 Timeout
= DivU64x32 (
138 GetPerformanceCounterProperties (&Start
, &End
),
139 FixedPcdGet32 (PcdSpinLockTimeout
)
150 while (!AcquireSpinLockOrFail (SpinLock
)) {
152 ASSERT ((Start
< End
) ^ (Tick
<= GetPerformanceCounter ()));
158 Attempts to place a spin lock in the acquired state.
160 This function checks the state of the spin lock specified by SpinLock. If
161 SpinLock is in the released state, then this function places SpinLock in the
162 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
163 transitions of SpinLock must be performed using MP safe mechanisms.
165 If SpinLock is NULL, then ASSERT().
166 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
168 @param SpinLock A pointer to the spin lock to place in the acquired state.
170 @retval TRUE SpinLock was placed in the acquired state.
171 @retval FALSE SpinLock could not be acquired.
176 AcquireSpinLockOrFail (
177 IN OUT SPIN_LOCK
*SpinLock
180 ASSERT (SpinLock
!= NULL
);
182 InterlockedCompareExchangePointer (
184 (VOID
*)SPIN_LOCK_RELEASED
,
185 (VOID
*)SPIN_LOCK_ACQUIRED
186 ) == (VOID
*)SPIN_LOCK_RELEASED
191 Releases a spin lock.
193 This function places the spin lock specified by SpinLock in the release state
194 and returns SpinLock.
196 If SpinLock is NULL, then ASSERT().
197 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
199 @param SpinLock A pointer to the spin lock to release.
207 IN OUT SPIN_LOCK
*SpinLock
210 ASSERT (SpinLock
!= NULL
);
216 Performs an atomic increment of an 32-bit unsigned integer.
218 Performs an atomic increment of the 32-bit unsigned integer specified by
219 Value and returns the incremented value. The increment operation must be
220 performed using MP safe mechanisms. The state of the return value is not
221 guaranteed to be MP safe.
223 If Value is NULL, then ASSERT().
225 @param Value A pointer to the 32-bit value to increment.
227 @return The incremented value.
232 InterlockedIncrement (
236 ASSERT (Value
!= NULL
);
237 return InternalSyncIncrement (Value
);
241 Performs an atomic decrement of an 32-bit unsigned integer.
243 Performs an atomic decrement of the 32-bit unsigned integer specified by
244 Value and returns the decremented value. The decrement operation must be
245 performed using MP safe mechanisms. The state of the return value is not
246 guaranteed to be MP safe.
248 If Value is NULL, then ASSERT().
250 @param Value A pointer to the 32-bit value to decrement.
252 @return The decremented value.
257 InterlockedDecrement (
261 ASSERT (Value
!= NULL
);
262 return InternalSyncDecrement (Value
);
266 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
268 @param Value A pointer to the 32-bit value for the compare exchange
270 @param CompareValue 32-bit value used in compare operation.
271 @param ExchangeValue 32-bit value used in exchange operation.
273 @return The original *Value before exchange.
278 InterlockedCompareExchange32 (
280 IN UINT32 CompareValue
,
281 IN UINT32 ExchangeValue
284 ASSERT (Value
!= NULL
);
285 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
289 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
291 @param Value A pointer to the 64-bit value for the compare exchange
293 @param CompareValue 64-bit value used in compare operation.
294 @param ExchangeValue 64-bit value used in exchange operation.
296 @return The original *Value before exchange.
301 InterlockedCompareExchange64 (
303 IN UINT64 CompareValue
,
304 IN UINT64 ExchangeValue
307 ASSERT (Value
!= NULL
);
308 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
312 Performs an atomic compare exchange operation on a pointer value.
314 Performs an atomic compare exchange operation on the pointer value specified
315 by Value. If Value is equal to CompareValue, then Value is set to
316 ExchangeValue and CompareValue is returned. If Value is not equal to
317 CompareValue, then Value is returned. The compare exchange operation must be
318 performed using MP safe mechanisms.
320 If Value is NULL, then ASSERT().
322 @param Value A pointer to the pointer value for the compare exchange
324 @param CompareValue Pointer value used in compare operation.
325 @param ExchangeValue Pointer value used in exchange operation.
330 InterlockedCompareExchangePointer (
332 IN VOID
*CompareValue
,
333 IN VOID
*ExchangeValue
336 switch (sizeof (*Value
)) {
337 case sizeof (UINT32
):
338 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
340 (UINT32
)(UINTN
)CompareValue
,
341 (UINT32
)(UINTN
)ExchangeValue
343 case sizeof (UINT64
):
344 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
346 (UINT64
)(UINTN
)CompareValue
,
347 (UINT64
)(UINTN
)ExchangeValue