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