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