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