]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/SynchronizationGcc.c
MdePkg/SynchronizationLib: fix Interlocked[De|In]crement return value
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / SynchronizationGcc.c
1 /** @file
2 Implementation of synchronization functions.
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "BaseSynchronizationLibInternals.h"
17
18 //
19 // GCC inline assembly for Read Write Barrier
20 //
21 #define _ReadWriteBarrier() do { __asm__ __volatile__ ("": : : "memory"); } while(0)
22
23 #define SPIN_LOCK_RELEASED ((UINTN) 1)
24 #define SPIN_LOCK_ACQUIRED ((UINTN) 2)
25
26 /**
27 Retrieves the architecture specific spin lock alignment requirements for
28 optimal spin lock performance.
29
30 This function retrieves the spin lock alignment requirements for optimal
31 performance on a given CPU architecture. The spin lock alignment is byte alignment.
32 It must be a power of two and is returned by this function. If there are no alignment
33 requirements, then 1 must be returned. The spin lock synchronization
34 functions must function correctly if the spin lock size and alignment values
35 returned by this function are not used at all. These values are hints to the
36 consumers of the spin lock synchronization functions to obtain optimal spin
37 lock performance.
38
39 @return The architecture specific spin lock alignment.
40
41 **/
42 UINTN
43 EFIAPI
44 GetSpinLockProperties (
45 VOID
46 )
47 {
48 return InternalGetSpinLockProperties ();
49 }
50
51 /**
52 Initializes a spin lock to the released state and returns the spin lock.
53
54 This function initializes the spin lock specified by SpinLock to the released
55 state, and returns SpinLock. Optimal performance can be achieved by calling
56 GetSpinLockProperties() to determine the size and alignment requirements for
57 SpinLock.
58
59 If SpinLock is NULL, then ASSERT().
60
61 @param SpinLock A pointer to the spin lock to initialize to the released
62 state.
63
64 @return SpinLock is in release state.
65
66 **/
67 SPIN_LOCK *
68 EFIAPI
69 InitializeSpinLock (
70 OUT SPIN_LOCK *SpinLock
71 )
72 {
73 ASSERT (SpinLock != NULL);
74
75 _ReadWriteBarrier();
76 *SpinLock = SPIN_LOCK_RELEASED;
77 _ReadWriteBarrier();
78
79 return SpinLock;
80 }
81
82 /**
83 Waits until a spin lock can be placed in the acquired state.
84
85 This function checks the state of the spin lock specified by SpinLock. If
86 SpinLock is in the released state, then this function places SpinLock in the
87 acquired state and returns SpinLock. Otherwise, this function waits
88 indefinitely for the spin lock to be released, and then places it in the
89 acquired state and returns SpinLock. All state transitions of SpinLock must
90 be performed using MP safe mechanisms.
91
92 If SpinLock is NULL, then ASSERT().
93 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
94 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in
95 PcdSpinLockTimeout microseconds, then ASSERT().
96
97 @param SpinLock A pointer to the spin lock to place in the acquired state.
98
99 @return SpinLock acquired the lock.
100
101 **/
102 SPIN_LOCK *
103 EFIAPI
104 AcquireSpinLock (
105 IN OUT SPIN_LOCK *SpinLock
106 )
107 {
108 UINT64 Current;
109 UINT64 Previous;
110 UINT64 Total;
111 UINT64 Start;
112 UINT64 End;
113 UINT64 Timeout;
114 INT64 Cycle;
115 INT64 Delta;
116
117 if (PcdGet32 (PcdSpinLockTimeout) == 0) {
118 while (!AcquireSpinLockOrFail (SpinLock)) {
119 CpuPause ();
120 }
121 } else if (!AcquireSpinLockOrFail (SpinLock)) {
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 }
166 return SpinLock;
167 }
168
169 /**
170 Attempts to place a spin lock in the acquired state.
171
172 This function checks the state of the spin lock specified by SpinLock. If
173 SpinLock is in the released state, then this function places SpinLock in the
174 acquired state and returns TRUE. Otherwise, FALSE is returned. All state
175 transitions of SpinLock must be performed using MP safe mechanisms.
176
177 If SpinLock is NULL, then ASSERT().
178 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
179
180 @param SpinLock A pointer to the spin lock to place in the acquired state.
181
182 @retval TRUE SpinLock was placed in the acquired state.
183 @retval FALSE SpinLock could not be acquired.
184
185 **/
186 BOOLEAN
187 EFIAPI
188 AcquireSpinLockOrFail (
189 IN OUT SPIN_LOCK *SpinLock
190 )
191 {
192 SPIN_LOCK LockValue;
193 VOID *Result;
194
195 ASSERT (SpinLock != NULL);
196
197 LockValue = *SpinLock;
198 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);
199
200 _ReadWriteBarrier ();
201 Result = InterlockedCompareExchangePointer (
202 (VOID**)SpinLock,
203 (VOID*)SPIN_LOCK_RELEASED,
204 (VOID*)SPIN_LOCK_ACQUIRED
205 );
206
207 _ReadWriteBarrier ();
208 return (BOOLEAN) (Result == (VOID*) SPIN_LOCK_RELEASED);
209 }
210
211 /**
212 Releases a spin lock.
213
214 This function places the spin lock specified by SpinLock in the release state
215 and returns SpinLock.
216
217 If SpinLock is NULL, then ASSERT().
218 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
219
220 @param SpinLock A pointer to the spin lock to release.
221
222 @return SpinLock released the lock.
223
224 **/
225 SPIN_LOCK *
226 EFIAPI
227 ReleaseSpinLock (
228 IN OUT SPIN_LOCK *SpinLock
229 )
230 {
231 SPIN_LOCK LockValue;
232
233 ASSERT (SpinLock != NULL);
234
235 LockValue = *SpinLock;
236 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);
237
238 _ReadWriteBarrier ();
239 *SpinLock = SPIN_LOCK_RELEASED;
240 _ReadWriteBarrier ();
241
242 return SpinLock;
243 }
244
245 /**
246 Performs an atomic increment of an 32-bit unsigned integer.
247
248 Performs an atomic increment of the 32-bit unsigned integer specified by
249 Value and returns the incremented value. The increment operation must be
250 performed using MP safe mechanisms.
251
252 If Value is NULL, then ASSERT().
253
254 @param Value A pointer to the 32-bit value to increment.
255
256 @return The incremented value.
257
258 **/
259 UINT32
260 EFIAPI
261 InterlockedIncrement (
262 IN volatile UINT32 *Value
263 )
264 {
265 ASSERT (Value != NULL);
266 return InternalSyncIncrement (Value);
267 }
268
269 /**
270 Performs an atomic decrement of an 32-bit unsigned integer.
271
272 Performs an atomic decrement of the 32-bit unsigned integer specified by
273 Value and returns the decremented value. The decrement operation must be
274 performed using MP safe mechanisms.
275
276 If Value is NULL, then ASSERT().
277
278 @param Value A pointer to the 32-bit value to decrement.
279
280 @return The decremented value.
281
282 **/
283 UINT32
284 EFIAPI
285 InterlockedDecrement (
286 IN volatile UINT32 *Value
287 )
288 {
289 ASSERT (Value != NULL);
290 return InternalSyncDecrement (Value);
291 }
292
293 /**
294 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
295
296 Performs an atomic compare exchange operation on the 16-bit unsigned integer
297 specified by Value. If Value is equal to CompareValue, then Value is set to
298 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
299 then Value is returned. The compare exchange operation must be performed using
300 MP safe mechanisms.
301
302 If Value is NULL, then ASSERT().
303
304 @param Value A pointer to the 16-bit value for the compare exchange
305 operation.
306 @param CompareValue A 16-bit value used in compare operation.
307 @param ExchangeValue A 16-bit value used in exchange operation.
308
309 @return The original *Value before exchange.
310
311 **/
312 UINT16
313 EFIAPI
314 InterlockedCompareExchange16 (
315 IN OUT volatile UINT16 *Value,
316 IN UINT16 CompareValue,
317 IN UINT16 ExchangeValue
318 )
319 {
320 ASSERT (Value != NULL);
321 return InternalSyncCompareExchange16 (Value, CompareValue, ExchangeValue);
322 }
323
324 /**
325 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
326
327 Performs an atomic compare exchange operation on the 32-bit unsigned integer
328 specified by Value. If Value is equal to CompareValue, then Value is set to
329 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
330 then Value is returned. The compare exchange operation must be performed using
331 MP safe mechanisms.
332
333 If Value is NULL, then ASSERT().
334
335 @param Value A pointer to the 32-bit value for the compare exchange
336 operation.
337 @param CompareValue A 32-bit value used in compare operation.
338 @param ExchangeValue A 32-bit value used in exchange operation.
339
340 @return The original *Value before exchange.
341
342 **/
343 UINT32
344 EFIAPI
345 InterlockedCompareExchange32 (
346 IN OUT volatile UINT32 *Value,
347 IN UINT32 CompareValue,
348 IN UINT32 ExchangeValue
349 )
350 {
351 ASSERT (Value != NULL);
352 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);
353 }
354
355 /**
356 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
357
358 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
359 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
360 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
361 The compare exchange operation must be performed using MP safe mechanisms.
362
363 If Value is NULL, then ASSERT().
364
365 @param Value A pointer to the 64-bit value for the compare exchange
366 operation.
367 @param CompareValue A 64-bit value used in a compare operation.
368 @param ExchangeValue A 64-bit value used in an exchange operation.
369
370 @return The original *Value before exchange.
371
372 **/
373 UINT64
374 EFIAPI
375 InterlockedCompareExchange64 (
376 IN OUT volatile UINT64 *Value,
377 IN UINT64 CompareValue,
378 IN UINT64 ExchangeValue
379 )
380 {
381 ASSERT (Value != NULL);
382 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);
383 }
384
385 /**
386 Performs an atomic compare exchange operation on a pointer value.
387
388 Performs an atomic compare exchange operation on the pointer value specified
389 by Value. If Value is equal to CompareValue, then Value is set to
390 ExchangeValue and CompareValue is returned. If Value is not equal to
391 CompareValue, then Value is returned. The compare exchange operation must be
392 performed using MP safe mechanisms.
393
394 If Value is NULL, then ASSERT().
395
396 @param Value A pointer to the pointer value for the compare exchange
397 operation.
398 @param CompareValue A pointer value used in a compare operation.
399 @param ExchangeValue A pointer value used in an exchange operation.
400
401 @return The original *Value before exchange.
402 **/
403 VOID *
404 EFIAPI
405 InterlockedCompareExchangePointer (
406 IN OUT VOID * volatile *Value,
407 IN VOID *CompareValue,
408 IN VOID *ExchangeValue
409 )
410 {
411 UINT8 SizeOfValue;
412
413 SizeOfValue = sizeof (*Value);
414
415 switch (SizeOfValue) {
416 case sizeof (UINT32):
417 return (VOID*)(UINTN)InterlockedCompareExchange32 (
418 (volatile UINT32 *)Value,
419 (UINT32)(UINTN)CompareValue,
420 (UINT32)(UINTN)ExchangeValue
421 );
422 case sizeof (UINT64):
423 return (VOID*)(UINTN)InterlockedCompareExchange64 (
424 (volatile UINT64 *)Value,
425 (UINT64)(UINTN)CompareValue,
426 (UINT64)(UINTN)ExchangeValue
427 );
428 default:
429 ASSERT (FALSE);
430 return NULL;
431 }
432 }