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