]>
Commit | Line | Data |
---|---|---|
720d3c5f | 1 | /** @file\r |
2 | Implementation of synchronization functions.\r | |
3 | \r | |
0f18e1ed | 4 | Copyright (c) 2006 - 2016, 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 | |
744265eb LG |
31 | performance on a given CPU architecture. The spin lock alignment is byte alignment. \r |
32 | It must be a power of two and is returned by this function. If there are no alignment\r | |
720d3c5f | 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 | |
5f0a17d8 | 48 | return InternalGetSpinLockProperties ();\r |
720d3c5f | 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 | |
0f18e1ed JF |
117 | if (PcdGet32 (PcdSpinLockTimeout) == 0) {\r |
118 | while (!AcquireSpinLockOrFail (SpinLock)) {\r | |
119 | CpuPause ();\r | |
120 | }\r | |
121 | } else if (!AcquireSpinLockOrFail (SpinLock)) {\r | |
720d3c5f | 122 | //\r |
123 | // Get the current timer value\r | |
124 | //\r | |
125 | Current = GetPerformanceCounter();\r | |
126 | \r | |
127 | //\r | |
128 | // Initialize local variables\r | |
129 | //\r | |
130 | Start = 0;\r | |
131 | End = 0;\r | |
132 | Total = 0;\r | |
133 | \r | |
134 | //\r | |
135 | // Retrieve the performance counter properties and compute the number of performance\r | |
136 | // counter ticks required to reach the timeout\r | |
137 | //\r | |
138 | Timeout = DivU64x32 (\r | |
139 | MultU64x32 (\r | |
140 | GetPerformanceCounterProperties (&Start, &End),\r | |
141 | PcdGet32 (PcdSpinLockTimeout)\r | |
142 | ),\r | |
143 | 1000000\r | |
144 | );\r | |
145 | Cycle = End - Start;\r | |
146 | if (Cycle < 0) {\r | |
147 | Cycle = -Cycle;\r | |
148 | }\r | |
149 | Cycle++;\r | |
150 | \r | |
151 | while (!AcquireSpinLockOrFail (SpinLock)) {\r | |
152 | CpuPause ();\r | |
153 | Previous = Current;\r | |
154 | Current = GetPerformanceCounter();\r | |
155 | Delta = (INT64) (Current - Previous);\r | |
156 | if (Start > End) {\r | |
157 | Delta = -Delta;\r | |
158 | }\r | |
159 | if (Delta < 0) {\r | |
160 | Delta += Cycle;\r | |
161 | }\r | |
162 | Total += Delta;\r | |
163 | ASSERT (Total < Timeout);\r | |
164 | }\r | |
720d3c5f | 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 | |
4cee954e | 263 | IN volatile UINT32 *Value\r |
720d3c5f | 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 | |
4cee954e | 288 | IN volatile UINT32 *Value\r |
720d3c5f | 289 | )\r |
290 | {\r | |
291 | ASSERT (Value != NULL);\r | |
292 | return InternalSyncDecrement (Value);\r | |
293 | }\r | |
294 | \r | |
9b89163e AB |
295 | /**\r |
296 | Performs an atomic compare exchange operation on a 16-bit unsigned integer.\r | |
297 | \r | |
298 | Performs an atomic compare exchange operation on the 16-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 16-bit value for the compare exchange\r | |
307 | operation.\r | |
308 | @param CompareValue A 16-bit value used in compare operation.\r | |
309 | @param ExchangeValue A 16-bit value used in exchange operation.\r | |
310 | \r | |
311 | @return The original *Value before exchange.\r | |
312 | \r | |
313 | **/\r | |
314 | UINT16\r | |
315 | EFIAPI\r | |
316 | InterlockedCompareExchange16 (\r | |
4cee954e | 317 | IN OUT volatile UINT16 *Value,\r |
9b89163e AB |
318 | IN UINT16 CompareValue,\r |
319 | IN UINT16 ExchangeValue\r | |
320 | )\r | |
321 | {\r | |
322 | ASSERT (Value != NULL);\r | |
323 | return InternalSyncCompareExchange16 (Value, CompareValue, ExchangeValue);\r | |
324 | }\r | |
325 | \r | |
720d3c5f | 326 | /**\r |
327 | Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r | |
328 | \r | |
329 | Performs an atomic compare exchange operation on the 32-bit unsigned integer\r | |
330 | specified by Value. If Value is equal to CompareValue, then Value is set to\r | |
331 | ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r | |
332 | then Value is returned. The compare exchange operation must be performed using\r | |
333 | MP safe mechanisms.\r | |
334 | \r | |
335 | If Value is NULL, then ASSERT().\r | |
336 | \r | |
337 | @param Value A pointer to the 32-bit value for the compare exchange\r | |
338 | operation.\r | |
2fc59a00 | 339 | @param CompareValue A 32-bit value used in compare operation.\r |
340 | @param ExchangeValue A 32-bit value used in exchange operation.\r | |
720d3c5f | 341 | \r |
342 | @return The original *Value before exchange.\r | |
343 | \r | |
344 | **/\r | |
345 | UINT32\r | |
346 | EFIAPI\r | |
347 | InterlockedCompareExchange32 (\r | |
4cee954e | 348 | IN OUT volatile UINT32 *Value,\r |
720d3c5f | 349 | IN UINT32 CompareValue,\r |
350 | IN UINT32 ExchangeValue\r | |
351 | )\r | |
352 | {\r | |
353 | ASSERT (Value != NULL);\r | |
354 | return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);\r | |
355 | }\r | |
356 | \r | |
357 | /**\r | |
358 | Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r | |
359 | \r | |
360 | Performs an atomic compare exchange operation on the 64-bit unsigned integer specified\r | |
361 | by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and\r | |
362 | CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.\r | |
363 | The compare exchange operation must be 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 64-bit value for the compare exchange\r | |
368 | operation.\r | |
2fc59a00 | 369 | @param CompareValue A 64-bit value used in a compare operation.\r |
370 | @param ExchangeValue A 64-bit value used in an exchange operation.\r | |
720d3c5f | 371 | \r |
372 | @return The original *Value before exchange.\r | |
373 | \r | |
374 | **/\r | |
375 | UINT64\r | |
376 | EFIAPI\r | |
377 | InterlockedCompareExchange64 (\r | |
4cee954e | 378 | IN OUT volatile UINT64 *Value,\r |
720d3c5f | 379 | IN UINT64 CompareValue,\r |
380 | IN UINT64 ExchangeValue\r | |
381 | )\r | |
382 | {\r | |
383 | ASSERT (Value != NULL);\r | |
384 | return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);\r | |
385 | }\r | |
386 | \r | |
387 | /**\r | |
388 | Performs an atomic compare exchange operation on a pointer value.\r | |
389 | \r | |
390 | Performs an atomic compare exchange operation on the pointer value specified\r | |
391 | by Value. If Value is equal to CompareValue, then Value is set to\r | |
392 | ExchangeValue and CompareValue is returned. If Value is not equal to\r | |
393 | CompareValue, then Value is returned. The compare exchange operation must be\r | |
394 | performed using MP safe mechanisms.\r | |
395 | \r | |
396 | If Value is NULL, then ASSERT().\r | |
397 | \r | |
398 | @param Value A pointer to the pointer value for the compare exchange\r | |
399 | operation.\r | |
2fc59a00 | 400 | @param CompareValue A pointer value used in a compare operation.\r |
401 | @param ExchangeValue A pointer value used in an exchange operation.\r | |
720d3c5f | 402 | \r |
403 | @return The original *Value before exchange.\r | |
404 | **/\r | |
405 | VOID *\r | |
406 | EFIAPI\r | |
407 | InterlockedCompareExchangePointer (\r | |
4cee954e | 408 | IN OUT VOID * volatile *Value,\r |
720d3c5f | 409 | IN VOID *CompareValue,\r |
410 | IN VOID *ExchangeValue\r | |
411 | )\r | |
412 | {\r | |
413 | UINT8 SizeOfValue;\r | |
414 | \r | |
415 | SizeOfValue = sizeof (*Value);\r | |
416 | \r | |
417 | switch (SizeOfValue) {\r | |
418 | case sizeof (UINT32):\r | |
419 | return (VOID*)(UINTN)InterlockedCompareExchange32 (\r | |
4cee954e | 420 | (volatile UINT32 *)Value,\r |
720d3c5f | 421 | (UINT32)(UINTN)CompareValue,\r |
422 | (UINT32)(UINTN)ExchangeValue\r | |
423 | );\r | |
424 | case sizeof (UINT64):\r | |
425 | return (VOID*)(UINTN)InterlockedCompareExchange64 (\r | |
4cee954e | 426 | (volatile UINT64 *)Value,\r |
720d3c5f | 427 | (UINT64)(UINTN)CompareValue,\r |
428 | (UINT64)(UINTN)ExchangeValue\r | |
429 | );\r | |
430 | default:\r | |
431 | ASSERT (FALSE);\r | |
432 | return NULL;\r | |
433 | }\r | |
434 | }\r |