qatomic.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_H
00043 #define QATOMIC_H
00044 
00045 #include <QtCore/qglobal.h>
00046 #include <QtCore/qbasicatomic.h>
00047 
00048 QT_BEGIN_HEADER
00049 
00050 QT_BEGIN_NAMESPACE
00051 
00052 QT_MODULE(Core)
00053 
00054 // High-level atomic integer operations
00055 class Q_CORE_EXPORT QAtomicInt : public QBasicAtomicInt
00056 {
00057 public:
00058     inline QAtomicInt(int value = 0)
00059     {
00060 #ifdef QT_ARCH_PARISC
00061         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
00062 #endif
00063         _q_value = value;
00064     }
00065     inline QAtomicInt(const QAtomicInt &other)
00066     {
00067 #ifdef QT_ARCH_PARISC
00068         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
00069 #endif
00070         _q_value = other._q_value;
00071     }
00072 
00073     inline QAtomicInt &operator=(int value)
00074     {
00075         (void) QBasicAtomicInt::operator=(value);
00076         return *this;
00077     }
00078 
00079     inline QAtomicInt &operator=(const QAtomicInt &other)
00080     {
00081         (void) QBasicAtomicInt::operator=(other);
00082         return *this;
00083     }
00084 
00085 #ifdef qdoc
00086     bool operator==(int value) const;
00087     bool operator!=(int value) const;
00088     bool operator!() const;
00089     operator int() const;
00090 
00091     static bool isReferenceCountingNative();
00092     static bool isReferenceCountingWaitFree();
00093 
00094     bool ref();
00095     bool deref();
00096 
00097     static bool isTestAndSetNative();
00098     static bool isTestAndSetWaitFree();
00099 
00100     bool testAndSetRelaxed(int expectedValue, int newValue);
00101     bool testAndSetAcquire(int expectedValue, int newValue);
00102     bool testAndSetRelease(int expectedValue, int newValue);
00103     bool testAndSetOrdered(int expectedValue, int newValue);
00104 
00105     static bool isFetchAndStoreNative();
00106     static bool isFetchAndStoreWaitFree();
00107 
00108     int fetchAndStoreRelaxed(int newValue);
00109     int fetchAndStoreAcquire(int newValue);
00110     int fetchAndStoreRelease(int newValue);
00111     int fetchAndStoreOrdered(int newValue);
00112 
00113     static bool isFetchAndAddNative();
00114     static bool isFetchAndAddWaitFree();
00115 
00116     int fetchAndAddRelaxed(int valueToAdd);
00117     int fetchAndAddAcquire(int valueToAdd);
00118     int fetchAndAddRelease(int valueToAdd);
00119     int fetchAndAddOrdered(int valueToAdd);
00120 #endif
00121 };
00122 
00123 // High-level atomic pointer operations
00124 template <typename T>
00125 class QAtomicPointer : public QBasicAtomicPointer<T>
00126 {
00127 public:
00128     inline QAtomicPointer(T *value = 0)
00129     {
00130 #ifdef QT_ARCH_PARISC
00131         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
00132 #endif
00133         QBasicAtomicPointer<T>::_q_value = value;
00134     }
00135     inline QAtomicPointer(const QAtomicPointer<T> &other)
00136     {
00137 #ifdef QT_ARCH_PARISC
00138         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
00139 #endif
00140         QBasicAtomicPointer<T>::_q_value = other._q_value;
00141     }
00142 
00143     inline QAtomicPointer<T> &operator=(T *value)
00144     {
00145         (void) QBasicAtomicPointer<T>::operator=(value);
00146         return *this;
00147     }
00148 
00149     inline QAtomicPointer<T> &operator=(const QAtomicPointer<T> &other)
00150     {
00151         (void) QBasicAtomicPointer<T>::operator=(other);
00152         return *this;
00153     }
00154 
00155 #ifdef qdoc
00156     bool operator==(T *value) const;
00157     bool operator!=(T *value) const;
00158     bool operator!() const;
00159     operator T *() const;
00160     T *operator->() const;
00161 
00162     static bool isTestAndSetNative();
00163     static bool isTestAndSetWaitFree();
00164 
00165     bool testAndSetRelaxed(T *expectedValue, T *newValue);
00166     bool testAndSetAcquire(T *expectedValue, T *newValue);
00167     bool testAndSetRelease(T *expectedValue, T *newValue);
00168     bool testAndSetOrdered(T *expectedValue, T *newValue);
00169 
00170     static bool isFetchAndStoreNative();
00171     static bool isFetchAndStoreWaitFree();
00172 
00173     T *fetchAndStoreRelaxed(T *newValue);
00174     T *fetchAndStoreAcquire(T *newValue);
00175     T *fetchAndStoreRelease(T *newValue);
00176     T *fetchAndStoreOrdered(T *newValue);
00177 
00178     static bool isFetchAndAddNative();
00179     static bool isFetchAndAddWaitFree();
00180 
00181     T *fetchAndAddRelaxed(qptrdiff valueToAdd);
00182     T *fetchAndAddAcquire(qptrdiff valueToAdd);
00183     T *fetchAndAddRelease(qptrdiff valueToAdd);
00184     T *fetchAndAddOrdered(qptrdiff valueToAdd);
00185 #endif
00186 };
00187 
00194 template <typename T>
00195 inline void qAtomicAssign(T *&d, T *x)
00196 {
00197     if (d == x)
00198         return;
00199     x->ref.ref();
00200     if (!d->ref.deref())
00201         delete d;
00202     d = x;
00203 }
00204 
00213 template <typename T>
00214 inline void qAtomicDetach(T *&d)
00215 {
00216     if (d->ref == 1)
00217         return;
00218     T *x = d;
00219     d = new T(*d);
00220     if (!x->ref.deref())
00221         delete x;
00222 }
00223 
00224 QT_END_NAMESPACE
00225 QT_END_HEADER
00226 
00227 #endif // QATOMIC_H