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