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