qatomic_x86_64.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QATOMIC_X86_64_H
00043 #define QATOMIC_X86_64_H
00044 
00045 QT_BEGIN_HEADER
00046 
00047 QT_BEGIN_NAMESPACE
00048 
00049 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
00050 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
00051 
00052 inline bool QBasicAtomicInt::isReferenceCountingNative()
00053 { return true; }
00054 inline bool QBasicAtomicInt::isReferenceCountingWaitFree()
00055 { return true; }
00056 
00057 #define Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
00058 #define Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
00059 
00060 inline bool QBasicAtomicInt::isTestAndSetNative()
00061 { return true; }
00062 inline bool QBasicAtomicInt::isTestAndSetWaitFree()
00063 { return true; }
00064 
00065 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
00066 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
00067 
00068 inline bool QBasicAtomicInt::isFetchAndStoreNative()
00069 { return true; }
00070 inline bool QBasicAtomicInt::isFetchAndStoreWaitFree()
00071 { return true; }
00072 
00073 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
00074 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
00075 
00076 inline bool QBasicAtomicInt::isFetchAndAddNative()
00077 { return true; }
00078 inline bool QBasicAtomicInt::isFetchAndAddWaitFree()
00079 { return true; }
00080 
00081 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
00082 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
00083 
00084 template <typename T>
00085 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetNative()
00086 { return true; }
00087 template <typename T>
00088 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetWaitFree()
00089 { return true; }
00090 
00091 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
00092 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
00093 
00094 template <typename T>
00095 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreNative()
00096 { return true; }
00097 template <typename T>
00098 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreWaitFree()
00099 { return true; }
00100 
00101 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
00102 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
00103 
00104 template <typename T>
00105 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddNative()
00106 { return true; }
00107 template <typename T>
00108 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddWaitFree()
00109 { return true; }
00110 
00111 #if defined(Q_CC_GNU) || defined(Q_CC_INTEL)
00112 
00113 inline bool QBasicAtomicInt::ref()
00114 {
00115     unsigned char ret;
00116     asm volatile("lock\n"
00117                  "incl %0\n"
00118                  "setne %1"
00119                  : "=m" (_q_value), "=qm" (ret)
00120                  : "m" (_q_value)
00121                  : "memory");
00122     return ret != 0;
00123 }
00124 
00125 inline bool QBasicAtomicInt::deref()
00126 {
00127     unsigned char ret;
00128     asm volatile("lock\n"
00129                  "decl %0\n"
00130                  "setne %1"
00131                  : "=m" (_q_value), "=qm" (ret)
00132                  : "m" (_q_value)
00133                  : "memory");
00134     return ret != 0;
00135 }
00136 
00137 inline bool QBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
00138 {
00139     unsigned char ret;
00140     asm volatile("lock\n"
00141                  "cmpxchgl %3,%2\n"
00142                  "sete %1\n"
00143                  : "=a" (newValue), "=qm" (ret), "+m" (_q_value)
00144                  : "r" (newValue), "0" (expectedValue)
00145                  : "memory");
00146     return ret != 0;
00147 }
00148 
00149 inline int QBasicAtomicInt::fetchAndStoreOrdered(int newValue)
00150 {
00151     asm volatile("xchgl %0,%1"
00152                  : "=r" (newValue), "+m" (_q_value)
00153                  : "0" (newValue)
00154                  : "memory");
00155     return newValue;
00156 }
00157 
00158 inline int QBasicAtomicInt::fetchAndAddOrdered(int valueToAdd)
00159 {
00160     asm volatile("lock\n"
00161                  "xaddl %0,%1"
00162                  : "=r" (valueToAdd), "+m" (_q_value)
00163                  : "0" (valueToAdd)
00164                  : "memory");
00165     return valueToAdd;
00166 }
00167 
00168 template <typename T>
00169 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
00170 {
00171     unsigned char ret;
00172     asm volatile("lock\n"
00173                  "cmpxchgq %3,%2\n"
00174                  "sete %1\n"
00175                  : "=a" (newValue), "=qm" (ret), "+m" (_q_value)
00176                  : "r" (newValue), "0" (expectedValue)
00177                  : "memory");
00178     return ret != 0;
00179 }
00180 
00181 template <typename T>
00182 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreOrdered(T *newValue)
00183 {
00184     asm volatile("xchgq %0,%1"
00185                  : "=r" (newValue), "+m" (_q_value)
00186                  : "0" (newValue)
00187                  : "memory");
00188     return newValue;
00189 }
00190 
00191 template <typename T>
00192 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
00193 {
00194     asm volatile("lock\n"
00195                  "xaddq %0,%1"
00196                  : "=r" (valueToAdd), "+m" (_q_value)
00197                  : "0" (valueToAdd * sizeof(T))
00198                  : "memory");
00199     return reinterpret_cast<T *>(valueToAdd);
00200 }
00201 
00202 #else // !Q_CC_INTEL && !Q_CC_GNU
00203 
00204 extern "C" {
00205     Q_CORE_EXPORT int q_atomic_test_and_set_int(volatile int *ptr, int expected, int newval);
00206     Q_CORE_EXPORT int q_atomic_test_and_set_ptr(volatile void *ptr, void *expected, void *newval);
00207     Q_CORE_EXPORT int q_atomic_increment(volatile int *ptr);
00208     Q_CORE_EXPORT int q_atomic_decrement(volatile int *ptr);
00209     Q_CORE_EXPORT int q_atomic_set_int(volatile int *ptr, int newval);
00210     Q_CORE_EXPORT void *q_atomic_set_ptr(volatile void *ptr, void *newval);
00211     Q_CORE_EXPORT int q_atomic_fetch_and_add_int(volatile int *ptr, int value);
00212     Q_CORE_EXPORT void *q_atomic_fetch_and_add_ptr(volatile void *ptr, qptrdiff value);
00213 } // extern "C"
00214 
00215 inline bool QBasicAtomicInt::ref()
00216 {
00217     return q_atomic_increment(&_q_value) != 0;
00218 }
00219 
00220 inline bool QBasicAtomicInt::deref()
00221 {
00222     return q_atomic_decrement(&_q_value) != 0;
00223 }
00224 
00225 inline bool QBasicAtomicInt::testAndSetOrdered(int expected, int newval)
00226 {
00227     return q_atomic_test_and_set_int(&_q_value, expected, newval) != 0;
00228 }
00229 
00230 inline int QBasicAtomicInt::fetchAndStoreOrdered(int newval)
00231 {
00232     return q_atomic_set_int(&_q_value, newval);
00233 }
00234 
00235 inline int QBasicAtomicInt::fetchAndAddOrdered(int aValue)
00236 {
00237     return q_atomic_fetch_and_add_int(&_q_value, aValue);
00238 }
00239 
00240 template <typename T>
00241 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
00242 {
00243     return q_atomic_test_and_set_ptr(&_q_value, expectedValue, newValue);
00244 }
00245 
00246 template <typename T>
00247 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreOrdered(T *newValue)
00248 {
00249     return reinterpret_cast<T *>(q_atomic_set_ptr(&_q_value, newValue));
00250 }
00251 
00252 template <typename T>
00253 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
00254 {
00255     return reinterpret_cast<T *>(q_atomic_fetch_and_add_ptr(&_q_value, valueToAdd * sizeof(T)));
00256 }
00257 
00258 #endif // Q_CC_GNU || Q_CC_INTEL
00259 
00260 inline bool QBasicAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
00261 {
00262     return testAndSetOrdered(expectedValue, newValue);
00263 }
00264 
00265 inline bool QBasicAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
00266 {
00267     return testAndSetOrdered(expectedValue, newValue);
00268 }
00269 
00270 inline bool QBasicAtomicInt::testAndSetRelease(int expectedValue, int newValue)
00271 {
00272     return testAndSetOrdered(expectedValue, newValue);
00273 }
00274 
00275 inline int QBasicAtomicInt::fetchAndStoreRelaxed(int newValue)
00276 {
00277     return fetchAndStoreOrdered(newValue);
00278 }
00279 
00280 inline int QBasicAtomicInt::fetchAndStoreAcquire(int newValue)
00281 {
00282     return fetchAndStoreOrdered(newValue);
00283 }
00284 
00285 inline int QBasicAtomicInt::fetchAndStoreRelease(int newValue)
00286 {
00287     return fetchAndStoreOrdered(newValue);
00288 }
00289 
00290 inline int QBasicAtomicInt::fetchAndAddRelaxed(int valueToAdd)
00291 {
00292     return fetchAndAddOrdered(valueToAdd);
00293 }
00294 
00295 inline int QBasicAtomicInt::fetchAndAddAcquire(int valueToAdd)
00296 {
00297     return fetchAndAddOrdered(valueToAdd);
00298 }
00299 
00300 inline int QBasicAtomicInt::fetchAndAddRelease(int valueToAdd)
00301 {
00302     return fetchAndAddOrdered(valueToAdd);
00303 }
00304 
00305 template <typename T>
00306 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
00307 {
00308     return testAndSetOrdered(expectedValue, newValue);
00309 }
00310 
00311 template <typename T>
00312 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
00313 {
00314     return testAndSetOrdered(expectedValue, newValue);
00315 }
00316 
00317 template <typename T>
00318 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
00319 {
00320     return testAndSetOrdered(expectedValue, newValue);
00321 }
00322 
00323 template <typename T>
00324 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelaxed(T *newValue)
00325 {
00326     return fetchAndStoreOrdered(newValue);
00327 }
00328 
00329 template <typename T>
00330 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreAcquire(T *newValue)
00331 {
00332     return fetchAndStoreOrdered(newValue);
00333 }
00334 
00335 template <typename T>
00336 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelease(T *newValue)
00337 {
00338     return fetchAndStoreOrdered(newValue);
00339 }
00340 
00341 template <typename T>
00342 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelaxed(qptrdiff valueToAdd)
00343 {
00344     return fetchAndAddOrdered(valueToAdd);
00345 }
00346 
00347 template <typename T>
00348 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddAcquire(qptrdiff valueToAdd)
00349 {
00350     return fetchAndAddOrdered(valueToAdd);
00351 }
00352 
00353 template <typename T>
00354 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelease(qptrdiff valueToAdd)
00355 {
00356     return fetchAndAddOrdered(valueToAdd);
00357 }
00358 
00359 QT_END_NAMESPACE
00360 
00361 QT_END_HEADER
00362 
00363 #endif // QATOMIC_X86_64_H