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