Merge branch 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-1 into master-integration
[qt-netbsd.git] / src / corelib / arch / qatomic_armv6.h
blob38b7069943ac81bd9f757d254eea85517d9290f9
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #ifndef QATOMIC_ARMV6_H
43 #define QATOMIC_ARMV6_H
45 QT_BEGIN_HEADER
47 QT_BEGIN_NAMESPACE
48 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
50 inline bool QBasicAtomicInt::isReferenceCountingNative()
51 { return true; }
52 inline bool QBasicAtomicInt::isReferenceCountingWaitFree()
53 { return false; }
55 #define Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
57 inline bool QBasicAtomicInt::isTestAndSetNative()
58 { return true; }
59 inline bool QBasicAtomicInt::isTestAndSetWaitFree()
60 { return false; }
62 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
64 inline bool QBasicAtomicInt::isFetchAndStoreNative()
65 { return true; }
66 inline bool QBasicAtomicInt::isFetchAndStoreWaitFree()
67 { return false; }
69 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
71 inline bool QBasicAtomicInt::isFetchAndAddNative()
72 { return true; }
73 inline bool QBasicAtomicInt::isFetchAndAddWaitFree()
74 { return false; }
76 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
78 template <typename T>
79 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetNative()
80 { return true; }
81 template <typename T>
82 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetWaitFree()
83 { return false; }
85 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
87 template <typename T>
88 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreNative()
89 { return true; }
90 template <typename T>
91 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreWaitFree()
92 { return false; }
94 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
96 template <typename T>
97 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddNative()
98 { return true; }
99 template <typename T>
100 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddWaitFree()
101 { return false; }
103 #ifndef Q_CC_RVCT
105 inline bool QBasicAtomicInt::ref()
107 register int newValue;
108 register int result;
109 asm volatile("0:\n"
110 "ldrex %[newValue], [%[_q_value]]\n"
111 "add %[newValue], %[newValue], #1\n"
112 "strex %[result], %[newValue], [%[_q_value]]\n"
113 "teq %[result], #0\n"
114 "bne 0b\n"
115 : [newValue] "=&r" (newValue),
116 [result] "=&r" (result),
117 "+m" (_q_value)
118 : [_q_value] "r" (&_q_value)
119 : "cc", "memory");
120 return newValue != 0;
123 inline bool QBasicAtomicInt::deref()
125 register int newValue;
126 register int result;
127 asm volatile("0:\n"
128 "ldrex %[newValue], [%[_q_value]]\n"
129 "sub %[newValue], %[newValue], #1\n"
130 "strex %[result], %[newValue], [%[_q_value]]\n"
131 "teq %[result], #0\n"
132 "bne 0b\n"
133 : [newValue] "=&r" (newValue),
134 [result] "=&r" (result),
135 "+m" (_q_value)
136 : [_q_value] "r" (&_q_value)
137 : "cc", "memory");
138 return newValue != 0;
141 inline bool QBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
143 register int result;
144 asm volatile("0:\n"
145 "ldrex %[result], [%[_q_value]]\n"
146 "eors %[result], %[result], %[expectedValue]\n"
147 "strexeq %[result], %[newValue], [%[_q_value]]\n"
148 "teqeq %[result], #1\n"
149 "beq 0b\n"
150 : [result] "=&r" (result),
151 "+m" (_q_value)
152 : [expectedValue] "r" (expectedValue),
153 [newValue] "r" (newValue),
154 [_q_value] "r" (&_q_value)
155 : "cc", "memory");
156 return result == 0;
159 inline int QBasicAtomicInt::fetchAndStoreOrdered(int newValue)
161 register int originalValue;
162 register int result;
163 asm volatile("0:\n"
164 "ldrex %[originalValue], [%[_q_value]]\n"
165 "strex %[result], %[newValue], [%[_q_value]]\n"
166 "teq %[result], #0\n"
167 "bne 0b\n"
168 : [originalValue] "=&r" (originalValue),
169 [result] "=&r" (result),
170 "+m" (_q_value)
171 : [newValue] "r" (newValue),
172 [_q_value] "r" (&_q_value)
173 : "cc", "memory");
174 return originalValue;
177 inline int QBasicAtomicInt::fetchAndAddOrdered(int valueToAdd)
179 register int originalValue;
180 register int newValue;
181 register int result;
182 asm volatile("0:\n"
183 "ldrex %[originalValue], [%[_q_value]]\n"
184 "add %[newValue], %[originalValue], %[valueToAdd]\n"
185 "strex %[result], %[newValue], [%[_q_value]]\n"
186 "teq %[result], #0\n"
187 "bne 0b\n"
188 : [originalValue] "=&r" (originalValue),
189 [newValue] "=&r" (newValue),
190 [result] "=&r" (result),
191 "+m" (_q_value)
192 : [valueToAdd] "r" (valueToAdd),
193 [_q_value] "r" (&_q_value)
194 : "cc", "memory");
195 return originalValue;
198 template <typename T>
199 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
201 register T *result;
202 asm volatile("0:\n"
203 "ldrex %[result], [%[_q_value]]\n"
204 "eors %[result], %[result], %[expectedValue]\n"
205 "strexeq %[result], %[newValue], [%[_q_value]]\n"
206 "teqeq %[result], #1\n"
207 "beq 0b\n"
208 : [result] "=&r" (result),
209 "+m" (_q_value)
210 : [expectedValue] "r" (expectedValue),
211 [newValue] "r" (newValue),
212 [_q_value] "r" (&_q_value)
213 : "cc", "memory");
214 return result == 0;
217 template <typename T>
218 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreOrdered(T *newValue)
220 register T *originalValue;
221 register int result;
222 asm volatile("0:\n"
223 "ldrex %[originalValue], [%[_q_value]]\n"
224 "strex %[result], %[newValue], [%[_q_value]]\n"
225 "teq %[result], #0\n"
226 "bne 0b\n"
227 : [originalValue] "=&r" (originalValue),
228 [result] "=&r" (result),
229 "+m" (_q_value)
230 : [newValue] "r" (newValue),
231 [_q_value] "r" (&_q_value)
232 : "cc", "memory");
233 return originalValue;
236 template <typename T>
237 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
239 register T *originalValue;
240 register T *newValue;
241 register int result;
242 asm volatile("0:\n"
243 "ldrex %[originalValue], [%[_q_value]]\n"
244 "add %[newValue], %[originalValue], %[valueToAdd]\n"
245 "strex %[result], %[newValue], [%[_q_value]]\n"
246 "teq %[result], #0\n"
247 "bne 0b\n"
248 : [originalValue] "=&r" (originalValue),
249 [newValue] "=&r" (newValue),
250 [result] "=&r" (result),
251 "+m" (_q_value)
252 : [valueToAdd] "r" (valueToAdd * sizeof(T)),
253 [_q_value] "r" (&_q_value)
254 : "cc", "memory");
255 return originalValue;
258 #else
259 // This is Q_CC_RVCT
261 // RVCT inline assembly documentation:
262 // http://www.keil.com/support/man/docs/armcc/armcc_chdcffdb.htm
263 // RVCT embedded assembly documentation:
264 // http://www.keil.com/support/man/docs/armcc/armcc_chddbeib.htm
266 // save our pragma state and switch to ARM mode
267 #pragma push
268 #pragma arm
270 inline bool QBasicAtomicInt::ref()
272 register int newValue;
273 register int result;
274 retry:
275 __asm {
276 ldrex newValue, [&_q_value]
277 add newValue, newValue, #1
278 strex result, newValue, [&_q_value]
279 teq result, #0
280 bne retry
282 return newValue != 0;
285 inline bool QBasicAtomicInt::deref()
287 register int newValue;
288 register int result;
289 retry:
290 __asm {
291 ldrex newValue, [&_q_value]
292 sub newValue, newValue, #1
293 strex result, newValue, [&_q_value]
294 teq result, #0
295 bne retry
297 return newValue != 0;
300 inline bool QBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
302 register int result;
303 retry:
304 __asm {
305 ldrex result, [&_q_value]
306 eors result, result, expectedValue
307 strexeq result, newValue, [&_q_value]
308 teqeq result, #1
309 beq retry
311 return result == 0;
314 inline int QBasicAtomicInt::fetchAndStoreOrdered(int newValue)
316 register int originalValue;
317 register int result;
318 retry:
319 __asm {
320 ldrex originalValue, [&_q_value]
321 strex result, newValue, [&_q_value]
322 teq result, #0
323 bne retry
325 return originalValue;
328 inline int QBasicAtomicInt::fetchAndAddOrdered(int valueToAdd)
330 register int originalValue;
331 register int newValue;
332 register int result;
333 retry:
334 __asm {
335 ldrex originalValue, [&_q_value]
336 add newValue, originalValue, valueToAdd
337 strex result, newValue, [&_q_value]
338 teq result, #0
339 bne retry
341 return originalValue;
344 template <typename T>
345 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
347 register T *result;
348 retry:
349 __asm {
350 ldrex result, [&_q_value]
351 eors result, result, expectedValue
352 strexeq result, newValue, [&_q_value]
353 teqeq result, #1
354 beq retry
356 return result == 0;
359 template <typename T>
360 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreOrdered(T *newValue)
362 register T *originalValue;
363 register int result;
364 retry:
365 __asm {
366 ldrex originalValue, [&_q_value]
367 strex result, newValue, [&_q_value]
368 teq result, #0
369 bne retry
371 return originalValue;
374 template <typename T>
375 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
377 register T *originalValue;
378 register T *newValue;
379 register int result;
380 retry:
381 __asm {
382 ldrex originalValue, [&_q_value]
383 add newValue, originalValue, valueToAdd * sizeof(T)
384 strex result, newValue, [&_q_value]
385 teq result, #0
386 bne retry
388 return originalValue;
391 // go back to the previous pragma state (probably Thumb mode)
392 #pragma pop
393 #endif
395 // common code
397 inline bool QBasicAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
399 return testAndSetOrdered(expectedValue, newValue);
402 inline bool QBasicAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
404 return testAndSetOrdered(expectedValue, newValue);
407 inline bool QBasicAtomicInt::testAndSetRelease(int expectedValue, int newValue)
409 return testAndSetOrdered(expectedValue, newValue);
412 inline int QBasicAtomicInt::fetchAndStoreRelaxed(int newValue)
414 return fetchAndStoreOrdered(newValue);
417 inline int QBasicAtomicInt::fetchAndStoreAcquire(int newValue)
419 return fetchAndStoreOrdered(newValue);
422 inline int QBasicAtomicInt::fetchAndStoreRelease(int newValue)
424 return fetchAndStoreOrdered(newValue);
427 inline int QBasicAtomicInt::fetchAndAddRelaxed(int valueToAdd)
429 return fetchAndAddOrdered(valueToAdd);
432 inline int QBasicAtomicInt::fetchAndAddAcquire(int valueToAdd)
434 return fetchAndAddOrdered(valueToAdd);
437 inline int QBasicAtomicInt::fetchAndAddRelease(int valueToAdd)
439 return fetchAndAddOrdered(valueToAdd);
442 template <typename T>
443 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
445 return testAndSetOrdered(expectedValue, newValue);
448 template <typename T>
449 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
451 return testAndSetOrdered(expectedValue, newValue);
454 template <typename T>
455 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
457 return testAndSetOrdered(expectedValue, newValue);
460 template <typename T>
461 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelaxed(T *newValue)
463 return fetchAndStoreOrdered(newValue);
466 template <typename T>
467 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreAcquire(T *newValue)
469 return fetchAndStoreOrdered(newValue);
472 template <typename T>
473 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelease(T *newValue)
475 return fetchAndStoreOrdered(newValue);
478 template <typename T>
479 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelaxed(qptrdiff valueToAdd)
481 return fetchAndAddOrdered(valueToAdd);
484 template <typename T>
485 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddAcquire(qptrdiff valueToAdd)
487 return fetchAndAddOrdered(valueToAdd);
490 template <typename T>
491 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelease(qptrdiff valueToAdd)
493 return fetchAndAddOrdered(valueToAdd);
496 QT_END_NAMESPACE
498 QT_END_HEADER
500 #endif // QATOMIC_ARMV6_H