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