1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Copyright (C) 2011 Thiago Macieira <thiago@kde.org>
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
41 ****************************************************************************/
43 #ifndef QATOMIC_X86_64_H
44 #define QATOMIC_X86_64_H
46 #include <QtCore/qgenericatomic.h>
53 // silence syncqt warnings
57 #pragma qt_sync_stop_processing
60 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
61 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
63 #define Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
64 #define Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
66 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
67 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
69 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
70 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
72 #define Q_ATOMIC_INT32_IS_SUPPORTED
74 #define Q_ATOMIC_INT32_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
75 #define Q_ATOMIC_INT32_REFERENCE_COUNTING_IS_WAIT_FREE
77 #define Q_ATOMIC_INT32_TEST_AND_SET_IS_ALWAYS_NATIVE
78 #define Q_ATOMIC_INT32_TEST_AND_SET_IS_WAIT_FREE
80 #define Q_ATOMIC_INT32_FETCH_AND_STORE_IS_ALWAYS_NATIVE
81 #define Q_ATOMIC_INT32_FETCH_AND_STORE_IS_WAIT_FREE
83 #define Q_ATOMIC_INT32_FETCH_AND_ADD_IS_ALWAYS_NATIVE
84 #define Q_ATOMIC_INT32_FETCH_AND_ADD_IS_WAIT_FREE
86 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
87 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
89 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
90 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
92 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
93 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
95 template<> struct QAtomicIntegerTraits<int> { enum { IsInteger = 1 }; };
96 template<> struct QAtomicIntegerTraits<unsigned int> { enum { IsInteger = 1 }; };
98 template <int size> struct QBasicAtomicOps: QGenericAtomicOps<QBasicAtomicOps<size> >
100 static inline bool isReferenceCountingNative() { return true; }
101 static inline bool isReferenceCountingWaitFree() { return true; }
102 template <typename T> static bool ref(T &_q_value);
103 template <typename T> static bool deref(T &_q_value);
105 static inline bool isTestAndSetNative() { return true; }
106 static inline bool isTestAndSetWaitFree() { return true; }
107 template <typename T> static bool testAndSetRelaxed(T &_q_value, T expectedValue, T newValue);
109 static inline bool isFetchAndStoreNative() { return true; }
110 static inline bool isFetchAndStoreWaitFree() { return true; }
111 template <typename T> static T fetchAndStoreRelaxed(T &_q_value, T newValue);
113 static inline bool isFetchAndAddNative() { return true; }
114 static inline bool isFetchAndAddWaitFree() { return true; }
115 template <typename T> static
116 T fetchAndAddRelaxed(T &_q_value, typename QAtomicAdditiveType<T>::AdditiveT valueToAdd);
119 template <typename T> struct QAtomicOps : QBasicAtomicOps<sizeof(T)>
124 #if defined(Q_CC_GNU) || defined(Q_CC_INTEL)
126 template<> struct QAtomicIntegerTraits<char> { enum { IsInteger = 1 }; };
127 template<> struct QAtomicIntegerTraits<signed char> { enum { IsInteger = 1 }; };
128 template<> struct QAtomicIntegerTraits<unsigned char> { enum { IsInteger = 1 }; };
129 template<> struct QAtomicIntegerTraits<short> { enum { IsInteger = 1 }; };
130 template<> struct QAtomicIntegerTraits<unsigned short> { enum { IsInteger = 1 }; };
131 template<> struct QAtomicIntegerTraits<long> { enum { IsInteger = 1 }; };
132 template<> struct QAtomicIntegerTraits<unsigned long> { enum { IsInteger = 1 }; };
133 template<> struct QAtomicIntegerTraits<long long> { enum { IsInteger = 1 }; };
134 template<> struct QAtomicIntegerTraits<unsigned long long> { enum { IsInteger = 1 }; };
136 template<> template<typename T> inline
137 bool QBasicAtomicOps<1>::ref(T &_q_value)
140 asm volatile("lock\n"
143 : "=m" (_q_value), "=qm" (ret)
149 template<> template<typename T> inline
150 bool QBasicAtomicOps<2>::ref(T &_q_value)
153 asm volatile("lock\n"
156 : "=m" (_q_value), "=qm" (ret)
162 template<> template<typename T> inline
163 bool QBasicAtomicOps<4>::ref(T &_q_value)
166 asm volatile("lock\n"
169 : "=m" (_q_value), "=qm" (ret)
175 template<> template<typename T> inline
176 bool QBasicAtomicOps<8>::ref(T &_q_value)
179 asm volatile("lock\n"
182 : "=m" (_q_value), "=qm" (ret)
188 template<> template <typename T> inline
189 bool QBasicAtomicOps<1>::deref(T &_q_value)
192 asm volatile("lock\n"
195 : "=m" (_q_value), "=qm" (ret)
201 template<> template <typename T> inline
202 bool QBasicAtomicOps<2>::deref(T &_q_value)
205 asm volatile("lock\n"
208 : "=m" (_q_value), "=qm" (ret)
213 template<> template <typename T> inline
214 bool QBasicAtomicOps<4>::deref(T &_q_value)
217 asm volatile("lock\n"
220 : "=m" (_q_value), "=qm" (ret)
226 template<> template <typename T> inline
227 bool QBasicAtomicOps<8>::deref(T &_q_value)
230 asm volatile("lock\n"
233 : "=m" (_q_value), "=qm" (ret)
239 template<int size> template <typename T> inline
240 bool QBasicAtomicOps<size>::testAndSetRelaxed(T &_q_value, T expectedValue, T newValue)
243 asm volatile("lock\n"
246 : "=a" (newValue), "=qm" (ret), "+m" (_q_value)
247 : "r" (newValue), "0" (expectedValue)
252 template<> template <typename T> inline
253 bool QBasicAtomicOps<1>::testAndSetRelaxed(T &_q_value, T expectedValue, T newValue)
256 asm volatile("lock\n"
259 : "=a" (newValue), "=qm" (ret), "+m" (_q_value)
260 : "q" (newValue), "0" (expectedValue)
265 template<int size> template <typename T> inline
266 T QBasicAtomicOps<size>::fetchAndStoreRelaxed(T &_q_value, T newValue)
268 asm volatile("xchg %0,%1"
269 : "=r" (newValue), "+m" (_q_value)
275 template<> template <typename T> inline
276 T QBasicAtomicOps<1>::fetchAndStoreRelaxed(T &_q_value, T newValue)
278 asm volatile("xchg %0,%1"
279 : "=q" (newValue), "+m" (_q_value)
285 template<int size> template <typename T> inline
286 T QBasicAtomicOps<size>::fetchAndAddRelaxed(T &_q_value, typename QAtomicAdditiveType<T>::AdditiveT valueToAdd)
289 asm volatile("lock\n"
291 : "=r" (result), "+m" (_q_value)
292 : "0" (T(valueToAdd * QAtomicAdditiveType<T>::AddScale))
297 template<> template <typename T> inline
298 T QBasicAtomicOps<1>::fetchAndAddRelaxed(T &_q_value, typename QAtomicAdditiveType<T>::AdditiveT valueToAdd)
301 asm volatile("lock\n"
303 : "=q" (result), "+m" (_q_value)
304 : "0" (T(valueToAdd * QAtomicAdditiveType<T>::AddScale))
309 #define Q_ATOMIC_INT8_IS_SUPPORTED
311 #define Q_ATOMIC_INT8_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
312 #define Q_ATOMIC_INT8_REFERENCE_COUNTING_IS_WAIT_FREE
314 #define Q_ATOMIC_INT8_TEST_AND_SET_IS_ALWAYS_NATIVE
315 #define Q_ATOMIC_INT8_TEST_AND_SET_IS_WAIT_FREE
317 #define Q_ATOMIC_INT8_FETCH_AND_STORE_IS_ALWAYS_NATIVE
318 #define Q_ATOMIC_INT8_FETCH_AND_STORE_IS_WAIT_FREE
320 #define Q_ATOMIC_INT8_FETCH_AND_ADD_IS_ALWAYS_NATIVE
321 #define Q_ATOMIC_INT8_FETCH_AND_ADD_IS_WAIT_FREE
323 #define Q_ATOMIC_INT16_IS_SUPPORTED
325 #define Q_ATOMIC_INT16_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
326 #define Q_ATOMIC_INT16_REFERENCE_COUNTING_IS_WAIT_FREE
328 #define Q_ATOMIC_INT16_TEST_AND_SET_IS_ALWAYS_NATIVE
329 #define Q_ATOMIC_INT16_TEST_AND_SET_IS_WAIT_FREE
331 #define Q_ATOMIC_INT16_FETCH_AND_STORE_IS_ALWAYS_NATIVE
332 #define Q_ATOMIC_INT16_FETCH_AND_STORE_IS_WAIT_FREE
334 #define Q_ATOMIC_INT16_FETCH_AND_ADD_IS_ALWAYS_NATIVE
335 #define Q_ATOMIC_INT16_FETCH_AND_ADD_IS_WAIT_FREE
337 #define Q_ATOMIC_INT64_IS_SUPPORTED
339 #define Q_ATOMIC_INT64_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
340 #define Q_ATOMIC_INT64_REFERENCE_COUNTING_IS_WAIT_FREE
342 #define Q_ATOMIC_INT64_TEST_AND_SET_IS_ALWAYS_NATIVE
343 #define Q_ATOMIC_INT64_TEST_AND_SET_IS_WAIT_FREE
345 #define Q_ATOMIC_INT64_FETCH_AND_STORE_IS_ALWAYS_NATIVE
346 #define Q_ATOMIC_INT64_FETCH_AND_STORE_IS_WAIT_FREE
348 #define Q_ATOMIC_INT64_FETCH_AND_ADD_IS_ALWAYS_NATIVE
349 #define Q_ATOMIC_INT64_FETCH_AND_ADD_IS_WAIT_FREE
351 #else // !Q_CC_INTEL && !Q_CC_GNU
352 # error "This compiler for x86_64 is not supported"
353 #endif // Q_CC_GNU || Q_CC_INTEL
359 #endif // QATOMIC_X86_64_H