]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/SynchronizationMsc.c
Regenerate Fat binaries for the bug fix of LookupUnicodeString2() in UefiLib (r4655).
[mirror_edk2.git] / MdePkg / Library / BaseLib / SynchronizationMsc.c
CommitLineData
e1f414b6 1/** @file\r
2 Implementation of synchronization functions.\r
3\r
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
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
8 http://opensource.org/licenses/bsd-license.php\r
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
e1f414b6 13**/\r
14\r
15//\r
16// Include common header file for this module.\r
17//\r
f734a10a 18\r
e1f414b6 19\r
20#include "BaseLibInternals.h"\r
21\r
22//\r
23// Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics\r
24//\r
25void _ReadWriteBarrier (void);\r
26#pragma intrinsic(_ReadWriteBarrier)\r
27\r
28\r
29#define SPIN_LOCK_RELEASED ((UINTN) 1)\r
30#define SPIN_LOCK_ACQUIRED ((UINTN) 2)\r
31\r
32/**\r
33 Retrieves the architecture specific spin lock alignment requirements for\r
34 optimal spin lock performance.\r
35\r
36 This function retrieves the spin lock alignment requirements for optimal\r
37 performance on a given CPU architecture. The spin lock alignment must be a\r
38 power of two and is returned by this function. If there are no alignment\r
39 requirements, then 1 must be returned. The spin lock synchronization\r
40 functions must function correctly if the spin lock size and alignment values\r
41 returned by this function are not used at all. These values are hints to the\r
42 consumers of the spin lock synchronization functions to obtain optimal spin\r
43 lock performance.\r
44\r
45 @return The architecture specific spin lock alignment.\r
46\r
47**/\r
48UINTN\r
49EFIAPI\r
50GetSpinLockProperties (\r
51 VOID\r
52 )\r
53{\r
54 // @bug May use a PCD entry to determine this alignment.\r
55 return 32;\r
56}\r
57\r
58/**\r
59 Initializes a spin lock to the released state and returns the spin lock.\r
60\r
61 This function initializes the spin lock specified by SpinLock to the released\r
62 state, and returns SpinLock. Optimal performance can be achieved by calling\r
63 GetSpinLockProperties() to determine the size and alignment requirements for\r
64 SpinLock.\r
65\r
66 If SpinLock is NULL, then ASSERT().\r
67\r
68 @param SpinLock A pointer to the spin lock to initialize to the released\r
69 state.\r
70\r
71 @return SpinLock\r
72\r
73**/\r
74SPIN_LOCK *\r
75EFIAPI\r
76InitializeSpinLock (\r
77 OUT SPIN_LOCK *SpinLock\r
78 )\r
79{\r
80 ASSERT (SpinLock != NULL);\r
81\r
82 _ReadWriteBarrier();\r
83 *SpinLock = SPIN_LOCK_RELEASED;\r
84 _ReadWriteBarrier();\r
85\r
86 return SpinLock;\r
87}\r
88\r
89/**\r
90 Waits until a spin lock can be placed in the acquired state.\r
91\r
92 This function checks the state of the spin lock specified by SpinLock. If\r
93 SpinLock is in the released state, then this function places SpinLock in the\r
94 acquired state and returns SpinLock. Otherwise, this function waits\r
95 indefinitely for the spin lock to be released, and then places it in the\r
96 acquired state and returns SpinLock. All state transitions of SpinLock must\r
97 be performed using MP safe mechanisms.\r
98\r
99 If SpinLock is NULL, then ASSERT().\r
100 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
101 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in\r
102 PcdSpinLockTimeout microseconds, then ASSERT().\r
103\r
104 @param SpinLock A pointer to the spin lock to place in the acquired state.\r
105\r
106 @return SpinLock\r
107\r
108**/\r
109SPIN_LOCK *\r
110EFIAPI\r
111AcquireSpinLock (\r
112 IN OUT SPIN_LOCK *SpinLock\r
113 )\r
114{\r
a82da388 115 UINT64 Current;\r
116 UINT64 Previous;\r
117 UINT64 Total;\r
118 UINT64 Start;\r
119 UINT64 End;\r
120 UINT64 Timeout;\r
121 INT64 Cycle;\r
122 INT64 Delta;\r
e1f414b6 123\r
e1f414b6 124 if (PcdGet32 (PcdSpinLockTimeout) > 0) {\r
a82da388 125 //\r
126 // Get the current timer value\r
127 //\r
128 Current = GetPerformanceCounter();\r
129\r
130 //\r
131 // Initialize local variables\r
132 //\r
133 Start = 0;\r
134 End = 0;\r
135 Total = 0;\r
136\r
137 //\r
138 // Retrieve the performance counter properties and compute the number of performance\r
139 // counter ticks required to reach the timeout\r
140 //\r
e1f414b6 141 Timeout = DivU64x32 (\r
142 MultU64x32 (\r
143 GetPerformanceCounterProperties (&Start, &End),\r
144 PcdGet32 (PcdSpinLockTimeout)\r
145 ),\r
146 1000000\r
147 );\r
a82da388 148 Cycle = End - Start;\r
149 if (Cycle < 0) {\r
150 Cycle = -Cycle;\r
151 }\r
152 Cycle++;\r
153\r
154 while (!AcquireSpinLockOrFail (SpinLock)) {\r
155 CpuPause ();\r
156 Previous = Current;\r
157 Current = GetPerformanceCounter();\r
158 Delta = (INT64) (Current - Previous);\r
159 if (Start > End) {\r
160 Delta = -Delta;\r
161 }\r
162 if (Delta < 0) {\r
163 Delta += Cycle;\r
164 }\r
165 Total += Delta;\r
166 ASSERT (Total < Timeout);\r
167 }\r
168 } else {\r
169 while (!AcquireSpinLockOrFail (SpinLock)) {\r
170 CpuPause ();\r
e1f414b6 171 }\r
e1f414b6 172 }\r
173 return SpinLock;\r
174}\r
175\r
176/**\r
177 Attempts to place a spin lock in the acquired state.\r
178\r
179 This function checks the state of the spin lock specified by SpinLock. If\r
180 SpinLock is in the released state, then this function places SpinLock in the\r
181 acquired state and returns TRUE. Otherwise, FALSE is returned. All state\r
182 transitions of SpinLock must be performed using MP safe mechanisms.\r
183\r
184 If SpinLock is NULL, then ASSERT().\r
185 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
186\r
187 @param SpinLock A pointer to the spin lock to place in the acquired state.\r
188\r
189 @retval TRUE SpinLock was placed in the acquired state.\r
190 @retval FALSE SpinLock could not be acquired.\r
191\r
192**/\r
193BOOLEAN\r
194EFIAPI\r
195AcquireSpinLockOrFail (\r
196 IN OUT SPIN_LOCK *SpinLock\r
197 )\r
198{\r
199 SPIN_LOCK LockValue;\r
200 VOID *Result;\r
201 \r
202 ASSERT (SpinLock != NULL);\r
203\r
204 LockValue = *SpinLock;\r
205 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r
206\r
207 _ReadWriteBarrier ();\r
208 Result = InterlockedCompareExchangePointer (\r
209 (VOID**)SpinLock,\r
210 (VOID*)SPIN_LOCK_RELEASED,\r
211 (VOID*)SPIN_LOCK_ACQUIRED\r
212 );\r
213\r
214 _ReadWriteBarrier ();\r
215 return (BOOLEAN) (Result == (VOID*) SPIN_LOCK_RELEASED);\r
216}\r
217\r
218/**\r
219 Releases a spin lock.\r
220\r
221 This function places the spin lock specified by SpinLock in the release state\r
222 and returns SpinLock.\r
223\r
224 If SpinLock is NULL, then ASSERT().\r
225 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
226\r
227 @param SpinLock A pointer to the spin lock to release.\r
228\r
229 @return SpinLock\r
230\r
231**/\r
232SPIN_LOCK *\r
233EFIAPI\r
234ReleaseSpinLock (\r
235 IN OUT SPIN_LOCK *SpinLock\r
236 )\r
237{\r
238 SPIN_LOCK LockValue;\r
239\r
240 ASSERT (SpinLock != NULL);\r
241\r
242 LockValue = *SpinLock;\r
243 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r
244\r
245 _ReadWriteBarrier ();\r
246 *SpinLock = SPIN_LOCK_RELEASED;\r
247 _ReadWriteBarrier ();\r
248\r
249 return SpinLock;\r
250}\r
251\r
252/**\r
253 Performs an atomic increment of an 32-bit unsigned integer.\r
254\r
255 Performs an atomic increment of the 32-bit unsigned integer specified by\r
256 Value and returns the incremented value. The increment operation must be\r
257 performed using MP safe mechanisms. The state of the return value is not\r
258 guaranteed to be MP safe.\r
259\r
260 If Value is NULL, then ASSERT().\r
261\r
262 @param Value A pointer to the 32-bit value to increment.\r
263\r
264 @return The incremented value.\r
265\r
266**/\r
267UINT32\r
268EFIAPI\r
269InterlockedIncrement (\r
270 IN UINT32 *Value\r
271 )\r
272{\r
273 ASSERT (Value != NULL);\r
274 return InternalSyncIncrement (Value);\r
275}\r
276\r
277/**\r
278 Performs an atomic decrement of an 32-bit unsigned integer.\r
279\r
280 Performs an atomic decrement of the 32-bit unsigned integer specified by\r
281 Value and returns the decremented value. The decrement operation must be\r
282 performed using MP safe mechanisms. The state of the return value is not\r
283 guaranteed to be MP safe.\r
284\r
285 If Value is NULL, then ASSERT().\r
286\r
287 @param Value A pointer to the 32-bit value to decrement.\r
288\r
289 @return The decremented value.\r
290\r
291**/\r
292UINT32\r
293EFIAPI\r
294InterlockedDecrement (\r
295 IN UINT32 *Value\r
296 )\r
297{\r
298 ASSERT (Value != NULL);\r
299 return InternalSyncDecrement (Value);\r
300}\r
301\r
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
324 IN OUT UINT32 *Value,\r
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
354 IN OUT UINT64 *Value,\r
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**/\r
380VOID *\r
381EFIAPI\r
382InterlockedCompareExchangePointer (\r
383 IN OUT VOID **Value,\r
384 IN VOID *CompareValue,\r
385 IN VOID *ExchangeValue\r
386 )\r
387{\r
388 UINT8 SizeOfValue;\r
389\r
390 SizeOfValue = sizeof (*Value);\r
391\r
392 switch (SizeOfValue) {\r
393 case sizeof (UINT32):\r
394 return (VOID*)(UINTN)InterlockedCompareExchange32 (\r
395 (UINT32*)Value,\r
396 (UINT32)(UINTN)CompareValue,\r
397 (UINT32)(UINTN)ExchangeValue\r
398 );\r
399 case sizeof (UINT64):\r
400 return (VOID*)(UINTN)InterlockedCompareExchange64 (\r
401 (UINT64*)Value,\r
402 (UINT64)(UINTN)CompareValue,\r
403 (UINT64)(UINTN)ExchangeValue\r
404 );\r
405 default:\r
406 ASSERT (FALSE);\r
407 return NULL;\r
408 }\r
409}\r