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