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