Qt 4.8
qatomic_armv5.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QATOMIC_ARMV5_H
43 #define QATOMIC_ARMV5_H
44 
46 
48 
49 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE
50 
52 { return false; }
54 { return false; }
55 
56 #define Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE
57 
59 { return false; }
61 { return false; }
62 
63 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
64 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
65 
67 { return true; }
69 { return true; }
70 
71 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE
72 
74 { return false; }
76 { return false; }
77 
78 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_NOT_NATIVE
79 
80 template <typename T>
82 { return false; }
83 template <typename T>
85 { return false; }
86 
87 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
88 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
89 
90 template <typename T>
92 { return true; }
93 template <typename T>
95 { return true; }
96 
97 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_NOT_NATIVE
98 
99 template <typename T>
101 { return false; }
102 template <typename T>
104 { return false; }
105 
106 #ifndef QT_NO_ARM_EABI
107 
108 // kernel places a restartable cmpxchg implementation at a fixed address
109 extern "C" typedef int (qt_atomic_eabi_cmpxchg_int_t)(int oldval, int newval, volatile int *ptr);
110 extern "C" typedef int (qt_atomic_eabi_cmpxchg_ptr_t)(const void *oldval, const void *newval, volatile void *ptr);
111 #define qt_atomic_eabi_cmpxchg_int (*reinterpret_cast<qt_atomic_eabi_cmpxchg_int_t *>(0xffff0fc0))
112 #define qt_atomic_eabi_cmpxchg_ptr (*reinterpret_cast<qt_atomic_eabi_cmpxchg_ptr_t *>(0xffff0fc0))
113 
114 #else
115 
116 extern Q_CORE_EXPORT char q_atomic_lock;
117 Q_CORE_EXPORT void qt_atomic_yield(int *);
118 
119 #ifdef Q_CC_RVCT
120 
121 Q_CORE_EXPORT __asm char q_atomic_swp(volatile char *ptr, char newval);
122 
123 #else
124 
125 inline char q_atomic_swp(volatile char *ptr, char newval)
126 {
127  register char ret;
128  asm volatile("swpb %0,%2,[%3]"
129  : "=&r"(ret), "=m" (*ptr)
130  : "r"(newval), "r"(ptr)
131  : "cc", "memory");
132  return ret;
133 }
134 
135 #endif // Q_CC_RVCT
136 
137 #endif // QT_NO_ARM_EABI
138 
139 // Reference counting
140 
141 inline bool QBasicAtomicInt::ref()
142 {
143 #ifndef QT_NO_ARM_EABI
144  register int originalValue;
145  register int newValue;
146  do {
147  originalValue = _q_value;
148  newValue = originalValue + 1;
149  } while (qt_atomic_eabi_cmpxchg_int(originalValue, newValue, &_q_value) != 0);
150  return newValue != 0;
151 #else
152  int count = 0;
153  while (q_atomic_swp(&q_atomic_lock, ~0) != 0)
154  qt_atomic_yield(&count);
155  int originalValue = _q_value++;
156  q_atomic_swp(&q_atomic_lock, 0);
157  return originalValue != -1;
158 #endif
159 }
160 
161 inline bool QBasicAtomicInt::deref()
162 {
163 #ifndef QT_NO_ARM_EABI
164  register int originalValue;
165  register int newValue;
166  do {
167  originalValue = _q_value;
168  newValue = originalValue - 1;
169  } while (qt_atomic_eabi_cmpxchg_int(originalValue, newValue, &_q_value) != 0);
170  return newValue != 0;
171 #else
172  int count = 0;
173  while (q_atomic_swp(&q_atomic_lock, ~0) != 0)
174  qt_atomic_yield(&count);
175  int originalValue = _q_value--;
176  q_atomic_swp(&q_atomic_lock, 0);
177  return originalValue != 1;
178 #endif
179 }
180 
181 // Test and set for integers
182 
183 inline bool QBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
184 {
185 #ifndef QT_NO_ARM_EABI
186  register int originalValue;
187  do {
188  originalValue = _q_value;
189  if (originalValue != expectedValue)
190  return false;
191  } while (qt_atomic_eabi_cmpxchg_int(expectedValue, newValue, &_q_value) != 0);
192  return true;
193 #else
194  bool returnValue = false;
195  int count = 0;
196  while (q_atomic_swp(&q_atomic_lock, ~0) != 0)
197  qt_atomic_yield(&count);
198  if (_q_value == expectedValue) {
199  _q_value = newValue;
200  returnValue = true;
201  }
202  q_atomic_swp(&q_atomic_lock, 0);
203  return returnValue;
204 #endif
205 }
206 
207 inline bool QBasicAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
208 {
209  return testAndSetOrdered(expectedValue, newValue);
210 }
211 
212 inline bool QBasicAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
213 {
214  return testAndSetOrdered(expectedValue, newValue);
215 }
216 
217 inline bool QBasicAtomicInt::testAndSetRelease(int expectedValue, int newValue)
218 {
219  return testAndSetOrdered(expectedValue, newValue);
220 }
221 
222 // Fetch and store for integers
223 
224 #ifndef Q_CC_RVCT
225 
226 inline int QBasicAtomicInt::fetchAndStoreOrdered(int newValue)
227 {
228  int originalValue;
229 #ifndef QT_NO_ARM_EABI
230  asm volatile("swp %0,%2,[%3]"
231  : "=&r"(originalValue), "=m" (_q_value)
232  : "r"(newValue), "r"(&_q_value)
233  : "cc", "memory");
234 #else
235  int count = 0;
236  while (q_atomic_swp(&q_atomic_lock, ~0) != 0)
237  qt_atomic_yield(&count);
238  originalValue=_q_value;
239  _q_value = newValue;
240  q_atomic_swp(&q_atomic_lock, 0);
241 #endif
242  return originalValue;
243 }
244 
245 #endif
246 
247 inline int QBasicAtomicInt::fetchAndStoreRelaxed(int newValue)
248 {
249  return fetchAndStoreOrdered(newValue);
250 }
251 
252 inline int QBasicAtomicInt::fetchAndStoreAcquire(int newValue)
253 {
254  return fetchAndStoreOrdered(newValue);
255 }
256 
257 inline int QBasicAtomicInt::fetchAndStoreRelease(int newValue)
258 {
259  return fetchAndStoreOrdered(newValue);
260 }
261 
262 // Fetch and add for integers
263 
264 inline int QBasicAtomicInt::fetchAndAddOrdered(int valueToAdd)
265 {
266 #ifndef QT_NO_ARM_EABI
267  register int originalValue;
268  register int newValue;
269  do {
270  originalValue = _q_value;
271  newValue = originalValue + valueToAdd;
272  } while (qt_atomic_eabi_cmpxchg_int(originalValue, newValue, &_q_value) != 0);
273  return originalValue;
274 #else
275  int count = 0;
276  while (q_atomic_swp(&q_atomic_lock, ~0) != 0)
277  qt_atomic_yield(&count);
278  int originalValue = _q_value;
279  _q_value += valueToAdd;
280  q_atomic_swp(&q_atomic_lock, 0);
281  return originalValue;
282 #endif
283 }
284 
285 inline int QBasicAtomicInt::fetchAndAddRelaxed(int valueToAdd)
286 {
287  return fetchAndAddOrdered(valueToAdd);
288 }
289 
290 inline int QBasicAtomicInt::fetchAndAddAcquire(int valueToAdd)
291 {
292  return fetchAndAddOrdered(valueToAdd);
293 }
294 
295 inline int QBasicAtomicInt::fetchAndAddRelease(int valueToAdd)
296 {
297  return fetchAndAddOrdered(valueToAdd);
298 }
299 
300 // Test and set for pointers
301 
302 template <typename T>
303 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
304 {
305 #ifndef QT_NO_ARM_EABI
306  register T *originalValue;
307  do {
308  originalValue = _q_value;
309  if (originalValue != expectedValue)
310  return false;
311  } while (qt_atomic_eabi_cmpxchg_ptr(expectedValue, newValue, &_q_value) != 0);
312  return true;
313 #else
314  bool returnValue = false;
315  int count = 0;
316  while (q_atomic_swp(&q_atomic_lock, ~0) != 0)
317  qt_atomic_yield(&count);
318  if (_q_value == expectedValue) {
319  _q_value = newValue;
320  returnValue = true;
321  }
322  q_atomic_swp(&q_atomic_lock, 0);
323  return returnValue;
324 #endif
325 }
326 
327 template <typename T>
328 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
329 {
330  return testAndSetOrdered(expectedValue, newValue);
331 }
332 
333 template <typename T>
334 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
335 {
336  return testAndSetOrdered(expectedValue, newValue);
337 }
338 
339 template <typename T>
340 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
341 {
342  return testAndSetOrdered(expectedValue, newValue);
343 }
344 
345 // Fetch and store for pointers
346 
347 #ifdef Q_CC_RVCT
348 
349 template <typename T>
351 {
352  add r2, pc, #0
353  bx r2
354  arm
355  swp r2,r1,[r0]
356  mov r0, r2
357  bx lr
358  thumb
359 }
360 
361 #else
362 
363 template <typename T>
365 {
366  T *originalValue;
367 #ifndef QT_NO_ARM_EABI
368  asm volatile("swp %0,%2,[%3]"
369  : "=&r"(originalValue), "=m" (_q_value)
370  : "r"(newValue), "r"(&_q_value)
371  : "cc", "memory");
372 #else
373  int count = 0;
374  while (q_atomic_swp(&q_atomic_lock, ~0) != 0)
375  qt_atomic_yield(&count);
376  originalValue=_q_value;
377  _q_value = newValue;
378  q_atomic_swp(&q_atomic_lock, 0);
379 #endif
380  return originalValue;
381 }
382 
383 #endif // Q_CC_RVCT
384 
385 template <typename T>
387 {
388  return fetchAndStoreOrdered(newValue);
389 }
390 
391 template <typename T>
393 {
394  return fetchAndStoreOrdered(newValue);
395 }
396 
397 template <typename T>
399 {
400  return fetchAndStoreOrdered(newValue);
401 }
402 
403 // Fetch and add for pointers
404 
405 template <typename T>
407 {
408 #ifndef QT_NO_ARM_EABI
409  register T *originalValue;
410  register T *newValue;
411  do {
412  originalValue = _q_value;
413  newValue = originalValue + valueToAdd;
414  } while (qt_atomic_eabi_cmpxchg_ptr(originalValue, newValue, &_q_value) != 0);
415  return originalValue;
416 #else
417  int count = 0;
418  while (q_atomic_swp(&q_atomic_lock, ~0) != 0)
419  qt_atomic_yield(&count);
420  T *originalValue = (_q_value);
421  _q_value += valueToAdd;
422  q_atomic_swp(&q_atomic_lock, 0);
423  return originalValue;
424 #endif
425 }
426 
427 template <typename T>
429 {
430  return fetchAndAddOrdered(valueToAdd);
431 }
432 
433 template <typename T>
435 {
436  return fetchAndAddOrdered(valueToAdd);
437 }
438 
439 template <typename T>
441 {
442  return fetchAndAddOrdered(valueToAdd);
443 }
444 
446 
448 
449 #endif // QATOMIC_ARMV5_H
static bool isFetchAndStoreNative()
Definition: qatomic_alpha.h:65
#define qt_atomic_eabi_cmpxchg_int
static bool isTestAndSetNative()
Definition: qatomic_alpha.h:58
int fetchAndStoreRelease(int newValue)
static bool isReferenceCountingNative()
Definition: qatomic_alpha.h:51
T * fetchAndAddRelaxed(qptrdiff valueToAdd)
volatile int _q_value
Definition: qbasicatomic.h:64
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static bool isFetchAndAddNative()
Definition: qatomic_alpha.h:98
#define add(aName)
int() qt_atomic_eabi_cmpxchg_ptr_t(const void *oldval, const void *newval, volatile void *ptr)
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
static bool isTestAndSetNative()
Definition: qatomic_alpha.h:80
Q_CORE_EXPORT void qt_atomic_yield(int *count)
Definition: qatomic_arm.cpp:56
static bool isFetchAndAddNative()
Definition: qatomic_alpha.h:72
T * fetchAndStoreRelease(T *newValue)
static bool isTestAndSetWaitFree()
Definition: qatomic_alpha.h:83
int() qt_atomic_eabi_cmpxchg_int_t(int oldval, int newval, volatile int *ptr)
T * fetchAndAddRelease(qptrdiff valueToAdd)
bool testAndSetOrdered(T *expectedValue, T *newValue)
int fetchAndAddAcquire(int valueToAdd)
T * fetchAndAddAcquire(qptrdiff valueToAdd)
int fetchAndStoreRelaxed(int newValue)
T * fetchAndAddOrdered(qptrdiff valueToAdd)
bool testAndSetAcquire(int expectedValue, int newValue)
bool testAndSetRelaxed(int expectedValue, int newValue)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int fetchAndStoreAcquire(int newValue)
QIntegerForSizeof< void * >::Signed qptrdiff
Definition: qglobal.h:987
T * fetchAndStoreOrdered(T *newValue)
#define Q_INLINE_TEMPLATE
Definition: qglobal.h:1713
int fetchAndAddRelease(int valueToAdd)
const T * ptr(const T &t)
bool testAndSetOrdered(int expectedValue, int newValue)
T * fetchAndStoreAcquire(T *newValue)
static bool isFetchAndStoreWaitFree()
Definition: qatomic_alpha.h:67
static bool isTestAndSetWaitFree()
Definition: qatomic_alpha.h:60
static bool isReferenceCountingWaitFree()
Definition: qatomic_alpha.h:53
int fetchAndAddOrdered(int valueToAdd)
static bool isFetchAndStoreWaitFree()
Definition: qatomic_alpha.h:92
static bool isFetchAndAddWaitFree()
Definition: qatomic_alpha.h:74
static bool isFetchAndAddWaitFree()
bool testAndSetRelease(int expectedValue, int newValue)
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
int fetchAndAddRelaxed(int valueToAdd)
int fetchAndStoreOrdered(int newValue)
bool testAndSetRelaxed(T *expectedValue, T *newValue)
bool testAndSetRelease(T *expectedValue, T *newValue)
Q_CORE_EXPORT char q_atomic_lock
Definition: qatomic_arm.cpp:54
#define QT_END_HEADER
Definition: qglobal.h:137
T * fetchAndStoreRelaxed(T *newValue)
#define qt_atomic_eabi_cmpxchg_ptr
static bool isFetchAndStoreNative()
Definition: qatomic_alpha.h:89
bool testAndSetAcquire(T *expectedValue, T *newValue)