]>
Commit | Line | Data |
---|---|---|
720d3c5f | 1 | /** @file\r |
2 | Implementation of synchronization functions.\r | |
3 | \r | |
2fc59a00 | 4 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r |
e2d9bfb2 HT |
5 | Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r |
6 | This program and the accompanying materials\r | |
720d3c5f | 7 | are licensed and made available under the terms and conditions of the BSD License\r |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
2fc59a00 | 9 | http://opensource.org/licenses/bsd-license.php.\r |
720d3c5f | 10 | \r |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include "BaseSynchronizationLibInternals.h"\r | |
17 | \r | |
18 | //\r | |
19 | // GCC inline assembly for Read Write Barrier \r | |
20 | //\r | |
ebd04fc2 | 21 | #define _ReadWriteBarrier() do { __asm__ __volatile__ ("": : : "memory"); } while(0)\r |
720d3c5f | 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 | |
42 | UINTN\r | |
43 | EFIAPI\r | |
44 | GetSpinLockProperties (\r | |
45 | VOID\r | |
46 | )\r | |
47 | {\r | |
48 | return 32;\r | |
49 | }\r | |
50 | \r | |
51 | /**\r | |
52 | Initializes a spin lock to the released state and returns the spin lock.\r | |
53 | \r | |
54 | This function initializes the spin lock specified by SpinLock to the released\r | |
55 | state, and returns SpinLock. Optimal performance can be achieved by calling\r | |
56 | GetSpinLockProperties() to determine the size and alignment requirements for\r | |
57 | SpinLock.\r | |
58 | \r | |
59 | If SpinLock is NULL, then ASSERT().\r | |
60 | \r | |
61 | @param SpinLock A pointer to the spin lock to initialize to the released\r | |
62 | state.\r | |
63 | \r | |
2fc59a00 | 64 | @return SpinLock is in release state.\r |
720d3c5f | 65 | \r |
66 | **/\r | |
67 | SPIN_LOCK *\r | |
68 | EFIAPI\r | |
69 | InitializeSpinLock (\r | |
70 | OUT SPIN_LOCK *SpinLock\r | |
71 | )\r | |
72 | {\r | |
73 | ASSERT (SpinLock != NULL);\r | |
74 | \r | |
75 | _ReadWriteBarrier();\r | |
76 | *SpinLock = SPIN_LOCK_RELEASED;\r | |
77 | _ReadWriteBarrier();\r | |
78 | \r | |
79 | return SpinLock;\r | |
80 | }\r | |
81 | \r | |
82 | /**\r | |
83 | Waits until a spin lock can be placed in the acquired state.\r | |
84 | \r | |
85 | This function checks the state of the spin lock specified by SpinLock. If\r | |
86 | SpinLock is in the released state, then this function places SpinLock in the\r | |
87 | acquired state and returns SpinLock. Otherwise, this function waits\r | |
88 | indefinitely for the spin lock to be released, and then places it in the\r | |
89 | acquired state and returns SpinLock. All state transitions of SpinLock must\r | |
90 | be performed using MP safe mechanisms.\r | |
91 | \r | |
92 | If SpinLock is NULL, then ASSERT().\r | |
93 | If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r | |
94 | If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in\r | |
95 | PcdSpinLockTimeout microseconds, then ASSERT().\r | |
96 | \r | |
97 | @param SpinLock A pointer to the spin lock to place in the acquired state.\r | |
98 | \r | |
2fc59a00 | 99 | @return SpinLock acquired the lock.\r |
720d3c5f | 100 | \r |
101 | **/\r | |
102 | SPIN_LOCK *\r | |
103 | EFIAPI\r | |
104 | AcquireSpinLock (\r | |
105 | IN OUT SPIN_LOCK *SpinLock\r | |
106 | )\r | |
107 | {\r | |
108 | UINT64 Current;\r | |
109 | UINT64 Previous;\r | |
110 | UINT64 Total;\r | |
111 | UINT64 Start;\r | |
112 | UINT64 End;\r | |
113 | UINT64 Timeout;\r | |
114 | INT64 Cycle;\r | |
115 | INT64 Delta;\r | |
116 | \r | |
117 | if (PcdGet32 (PcdSpinLockTimeout) > 0) {\r | |
118 | //\r | |
119 | // Get the current timer value\r | |
120 | //\r | |
121 | Current = GetPerformanceCounter();\r | |
122 | \r | |
123 | //\r | |
124 | // Initialize local variables\r | |
125 | //\r | |
126 | Start = 0;\r | |
127 | End = 0;\r | |
128 | Total = 0;\r | |
129 | \r | |
130 | //\r | |
131 | // Retrieve the performance counter properties and compute the number of performance\r | |
132 | // counter ticks required to reach the timeout\r | |
133 | //\r | |
134 | Timeout = DivU64x32 (\r | |
135 | MultU64x32 (\r | |
136 | GetPerformanceCounterProperties (&Start, &End),\r | |
137 | PcdGet32 (PcdSpinLockTimeout)\r | |
138 | ),\r | |
139 | 1000000\r | |
140 | );\r | |
141 | Cycle = End - Start;\r | |
142 | if (Cycle < 0) {\r | |
143 | Cycle = -Cycle;\r | |
144 | }\r | |
145 | Cycle++;\r | |
146 | \r | |
147 | while (!AcquireSpinLockOrFail (SpinLock)) {\r | |
148 | CpuPause ();\r | |
149 | Previous = Current;\r | |
150 | Current = GetPerformanceCounter();\r | |
151 | Delta = (INT64) (Current - Previous);\r | |
152 | if (Start > End) {\r | |
153 | Delta = -Delta;\r | |
154 | }\r | |
155 | if (Delta < 0) {\r | |
156 | Delta += Cycle;\r | |
157 | }\r | |
158 | Total += Delta;\r | |
159 | ASSERT (Total < Timeout);\r | |
160 | }\r | |
161 | } else {\r | |
162 | while (!AcquireSpinLockOrFail (SpinLock)) {\r | |
163 | CpuPause ();\r | |
164 | }\r | |
165 | }\r | |
166 | return SpinLock;\r | |
167 | }\r | |
168 | \r | |
169 | /**\r | |
170 | Attempts to place a spin lock in the acquired state.\r | |
171 | \r | |
172 | This function checks the state of the spin lock specified by SpinLock. If\r | |
173 | SpinLock is in the released state, then this function places SpinLock in the\r | |
174 | acquired state and returns TRUE. Otherwise, FALSE is returned. All state\r | |
175 | transitions of SpinLock must be performed using MP safe mechanisms.\r | |
176 | \r | |
177 | If SpinLock is NULL, then ASSERT().\r | |
178 | If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r | |
179 | \r | |
180 | @param SpinLock A pointer to the spin lock to place in the acquired state.\r | |
181 | \r | |
182 | @retval TRUE SpinLock was placed in the acquired state.\r | |
183 | @retval FALSE SpinLock could not be acquired.\r | |
184 | \r | |
185 | **/\r | |
186 | BOOLEAN\r | |
187 | EFIAPI\r | |
188 | AcquireSpinLockOrFail (\r | |
189 | IN OUT SPIN_LOCK *SpinLock\r | |
190 | )\r | |
191 | {\r | |
192 | SPIN_LOCK LockValue;\r | |
193 | VOID *Result;\r | |
194 | \r | |
195 | ASSERT (SpinLock != NULL);\r | |
196 | \r | |
197 | LockValue = *SpinLock;\r | |
198 | ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r | |
199 | \r | |
200 | _ReadWriteBarrier ();\r | |
201 | Result = InterlockedCompareExchangePointer (\r | |
202 | (VOID**)SpinLock,\r | |
203 | (VOID*)SPIN_LOCK_RELEASED,\r | |
204 | (VOID*)SPIN_LOCK_ACQUIRED\r | |
205 | );\r | |
206 | \r | |
207 | _ReadWriteBarrier ();\r | |
208 | return (BOOLEAN) (Result == (VOID*) SPIN_LOCK_RELEASED);\r | |
209 | }\r | |
210 | \r | |
211 | /**\r | |
212 | Releases a spin lock.\r | |
213 | \r | |
214 | This function places the spin lock specified by SpinLock in the release state\r | |
215 | and returns SpinLock.\r | |
216 | \r | |
217 | If SpinLock is NULL, then ASSERT().\r | |
218 | If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r | |
219 | \r | |
220 | @param SpinLock A pointer to the spin lock to release.\r | |
221 | \r | |
2fc59a00 | 222 | @return SpinLock released the lock.\r |
720d3c5f | 223 | \r |
224 | **/\r | |
225 | SPIN_LOCK *\r | |
226 | EFIAPI\r | |
227 | ReleaseSpinLock (\r | |
228 | IN OUT SPIN_LOCK *SpinLock\r | |
229 | )\r | |
230 | {\r | |
231 | SPIN_LOCK LockValue;\r | |
232 | \r | |
233 | ASSERT (SpinLock != NULL);\r | |
234 | \r | |
235 | LockValue = *SpinLock;\r | |
236 | ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r | |
237 | \r | |
238 | _ReadWriteBarrier ();\r | |
239 | *SpinLock = SPIN_LOCK_RELEASED;\r | |
240 | _ReadWriteBarrier ();\r | |
241 | \r | |
242 | return SpinLock;\r | |
243 | }\r | |
244 | \r | |
245 | /**\r | |
246 | Performs an atomic increment of an 32-bit unsigned integer.\r | |
247 | \r | |
248 | Performs an atomic increment of the 32-bit unsigned integer specified by\r | |
249 | Value and returns the incremented value. The increment operation must be\r | |
250 | performed using MP safe mechanisms. The state of the return value is not\r | |
251 | guaranteed to be MP safe.\r | |
252 | \r | |
253 | If Value is NULL, then ASSERT().\r | |
254 | \r | |
255 | @param Value A pointer to the 32-bit value to increment.\r | |
256 | \r | |
257 | @return The incremented value.\r | |
258 | \r | |
259 | **/\r | |
260 | UINT32\r | |
261 | EFIAPI\r | |
262 | InterlockedIncrement (\r | |
263 | IN UINT32 *Value\r | |
264 | )\r | |
265 | {\r | |
266 | ASSERT (Value != NULL);\r | |
267 | return InternalSyncIncrement (Value);\r | |
268 | }\r | |
269 | \r | |
270 | /**\r | |
271 | Performs an atomic decrement of an 32-bit unsigned integer.\r | |
272 | \r | |
273 | Performs an atomic decrement of the 32-bit unsigned integer specified by\r | |
274 | Value and returns the decremented value. The decrement operation must be\r | |
275 | performed using MP safe mechanisms. The state of the return value is not\r | |
276 | guaranteed to be MP safe.\r | |
277 | \r | |
278 | If Value is NULL, then ASSERT().\r | |
279 | \r | |
280 | @param Value A pointer to the 32-bit value to decrement.\r | |
281 | \r | |
282 | @return The decremented value.\r | |
283 | \r | |
284 | **/\r | |
285 | UINT32\r | |
286 | EFIAPI\r | |
287 | InterlockedDecrement (\r | |
288 | IN UINT32 *Value\r | |
289 | )\r | |
290 | {\r | |
291 | ASSERT (Value != NULL);\r | |
292 | return InternalSyncDecrement (Value);\r | |
293 | }\r | |
294 | \r | |
295 | /**\r | |
296 | Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r | |
297 | \r | |
298 | Performs an atomic compare exchange operation on the 32-bit unsigned integer\r | |
299 | specified by Value. If Value is equal to CompareValue, then Value is set to\r | |
300 | ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r | |
301 | then Value is returned. The compare exchange operation must be performed using\r | |
302 | MP safe mechanisms.\r | |
303 | \r | |
304 | If Value is NULL, then ASSERT().\r | |
305 | \r | |
306 | @param Value A pointer to the 32-bit value for the compare exchange\r | |
307 | operation.\r | |
2fc59a00 | 308 | @param CompareValue A 32-bit value used in compare operation.\r |
309 | @param ExchangeValue A 32-bit value used in exchange operation.\r | |
720d3c5f | 310 | \r |
311 | @return The original *Value before exchange.\r | |
312 | \r | |
313 | **/\r | |
314 | UINT32\r | |
315 | EFIAPI\r | |
316 | InterlockedCompareExchange32 (\r | |
317 | IN OUT UINT32 *Value,\r | |
318 | IN UINT32 CompareValue,\r | |
319 | IN UINT32 ExchangeValue\r | |
320 | )\r | |
321 | {\r | |
322 | ASSERT (Value != NULL);\r | |
323 | return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);\r | |
324 | }\r | |
325 | \r | |
326 | /**\r | |
327 | Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r | |
328 | \r | |
329 | Performs an atomic compare exchange operation on the 64-bit unsigned integer specified\r | |
330 | by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and\r | |
331 | CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.\r | |
332 | The compare exchange operation must be performed using MP safe mechanisms.\r | |
333 | \r | |
334 | If Value is NULL, then ASSERT().\r | |
335 | \r | |
336 | @param Value A pointer to the 64-bit value for the compare exchange\r | |
337 | operation.\r | |
2fc59a00 | 338 | @param CompareValue A 64-bit value used in a compare operation.\r |
339 | @param ExchangeValue A 64-bit value used in an exchange operation.\r | |
720d3c5f | 340 | \r |
341 | @return The original *Value before exchange.\r | |
342 | \r | |
343 | **/\r | |
344 | UINT64\r | |
345 | EFIAPI\r | |
346 | InterlockedCompareExchange64 (\r | |
347 | IN OUT UINT64 *Value,\r | |
348 | IN UINT64 CompareValue,\r | |
349 | IN UINT64 ExchangeValue\r | |
350 | )\r | |
351 | {\r | |
352 | ASSERT (Value != NULL);\r | |
353 | return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);\r | |
354 | }\r | |
355 | \r | |
356 | /**\r | |
357 | Performs an atomic compare exchange operation on a pointer value.\r | |
358 | \r | |
359 | Performs an atomic compare exchange operation on the pointer value specified\r | |
360 | by Value. If Value is equal to CompareValue, then Value is set to\r | |
361 | ExchangeValue and CompareValue is returned. If Value is not equal to\r | |
362 | CompareValue, then Value is returned. The compare exchange operation must be\r | |
363 | performed using MP safe mechanisms.\r | |
364 | \r | |
365 | If Value is NULL, then ASSERT().\r | |
366 | \r | |
367 | @param Value A pointer to the pointer value for the compare exchange\r | |
368 | operation.\r | |
2fc59a00 | 369 | @param CompareValue A pointer value used in a compare operation.\r |
370 | @param ExchangeValue A pointer value used in an exchange operation.\r | |
720d3c5f | 371 | \r |
372 | @return The original *Value before exchange.\r | |
373 | **/\r | |
374 | VOID *\r | |
375 | EFIAPI\r | |
376 | InterlockedCompareExchangePointer (\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 |