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