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