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