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