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