]>
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)
21 Performs an atomic increment of an 32-bit unsigned integer.
23 Performs an atomic increment of the 32-bit unsigned integer specified by
24 Value and returns the incremented value. The increment operation must be
25 performed using MP safe mechanisms. The state of the return value is not
26 guaranteed to be MP safe.
28 @param Value A pointer to the 32-bit value to increment.
30 @return The incremented value.
35 InternalSyncIncrement (
36 IN
volatile UINT32
*Value
40 Performs an atomic decrement of an 32-bit unsigned integer.
42 Performs an atomic decrement of the 32-bit unsigned integer specified by
43 Value and returns the decrement value. The decrement operation must be
44 performed using MP safe mechanisms. The state of the return value is not
45 guaranteed to be MP safe.
47 @param Value A pointer to the 32-bit value to decrement.
49 @return The decrement value.
54 InternalSyncDecrement (
55 IN
volatile UINT32
*Value
59 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
61 Performs an atomic compare exchange operation on the 32-bit unsigned integer
62 specified by Value. If Value is equal to CompareValue, then Value is set to
63 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
64 then Value is returned. The compare exchange operation must be performed using
67 @param Value A pointer to the 32-bit value for the compare exchange
69 @param CompareValue 32-bit value used in compare operation.
70 @param ExchangeValue 32-bit value used in exchange operation.
72 @return The original *Value before exchange.
77 InternalSyncCompareExchange32 (
78 IN
volatile UINT32
*Value
,
79 IN UINT32 CompareValue
,
80 IN UINT32 ExchangeValue
84 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
86 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
87 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
88 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
89 The compare exchange operation must be performed using MP safe mechanisms.
91 @param Value A pointer to the 64-bit value for the compare exchange
93 @param CompareValue 64-bit value used in compare operation.
94 @param ExchangeValue 64-bit value used in exchange operation.
96 @return The original *Value before exchange.
101 InternalSyncCompareExchange64 (
102 IN
volatile UINT64
*Value
,
103 IN UINT64 CompareValue
,
104 IN UINT64 ExchangeValue
108 Retrieves the architecture specific spin lock alignment requirements for
109 optimal spin lock performance.
111 This function retrieves the spin lock alignment requirements for optimal
112 performance on a given CPU architecture. The spin lock alignment must be a
113 power of two and is returned by this function. If there are no alignment
114 requirements, then 1 must be returned. The spin lock synchronization
115 functions must function correctly if the spin lock size and alignment values
116 returned by this function are not used at all. These values are hints to the
117 consumers of the spin lock synchronization functions to obtain optimal spin
120 @return The architecture specific spin lock alignment.
125 GetSpinLockProperties (
129 // @bug May use a PCD entry to determine this alignment.
134 Initializes a spin lock to the released state and returns the spin lock.
136 This function initializes the spin lock specified by SpinLock to the released
137 state, and returns SpinLock. Optimal performance can be achieved by calling
138 GetSpinLockProperties() to determine the size and alignment requirements for
141 If SpinLock is NULL, then ASSERT().
143 @param SpinLock A pointer to the spin lock to initialize to the released
152 OUT SPIN_LOCK
*SpinLock
155 ASSERT (SpinLock
!= NULL
);
156 *SpinLock
= SPIN_LOCK_RELEASED
;
161 Waits until a spin lock can be placed in the acquired state.
163 This function checks the state of the spin lock specified by SpinLock. If
164 SpinLock is in the released state, then this function places SpinLock in the
165 acquired state and returns SpinLock. Otherwise, this function waits
166 indefinitely for the spin lock to be released, and then places it in the
167 acquired state and returns SpinLock. All state transitions of SpinLock must
168 be performed using MP safe mechanisms.
170 If SpinLock is NULL, then ASSERT().
171 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
172 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
173 PcdSpinLockTimeout microseconds, then ASSERT().
175 @param SpinLock A pointer to the spin lock to place in the acquired state.
183 IN OUT SPIN_LOCK
*SpinLock
193 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
194 Tick
= GetPerformanceCounter ();
195 Timeout
= DivU64x32 (
197 GetPerformanceCounterProperties (&Start
, &End
),
198 PcdGet32 (PcdSpinLockTimeout
)
209 while (!AcquireSpinLockOrFail (SpinLock
)) {
211 ASSERT ((Start
< End
) ^ (Tick
<= GetPerformanceCounter ()));
217 Attempts to place a spin lock in the acquired state.
219 This function checks the state of the spin lock specified by SpinLock. If
220 SpinLock is in the released state, then this function places SpinLock in the
221 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
222 transitions of SpinLock must be performed using MP safe mechanisms.
224 If SpinLock is NULL, then ASSERT().
225 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
227 @param SpinLock A pointer to the spin lock to place in the acquired state.
229 @retval TRUE SpinLock was placed in the acquired state.
230 @retval FALSE SpinLock could not be acquired.
235 AcquireSpinLockOrFail (
236 IN OUT SPIN_LOCK
*SpinLock
239 volatile SPIN_LOCK LockValue
;
241 ASSERT (SpinLock
!= NULL
);
243 LockValue
= *SpinLock
;
244 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
247 InterlockedCompareExchangePointer (
249 (VOID
*)SPIN_LOCK_RELEASED
,
250 (VOID
*)SPIN_LOCK_ACQUIRED
251 ) == (VOID
*)SPIN_LOCK_RELEASED
256 Releases a spin lock.
258 This function places the spin lock specified by SpinLock in the release state
259 and returns SpinLock.
261 If SpinLock is NULL, then ASSERT().
262 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
264 @param SpinLock A pointer to the spin lock to release.
272 IN OUT SPIN_LOCK
*SpinLock
275 volatile SPIN_LOCK LockValue
;
277 ASSERT (SpinLock
!= NULL
);
279 LockValue
= *SpinLock
;
280 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
282 *SpinLock
= SPIN_LOCK_RELEASED
;
287 Performs an atomic increment of an 32-bit unsigned integer.
289 Performs an atomic increment of the 32-bit unsigned integer specified by
290 Value and returns the incremented value. The increment operation must be
291 performed using MP safe mechanisms. The state of the return value is not
292 guaranteed to be MP safe.
294 If Value is NULL, then ASSERT().
296 @param Value A pointer to the 32-bit value to increment.
298 @return The incremented value.
303 InterlockedIncrement (
307 ASSERT (Value
!= NULL
);
308 return InternalSyncIncrement (Value
);
312 Performs an atomic decrement of an 32-bit unsigned integer.
314 Performs an atomic decrement of the 32-bit unsigned integer specified by
315 Value and returns the decremented value. The decrement operation must be
316 performed using MP safe mechanisms. The state of the return value is not
317 guaranteed to be MP safe.
319 If Value is NULL, then ASSERT().
321 @param Value A pointer to the 32-bit value to decrement.
323 @return The decremented value.
328 InterlockedDecrement (
332 ASSERT (Value
!= NULL
);
333 return InternalSyncDecrement (Value
);
337 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
339 Performs an atomic compare exchange operation on the 32-bit unsigned integer
340 specified by Value. If Value is equal to CompareValue, then Value is set to
341 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
342 then Value is returned. The compare exchange operation must be performed using
345 If Value is NULL, then ASSERT().
347 @param Value A pointer to the 32-bit value for the compare exchange
349 @param CompareValue 32-bit value used in compare operation.
350 @param ExchangeValue 32-bit value used in exchange operation.
352 @return The original *Value before exchange.
357 InterlockedCompareExchange32 (
358 IN OUT UINT32
*Value
,
359 IN UINT32 CompareValue
,
360 IN UINT32 ExchangeValue
363 ASSERT (Value
!= NULL
);
364 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
368 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
370 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
371 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
372 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
373 The compare exchange operation must be performed using MP safe mechanisms.
375 If Value is NULL, then ASSERT().
377 @param Value A pointer to the 64-bit value for the compare exchange
379 @param CompareValue 64-bit value used in compare operation.
380 @param ExchangeValue 64-bit value used in exchange operation.
382 @return The original *Value before exchange.
387 InterlockedCompareExchange64 (
388 IN OUT UINT64
*Value
,
389 IN UINT64 CompareValue
,
390 IN UINT64 ExchangeValue
393 ASSERT (Value
!= NULL
);
394 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
398 Performs an atomic compare exchange operation on a pointer value.
400 Performs an atomic compare exchange operation on the pointer value specified
401 by Value. If Value is equal to CompareValue, then Value is set to
402 ExchangeValue and CompareValue is returned. If Value is not equal to
403 CompareValue, then Value is returned. The compare exchange operation must be
404 performed using MP safe mechanisms.
406 If Value is NULL, then ASSERT().
408 @param Value A pointer to the pointer value for the compare exchange
410 @param CompareValue Pointer value used in compare operation.
411 @param ExchangeValue Pointer value used in exchange operation.
416 InterlockedCompareExchangePointer (
418 IN VOID
*CompareValue
,
419 IN VOID
*ExchangeValue
422 switch (sizeof (*Value
)) {
423 case sizeof (UINT32
):
424 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
426 (UINT32
)(UINTN
)CompareValue
,
427 (UINT32
)(UINTN
)ExchangeValue
429 case sizeof (UINT64
):
430 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
432 (UINT64
)(UINTN
)CompareValue
,
433 (UINT64
)(UINTN
)ExchangeValue