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