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