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