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