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