]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseSynchronizationLib/Synchronization.c
MdePkg: Replace BSD License with BSD+Patent License
[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
11#define SPIN_LOCK_RELEASED ((UINTN) 1)\r
12#define SPIN_LOCK_ACQUIRED ((UINTN) 2)\r
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
58 OUT SPIN_LOCK *SpinLock\r
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
89 IN OUT SPIN_LOCK *SpinLock\r
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
109 Current = GetPerformanceCounter();\r
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
133 Cycle++;\r
134\r
135 while (!AcquireSpinLockOrFail (SpinLock)) {\r
136 CpuPause ();\r
137 Previous = Current;\r
138 Current = GetPerformanceCounter();\r
139 Delta = (INT64) (Current - Previous);\r
140 if (Start > End) {\r
141 Delta = -Delta;\r
142 }\r
143 if (Delta < 0) {\r
144 Delta += Cycle;\r
145 }\r
146 Total += Delta;\r
147 ASSERT (Total < Timeout);\r
148 }\r
720d3c5f 149 }\r
150 return SpinLock;\r
151}\r
152\r
153/**\r
154 Attempts to place a spin lock in the acquired state.\r
155\r
156 This function checks the state of the spin lock specified by SpinLock. If\r
157 SpinLock is in the released state, then this function places SpinLock in the\r
158 acquired state and returns TRUE. Otherwise, FALSE is returned. All state\r
159 transitions of SpinLock must be performed using MP safe mechanisms.\r
160\r
161 If SpinLock is NULL, then ASSERT().\r
162 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
163\r
164 @param SpinLock A pointer to the spin lock to place in the acquired state.\r
165\r
166 @retval TRUE SpinLock was placed in the acquired state.\r
167 @retval FALSE SpinLock could not be acquired.\r
168\r
169**/\r
170BOOLEAN\r
171EFIAPI\r
172AcquireSpinLockOrFail (\r
173 IN OUT SPIN_LOCK *SpinLock\r
174 )\r
175{\r
176 SPIN_LOCK LockValue;\r
177\r
178 ASSERT (SpinLock != NULL);\r
179\r
180 LockValue = *SpinLock;\r
181 ASSERT (SPIN_LOCK_ACQUIRED == LockValue || SPIN_LOCK_RELEASED == LockValue);\r
182\r
183 return (BOOLEAN)(\r
184 InterlockedCompareExchangePointer (\r
185 (VOID**)SpinLock,\r
186 (VOID*)SPIN_LOCK_RELEASED,\r
187 (VOID*)SPIN_LOCK_ACQUIRED\r
188 ) == (VOID*)SPIN_LOCK_RELEASED\r
189 );\r
190}\r
191\r
192/**\r
193 Releases a spin lock.\r
194\r
195 This function places the spin lock specified by SpinLock in the release state\r
196 and returns SpinLock.\r
197\r
198 If SpinLock is NULL, then ASSERT().\r
199 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
200\r
201 @param SpinLock A pointer to the spin lock to release.\r
202\r
203 @return SpinLock released lock.\r
204\r
205**/\r
206SPIN_LOCK *\r
207EFIAPI\r
208ReleaseSpinLock (\r
209 IN OUT SPIN_LOCK *SpinLock\r
210 )\r
211{\r
212 SPIN_LOCK LockValue;\r
213\r
214 ASSERT (SpinLock != NULL);\r
215\r
216 LockValue = *SpinLock;\r
217 ASSERT (SPIN_LOCK_ACQUIRED == LockValue || SPIN_LOCK_RELEASED == LockValue);\r
218\r
219 *SpinLock = SPIN_LOCK_RELEASED;\r
220 return SpinLock;\r
221}\r
222\r
223/**\r
224 Performs an atomic increment of an 32-bit unsigned integer.\r
225\r
226 Performs an atomic increment of the 32-bit unsigned integer specified by\r
227 Value and returns the incremented value. The increment operation must be\r
17634d02 228 performed using MP safe mechanisms.\r
720d3c5f 229\r
230 If Value is NULL, then ASSERT().\r
231\r
232 @param Value A pointer to the 32-bit value to increment.\r
233\r
234 @return The incremented value.\r
235\r
236**/\r
237UINT32\r
238EFIAPI\r
239InterlockedIncrement (\r
4cee954e 240 IN volatile UINT32 *Value\r
720d3c5f 241 )\r
242{\r
243 ASSERT (Value != NULL);\r
244 return InternalSyncIncrement (Value);\r
245}\r
246\r
247/**\r
248 Performs an atomic decrement of an 32-bit unsigned integer.\r
249\r
250 Performs an atomic decrement of the 32-bit unsigned integer specified by\r
251 Value and returns the decremented value. The decrement 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 decrement.\r
257\r
258 @return The decremented value.\r
259\r
260**/\r
261UINT32\r
262EFIAPI\r
263InterlockedDecrement (\r
4cee954e 264 IN volatile UINT32 *Value\r
720d3c5f 265 )\r
266{\r
267 ASSERT (Value != NULL);\r
268 return InternalSyncDecrement (Value);\r
269}\r
270\r
9b89163e
AB
271/**\r
272 Performs an atomic compare exchange operation on a 16-bit unsigned integer.\r
273\r
274 Performs an atomic compare exchange operation on the 16-bit unsigned integer\r
275 specified by Value. If Value is equal to CompareValue, then Value is set to\r
276 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
277 then Value is returned. The compare exchange operation must be performed using\r
278 MP safe mechanisms.\r
279\r
280 If Value is NULL, then ASSERT().\r
281\r
282 @param Value A pointer to the 16-bit value for the compare exchange\r
283 operation.\r
284 @param CompareValue 16-bit value used in compare operation.\r
285 @param ExchangeValue 16-bit value used in exchange operation.\r
286\r
287 @return The original *Value before exchange.\r
288\r
289**/\r
290UINT16\r
291EFIAPI\r
292InterlockedCompareExchange16 (\r
4cee954e 293 IN OUT volatile UINT16 *Value,\r
9b89163e
AB
294 IN UINT16 CompareValue,\r
295 IN UINT16 ExchangeValue\r
296 )\r
297{\r
298 ASSERT (Value != NULL);\r
299 return InternalSyncCompareExchange16 (Value, CompareValue, ExchangeValue);\r
300}\r
301\r
720d3c5f 302/**\r
303 Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
304\r
305 Performs an atomic compare exchange operation on the 32-bit unsigned integer\r
306 specified by Value. If Value is equal to CompareValue, then Value is set to\r
307 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
308 then Value is returned. The compare exchange operation must be performed using\r
309 MP safe mechanisms.\r
310\r
311 If Value is NULL, then ASSERT().\r
312\r
313 @param Value A pointer to the 32-bit value for the compare exchange\r
314 operation.\r
315 @param CompareValue 32-bit value used in compare operation.\r
316 @param ExchangeValue 32-bit value used in exchange operation.\r
317\r
318 @return The original *Value before exchange.\r
319\r
320**/\r
321UINT32\r
322EFIAPI\r
323InterlockedCompareExchange32 (\r
4cee954e 324 IN OUT volatile UINT32 *Value,\r
720d3c5f 325 IN UINT32 CompareValue,\r
326 IN UINT32 ExchangeValue\r
327 )\r
328{\r
329 ASSERT (Value != NULL);\r
330 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);\r
331}\r
332\r
333/**\r
334 Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r
335\r
336 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified\r
337 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and\r
338 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.\r
339 The compare exchange operation must be performed using MP safe mechanisms.\r
340\r
341 If Value is NULL, then ASSERT().\r
342\r
343 @param Value A pointer to the 64-bit value for the compare exchange\r
344 operation.\r
345 @param CompareValue 64-bit value used in compare operation.\r
346 @param ExchangeValue 64-bit value used in exchange operation.\r
347\r
348 @return The original *Value before exchange.\r
349\r
350**/\r
351UINT64\r
352EFIAPI\r
353InterlockedCompareExchange64 (\r
4cee954e 354 IN OUT volatile UINT64 *Value,\r
720d3c5f 355 IN UINT64 CompareValue,\r
356 IN UINT64 ExchangeValue\r
357 )\r
358{\r
359 ASSERT (Value != NULL);\r
360 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);\r
361}\r
362\r
363/**\r
364 Performs an atomic compare exchange operation on a pointer value.\r
365\r
366 Performs an atomic compare exchange operation on the pointer value specified\r
367 by Value. If Value is equal to CompareValue, then Value is set to\r
368 ExchangeValue and CompareValue is returned. If Value is not equal to\r
369 CompareValue, then Value is returned. The compare exchange operation must be\r
370 performed using MP safe mechanisms.\r
371\r
372 If Value is NULL, then ASSERT().\r
373\r
374 @param Value A pointer to the pointer value for the compare exchange\r
375 operation.\r
376 @param CompareValue Pointer value used in compare operation.\r
377 @param ExchangeValue Pointer value used in exchange operation.\r
378\r
379 @return The original *Value before exchange.\r
380**/\r
381VOID *\r
382EFIAPI\r
383InterlockedCompareExchangePointer (\r
4cee954e 384 IN OUT VOID * volatile *Value,\r
720d3c5f 385 IN VOID *CompareValue,\r
386 IN VOID *ExchangeValue\r
387 )\r
388{\r
389 UINT8 SizeOfValue;\r
390\r
391 SizeOfValue = sizeof (*Value);\r
392\r
393 switch (SizeOfValue) {\r
394 case sizeof (UINT32):\r
395 return (VOID*)(UINTN)InterlockedCompareExchange32 (\r
4cee954e 396 (volatile UINT32 *)Value,\r
720d3c5f 397 (UINT32)(UINTN)CompareValue,\r
398 (UINT32)(UINTN)ExchangeValue\r
399 );\r
400 case sizeof (UINT64):\r
401 return (VOID*)(UINTN)InterlockedCompareExchange64 (\r
4cee954e 402 (volatile UINT64 *)Value,\r
720d3c5f 403 (UINT64)(UINTN)CompareValue,\r
404 (UINT64)(UINTN)ExchangeValue\r
405 );\r
406 default:\r
407 ASSERT (FALSE);\r
408 return NULL;\r
409 }\r
410}\r