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