]>
Commit | Line | Data |
---|---|---|
3eb9473e | 1 | /*++\r |
2 | \r | |
3 | Copyright (c) 2004 - 2006, Intel Corporation \r | |
4 | All rights reserved. This program and the accompanying materials \r | |
5 | are licensed and made available under the terms and conditions of the BSD License \r | |
6 | which accompanies this distribution. The full text of the license may be found at \r | |
7 | http://opensource.org/licenses/bsd-license.php \r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
11 | \r | |
12 | \r | |
13 | Module Name:\r | |
14 | \r | |
15 | Synchronization.c\r | |
16 | \r | |
17 | Abstract: \r | |
18 | \r | |
19 | Implementation of synchronization functions.\r | |
20 | \r | |
21 | --*/\r | |
22 | \r | |
c7f33ca4 | 23 | #include "BaseLibInternals.h"\r |
3eb9473e | 24 | \r |
c7f33ca4 | 25 | #define SPIN_LOCK_RELEASED ((UINTN) 1)\r |
26 | #define SPIN_LOCK_ACQUIRED ((UINTN) 2)\r | |
3eb9473e | 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 | // @bug May use a PCD entry to determine this alignment.\r | |
51 | return 32;\r | |
52 | }\r | |
53 | \r | |
54 | /**\r | |
55 | Initializes a spin lock to the released state and returns the spin lock.\r | |
56 | \r | |
57 | This function initializes the spin lock specified by SpinLock to the released\r | |
58 | state, and returns SpinLock. Optimal performance can be achieved by calling\r | |
59 | GetSpinLockProperties() to determine the size and alignment requirements for\r | |
60 | SpinLock.\r | |
61 | \r | |
62 | If SpinLock is NULL, then ASSERT().\r | |
63 | \r | |
64 | @param SpinLock A pointer to the spin lock to initialize to the released\r | |
65 | state.\r | |
66 | \r | |
67 | @return SpinLock\r | |
68 | \r | |
69 | **/\r | |
70 | SPIN_LOCK *\r | |
71 | EFIAPI\r | |
72 | InitializeSpinLock (\r | |
73 | OUT SPIN_LOCK *SpinLock\r | |
74 | )\r | |
75 | {\r | |
76 | ASSERT (SpinLock != NULL);\r | |
77 | *SpinLock = SPIN_LOCK_RELEASED;\r | |
78 | return SpinLock;\r | |
79 | }\r | |
80 | \r | |
81 | /**\r | |
82 | Waits until a spin lock can be placed in the acquired state.\r | |
83 | \r | |
84 | This function checks the state of the spin lock specified by SpinLock. If\r | |
85 | SpinLock is in the released state, then this function places SpinLock in the\r | |
86 | acquired state and returns SpinLock. Otherwise, this function waits\r | |
87 | indefinitely for the spin lock to be released, and then places it in the\r | |
88 | acquired state and returns SpinLock. All state transitions of SpinLock must\r | |
89 | be performed using MP safe mechanisms.\r | |
90 | \r | |
91 | If SpinLock is NULL, then ASSERT().\r | |
92 | If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r | |
93 | If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in\r | |
94 | PcdSpinLockTimeout microseconds, then ASSERT().\r | |
95 | \r | |
96 | @param SpinLock A pointer to the spin lock to place in the acquired state.\r | |
97 | \r | |
98 | @return SpinLock\r | |
99 | \r | |
100 | **/\r | |
101 | SPIN_LOCK *\r | |
102 | EFIAPI\r | |
103 | AcquireSpinLock (\r | |
104 | IN OUT SPIN_LOCK *SpinLock\r | |
105 | )\r | |
106 | {\r | |
107 | UINT64 Tick;\r | |
108 | UINT64 Start, End;\r | |
109 | UINT64 Timeout;\r | |
110 | \r | |
111 | Tick = 0;\r | |
112 | Start = 0;\r | |
113 | End = 0;\r | |
114 | if (PcdGet32 (PcdSpinLockTimeout) > 0) {\r | |
115 | Tick = GetPerformanceCounter ();\r | |
116 | Timeout = DivU64x32 (\r | |
117 | MultU64x32 (\r | |
118 | GetPerformanceCounterProperties (&Start, &End),\r | |
119 | PcdGet32 (PcdSpinLockTimeout)\r | |
120 | ),\r | |
121 | 1000000\r | |
122 | );\r | |
123 | if (Start < End) {\r | |
124 | Tick += Timeout;\r | |
125 | } else {\r | |
126 | Tick -= Timeout;\r | |
127 | }\r | |
128 | }\r | |
129 | \r | |
130 | while (!AcquireSpinLockOrFail (SpinLock)) {\r | |
131 | CpuPause ();\r | |
132 | ASSERT ((Start < End) ^ (Tick <= GetPerformanceCounter ()));\r | |
133 | }\r | |
134 | return SpinLock;\r | |
135 | }\r | |
136 | \r | |
137 | /**\r | |
138 | Attempts to place a spin lock in the acquired state.\r | |
139 | \r | |
140 | This function checks the state of the spin lock specified by SpinLock. If\r | |
141 | SpinLock is in the released state, then this function places SpinLock in the\r | |
142 | acquired state and returns TRUE. Otherwise, FALSE is returned. All state\r | |
143 | transitions of SpinLock must be performed using MP safe mechanisms.\r | |
144 | \r | |
145 | If SpinLock is NULL, then ASSERT().\r | |
146 | If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r | |
147 | \r | |
148 | @param SpinLock A pointer to the spin lock to place in the acquired state.\r | |
149 | \r | |
150 | @retval TRUE SpinLock was placed in the acquired state.\r | |
151 | @retval FALSE SpinLock could not be acquired.\r | |
152 | \r | |
153 | **/\r | |
154 | BOOLEAN\r | |
155 | EFIAPI\r | |
156 | AcquireSpinLockOrFail (\r | |
157 | IN OUT SPIN_LOCK *SpinLock\r | |
158 | )\r | |
159 | {\r | |
c7f33ca4 | 160 | SPIN_LOCK LockValue;\r |
3eb9473e | 161 | \r |
162 | ASSERT (SpinLock != NULL);\r | |
163 | \r | |
164 | LockValue = *SpinLock;\r | |
165 | ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r | |
166 | \r | |
167 | return (BOOLEAN)(\r | |
168 | InterlockedCompareExchangePointer (\r | |
169 | (VOID**)SpinLock,\r | |
170 | (VOID*)SPIN_LOCK_RELEASED,\r | |
171 | (VOID*)SPIN_LOCK_ACQUIRED\r | |
172 | ) == (VOID*)SPIN_LOCK_RELEASED\r | |
173 | );\r | |
174 | }\r | |
175 | \r | |
176 | /**\r | |
177 | Releases a spin lock.\r | |
178 | \r | |
179 | This function places the spin lock specified by SpinLock in the release state\r | |
180 | and returns SpinLock.\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 release.\r | |
186 | \r | |
187 | @return SpinLock\r | |
188 | \r | |
189 | **/\r | |
190 | SPIN_LOCK *\r | |
191 | EFIAPI\r | |
192 | ReleaseSpinLock (\r | |
193 | IN OUT SPIN_LOCK *SpinLock\r | |
194 | )\r | |
195 | {\r | |
c7f33ca4 | 196 | SPIN_LOCK LockValue;\r |
3eb9473e | 197 | \r |
198 | ASSERT (SpinLock != NULL);\r | |
199 | \r | |
200 | LockValue = *SpinLock;\r | |
201 | ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r | |
202 | \r | |
203 | *SpinLock = SPIN_LOCK_RELEASED;\r | |
204 | return SpinLock;\r | |
205 | }\r | |
206 | \r | |
207 | /**\r | |
208 | Performs an atomic increment of an 32-bit unsigned integer.\r | |
209 | \r | |
210 | Performs an atomic increment of the 32-bit unsigned integer specified by\r | |
211 | Value and returns the incremented value. The increment operation must be\r | |
212 | performed using MP safe mechanisms. The state of the return value is not\r | |
213 | guaranteed to be MP safe.\r | |
214 | \r | |
215 | If Value is NULL, then ASSERT().\r | |
216 | \r | |
217 | @param Value A pointer to the 32-bit value to increment.\r | |
218 | \r | |
219 | @return The incremented value.\r | |
220 | \r | |
221 | **/\r | |
222 | UINT32\r | |
223 | EFIAPI\r | |
224 | InterlockedIncrement (\r | |
225 | IN UINT32 *Value\r | |
226 | )\r | |
227 | {\r | |
228 | ASSERT (Value != NULL);\r | |
229 | return InternalSyncIncrement (Value);\r | |
230 | }\r | |
231 | \r | |
232 | /**\r | |
233 | Performs an atomic decrement of an 32-bit unsigned integer.\r | |
234 | \r | |
235 | Performs an atomic decrement of the 32-bit unsigned integer specified by\r | |
236 | Value and returns the decremented value. The decrement operation must be\r | |
237 | performed using MP safe mechanisms. The state of the return value is not\r | |
238 | guaranteed to be MP safe.\r | |
239 | \r | |
240 | If Value is NULL, then ASSERT().\r | |
241 | \r | |
242 | @param Value A pointer to the 32-bit value to decrement.\r | |
243 | \r | |
244 | @return The decremented value.\r | |
245 | \r | |
246 | **/\r | |
247 | UINT32\r | |
248 | EFIAPI\r | |
249 | InterlockedDecrement (\r | |
250 | IN UINT32 *Value\r | |
251 | )\r | |
252 | {\r | |
253 | ASSERT (Value != NULL);\r | |
254 | return InternalSyncDecrement (Value);\r | |
255 | }\r | |
256 | \r | |
257 | /**\r | |
258 | Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r | |
259 | \r | |
260 | Performs an atomic compare exchange operation on the 32-bit unsigned integer\r | |
261 | specified by Value. If Value is equal to CompareValue, then Value is set to \r | |
262 | ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r | |
263 | then Value is returned. The compare exchange operation must be performed using \r | |
264 | MP safe mechanisms.\r | |
265 | \r | |
266 | If Value is NULL, then ASSERT().\r | |
267 | \r | |
268 | @param Value A pointer to the 32-bit value for the compare exchange\r | |
269 | operation.\r | |
270 | @param CompareValue 32-bit value used in compare operation.\r | |
271 | @param ExchangeValue 32-bit value used in exchange operation.\r | |
272 | \r | |
273 | @return The original *Value before exchange.\r | |
274 | \r | |
275 | **/\r | |
276 | UINT32\r | |
277 | EFIAPI\r | |
278 | InterlockedCompareExchange32 (\r | |
279 | IN OUT UINT32 *Value,\r | |
280 | IN UINT32 CompareValue,\r | |
281 | IN UINT32 ExchangeValue\r | |
282 | )\r | |
283 | {\r | |
284 | ASSERT (Value != NULL);\r | |
285 | return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);\r | |
286 | }\r | |
287 | \r | |
288 | /**\r | |
289 | Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r | |
290 | \r | |
291 | Performs an atomic compare exchange operation on the 64-bit unsigned integer specified \r | |
292 | by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and \r | |
293 | CompareValue is returned. If Value is not equal to CompareValue, then Value is returned. \r | |
294 | The compare exchange operation must be performed using MP safe mechanisms.\r | |
295 | \r | |
296 | If Value is NULL, then ASSERT().\r | |
297 | \r | |
298 | @param Value A pointer to the 64-bit value for the compare exchange\r | |
299 | operation.\r | |
300 | @param CompareValue 64-bit value used in compare operation.\r | |
301 | @param ExchangeValue 64-bit value used in exchange operation.\r | |
302 | \r | |
303 | @return The original *Value before exchange.\r | |
304 | \r | |
305 | **/\r | |
306 | UINT64\r | |
307 | EFIAPI\r | |
308 | InterlockedCompareExchange64 (\r | |
309 | IN OUT UINT64 *Value,\r | |
310 | IN UINT64 CompareValue,\r | |
311 | IN UINT64 ExchangeValue\r | |
312 | )\r | |
313 | {\r | |
314 | ASSERT (Value != NULL);\r | |
315 | return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);\r | |
316 | }\r | |
317 | \r | |
318 | /**\r | |
319 | Performs an atomic compare exchange operation on a pointer value.\r | |
320 | \r | |
321 | Performs an atomic compare exchange operation on the pointer value specified\r | |
322 | by Value. If Value is equal to CompareValue, then Value is set to\r | |
323 | ExchangeValue and CompareValue is returned. If Value is not equal to\r | |
324 | CompareValue, then Value is returned. The compare exchange operation must be\r | |
325 | performed using MP safe mechanisms.\r | |
326 | \r | |
327 | If Value is NULL, then ASSERT().\r | |
328 | \r | |
329 | @param Value A pointer to the pointer value for the compare exchange\r | |
330 | operation.\r | |
331 | @param CompareValue Pointer value used in compare operation.\r | |
332 | @param ExchangeValue Pointer value used in exchange operation.\r | |
333 | \r | |
334 | **/\r | |
335 | VOID *\r | |
336 | EFIAPI\r | |
337 | InterlockedCompareExchangePointer (\r | |
338 | IN OUT VOID **Value,\r | |
339 | IN VOID *CompareValue,\r | |
340 | IN VOID *ExchangeValue\r | |
341 | )\r | |
342 | {\r | |
343 | UINT8 SizeOfValue;\r | |
344 | \r | |
345 | SizeOfValue = sizeof (*Value);\r | |
346 | \r | |
347 | switch (SizeOfValue) {\r | |
348 | case sizeof (UINT32):\r | |
349 | return (VOID*)(UINTN)InterlockedCompareExchange32 (\r | |
350 | (UINT32*)Value,\r | |
351 | (UINT32)(UINTN)CompareValue,\r | |
352 | (UINT32)(UINTN)ExchangeValue\r | |
353 | );\r | |
354 | case sizeof (UINT64):\r | |
355 | return (VOID*)(UINTN)InterlockedCompareExchange64 (\r | |
356 | (UINT64*)Value,\r | |
357 | (UINT64)(UINTN)CompareValue,\r | |
358 | (UINT64)(UINTN)ExchangeValue\r | |
359 | );\r | |
360 | default:\r | |
361 | ASSERT (FALSE);\r | |
362 | return NULL;\r | |
363 | }\r | |
364 | }\r |