df395869a295a1b8f0675caaf5624e3b55152ef0
[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)0)
18 #define SPIN_LOCK_ACQUIRED ((SPIN_LOCK)-1)
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 = 0;
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 (FixedPcdGet32 (PcdSpinLockTimeout) > 0) {
135 Tick = GetPerformanceCounter ();
136 Timeout = DivU64x32 (
137 MultU64x32 (
138 GetPerformanceCounterProperties (&Start, &End),
139 FixedPcdGet32 (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 return (BOOLEAN)(
182 InterlockedCompareExchangePointer (
183 (VOID**)SpinLock,
184 (VOID*)SPIN_LOCK_RELEASED,
185 (VOID*)SPIN_LOCK_ACQUIRED
186 ) == (VOID*)SPIN_LOCK_RELEASED
187 );
188 }
189
190 /**
191 Releases a spin lock.
192
193 This function places the spin lock specified by SpinLock in the release state
194 and returns SpinLock.
195
196 If SpinLock is NULL, then ASSERT().
197 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().
198
199 @param SpinLock A pointer to the spin lock to release.
200
201 @return SpinLock
202
203 **/
204 SPIN_LOCK *
205 EFIAPI
206 ReleaseSpinLock (
207 IN OUT SPIN_LOCK *SpinLock
208 )
209 {
210 ASSERT (SpinLock != NULL);
211 *SpinLock = 0;
212 return SpinLock;
213 }
214
215 /**
216 Performs an atomic increment of an 32-bit unsigned integer.
217
218 Performs an atomic increment of the 32-bit unsigned integer specified by
219 Value and returns the incremented value. The increment operation must be
220 performed using MP safe mechanisms. The state of the return value is not
221 guaranteed to be MP safe.
222
223 If Value is NULL, then ASSERT().
224
225 @param Value A pointer to the 32-bit value to increment.
226
227 @return The incremented value.
228
229 **/
230 UINT32
231 EFIAPI
232 InterlockedIncrement (
233 IN UINT32 *Value
234 )
235 {
236 ASSERT (Value != NULL);
237 return InternalSyncIncrement (Value);
238 }
239
240 /**
241 Performs an atomic decrement of an 32-bit unsigned integer.
242
243 Performs an atomic decrement of the 32-bit unsigned integer specified by
244 Value and returns the decremented value. The decrement operation must be
245 performed using MP safe mechanisms. The state of the return value is not
246 guaranteed to be MP safe.
247
248 If Value is NULL, then ASSERT().
249
250 @param Value A pointer to the 32-bit value to decrement.
251
252 @return The decremented value.
253
254 **/
255 UINT32
256 EFIAPI
257 InterlockedDecrement (
258 IN UINT32 *Value
259 )
260 {
261 ASSERT (Value != NULL);
262 return InternalSyncDecrement (Value);
263 }
264
265 /**
266 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
267
268 @param Value A pointer to the 32-bit value for the compare exchange
269 operation.
270 @param CompareValue 32-bit value used in compare operation.
271 @param ExchangeValue 32-bit value used in exchange operation.
272
273 @return The original *Value before exchange.
274
275 **/
276 UINT32
277 EFIAPI
278 InterlockedCompareExchange32 (
279 IN UINT32 *Value,
280 IN UINT32 CompareValue,
281 IN UINT32 ExchangeValue
282 )
283 {
284 ASSERT (Value != NULL);
285 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);
286 }
287
288 /**
289 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
290
291 @param Value A pointer to the 64-bit value for the compare exchange
292 operation.
293 @param CompareValue 64-bit value used in compare operation.
294 @param ExchangeValue 64-bit value used in exchange operation.
295
296 @return The original *Value before exchange.
297
298 **/
299 UINT64
300 EFIAPI
301 InterlockedCompareExchange64 (
302 IN UINT64 *Value,
303 IN UINT64 CompareValue,
304 IN UINT64 ExchangeValue
305 )
306 {
307 ASSERT (Value != NULL);
308 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);
309 }
310
311 /**
312 Performs an atomic compare exchange operation on a pointer value.
313
314 Performs an atomic compare exchange operation on the pointer value specified
315 by Value. If Value is equal to CompareValue, then Value is set to
316 ExchangeValue and CompareValue is returned. If Value is not equal to
317 CompareValue, then Value is returned. The compare exchange operation must be
318 performed using MP safe mechanisms.
319
320 If Value is NULL, then ASSERT().
321
322 @param Value A pointer to the pointer value for the compare exchange
323 operation.
324 @param CompareValue Pointer value used in compare operation.
325 @param ExchangeValue Pointer value used in exchange operation.
326
327 **/
328 VOID *
329 EFIAPI
330 InterlockedCompareExchangePointer (
331 IN VOID **Value,
332 IN VOID *CompareValue,
333 IN VOID *ExchangeValue
334 )
335 {
336 switch (sizeof (*Value)) {
337 case sizeof (UINT32):
338 return (VOID*)(UINTN)InterlockedCompareExchange32 (
339 (UINT32*)Value,
340 (UINT32)(UINTN)CompareValue,
341 (UINT32)(UINTN)ExchangeValue
342 );
343 case sizeof (UINT64):
344 return (VOID*)(UINTN)InterlockedCompareExchange64 (
345 (UINT64*)Value,
346 (UINT64)(UINTN)CompareValue,
347 (UINT64)(UINTN)ExchangeValue
348 );
349 default:
350 ASSERT (FALSE);
351 return NULL;
352 }
353 }