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