]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Synchronization.c
Removed MdePkg usage of ModuleName: in file headers
[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
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
41UINTN\r
42EFIAPI\r
43GetSpinLockProperties (\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
67SPIN_LOCK *\r
68EFIAPI\r
69InitializeSpinLock (\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
98SPIN_LOCK *\r
99EFIAPI\r
100AcquireSpinLock (\r
101 IN OUT SPIN_LOCK *SpinLock\r
102 )\r
103{\r
104 UINT64 Tick;\r
105 UINT64 Start, End;\r
106 UINT64 Timeout;\r
107\r
108 Tick = 0;\r
109 Start = 0;\r
110 End = 0;\r
111 if (PcdGet32 (PcdSpinLockTimeout) > 0) {\r
112 Tick = GetPerformanceCounter ();\r
113 Timeout = DivU64x32 (\r
114 MultU64x32 (\r
115 GetPerformanceCounterProperties (&Start, &End),\r
116 PcdGet32 (PcdSpinLockTimeout)\r
117 ),\r
118 1000000\r
119 );\r
120 if (Start < End) {\r
121 Tick += Timeout;\r
122 } else {\r
123 Tick -= Timeout;\r
124 }\r
125 }\r
126\r
127 while (!AcquireSpinLockOrFail (SpinLock)) {\r
128 CpuPause ();\r
129 ASSERT ((Start < End) ^ (Tick <= GetPerformanceCounter ()));\r
130 }\r
131 return SpinLock;\r
132}\r
133\r
134/**\r
135 Attempts to place a spin lock in the acquired state.\r
136\r
137 This function checks the state of the spin lock specified by SpinLock. If\r
138 SpinLock is in the released state, then this function places SpinLock in the\r
139 acquired state and returns TRUE. Otherwise, FALSE is returned. All state\r
140 transitions of SpinLock must be performed using MP safe mechanisms.\r
141\r
142 If SpinLock is NULL, then ASSERT().\r
143 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
144\r
145 @param SpinLock A pointer to the spin lock to place in the acquired state.\r
146\r
147 @retval TRUE SpinLock was placed in the acquired state.\r
148 @retval FALSE SpinLock could not be acquired.\r
149\r
150**/\r
151BOOLEAN\r
152EFIAPI\r
153AcquireSpinLockOrFail (\r
154 IN OUT SPIN_LOCK *SpinLock\r
155 )\r
156{\r
157 SPIN_LOCK LockValue;\r
158\r
159 ASSERT (SpinLock != NULL);\r
160\r
161 LockValue = *SpinLock;\r
162 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r
163\r
164 return (BOOLEAN)(\r
165 InterlockedCompareExchangePointer (\r
166 (VOID**)SpinLock,\r
167 (VOID*)SPIN_LOCK_RELEASED,\r
168 (VOID*)SPIN_LOCK_ACQUIRED\r
169 ) == (VOID*)SPIN_LOCK_RELEASED\r
170 );\r
171}\r
172\r
173/**\r
174 Releases a spin lock.\r
175\r
176 This function places the spin lock specified by SpinLock in the release state\r
177 and returns SpinLock.\r
178\r
179 If SpinLock is NULL, then ASSERT().\r
180 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
181\r
182 @param SpinLock A pointer to the spin lock to release.\r
183\r
184 @return SpinLock\r
185\r
186**/\r
187SPIN_LOCK *\r
188EFIAPI\r
189ReleaseSpinLock (\r
190 IN OUT SPIN_LOCK *SpinLock\r
191 )\r
192{\r
193 SPIN_LOCK LockValue;\r
194\r
195 ASSERT (SpinLock != NULL);\r
196\r
197 LockValue = *SpinLock;\r
198 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r
199\r
200 *SpinLock = SPIN_LOCK_RELEASED;\r
201 return SpinLock;\r
202}\r
203\r
204/**\r
205 Performs an atomic increment of an 32-bit unsigned integer.\r
206\r
207 Performs an atomic increment of the 32-bit unsigned integer specified by\r
208 Value and returns the incremented value. The increment operation must be\r
209 performed using MP safe mechanisms. The state of the return value is not\r
210 guaranteed to be MP safe.\r
211\r
212 If Value is NULL, then ASSERT().\r
213\r
214 @param Value A pointer to the 32-bit value to increment.\r
215\r
216 @return The incremented value.\r
217\r
218**/\r
219UINT32\r
220EFIAPI\r
221InterlockedIncrement (\r
222 IN UINT32 *Value\r
223 )\r
224{\r
225 ASSERT (Value != NULL);\r
226 return InternalSyncIncrement (Value);\r
227}\r
228\r
229/**\r
230 Performs an atomic decrement of an 32-bit unsigned integer.\r
231\r
232 Performs an atomic decrement of the 32-bit unsigned integer specified by\r
233 Value and returns the decremented value. The decrement 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 decrement.\r
240\r
241 @return The decremented value.\r
242\r
243**/\r
244UINT32\r
245EFIAPI\r
246InterlockedDecrement (\r
247 IN UINT32 *Value\r
248 )\r
249{\r
250 ASSERT (Value != NULL);\r
251 return InternalSyncDecrement (Value);\r
252}\r
253\r
254/**\r
255 Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
256\r
257 Performs an atomic compare exchange operation on the 32-bit unsigned integer\r
258 specified by Value. If Value is equal to CompareValue, then Value is set to \r
259 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
260 then Value is returned. The compare exchange operation must be performed using \r
261 MP safe mechanisms.\r
262\r
263 If Value is NULL, then ASSERT().\r
264\r
265 @param Value A pointer to the 32-bit value for the compare exchange\r
266 operation.\r
267 @param CompareValue 32-bit value used in compare operation.\r
268 @param ExchangeValue 32-bit value used in exchange operation.\r
269\r
270 @return The original *Value before exchange.\r
271\r
272**/\r
273UINT32\r
274EFIAPI\r
275InterlockedCompareExchange32 (\r
276 IN OUT UINT32 *Value,\r
277 IN UINT32 CompareValue,\r
278 IN UINT32 ExchangeValue\r
279 )\r
280{\r
281 ASSERT (Value != NULL);\r
282 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);\r
283}\r
284\r
285/**\r
286 Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r
287\r
288 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified \r
289 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and \r
290 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned. \r
291 The compare exchange operation must be performed using MP safe mechanisms.\r
292\r
293 If Value is NULL, then ASSERT().\r
294\r
295 @param Value A pointer to the 64-bit value for the compare exchange\r
296 operation.\r
297 @param CompareValue 64-bit value used in compare operation.\r
298 @param ExchangeValue 64-bit value used in exchange operation.\r
299\r
300 @return The original *Value before exchange.\r
301\r
302**/\r
303UINT64\r
304EFIAPI\r
305InterlockedCompareExchange64 (\r
306 IN OUT UINT64 *Value,\r
307 IN UINT64 CompareValue,\r
308 IN UINT64 ExchangeValue\r
309 )\r
310{\r
311 ASSERT (Value != NULL);\r
312 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);\r
313}\r
314\r
315/**\r
316 Performs an atomic compare exchange operation on a pointer value.\r
317\r
318 Performs an atomic compare exchange operation on the pointer value specified\r
319 by Value. If Value is equal to CompareValue, then Value is set to\r
320 ExchangeValue and CompareValue is returned. If Value is not equal to\r
321 CompareValue, then Value is returned. The compare exchange operation must be\r
322 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 pointer value for the compare exchange\r
327 operation.\r
328 @param CompareValue Pointer value used in compare operation.\r
329 @param ExchangeValue Pointer value used in exchange operation.\r
330\r
331**/\r
332VOID *\r
333EFIAPI\r
334InterlockedCompareExchangePointer (\r
335 IN OUT VOID **Value,\r
336 IN VOID *CompareValue,\r
337 IN VOID *ExchangeValue\r
338 )\r
339{\r
340 UINT8 SizeOfValue;\r
341\r
342 SizeOfValue = sizeof (*Value);\r
343\r
344 switch (SizeOfValue) {\r
345 case sizeof (UINT32):\r
346 return (VOID*)(UINTN)InterlockedCompareExchange32 (\r
347 (UINT32*)Value,\r
348 (UINT32)(UINTN)CompareValue,\r
349 (UINT32)(UINTN)ExchangeValue\r
350 );\r
351 case sizeof (UINT64):\r
352 return (VOID*)(UINTN)InterlockedCompareExchange64 (\r
353 (UINT64*)Value,\r
354 (UINT64)(UINTN)CompareValue,\r
355 (UINT64)(UINTN)ExchangeValue\r
356 );\r
357 default:\r
358 ASSERT (FALSE);\r
359 return NULL;\r
360 }\r
361}\r