Initial import.
[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
17#define SPIN_LOCK_RELEASED ((SPIN_LOCK)0)\r
18#define SPIN_LOCK_ACQUIRED ((SPIN_LOCK)-1)\r
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
97 *SpinLock = 0;\r
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
181 return (BOOLEAN)(\r
182 InterlockedCompareExchangePointer (\r
183 (VOID**)SpinLock,\r
184 (VOID*)SPIN_LOCK_RELEASED,\r
185 (VOID*)SPIN_LOCK_ACQUIRED\r
186 ) == (VOID*)SPIN_LOCK_RELEASED\r
187 );\r
188}\r
189\r
190/**\r
191 Releases a spin lock.\r
192\r
193 This function places the spin lock specified by SpinLock in the release state\r
194 and returns SpinLock.\r
195\r
196 If SpinLock is NULL, then ASSERT().\r
197 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
198\r
199 @param SpinLock A pointer to the spin lock to release.\r
200\r
201 @return SpinLock\r
202\r
203**/\r
204SPIN_LOCK *\r
205EFIAPI\r
206ReleaseSpinLock (\r
207 IN OUT SPIN_LOCK *SpinLock\r
208 )\r
209{\r
210 ASSERT (SpinLock != NULL);\r
211 *SpinLock = 0;\r
212 return SpinLock;\r
213}\r
214\r
215/**\r
216 Performs an atomic increment of an 32-bit unsigned integer.\r
217\r
218 Performs an atomic increment of the 32-bit unsigned integer specified by\r
219 Value and returns the incremented value. The increment operation must be\r
220 performed using MP safe mechanisms. The state of the return value is not\r
221 guaranteed to be MP safe.\r
222\r
223 If Value is NULL, then ASSERT().\r
224\r
225 @param Value A pointer to the 32-bit value to increment.\r
226\r
227 @return The incremented value.\r
228\r
229**/\r
230UINT32\r
231EFIAPI\r
232InterlockedIncrement (\r
233 IN UINT32 *Value\r
234 )\r
235{\r
236 ASSERT (Value != NULL);\r
237 return InternalSyncIncrement (Value);\r
238}\r
239\r
240/**\r
241 Performs an atomic decrement of an 32-bit unsigned integer.\r
242\r
243 Performs an atomic decrement of the 32-bit unsigned integer specified by\r
244 Value and returns the decremented value. The decrement operation must be\r
245 performed using MP safe mechanisms. The state of the return value is not\r
246 guaranteed to be MP safe.\r
247\r
248 If Value is NULL, then ASSERT().\r
249\r
250 @param Value A pointer to the 32-bit value to decrement.\r
251\r
252 @return The decremented value.\r
253\r
254**/\r
255UINT32\r
256EFIAPI\r
257InterlockedDecrement (\r
258 IN UINT32 *Value\r
259 )\r
260{\r
261 ASSERT (Value != NULL);\r
262 return InternalSyncDecrement (Value);\r
263}\r
264\r
265/**\r
266 Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
267\r
268 @param Value A pointer to the 32-bit value for the compare exchange\r
269 operation.\r
270 @param CompareValue 32-bit value used in compare operation.\r
271 @param ExchangeValue 32-bit value used in exchange operation.\r
272\r
273 @return The original *Value before exchange.\r
274\r
275**/\r
276UINT32\r
277EFIAPI\r
278InterlockedCompareExchange32 (\r
279 IN UINT32 *Value,\r
280 IN UINT32 CompareValue,\r
281 IN UINT32 ExchangeValue\r
282 )\r
283{\r
284 ASSERT (Value != NULL);\r
285 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);\r
286}\r
287\r
288/**\r
289 Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r
290\r
291 @param Value A pointer to the 64-bit value for the compare exchange\r
292 operation.\r
293 @param CompareValue 64-bit value used in compare operation.\r
294 @param ExchangeValue 64-bit value used in exchange operation.\r
295\r
296 @return The original *Value before exchange.\r
297\r
298**/\r
299UINT64\r
300EFIAPI\r
301InterlockedCompareExchange64 (\r
302 IN UINT64 *Value,\r
303 IN UINT64 CompareValue,\r
304 IN UINT64 ExchangeValue\r
305 )\r
306{\r
307 ASSERT (Value != NULL);\r
308 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);\r
309}\r
310\r
311/**\r
312 Performs an atomic compare exchange operation on a pointer value.\r
313\r
314 Performs an atomic compare exchange operation on the pointer value specified\r
315 by Value. If Value is equal to CompareValue, then Value is set to\r
316 ExchangeValue and CompareValue is returned. If Value is not equal to\r
317 CompareValue, then Value is returned. The compare exchange operation must be\r
318 performed using MP safe mechanisms.\r
319\r
320 If Value is NULL, then ASSERT().\r
321\r
322 @param Value A pointer to the pointer value for the compare exchange\r
323 operation.\r
324 @param CompareValue Pointer value used in compare operation.\r
325 @param ExchangeValue Pointer value used in exchange operation.\r
326\r
327**/\r
328VOID *\r
329EFIAPI\r
330InterlockedCompareExchangePointer (\r
331 IN VOID **Value,\r
332 IN VOID *CompareValue,\r
333 IN VOID *ExchangeValue\r
334 )\r
335{\r
336 switch (sizeof (*Value)) {\r
337 case sizeof (UINT32):\r
338 return (VOID*)(UINTN)InterlockedCompareExchange32 (\r
339 (UINT32*)Value,\r
340 (UINT32)(UINTN)CompareValue,\r
341 (UINT32)(UINTN)ExchangeValue\r
342 );\r
343 case sizeof (UINT64):\r
344 return (VOID*)(UINTN)InterlockedCompareExchange64 (\r
345 (UINT64*)Value,\r
346 (UINT64)(UINTN)CompareValue,\r
347 (UINT64)(UINTN)ExchangeValue\r
348 );\r
349 default:\r
350 ASSERT (FALSE);\r
351 return NULL;\r
352 }\r
353}\r