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