src/corelib/thread/qbasicatomic.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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.
       
    15 **
       
    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.
       
    23 **
       
    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.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QBASICATOMIC_H
       
    43 #define QBASICATOMIC_H
       
    44 
       
    45 #include <QtCore/qglobal.h>
       
    46 
       
    47 QT_BEGIN_HEADER
       
    48 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 QT_MODULE(Core)
       
    52 
       
    53 class Q_CORE_EXPORT QBasicAtomicInt
       
    54 {
       
    55 public:
       
    56 #ifdef QT_ARCH_PARISC
       
    57     int _q_lock[4];
       
    58 #endif
       
    59 #if defined(QT_ARCH_WINDOWS) || defined(QT_ARCH_WINDOWSCE)
       
    60     union { // needed for Q_BASIC_ATOMIC_INITIALIZER
       
    61         volatile long _q_value;
       
    62     };
       
    63 #else
       
    64     volatile int _q_value;
       
    65 #endif
       
    66 
       
    67     // Non-atomic API
       
    68     inline bool operator==(int value) const
       
    69     {
       
    70         return _q_value == value;
       
    71     }
       
    72 
       
    73     inline bool operator!=(int value) const
       
    74     {
       
    75         return _q_value != value;
       
    76     }
       
    77 
       
    78     inline bool operator!() const
       
    79     {
       
    80         return _q_value == 0;
       
    81     }
       
    82 
       
    83     inline operator int() const
       
    84     {
       
    85         return _q_value;
       
    86     }
       
    87 
       
    88     inline QBasicAtomicInt &operator=(int value)
       
    89     {
       
    90 #ifdef QT_ARCH_PARISC
       
    91         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
       
    92 #endif
       
    93         _q_value = value;
       
    94         return *this;
       
    95     }
       
    96 
       
    97     // Atomic API, implemented in qatomic_XXX.h
       
    98 
       
    99     static bool isReferenceCountingNative();
       
   100     static bool isReferenceCountingWaitFree();
       
   101 
       
   102     bool ref();
       
   103     bool deref();
       
   104 
       
   105     static bool isTestAndSetNative();
       
   106     static bool isTestAndSetWaitFree();
       
   107 
       
   108     bool testAndSetRelaxed(int expectedValue, int newValue);
       
   109     bool testAndSetAcquire(int expectedValue, int newValue);
       
   110     bool testAndSetRelease(int expectedValue, int newValue);
       
   111     bool testAndSetOrdered(int expectedValue, int newValue);
       
   112 
       
   113     static bool isFetchAndStoreNative();
       
   114     static bool isFetchAndStoreWaitFree();
       
   115 
       
   116     int fetchAndStoreRelaxed(int newValue);
       
   117     int fetchAndStoreAcquire(int newValue);
       
   118     int fetchAndStoreRelease(int newValue);
       
   119     int fetchAndStoreOrdered(int newValue);
       
   120 
       
   121     static bool isFetchAndAddNative();
       
   122     static bool isFetchAndAddWaitFree();
       
   123 
       
   124     int fetchAndAddRelaxed(int valueToAdd);
       
   125     int fetchAndAddAcquire(int valueToAdd);
       
   126     int fetchAndAddRelease(int valueToAdd);
       
   127     int fetchAndAddOrdered(int valueToAdd);
       
   128 };
       
   129 
       
   130 template <typename T>
       
   131 class QBasicAtomicPointer
       
   132 {
       
   133 public:
       
   134 #ifdef QT_ARCH_PARISC
       
   135     int _q_lock[4];
       
   136 #endif
       
   137 #if defined(QT_ARCH_WINDOWS) || defined(QT_ARCH_WINDOWSCE)
       
   138     union {
       
   139         T * volatile _q_value;
       
   140         long volatile _q_value_integral;
       
   141     };
       
   142 #else
       
   143     T * volatile _q_value;
       
   144 #endif
       
   145 
       
   146     // Non-atomic API
       
   147     inline bool operator==(T *value) const
       
   148     {
       
   149         return _q_value == value;
       
   150     }
       
   151 
       
   152     inline bool operator!=(T *value) const
       
   153     {
       
   154         return !operator==(value);
       
   155     }
       
   156 
       
   157     inline bool operator!() const
       
   158     {
       
   159         return operator==(0);
       
   160     }
       
   161 
       
   162     inline operator T *() const
       
   163     {
       
   164         return _q_value;
       
   165     }
       
   166 
       
   167     inline T *operator->() const
       
   168     {
       
   169         return _q_value;
       
   170     }
       
   171 
       
   172     inline QBasicAtomicPointer<T> &operator=(T *value)
       
   173     {
       
   174 #ifdef QT_ARCH_PARISC
       
   175         this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
       
   176 #endif
       
   177         _q_value = value;
       
   178         return *this;
       
   179     }
       
   180 
       
   181     // Atomic API, implemented in qatomic_XXX.h
       
   182 
       
   183     static bool isTestAndSetNative();
       
   184     static bool isTestAndSetWaitFree();
       
   185 
       
   186     bool testAndSetRelaxed(T *expectedValue, T *newValue);
       
   187     bool testAndSetAcquire(T *expectedValue, T *newValue);
       
   188     bool testAndSetRelease(T *expectedValue, T *newValue);
       
   189     bool testAndSetOrdered(T *expectedValue, T *newValue);
       
   190 
       
   191     static bool isFetchAndStoreNative();
       
   192     static bool isFetchAndStoreWaitFree();
       
   193 
       
   194     T *fetchAndStoreRelaxed(T *newValue);
       
   195     T *fetchAndStoreAcquire(T *newValue);
       
   196     T *fetchAndStoreRelease(T *newValue);
       
   197     T *fetchAndStoreOrdered(T *newValue);
       
   198 
       
   199     static bool isFetchAndAddNative();
       
   200     static bool isFetchAndAddWaitFree();
       
   201 
       
   202     T *fetchAndAddRelaxed(qptrdiff valueToAdd);
       
   203     T *fetchAndAddAcquire(qptrdiff valueToAdd);
       
   204     T *fetchAndAddRelease(qptrdiff valueToAdd);
       
   205     T *fetchAndAddOrdered(qptrdiff valueToAdd);
       
   206 };
       
   207 
       
   208 #ifdef QT_ARCH_PARISC
       
   209 #  define Q_BASIC_ATOMIC_INITIALIZER(a) {{-1,-1,-1,-1},(a)}
       
   210 #elif defined(QT_ARCH_WINDOWS) || defined(QT_ARCH_WINDOWSCE)
       
   211 #  define Q_BASIC_ATOMIC_INITIALIZER(a) { {(a)} }
       
   212 #else
       
   213 #  define Q_BASIC_ATOMIC_INITIALIZER(a) { (a) }
       
   214 #endif
       
   215 
       
   216 QT_END_NAMESPACE
       
   217 QT_END_HEADER
       
   218 
       
   219 #if defined(QT_MOC) || defined(QT_BUILD_QMAKE) || defined(QT_RCC) || defined(QT_UIC) || defined(QT_BOOTSTRAPPED)
       
   220 #  include <QtCore/qatomic_bootstrap.h>
       
   221 #else
       
   222 #  include <QtCore/qatomic_arch.h>
       
   223 #endif
       
   224 
       
   225 #endif // QBASIC_ATOMIC