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