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