]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Synchronization.c
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)1)
18 #define SPIN_LOCK_ACQUIRED ((SPIN_LOCK)2)
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
);
97 *SpinLock
= SPIN_LOCK_RELEASED
;
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 (PcdGet32 (PcdSpinLockTimeout
) > 0) {
135 Tick
= GetPerformanceCounter ();
136 Timeout
= DivU64x32 (
138 GetPerformanceCounterProperties (&Start
, &End
),
139 PcdGet32 (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
);
181 ASSERT (*SpinLock
== SPIN_LOCK_ACQUIRED
|| *SpinLock
== SPIN_LOCK_RELEASED
);
183 InterlockedCompareExchangePointer (
185 (VOID
*)SPIN_LOCK_RELEASED
,
186 (VOID
*)SPIN_LOCK_ACQUIRED
187 ) == (VOID
*)SPIN_LOCK_RELEASED
192 Releases a spin lock.
194 This function places the spin lock specified by SpinLock in the release state
195 and returns SpinLock.
197 If SpinLock is NULL, then ASSERT().
198 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
200 @param SpinLock A pointer to the spin lock to release.
208 IN OUT SPIN_LOCK
*SpinLock
211 ASSERT (SpinLock
!= NULL
);
212 ASSERT (*SpinLock
== SPIN_LOCK_ACQUIRED
|| *SpinLock
== SPIN_LOCK_RELEASED
);
213 *SpinLock
= SPIN_LOCK_RELEASED
;
218 Performs an atomic increment of an 32-bit unsigned integer.
220 Performs an atomic increment of the 32-bit unsigned integer specified by
221 Value and returns the incremented value. The increment operation must be
222 performed using MP safe mechanisms. The state of the return value is not
223 guaranteed to be MP safe.
225 If Value is NULL, then ASSERT().
227 @param Value A pointer to the 32-bit value to increment.
229 @return The incremented value.
234 InterlockedIncrement (
238 ASSERT (Value
!= NULL
);
239 return InternalSyncIncrement (Value
);
243 Performs an atomic decrement of an 32-bit unsigned integer.
245 Performs an atomic decrement of the 32-bit unsigned integer specified by
246 Value and returns the decremented value. The decrement operation must be
247 performed using MP safe mechanisms. The state of the return value is not
248 guaranteed to be MP safe.
250 If Value is NULL, then ASSERT().
252 @param Value A pointer to the 32-bit value to decrement.
254 @return The decremented value.
259 InterlockedDecrement (
263 ASSERT (Value
!= NULL
);
264 return InternalSyncDecrement (Value
);
268 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
270 @param Value A pointer to the 32-bit value for the compare exchange
272 @param CompareValue 32-bit value used in compare operation.
273 @param ExchangeValue 32-bit value used in exchange operation.
275 @return The original *Value before exchange.
280 InterlockedCompareExchange32 (
281 IN OUT UINT32
*Value
,
282 IN UINT32 CompareValue
,
283 IN UINT32 ExchangeValue
286 ASSERT (Value
!= NULL
);
287 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
291 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
293 @param Value A pointer to the 64-bit value for the compare exchange
295 @param CompareValue 64-bit value used in compare operation.
296 @param ExchangeValue 64-bit value used in exchange operation.
298 @return The original *Value before exchange.
303 InterlockedCompareExchange64 (
304 IN OUT UINT64
*Value
,
305 IN UINT64 CompareValue
,
306 IN UINT64 ExchangeValue
309 ASSERT (Value
!= NULL
);
310 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
314 Performs an atomic compare exchange operation on a pointer value.
316 Performs an atomic compare exchange operation on the pointer value specified
317 by Value. If Value is equal to CompareValue, then Value is set to
318 ExchangeValue and CompareValue is returned. If Value is not equal to
319 CompareValue, then Value is returned. The compare exchange operation must be
320 performed using MP safe mechanisms.
322 If Value is NULL, then ASSERT().
324 @param Value A pointer to the pointer value for the compare exchange
326 @param CompareValue Pointer value used in compare operation.
327 @param ExchangeValue Pointer value used in exchange operation.
332 InterlockedCompareExchangePointer (
334 IN VOID
*CompareValue
,
335 IN VOID
*ExchangeValue
338 switch (sizeof (*Value
)) {
339 case sizeof (UINT32
):
340 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
342 (UINT32
)(UINTN
)CompareValue
,
343 (UINT32
)(UINTN
)ExchangeValue
345 case sizeof (UINT64
):
346 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
348 (UINT64
)(UINTN
)CompareValue
,
349 (UINT64
)(UINTN
)ExchangeValue