]>
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.
13 Module Name: SynchronizationMsc.c
18 // Include common header file for this module.
20 #include "CommonHeader.h"
22 #include "BaseLibInternals.h"
25 // Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics
27 void _ReadWriteBarrier (void);
28 #pragma intrinsic(_ReadWriteBarrier)
31 #define SPIN_LOCK_RELEASED ((UINTN) 1)
32 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
35 Retrieves the architecture specific spin lock alignment requirements for
36 optimal spin lock performance.
38 This function retrieves the spin lock alignment requirements for optimal
39 performance on a given CPU architecture. The spin lock alignment must be a
40 power of two and is returned by this function. If there are no alignment
41 requirements, then 1 must be returned. The spin lock synchronization
42 functions must function correctly if the spin lock size and alignment values
43 returned by this function are not used at all. These values are hints to the
44 consumers of the spin lock synchronization functions to obtain optimal spin
47 @return The architecture specific spin lock alignment.
52 GetSpinLockProperties (
56 // @bug May use a PCD entry to determine this alignment.
61 Initializes a spin lock to the released state and returns the spin lock.
63 This function initializes the spin lock specified by SpinLock to the released
64 state, and returns SpinLock. Optimal performance can be achieved by calling
65 GetSpinLockProperties() to determine the size and alignment requirements for
68 If SpinLock is NULL, then ASSERT().
70 @param SpinLock A pointer to the spin lock to initialize to the released
79 OUT SPIN_LOCK
*SpinLock
82 ASSERT (SpinLock
!= NULL
);
85 *SpinLock
= SPIN_LOCK_RELEASED
;
92 Waits until a spin lock can be placed in the acquired state.
94 This function checks the state of the spin lock specified by SpinLock. If
95 SpinLock is in the released state, then this function places SpinLock in the
96 acquired state and returns SpinLock. Otherwise, this function waits
97 indefinitely for the spin lock to be released, and then places it in the
98 acquired state and returns SpinLock. All state transitions of SpinLock must
99 be performed using MP safe mechanisms.
101 If SpinLock is NULL, then ASSERT().
102 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
103 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
104 PcdSpinLockTimeout microseconds, then ASSERT().
106 @param SpinLock A pointer to the spin lock to place in the acquired state.
114 IN OUT SPIN_LOCK
*SpinLock
124 if (PcdGet32 (PcdSpinLockTimeout
) > 0) {
125 Tick
= GetPerformanceCounter ();
126 Timeout
= DivU64x32 (
128 GetPerformanceCounterProperties (&Start
, &End
),
129 PcdGet32 (PcdSpinLockTimeout
)
140 while (!AcquireSpinLockOrFail (SpinLock
)) {
142 ASSERT ((Start
< End
) ^ (Tick
<= GetPerformanceCounter ()));
148 Attempts to place a spin lock in the acquired state.
150 This function checks the state of the spin lock specified by SpinLock. If
151 SpinLock is in the released state, then this function places SpinLock in the
152 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
153 transitions of SpinLock must be performed using MP safe mechanisms.
155 If SpinLock is NULL, then ASSERT().
156 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
158 @param SpinLock A pointer to the spin lock to place in the acquired state.
160 @retval TRUE SpinLock was placed in the acquired state.
161 @retval FALSE SpinLock could not be acquired.
166 AcquireSpinLockOrFail (
167 IN OUT SPIN_LOCK
*SpinLock
173 ASSERT (SpinLock
!= NULL
);
175 LockValue
= *SpinLock
;
176 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
178 _ReadWriteBarrier ();
179 Result
= InterlockedCompareExchangePointer (
181 (VOID
*)SPIN_LOCK_RELEASED
,
182 (VOID
*)SPIN_LOCK_ACQUIRED
185 _ReadWriteBarrier ();
186 return (BOOLEAN
) (Result
== (VOID
*) SPIN_LOCK_RELEASED
);
190 Releases a spin lock.
192 This function places the spin lock specified by SpinLock in the release state
193 and returns SpinLock.
195 If SpinLock is NULL, then ASSERT().
196 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
198 @param SpinLock A pointer to the spin lock to release.
206 IN OUT SPIN_LOCK
*SpinLock
211 ASSERT (SpinLock
!= NULL
);
213 LockValue
= *SpinLock
;
214 ASSERT (LockValue
== SPIN_LOCK_ACQUIRED
|| LockValue
== SPIN_LOCK_RELEASED
);
216 _ReadWriteBarrier ();
217 *SpinLock
= SPIN_LOCK_RELEASED
;
218 _ReadWriteBarrier ();
224 Performs an atomic increment of an 32-bit unsigned integer.
226 Performs an atomic increment of the 32-bit unsigned integer specified by
227 Value and returns the incremented value. The increment operation must be
228 performed using MP safe mechanisms. The state of the return value is not
229 guaranteed to be MP safe.
231 If Value is NULL, then ASSERT().
233 @param Value A pointer to the 32-bit value to increment.
235 @return The incremented value.
240 InterlockedIncrement (
244 ASSERT (Value
!= NULL
);
245 return InternalSyncIncrement (Value
);
249 Performs an atomic decrement of an 32-bit unsigned integer.
251 Performs an atomic decrement of the 32-bit unsigned integer specified by
252 Value and returns the decremented value. The decrement operation must be
253 performed using MP safe mechanisms. The state of the return value is not
254 guaranteed to be MP safe.
256 If Value is NULL, then ASSERT().
258 @param Value A pointer to the 32-bit value to decrement.
260 @return The decremented value.
265 InterlockedDecrement (
269 ASSERT (Value
!= NULL
);
270 return InternalSyncDecrement (Value
);
274 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
276 Performs an atomic compare exchange operation on the 32-bit unsigned integer
277 specified by Value. If Value is equal to CompareValue, then Value is set to
278 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
279 then Value is returned. The compare exchange operation must be performed using
282 If Value is NULL, then ASSERT().
284 @param Value A pointer to the 32-bit value for the compare exchange
286 @param CompareValue 32-bit value used in compare operation.
287 @param ExchangeValue 32-bit value used in exchange operation.
289 @return The original *Value before exchange.
294 InterlockedCompareExchange32 (
295 IN OUT UINT32
*Value
,
296 IN UINT32 CompareValue
,
297 IN UINT32 ExchangeValue
300 ASSERT (Value
!= NULL
);
301 return InternalSyncCompareExchange32 (Value
, CompareValue
, ExchangeValue
);
305 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
307 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
308 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
309 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
310 The compare exchange operation must be performed using MP safe mechanisms.
312 If Value is NULL, then ASSERT().
314 @param Value A pointer to the 64-bit value for the compare exchange
316 @param CompareValue 64-bit value used in compare operation.
317 @param ExchangeValue 64-bit value used in exchange operation.
319 @return The original *Value before exchange.
324 InterlockedCompareExchange64 (
325 IN OUT UINT64
*Value
,
326 IN UINT64 CompareValue
,
327 IN UINT64 ExchangeValue
330 ASSERT (Value
!= NULL
);
331 return InternalSyncCompareExchange64 (Value
, CompareValue
, ExchangeValue
);
335 Performs an atomic compare exchange operation on a pointer value.
337 Performs an atomic compare exchange operation on the pointer value specified
338 by Value. If Value is equal to CompareValue, then Value is set to
339 ExchangeValue and CompareValue is returned. If Value is not equal to
340 CompareValue, then Value is returned. The compare exchange operation must be
341 performed using MP safe mechanisms.
343 If Value is NULL, then ASSERT().
345 @param Value A pointer to the pointer value for the compare exchange
347 @param CompareValue Pointer value used in compare operation.
348 @param ExchangeValue Pointer value used in exchange operation.
353 InterlockedCompareExchangePointer (
355 IN VOID
*CompareValue
,
356 IN VOID
*ExchangeValue
361 SizeOfValue
= sizeof (*Value
);
363 switch (SizeOfValue
) {
364 case sizeof (UINT32
):
365 return (VOID
*)(UINTN
)InterlockedCompareExchange32 (
367 (UINT32
)(UINTN
)CompareValue
,
368 (UINT32
)(UINTN
)ExchangeValue
370 case sizeof (UINT64
):
371 return (VOID
*)(UINTN
)InterlockedCompareExchange64 (
373 (UINT64
)(UINTN
)CompareValue
,
374 (UINT64
)(UINTN
)ExchangeValue