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