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