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