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