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