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