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