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