]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Synchronization.c
Code scrub:
[mirror_edk2.git] / MdePkg / Library / BaseLib / Synchronization.c
1 /** @file
2 Implementation of synchronization functions.
3
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
9
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.
12
13 **/
14
15 //
16 // Include common header file for this module.
17 //
18
19
20 #include "BaseLibInternals.h"
21
22 #define SPIN_LOCK_RELEASED ((UINTN) 1)
23 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
24
25 /**
26 Retrieves the architecture specific spin lock alignment requirements for
27 optimal spin lock performance.
28
29 This function retrieves the spin lock alignment requirements for optimal
30 performance on a given CPU architecture. The spin lock alignment must be a
31 power of two and is returned by this function. If there are no alignment
32 requirements, then 1 must be returned. The spin lock synchronization
33 functions must function correctly if the spin lock size and alignment values
34 returned by this function are not used at all. These values are hints to the
35 consumers of the spin lock synchronization functions to obtain optimal spin
36 lock performance.
37
38 @return The architecture specific spin lock alignment.
39
40 **/
41 UINTN
42 EFIAPI
43 GetSpinLockProperties (
44 VOID
45 )
46 {
47 return 32;
48 }
49
50 /**
51 Initializes a spin lock to the released state and returns the spin lock.
52
53 This function initializes the spin lock specified by SpinLock to the released
54 state, and returns SpinLock. Optimal performance can be achieved by calling
55 GetSpinLockProperties() to determine the size and alignment requirements for
56 SpinLock.
57
58 If SpinLock is NULL, then ASSERT().
59
60 @param SpinLock A pointer to the spin lock to initialize to the released
61 state.
62
63 @return SpinLock initialized in release state.
64
65 **/
66 SPIN_LOCK *
67 EFIAPI
68 InitializeSpinLock (
69 OUT SPIN_LOCK *SpinLock
70 )
71 {
72 ASSERT (SpinLock != NULL);
73 *SpinLock = SPIN_LOCK_RELEASED;
74 return SpinLock;
75 }
76
77 /**
78 Waits until a spin lock can be placed in the acquired state.
79
80 This function checks the state of the spin lock specified by SpinLock. If
81 SpinLock is in the released state, then this function places SpinLock in the
82 acquired state and returns SpinLock. Otherwise, this function waits
83 indefinitely for the spin lock to be released, and then places it in the
84 acquired state and returns SpinLock. All state transitions of SpinLock must
85 be performed using MP safe mechanisms.
86
87 If SpinLock is NULL, then ASSERT().
88 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
89 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
90 PcdSpinLockTimeout microseconds, then ASSERT().
91
92 @param SpinLock A pointer to the spin lock to place in the acquired state.
93
94 @return SpinLock aquired lock.
95
96 **/
97 SPIN_LOCK *
98 EFIAPI
99 AcquireSpinLock (
100 IN OUT SPIN_LOCK *SpinLock
101 )
102 {
103 UINT64 Current;
104 UINT64 Previous;
105 UINT64 Total;
106 UINT64 Start;
107 UINT64 End;
108 UINT64 Timeout;
109 INT64 Cycle;
110 INT64 Delta;
111
112 if (PcdGet32 (PcdSpinLockTimeout) > 0) {
113 //
114 // Get the current timer value
115 //
116 Current = GetPerformanceCounter();
117
118 //
119 // Initialize local variables
120 //
121 Start = 0;
122 End = 0;
123 Total = 0;
124
125 //
126 // Retrieve the performance counter properties and compute the number of performance
127 // counter ticks required to reach the timeout
128 //
129 Timeout = DivU64x32 (
130 MultU64x32 (
131 GetPerformanceCounterProperties (&Start, &End),
132 PcdGet32 (PcdSpinLockTimeout)
133 ),
134 1000000
135 );
136 Cycle = End - Start;
137 if (Cycle < 0) {
138 Cycle = -Cycle;
139 }
140 Cycle++;
141
142 while (!AcquireSpinLockOrFail (SpinLock)) {
143 CpuPause ();
144 Previous = Current;
145 Current = GetPerformanceCounter();
146 Delta = (INT64) (Current - Previous);
147 if (Start > End) {
148 Delta = -Delta;
149 }
150 if (Delta < 0) {
151 Delta += Cycle;
152 }
153 Total += Delta;
154 ASSERT (Total < Timeout);
155 }
156 } else {
157 while (!AcquireSpinLockOrFail (SpinLock)) {
158 CpuPause ();
159 }
160 }
161 return SpinLock;
162 }
163
164 /**
165 Attempts to place a spin lock in the acquired state.
166
167 This function checks the state of the spin lock specified by SpinLock. If
168 SpinLock is in the released state, then this function places SpinLock in the
169 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
170 transitions of SpinLock must be performed using MP safe mechanisms.
171
172 If SpinLock is NULL, then ASSERT().
173 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
174
175 @param SpinLock A pointer to the spin lock to place in the acquired state.
176
177 @retval TRUE SpinLock was placed in the acquired state.
178 @retval FALSE SpinLock could not be acquired.
179
180 **/
181 BOOLEAN
182 EFIAPI
183 AcquireSpinLockOrFail (
184 IN OUT SPIN_LOCK *SpinLock
185 )
186 {
187 SPIN_LOCK LockValue;
188
189 ASSERT (SpinLock != NULL);
190
191 LockValue = *SpinLock;
192 ASSERT (SPIN_LOCK_ACQUIRED == LockValue || SPIN_LOCK_RELEASED == LockValue);
193
194 return (BOOLEAN)(
195 InterlockedCompareExchangePointer (
196 (VOID**)SpinLock,
197 (VOID*)SPIN_LOCK_RELEASED,
198 (VOID*)SPIN_LOCK_ACQUIRED
199 ) == (VOID*)SPIN_LOCK_RELEASED
200 );
201 }
202
203 /**
204 Releases a spin lock.
205
206 This function places the spin lock specified by SpinLock in the release state
207 and returns SpinLock.
208
209 If SpinLock is NULL, then ASSERT().
210 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
211
212 @param SpinLock A pointer to the spin lock to release.
213
214 @return SpinLock
215
216 **/
217 SPIN_LOCK *
218 EFIAPI
219 ReleaseSpinLock (
220 IN OUT SPIN_LOCK *SpinLock
221 )
222 {
223 SPIN_LOCK LockValue;
224
225 ASSERT (SpinLock != NULL);
226
227 LockValue = *SpinLock;
228 ASSERT (SPIN_LOCK_ACQUIRED == LockValue || SPIN_LOCK_RELEASED == LockValue);
229
230 *SpinLock = SPIN_LOCK_RELEASED;
231 return SpinLock;
232 }
233
234 /**
235 Performs an atomic increment of an 32-bit unsigned integer.
236
237 Performs an atomic increment of the 32-bit unsigned integer specified by
238 Value and returns the incremented value. The increment operation must be
239 performed using MP safe mechanisms. The state of the return value is not
240 guaranteed to be MP safe.
241
242 If Value is NULL, then ASSERT().
243
244 @param Value A pointer to the 32-bit value to increment.
245
246 @return The incremented value.
247
248 **/
249 UINT32
250 EFIAPI
251 InterlockedIncrement (
252 IN UINT32 *Value
253 )
254 {
255 ASSERT (Value != NULL);
256 return InternalSyncIncrement (Value);
257 }
258
259 /**
260 Performs an atomic decrement of an 32-bit unsigned integer.
261
262 Performs an atomic decrement of the 32-bit unsigned integer specified by
263 Value and returns the decremented value. The decrement operation must be
264 performed using MP safe mechanisms. The state of the return value is not
265 guaranteed to be MP safe.
266
267 If Value is NULL, then ASSERT().
268
269 @param Value A pointer to the 32-bit value to decrement.
270
271 @return The decremented value.
272
273 **/
274 UINT32
275 EFIAPI
276 InterlockedDecrement (
277 IN UINT32 *Value
278 )
279 {
280 ASSERT (Value != NULL);
281 return InternalSyncDecrement (Value);
282 }
283
284 /**
285 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
286
287 Performs an atomic compare exchange operation on the 32-bit unsigned integer
288 specified by Value. If Value is equal to CompareValue, then Value is set to
289 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
290 then Value is returned. The compare exchange operation must be performed using
291 MP safe mechanisms.
292
293 If Value is NULL, then ASSERT().
294
295 @param Value A pointer to the 32-bit value for the compare exchange
296 operation.
297 @param CompareValue 32-bit value used in compare operation.
298 @param ExchangeValue 32-bit value used in exchange operation.
299
300 @return The original *Value before exchange.
301
302 **/
303 UINT32
304 EFIAPI
305 InterlockedCompareExchange32 (
306 IN OUT UINT32 *Value,
307 IN UINT32 CompareValue,
308 IN UINT32 ExchangeValue
309 )
310 {
311 ASSERT (Value != NULL);
312 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);
313 }
314
315 /**
316 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
317
318 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
319 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
320 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
321 The compare exchange operation must be performed using MP safe mechanisms.
322
323 If Value is NULL, then ASSERT().
324
325 @param Value A pointer to the 64-bit value for the compare exchange
326 operation.
327 @param CompareValue 64-bit value used in compare operation.
328 @param ExchangeValue 64-bit value used in exchange operation.
329
330 @return The original *Value before exchange.
331
332 **/
333 UINT64
334 EFIAPI
335 InterlockedCompareExchange64 (
336 IN OUT UINT64 *Value,
337 IN UINT64 CompareValue,
338 IN UINT64 ExchangeValue
339 )
340 {
341 ASSERT (Value != NULL);
342 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);
343 }
344
345 /**
346 Performs an atomic compare exchange operation on a pointer value.
347
348 Performs an atomic compare exchange operation on the pointer value specified
349 by Value. If Value is equal to CompareValue, then Value is set to
350 ExchangeValue and CompareValue is returned. If Value is not equal to
351 CompareValue, then Value is returned. The compare exchange operation must be
352 performed using MP safe mechanisms.
353
354 If Value is NULL, then ASSERT().
355
356 @param Value A pointer to the pointer value for the compare exchange
357 operation.
358 @param CompareValue Pointer value used in compare operation.
359 @param ExchangeValue Pointer value used in exchange operation.
360
361 @return The original *Value before exchange.
362
363 **/
364 VOID *
365 EFIAPI
366 InterlockedCompareExchangePointer (
367 IN OUT VOID **Value,
368 IN VOID *CompareValue,
369 IN VOID *ExchangeValue
370 )
371 {
372 UINT8 SizeOfValue;
373
374 SizeOfValue = sizeof (*Value);
375
376 switch (SizeOfValue) {
377 case sizeof (UINT32):
378 return (VOID*)(UINTN)InterlockedCompareExchange32 (
379 (UINT32*)Value,
380 (UINT32)(UINTN)CompareValue,
381 (UINT32)(UINTN)ExchangeValue
382 );
383 case sizeof (UINT64):
384 return (VOID*)(UINTN)InterlockedCompareExchange64 (
385 (UINT64*)Value,
386 (UINT64)(UINTN)CompareValue,
387 (UINT64)(UINTN)ExchangeValue
388 );
389 default:
390 ASSERT (FALSE);
391 return NULL;
392 }
393 }