]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Include/Library/SynchronizationLib.h
MdePkg/SynchronizationLib: fix Interlocked[De|In]crement return value
[mirror_edk2.git] / MdePkg / Include / Library / SynchronizationLib.h
CommitLineData
720d3c5f 1/** @file\r
2 Provides synchronization functions.\r
3\r
9095d37b 4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9df063a0 5This program and the accompanying materials\r
720d3c5f 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#ifndef __SYNCHRONIZATION_LIB__\r
16#define __SYNCHRONIZATION_LIB__\r
17\r
18///\r
19/// Definitions for SPIN_LOCK\r
20///\r
21typedef volatile UINTN SPIN_LOCK;\r
22\r
23\r
24/**\r
1a2f870c 25 Retrieves the architecture-specific spin lock alignment requirements for\r
720d3c5f 26 optimal spin lock performance.\r
27\r
28 This function retrieves the spin lock alignment requirements for optimal\r
9095d37b 29 performance on a given CPU architecture. The spin lock alignment is byte alignment.\r
744265eb 30 It must be a power of two and is returned by this function. If there are no alignment\r
720d3c5f 31 requirements, then 1 must be returned. The spin lock synchronization\r
32 functions must function correctly if the spin lock size and alignment values\r
33 returned by this function are not used at all. These values are hints to the\r
34 consumers of the spin lock synchronization functions to obtain optimal spin\r
35 lock performance.\r
36\r
1a2f870c 37 @return The architecture-specific spin lock alignment.\r
720d3c5f 38\r
39**/\r
40UINTN\r
41EFIAPI\r
42GetSpinLockProperties (\r
43 VOID\r
44 );\r
45\r
46\r
47/**\r
48 Initializes a spin lock to the released state and returns the spin lock.\r
49\r
50 This function initializes the spin lock specified by SpinLock to the released\r
51 state, and returns SpinLock. Optimal performance can be achieved by calling\r
52 GetSpinLockProperties() to determine the size and alignment requirements for\r
53 SpinLock.\r
54\r
55 If SpinLock is NULL, then ASSERT().\r
56\r
57 @param SpinLock A pointer to the spin lock to initialize to the released\r
58 state.\r
59\r
60 @return SpinLock in release state.\r
61\r
62**/\r
63SPIN_LOCK *\r
64EFIAPI\r
65InitializeSpinLock (\r
66 OUT SPIN_LOCK *SpinLock\r
67 );\r
68\r
69\r
70/**\r
71 Waits until a spin lock can be placed in the acquired state.\r
72\r
73 This function checks the state of the spin lock specified by SpinLock. If\r
74 SpinLock is in the released state, then this function places SpinLock in the\r
75 acquired state and returns SpinLock. Otherwise, this function waits\r
76 indefinitely for the spin lock to be released, and then places it in the\r
77 acquired state and returns SpinLock. All state transitions of SpinLock must\r
78 be performed using MP safe mechanisms.\r
79\r
80 If SpinLock is NULL, then ASSERT().\r
81 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
82 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in\r
83 PcdSpinLockTimeout microseconds, then ASSERT().\r
84\r
85 @param SpinLock A pointer to the spin lock to place in the acquired state.\r
86\r
87 @return SpinLock acquired lock.\r
88\r
89**/\r
90SPIN_LOCK *\r
91EFIAPI\r
92AcquireSpinLock (\r
93 IN OUT SPIN_LOCK *SpinLock\r
94 );\r
95\r
96\r
97/**\r
98 Attempts to place a spin lock in the acquired state.\r
99\r
100 This function checks the state of the spin lock specified by SpinLock. If\r
101 SpinLock is in the released state, then this function places SpinLock in the\r
102 acquired state and returns TRUE. Otherwise, FALSE is returned. All state\r
103 transitions of SpinLock must be performed using MP safe mechanisms.\r
104\r
105 If SpinLock is NULL, then ASSERT().\r
106 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
107\r
108 @param SpinLock A pointer to the spin lock to place in the acquired state.\r
109\r
110 @retval TRUE SpinLock was placed in the acquired state.\r
111 @retval FALSE SpinLock could not be acquired.\r
112\r
113**/\r
114BOOLEAN\r
115EFIAPI\r
116AcquireSpinLockOrFail (\r
117 IN OUT SPIN_LOCK *SpinLock\r
118 );\r
119\r
120\r
121/**\r
122 Releases a spin lock.\r
123\r
124 This function places the spin lock specified by SpinLock in the release state\r
125 and returns SpinLock.\r
126\r
127 If SpinLock is NULL, then ASSERT().\r
128 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
129\r
130 @param SpinLock A pointer to the spin lock to release.\r
131\r
132 @return SpinLock released lock.\r
133\r
134**/\r
135SPIN_LOCK *\r
136EFIAPI\r
137ReleaseSpinLock (\r
138 IN OUT SPIN_LOCK *SpinLock\r
139 );\r
140\r
141\r
142/**\r
1a2f870c 143 Performs an atomic increment of a 32-bit unsigned integer.\r
720d3c5f 144\r
145 Performs an atomic increment of the 32-bit unsigned integer specified by\r
146 Value and returns the incremented value. The increment operation must be\r
17634d02 147 performed using MP safe mechanisms.\r
720d3c5f 148\r
149 If Value is NULL, then ASSERT().\r
150\r
151 @param Value A pointer to the 32-bit value to increment.\r
152\r
153 @return The incremented value.\r
154\r
155**/\r
156UINT32\r
157EFIAPI\r
158InterlockedIncrement (\r
7375f3f1 159 IN volatile UINT32 *Value\r
720d3c5f 160 );\r
161\r
162\r
163/**\r
1a2f870c 164 Performs an atomic decrement of a 32-bit unsigned integer.\r
720d3c5f 165\r
166 Performs an atomic decrement of the 32-bit unsigned integer specified by\r
167 Value and returns the decremented value. The decrement operation must be\r
17634d02 168 performed using MP safe mechanisms.\r
720d3c5f 169\r
170 If Value is NULL, then ASSERT().\r
171\r
172 @param Value A pointer to the 32-bit value to decrement.\r
173\r
174 @return The decremented value.\r
175\r
176**/\r
177UINT32\r
178EFIAPI\r
179InterlockedDecrement (\r
7375f3f1 180 IN volatile UINT32 *Value\r
720d3c5f 181 );\r
182\r
183\r
9b89163e
AB
184/**\r
185 Performs an atomic compare exchange operation on a 16-bit unsigned integer.\r
186\r
187 Performs an atomic compare exchange operation on the 16-bit unsigned integer\r
188 specified by Value. If Value is equal to CompareValue, then Value is set to\r
189 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
190 then Value is returned. The compare exchange operation must be performed using\r
191 MP safe mechanisms.\r
192\r
193 If Value is NULL, then ASSERT().\r
194\r
195 @param Value A pointer to the 16-bit value for the compare exchange\r
196 operation.\r
197 @param CompareValue 16-bit value used in compare operation.\r
198 @param ExchangeValue 16-bit value used in exchange operation.\r
199\r
200 @return The original *Value before exchange.\r
201**/\r
202UINT16\r
203EFIAPI\r
204InterlockedCompareExchange16 (\r
7375f3f1 205 IN OUT volatile UINT16 *Value,\r
9b89163e
AB
206 IN UINT16 CompareValue,\r
207 IN UINT16 ExchangeValue\r
208 );\r
209\r
720d3c5f 210/**\r
211 Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
212\r
213 Performs an atomic compare exchange operation on the 32-bit unsigned integer\r
214 specified by Value. If Value is equal to CompareValue, then Value is set to\r
215 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
216 then Value is returned. The compare exchange operation must be performed using\r
217 MP safe mechanisms.\r
218\r
219 If Value is NULL, then ASSERT().\r
220\r
221 @param Value A pointer to the 32-bit value for the compare exchange\r
222 operation.\r
223 @param CompareValue 32-bit value used in compare operation.\r
224 @param ExchangeValue 32-bit value used in exchange operation.\r
225\r
226 @return The original *Value before exchange.\r
227\r
228**/\r
229UINT32\r
230EFIAPI\r
231InterlockedCompareExchange32 (\r
7375f3f1 232 IN OUT volatile UINT32 *Value,\r
720d3c5f 233 IN UINT32 CompareValue,\r
234 IN UINT32 ExchangeValue\r
235 );\r
236\r
237\r
238/**\r
239 Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r
240\r
241 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified\r
242 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and\r
243 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.\r
244 The compare exchange operation must be performed using MP safe mechanisms.\r
245\r
246 If Value is NULL, then ASSERT().\r
247\r
248 @param Value A pointer to the 64-bit value for the compare exchange\r
249 operation.\r
250 @param CompareValue 64-bit value used in compare operation.\r
251 @param ExchangeValue 64-bit value used in exchange operation.\r
252\r
253 @return The original *Value before exchange.\r
254\r
255**/\r
256UINT64\r
257EFIAPI\r
258InterlockedCompareExchange64 (\r
7375f3f1 259 IN OUT volatile UINT64 *Value,\r
720d3c5f 260 IN UINT64 CompareValue,\r
261 IN UINT64 ExchangeValue\r
262 );\r
263\r
264\r
265/**\r
266 Performs an atomic compare exchange operation on a pointer value.\r
267\r
268 Performs an atomic compare exchange operation on the pointer value specified\r
269 by Value. If Value is equal to CompareValue, then Value is set to\r
270 ExchangeValue and CompareValue is returned. If Value is not equal to\r
271 CompareValue, then Value is returned. The compare exchange operation must be\r
272 performed using MP safe mechanisms.\r
273\r
274 If Value is NULL, then ASSERT().\r
275\r
276 @param Value A pointer to the pointer value for the compare exchange\r
277 operation.\r
278 @param CompareValue Pointer value used in compare operation.\r
279 @param ExchangeValue Pointer value used in exchange operation.\r
280\r
281 @return The original *Value before exchange.\r
282**/\r
283VOID *\r
284EFIAPI\r
285InterlockedCompareExchangePointer (\r
7375f3f1 286 IN OUT VOID * volatile *Value,\r
720d3c5f 287 IN VOID *CompareValue,\r
288 IN VOID *ExchangeValue\r
289 );\r
290\r
291#endif\r
292\r
293\r