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