]>
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 | 5 | This program and the accompanying materials\r |
720d3c5f | 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 | |
2fc59a00 | 8 | http://opensource.org/licenses/bsd-license.php.\r |
720d3c5f | 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 | |
15 | #include "BaseSynchronizationLibInternals.h"\r | |
16 | \r | |
17 | /**\r | |
18 | Microsoft Visual Studio 7.1 Function Prototypes for read write barrier Intrinsics.\r | |
19 | **/\r | |
20 | \r | |
21 | void _ReadWriteBarrier (void);\r | |
22 | #pragma intrinsic(_ReadWriteBarrier)\r | |
23 | \r | |
24 | \r | |
25 | #define SPIN_LOCK_RELEASED ((UINTN) 1)\r | |
26 | #define SPIN_LOCK_ACQUIRED ((UINTN) 2)\r | |
27 | \r | |
28 | /**\r | |
29 | Retrieves the architecture specific spin lock alignment requirements for\r | |
30 | optimal spin lock performance.\r | |
31 | \r | |
32 | This function retrieves the spin lock alignment requirements for optimal\r | |
744265eb LG |
33 | performance on a given CPU architecture. The spin lock alignment is byte alignment. \r |
34 | It must be a power of two and is returned by this function. If there are no alignment\r | |
720d3c5f | 35 | requirements, then 1 must be returned. The spin lock synchronization\r |
36 | functions must function correctly if the spin lock size and alignment values\r | |
37 | returned by this function are not used at all. These values are hints to the\r | |
38 | consumers of the spin lock synchronization functions to obtain optimal spin\r | |
39 | lock performance.\r | |
40 | \r | |
41 | @return The architecture specific spin lock alignment.\r | |
42 | \r | |
43 | **/\r | |
44 | UINTN\r | |
45 | EFIAPI\r | |
46 | GetSpinLockProperties (\r | |
47 | VOID\r | |
48 | )\r | |
49 | {\r | |
5f0a17d8 | 50 | return InternalGetSpinLockProperties ();\r |
720d3c5f | 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 | |
2fc59a00 | 66 | @return SpinLock is in release state.\r |
720d3c5f | 67 | \r |
68 | **/\r | |
69 | SPIN_LOCK *\r | |
70 | EFIAPI\r | |
71 | InitializeSpinLock (\r | |
72 | OUT SPIN_LOCK *SpinLock\r | |
73 | )\r | |
74 | {\r | |
75 | ASSERT (SpinLock != NULL);\r | |
76 | \r | |
77 | _ReadWriteBarrier();\r | |
78 | *SpinLock = SPIN_LOCK_RELEASED;\r | |
79 | _ReadWriteBarrier();\r | |
80 | \r | |
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 | |
2fc59a00 | 101 | @return SpinLock acquired the lock.\r |
720d3c5f | 102 | \r |
103 | **/\r | |
104 | SPIN_LOCK *\r | |
105 | EFIAPI\r | |
106 | AcquireSpinLock (\r | |
107 | IN OUT SPIN_LOCK *SpinLock\r | |
108 | )\r | |
109 | {\r | |
110 | UINT64 Current;\r | |
111 | UINT64 Previous;\r | |
112 | UINT64 Total;\r | |
113 | UINT64 Start;\r | |
114 | UINT64 End;\r | |
115 | UINT64 Timeout;\r | |
116 | INT64 Cycle;\r | |
117 | INT64 Delta;\r | |
118 | \r | |
0f18e1ed JF |
119 | if (PcdGet32 (PcdSpinLockTimeout) == 0) {\r |
120 | while (!AcquireSpinLockOrFail (SpinLock)) {\r | |
121 | CpuPause ();\r | |
122 | }\r | |
123 | } else if (!AcquireSpinLockOrFail (SpinLock)) {\r | |
720d3c5f | 124 | //\r |
125 | // Get the current timer value\r | |
126 | //\r | |
127 | Current = GetPerformanceCounter();\r | |
128 | \r | |
129 | //\r | |
130 | // Initialize local variables\r | |
131 | //\r | |
132 | Start = 0;\r | |
133 | End = 0;\r | |
134 | Total = 0;\r | |
135 | \r | |
136 | //\r | |
137 | // Retrieve the performance counter properties and compute the number of performance\r | |
138 | // counter ticks required to reach the timeout\r | |
139 | //\r | |
140 | Timeout = DivU64x32 (\r | |
141 | MultU64x32 (\r | |
142 | GetPerformanceCounterProperties (&Start, &End),\r | |
143 | PcdGet32 (PcdSpinLockTimeout)\r | |
144 | ),\r | |
145 | 1000000\r | |
146 | );\r | |
147 | Cycle = End - Start;\r | |
148 | if (Cycle < 0) {\r | |
149 | Cycle = -Cycle;\r | |
150 | }\r | |
151 | Cycle++;\r | |
152 | \r | |
153 | while (!AcquireSpinLockOrFail (SpinLock)) {\r | |
154 | CpuPause ();\r | |
155 | Previous = Current;\r | |
156 | Current = GetPerformanceCounter();\r | |
157 | Delta = (INT64) (Current - Previous);\r | |
158 | if (Start > End) {\r | |
159 | Delta = -Delta;\r | |
160 | }\r | |
161 | if (Delta < 0) {\r | |
162 | Delta += Cycle;\r | |
163 | }\r | |
164 | Total += Delta;\r | |
165 | ASSERT (Total < Timeout);\r | |
166 | }\r | |
720d3c5f | 167 | }\r |
168 | return SpinLock;\r | |
169 | }\r | |
170 | \r | |
171 | /**\r | |
172 | Attempts to place a spin lock in the acquired state.\r | |
173 | \r | |
174 | This function checks the state of the spin lock specified by SpinLock. If\r | |
175 | SpinLock is in the released state, then this function places SpinLock in the\r | |
176 | acquired state and returns TRUE. Otherwise, FALSE is returned. All state\r | |
177 | transitions of SpinLock must be performed using MP safe mechanisms.\r | |
178 | \r | |
179 | If SpinLock is NULL, then ASSERT().\r | |
180 | If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r | |
181 | \r | |
182 | @param SpinLock A pointer to the spin lock to place in the acquired state.\r | |
183 | \r | |
184 | @retval TRUE SpinLock was placed in the acquired state.\r | |
185 | @retval FALSE SpinLock could not be acquired.\r | |
186 | \r | |
187 | **/\r | |
188 | BOOLEAN\r | |
189 | EFIAPI\r | |
190 | AcquireSpinLockOrFail (\r | |
191 | IN OUT SPIN_LOCK *SpinLock\r | |
192 | )\r | |
193 | {\r | |
194 | SPIN_LOCK LockValue;\r | |
195 | VOID *Result;\r | |
196 | \r | |
197 | ASSERT (SpinLock != NULL);\r | |
198 | \r | |
199 | LockValue = *SpinLock;\r | |
200 | ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r | |
201 | \r | |
202 | _ReadWriteBarrier ();\r | |
203 | Result = InterlockedCompareExchangePointer (\r | |
204 | (VOID**)SpinLock,\r | |
205 | (VOID*)SPIN_LOCK_RELEASED,\r | |
206 | (VOID*)SPIN_LOCK_ACQUIRED\r | |
207 | );\r | |
208 | \r | |
209 | _ReadWriteBarrier ();\r | |
210 | return (BOOLEAN) (Result == (VOID*) SPIN_LOCK_RELEASED);\r | |
211 | }\r | |
212 | \r | |
213 | /**\r | |
214 | Releases a spin lock.\r | |
215 | \r | |
216 | This function places the spin lock specified by SpinLock in the release state\r | |
217 | and returns SpinLock.\r | |
218 | \r | |
219 | If SpinLock is NULL, then ASSERT().\r | |
220 | If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r | |
221 | \r | |
222 | @param SpinLock A pointer to the spin lock to release.\r | |
223 | \r | |
2fc59a00 | 224 | @return SpinLock released the lock.\r |
720d3c5f | 225 | \r |
226 | **/\r | |
227 | SPIN_LOCK *\r | |
228 | EFIAPI\r | |
229 | ReleaseSpinLock (\r | |
230 | IN OUT SPIN_LOCK *SpinLock\r | |
231 | )\r | |
232 | {\r | |
233 | SPIN_LOCK LockValue;\r | |
234 | \r | |
235 | ASSERT (SpinLock != NULL);\r | |
236 | \r | |
237 | LockValue = *SpinLock;\r | |
238 | ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r | |
239 | \r | |
240 | _ReadWriteBarrier ();\r | |
241 | *SpinLock = SPIN_LOCK_RELEASED;\r | |
242 | _ReadWriteBarrier ();\r | |
243 | \r | |
244 | return SpinLock;\r | |
245 | }\r | |
246 | \r | |
247 | /**\r | |
248 | Performs an atomic increment of an 32-bit unsigned integer.\r | |
249 | \r | |
250 | Performs an atomic increment of the 32-bit unsigned integer specified by\r | |
251 | Value and returns the incremented value. The increment operation must be\r | |
252 | performed using MP safe mechanisms. The state of the return value is not\r | |
253 | guaranteed to be MP safe.\r | |
254 | \r | |
255 | If Value is NULL, then ASSERT().\r | |
256 | \r | |
257 | @param Value A pointer to the 32-bit value to increment.\r | |
258 | \r | |
259 | @return The incremented value.\r | |
260 | \r | |
261 | **/\r | |
262 | UINT32\r | |
263 | EFIAPI\r | |
264 | InterlockedIncrement (\r | |
4cee954e | 265 | IN volatile UINT32 *Value\r |
720d3c5f | 266 | )\r |
267 | {\r | |
268 | ASSERT (Value != NULL);\r | |
269 | return InternalSyncIncrement (Value);\r | |
270 | }\r | |
271 | \r | |
272 | /**\r | |
273 | Performs an atomic decrement of an 32-bit unsigned integer.\r | |
274 | \r | |
275 | Performs an atomic decrement of the 32-bit unsigned integer specified by\r | |
276 | Value and returns the decremented value. The decrement operation must be\r | |
277 | performed using MP safe mechanisms. The state of the return value is not\r | |
278 | guaranteed to be MP safe.\r | |
279 | \r | |
280 | If Value is NULL, then ASSERT().\r | |
281 | \r | |
282 | @param Value A pointer to the 32-bit value to decrement.\r | |
283 | \r | |
284 | @return The decremented value.\r | |
285 | \r | |
286 | **/\r | |
287 | UINT32\r | |
288 | EFIAPI\r | |
289 | InterlockedDecrement (\r | |
4cee954e | 290 | IN volatile UINT32 *Value\r |
720d3c5f | 291 | )\r |
292 | {\r | |
293 | ASSERT (Value != NULL);\r | |
294 | return InternalSyncDecrement (Value);\r | |
295 | }\r | |
296 | \r | |
9b89163e AB |
297 | /**\r |
298 | Performs an atomic compare exchange operation on a 16-bit unsigned integer.\r | |
299 | \r | |
300 | Performs an atomic compare exchange operation on the 16-bit unsigned integer\r | |
301 | specified by Value. If Value is equal to CompareValue, then Value is set to\r | |
302 | ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r | |
303 | then Value is returned. The compare exchange operation must be performed using\r | |
304 | MP safe mechanisms.\r | |
305 | \r | |
306 | If Value is NULL, then ASSERT().\r | |
307 | \r | |
308 | @param Value A pointer to the 16-bit value for the compare exchange\r | |
309 | operation.\r | |
310 | @param CompareValue A 16-bit value used in a compare operation.\r | |
311 | @param ExchangeValue A 16-bit value used in an exchange operation.\r | |
312 | \r | |
313 | @return The original *Value before exchange.\r | |
314 | \r | |
315 | **/\r | |
316 | UINT16\r | |
317 | EFIAPI\r | |
318 | InterlockedCompareExchange16 (\r | |
4cee954e | 319 | IN OUT volatile UINT16 *Value,\r |
9b89163e AB |
320 | IN UINT16 CompareValue,\r |
321 | IN UINT16 ExchangeValue\r | |
322 | )\r | |
323 | {\r | |
324 | ASSERT (Value != NULL);\r | |
325 | return InternalSyncCompareExchange16 (Value, CompareValue, ExchangeValue);\r | |
326 | }\r | |
327 | \r | |
720d3c5f | 328 | /**\r |
329 | Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r | |
330 | \r | |
331 | Performs an atomic compare exchange operation on the 32-bit unsigned integer\r | |
332 | specified by Value. If Value is equal to CompareValue, then Value is set to\r | |
333 | ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r | |
334 | then Value is returned. The compare exchange operation must be performed using\r | |
335 | MP safe mechanisms.\r | |
336 | \r | |
337 | If Value is NULL, then ASSERT().\r | |
338 | \r | |
339 | @param Value A pointer to the 32-bit value for the compare exchange\r | |
340 | operation.\r | |
2fc59a00 | 341 | @param CompareValue A 32-bit value used in a compare operation.\r |
342 | @param ExchangeValue A 32-bit value used in an exchange operation.\r | |
720d3c5f | 343 | \r |
344 | @return The original *Value before exchange.\r | |
345 | \r | |
346 | **/\r | |
347 | UINT32\r | |
348 | EFIAPI\r | |
349 | InterlockedCompareExchange32 (\r | |
4cee954e | 350 | IN OUT volatile UINT32 *Value,\r |
720d3c5f | 351 | IN UINT32 CompareValue,\r |
352 | IN UINT32 ExchangeValue\r | |
353 | )\r | |
354 | {\r | |
355 | ASSERT (Value != NULL);\r | |
356 | return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);\r | |
357 | }\r | |
358 | \r | |
359 | /**\r | |
360 | Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r | |
361 | \r | |
362 | Performs an atomic compare exchange operation on the 64-bit unsigned integer specified\r | |
363 | by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and\r | |
364 | CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.\r | |
365 | The compare exchange operation must be performed using MP safe mechanisms.\r | |
366 | \r | |
367 | If Value is NULL, then ASSERT().\r | |
368 | \r | |
369 | @param Value A pointer to the 64-bit value for the compare exchange\r | |
370 | operation.\r | |
2fc59a00 | 371 | @param CompareValue A 64-bit value used in a compare operation.\r |
372 | @param ExchangeValue A 64-bit value used in an exchange operation.\r | |
720d3c5f | 373 | \r |
374 | @return The original *Value before exchange.\r | |
375 | \r | |
376 | **/\r | |
377 | UINT64\r | |
378 | EFIAPI\r | |
379 | InterlockedCompareExchange64 (\r | |
4cee954e | 380 | IN OUT volatile UINT64 *Value,\r |
720d3c5f | 381 | IN UINT64 CompareValue,\r |
382 | IN UINT64 ExchangeValue\r | |
383 | )\r | |
384 | {\r | |
385 | ASSERT (Value != NULL);\r | |
386 | return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);\r | |
387 | }\r | |
388 | \r | |
389 | /**\r | |
390 | Performs an atomic compare exchange operation on a pointer value.\r | |
391 | \r | |
392 | Performs an atomic compare exchange operation on the pointer value specified\r | |
393 | by Value. If Value is equal to CompareValue, then Value is set to\r | |
394 | ExchangeValue and CompareValue is returned. If Value is not equal to\r | |
395 | CompareValue, then Value is returned. The compare exchange operation must be\r | |
396 | performed using MP safe mechanisms.\r | |
397 | \r | |
398 | If Value is NULL, then ASSERT().\r | |
399 | \r | |
400 | @param Value A pointer to the pointer value for the compare exchange\r | |
401 | operation.\r | |
2fc59a00 | 402 | @param CompareValue A pointer value used in a compare operation.\r |
403 | @param ExchangeValue A pointer value used in an exchange operation.\r | |
720d3c5f | 404 | \r |
405 | @return The original *Value before exchange.\r | |
406 | **/\r | |
407 | VOID *\r | |
408 | EFIAPI\r | |
409 | InterlockedCompareExchangePointer (\r | |
4cee954e | 410 | IN OUT VOID * volatile *Value,\r |
720d3c5f | 411 | IN VOID *CompareValue,\r |
412 | IN VOID *ExchangeValue\r | |
413 | )\r | |
414 | {\r | |
415 | UINT8 SizeOfValue;\r | |
416 | \r | |
f0b0ba31 | 417 | SizeOfValue = (UINT8) sizeof (*Value);\r |
720d3c5f | 418 | \r |
419 | switch (SizeOfValue) {\r | |
420 | case sizeof (UINT32):\r | |
421 | return (VOID*)(UINTN)InterlockedCompareExchange32 (\r | |
4cee954e | 422 | (volatile UINT32*)Value,\r |
720d3c5f | 423 | (UINT32)(UINTN)CompareValue,\r |
424 | (UINT32)(UINTN)ExchangeValue\r | |
425 | );\r | |
426 | case sizeof (UINT64):\r | |
427 | return (VOID*)(UINTN)InterlockedCompareExchange64 (\r | |
4cee954e | 428 | (volatile UINT64*)Value,\r |
720d3c5f | 429 | (UINT64)(UINTN)CompareValue,\r |
430 | (UINT64)(UINTN)ExchangeValue\r | |
431 | );\r | |
432 | default:\r | |
433 | ASSERT (FALSE);\r | |
434 | return NULL;\r | |
435 | }\r | |
436 | }\r |