]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/SynchronizationGcc.c
Add the ReadWriteBarrier in the Acquire/ReleaseSpin to block the unexpected optimizat...
[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
ef7baf58 163 VOID *Result;\r
164 \r
878ddf1f 165 ASSERT (SpinLock != NULL);\r
ef7baf58 166 ASSERT (*SpinLock == SPIN_LOCK_ACQUIRED || *SpinLock == SPIN_LOCK_RELEASED);\r
c9708ddd 167\r
ef7baf58 168 _ReadWriteBarrier ();\r
169 Result = InterlockedCompareExchangePointer (\r
878ddf1f 170 (VOID**)SpinLock,\r
171 (VOID*)SPIN_LOCK_RELEASED,\r
172 (VOID*)SPIN_LOCK_ACQUIRED\r
878ddf1f 173 );\r
ef7baf58 174\r
175 _ReadWriteBarrier ();\r
176 return (BOOLEAN) (Result == (VOID*) SPIN_LOCK_RELEASED);\r
878ddf1f 177}\r
178\r
179/**\r
180 Releases a spin lock.\r
181\r
182 This function places the spin lock specified by SpinLock in the release state\r
183 and returns SpinLock.\r
184\r
185 If SpinLock is NULL, then ASSERT().\r
186 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
187\r
188 @param SpinLock A pointer to the spin lock to release.\r
189\r
190 @return SpinLock\r
191\r
192**/\r
193SPIN_LOCK *\r
194EFIAPI\r
195ReleaseSpinLock (\r
196 IN OUT SPIN_LOCK *SpinLock\r
197 )\r
198{\r
199 ASSERT (SpinLock != NULL);\r
ef7baf58 200 ASSERT (*SpinLock == SPIN_LOCK_ACQUIRED || *SpinLock == SPIN_LOCK_RELEASED);\r
c9708ddd 201\r
ef7baf58 202 _ReadWriteBarrier ();\r
27787243 203 *SpinLock = SPIN_LOCK_RELEASED;\r
ef7baf58 204 _ReadWriteBarrier ();\r
205\r
878ddf1f 206 return SpinLock;\r
207}\r
208\r
209/**\r
210 Performs an atomic increment of an 32-bit unsigned integer.\r
211\r
212 Performs an atomic increment of the 32-bit unsigned integer specified by\r
213 Value and returns the incremented value. The increment operation must be\r
214 performed using MP safe mechanisms. The state of the return value is not\r
215 guaranteed to be MP safe.\r
216\r
217 If Value is NULL, then ASSERT().\r
218\r
219 @param Value A pointer to the 32-bit value to increment.\r
220\r
221 @return The incremented value.\r
222\r
223**/\r
224UINT32\r
225EFIAPI\r
226InterlockedIncrement (\r
227 IN UINT32 *Value\r
228 )\r
229{\r
230 ASSERT (Value != NULL);\r
231 return InternalSyncIncrement (Value);\r
232}\r
233\r
234/**\r
235 Performs an atomic decrement of an 32-bit unsigned integer.\r
236\r
237 Performs an atomic decrement of the 32-bit unsigned integer specified by\r
238 Value and returns the decremented value. The decrement operation must be\r
239 performed using MP safe mechanisms. The state of the return value is not\r
240 guaranteed to be MP safe.\r
241\r
242 If Value is NULL, then ASSERT().\r
243\r
244 @param Value A pointer to the 32-bit value to decrement.\r
245\r
246 @return The decremented value.\r
247\r
248**/\r
249UINT32\r
250EFIAPI\r
251InterlockedDecrement (\r
252 IN UINT32 *Value\r
253 )\r
254{\r
255 ASSERT (Value != NULL);\r
256 return InternalSyncDecrement (Value);\r
257}\r
258\r
259/**\r
260 Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
261\r
1ea5ca46 262 Performs an atomic compare exchange operation on the 32-bit unsigned integer\r
263 specified by Value. If Value is equal to CompareValue, then Value is set to \r
264 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
265 then Value is returned. The compare exchange operation must be performed using \r
266 MP safe mechanisms.\r
267\r
268 If Value is NULL, then ASSERT().\r
269\r
878ddf1f 270 @param Value A pointer to the 32-bit value for the compare exchange\r
271 operation.\r
272 @param CompareValue 32-bit value used in compare operation.\r
273 @param ExchangeValue 32-bit value used in exchange operation.\r
274\r
275 @return The original *Value before exchange.\r
276\r
277**/\r
278UINT32\r
279EFIAPI\r
280InterlockedCompareExchange32 (\r
0ffa1286 281 IN OUT UINT32 *Value,\r
878ddf1f 282 IN UINT32 CompareValue,\r
283 IN UINT32 ExchangeValue\r
284 )\r
285{\r
286 ASSERT (Value != NULL);\r
287 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);\r
288}\r
289\r
290/**\r
291 Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r
292\r
1ea5ca46 293 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified \r
294 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and \r
295 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned. \r
296 The compare exchange operation must be performed using MP safe mechanisms.\r
297\r
298 If Value is NULL, then ASSERT().\r
299\r
878ddf1f 300 @param Value A pointer to the 64-bit value for the compare exchange\r
301 operation.\r
302 @param CompareValue 64-bit value used in compare operation.\r
303 @param ExchangeValue 64-bit value used in exchange operation.\r
304\r
305 @return The original *Value before exchange.\r
306\r
307**/\r
308UINT64\r
309EFIAPI\r
310InterlockedCompareExchange64 (\r
0ffa1286 311 IN OUT UINT64 *Value,\r
878ddf1f 312 IN UINT64 CompareValue,\r
313 IN UINT64 ExchangeValue\r
314 )\r
315{\r
316 ASSERT (Value != NULL);\r
317 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);\r
318}\r
319\r
320/**\r
321 Performs an atomic compare exchange operation on a pointer value.\r
322\r
323 Performs an atomic compare exchange operation on the pointer value specified\r
324 by Value. If Value is equal to CompareValue, then Value is set to\r
325 ExchangeValue and CompareValue is returned. If Value is not equal to\r
326 CompareValue, then Value is returned. The compare exchange operation must be\r
327 performed using MP safe mechanisms.\r
328\r
329 If Value is NULL, then ASSERT().\r
330\r
331 @param Value A pointer to the pointer value for the compare exchange\r
332 operation.\r
333 @param CompareValue Pointer value used in compare operation.\r
334 @param ExchangeValue Pointer value used in exchange operation.\r
335\r
336**/\r
337VOID *\r
338EFIAPI\r
339InterlockedCompareExchangePointer (\r
0ffa1286 340 IN OUT VOID **Value,\r
878ddf1f 341 IN VOID *CompareValue,\r
342 IN VOID *ExchangeValue\r
343 )\r
344{\r
58251024 345 UINT8 SizeOfValue;\r
346\r
347 SizeOfValue = sizeof (*Value);\r
348\r
349 switch (SizeOfValue) {\r
878ddf1f 350 case sizeof (UINT32):\r
351 return (VOID*)(UINTN)InterlockedCompareExchange32 (\r
352 (UINT32*)Value,\r
353 (UINT32)(UINTN)CompareValue,\r
354 (UINT32)(UINTN)ExchangeValue\r
355 );\r
356 case sizeof (UINT64):\r
357 return (VOID*)(UINTN)InterlockedCompareExchange64 (\r
358 (UINT64*)Value,\r
359 (UINT64)(UINTN)CompareValue,\r
360 (UINT64)(UINTN)ExchangeValue\r
361 );\r
362 default:\r
363 ASSERT (FALSE);\r
364 return NULL;\r
365 }\r
366}\r