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