qatomic_windows.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_WINDOWS_H
00043 #define QATOMIC_WINDOWS_H
00044 
00045 #ifndef Q_CC_MSVC
00046 
00047 // Mingw and other GCC platforms get inline assembly
00048 
00049 # ifdef __i386__
00050 #  include "QtCore/qatomic_i386.h"
00051 # else
00052 #  include "QtCore/qatomic_x86_64.h"
00053 # endif
00054 
00055 #else // Q_CC_MSVC
00056 
00058 
00059 #ifndef Q_OS_WINCE
00060 
00061 // use compiler intrinsics for all atomic functions
00062 # define QT_INTERLOCKED_PREFIX _
00063 # define QT_INTERLOCKED_PROTOTYPE __cdecl
00064 # define QT_INTERLOCKED_DECLARE_PROTOTYPES
00065 # define QT_INTERLOCKED_INTRINSIC
00066 
00067 #else // Q_OS_WINCE
00068 
00069 # if _WIN32_WCE < 0x600 && defined(_X86_)
00070 // For X86 Windows CE, include winbase.h to catch inline functions which
00071 // override the regular definitions inside of coredll.dll.
00072 // Though one could use the original version of Increment/Decrement, others are
00073 // not exported at all.
00074 #  include <winbase.h>
00075 
00076 // It's safer to remove the volatile and let the compiler add it as needed.
00077 #  define QT_INTERLOCKED_NO_VOLATILE
00078 
00079 # else // _WIN32_WCE >= 0x600 || !_X86_
00080 
00081 #  define QT_INTERLOCKED_PROTOTYPE __cdecl
00082 #  define QT_INTERLOCKED_DECLARE_PROTOTYPES
00083 
00084 #  if _WIN32_WCE >= 0x600
00085 #   if defined(_X86_)
00086 #    define QT_INTERLOCKED_PREFIX _
00087 #    define QT_INTERLOCKED_INTRINSIC
00088 #   endif
00089 #  else
00090 #   define QT_INTERLOCKED_NO_VOLATILE
00091 #  endif
00092 
00093 # endif // _WIN32_WCE >= 0x600 || !_X86_
00094 
00095 #endif // Q_OS_WINCE
00096 
00098 // Prototype declaration
00099 
00100 #define QT_INTERLOCKED_CONCAT_I(prefix, suffix) \
00101     prefix ## suffix
00102 #define QT_INTERLOCKED_CONCAT(prefix, suffix) \
00103     QT_INTERLOCKED_CONCAT_I(prefix, suffix)
00104 
00105 // MSVC intrinsics prefix function names with an underscore. Also, if platform
00106 // SDK headers have been included, the Interlocked names may be defined as
00107 // macros.
00108 // To avoid double underscores, we paste the prefix with Interlocked first and
00109 // then the remainder of the function name.
00110 #define QT_INTERLOCKED_FUNCTION(name) \
00111     QT_INTERLOCKED_CONCAT( \
00112         QT_INTERLOCKED_CONCAT(QT_INTERLOCKED_PREFIX, Interlocked), name)
00113 
00114 #ifdef QT_INTERLOCKED_NO_VOLATILE
00115 # define QT_INTERLOCKED_VOLATILE
00116 # define QT_INTERLOCKED_REMOVE_VOLATILE(a) qt_interlocked_remove_volatile(a)
00117 #else
00118 # define QT_INTERLOCKED_VOLATILE volatile
00119 # define QT_INTERLOCKED_REMOVE_VOLATILE(a) a
00120 #endif
00121 
00122 #ifndef QT_INTERLOCKED_PREFIX
00123 #define QT_INTERLOCKED_PREFIX
00124 #endif
00125 
00126 #ifndef QT_INTERLOCKED_PROTOTYPE
00127 #define QT_INTERLOCKED_PROTOTYPE
00128 #endif
00129 
00130 #ifdef QT_INTERLOCKED_DECLARE_PROTOTYPES
00131 #undef QT_INTERLOCKED_DECLARE_PROTOTYPES
00132 
00133 extern "C" {
00134 
00135     long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( Increment )(long QT_INTERLOCKED_VOLATILE *);
00136     long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( Decrement )(long QT_INTERLOCKED_VOLATILE *);
00137     long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( CompareExchange )(long QT_INTERLOCKED_VOLATILE *, long, long);
00138     long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( Exchange )(long QT_INTERLOCKED_VOLATILE *, long);
00139     long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( ExchangeAdd )(long QT_INTERLOCKED_VOLATILE *, long);
00140 
00141 # if !defined(Q_OS_WINCE) && !defined(__i386__) && !defined(_M_IX86)
00142     void * QT_INTERLOCKED_FUNCTION( CompareExchangePointer )(void * QT_INTERLOCKED_VOLATILE *, void *, void *);
00143     void * QT_INTERLOCKED_FUNCTION( ExchangePointer )(void * QT_INTERLOCKED_VOLATILE *, void *);
00144     __int64 QT_INTERLOCKED_FUNCTION( ExchangeAdd64 )(__int64 QT_INTERLOCKED_VOLATILE *, __int64);
00145 # endif
00146 
00147 }
00148 
00149 #endif // QT_INTERLOCKED_DECLARE_PROTOTYPES
00150 
00151 #undef QT_INTERLOCKED_PROTOTYPE
00152 
00154 
00155 #ifdef QT_INTERLOCKED_INTRINSIC
00156 #undef QT_INTERLOCKED_INTRINSIC
00157 
00158 # pragma intrinsic (_InterlockedIncrement)
00159 # pragma intrinsic (_InterlockedDecrement)
00160 # pragma intrinsic (_InterlockedExchange)
00161 # pragma intrinsic (_InterlockedCompareExchange)
00162 # pragma intrinsic (_InterlockedExchangeAdd)
00163 
00164 # if !defined(Q_OS_WINCE) && !defined(_M_IX86)
00165 #  pragma intrinsic (_InterlockedCompareExchangePointer)
00166 #  pragma intrinsic (_InterlockedExchangePointer)
00167 #  pragma intrinsic (_InterlockedExchangeAdd64)
00168 # endif
00169 
00170 #endif // QT_INTERLOCKED_INTRINSIC
00171 
00173 // Interlocked* replacement macros
00174 
00175 #define QT_INTERLOCKED_INCREMENT(value) \
00176     QT_INTERLOCKED_FUNCTION( Increment )( \
00177             QT_INTERLOCKED_REMOVE_VOLATILE( value ) )
00178 
00179 #define QT_INTERLOCKED_DECREMENT(value) \
00180     QT_INTERLOCKED_FUNCTION( Decrement )( \
00181             QT_INTERLOCKED_REMOVE_VOLATILE( value ) )
00182 
00183 #define QT_INTERLOCKED_COMPARE_EXCHANGE(value, newValue, expectedValue) \
00184     QT_INTERLOCKED_FUNCTION( CompareExchange )( \
00185             QT_INTERLOCKED_REMOVE_VOLATILE( value ), \
00186             newValue, \
00187             expectedValue )
00188 
00189 #define QT_INTERLOCKED_EXCHANGE(value, newValue) \
00190     QT_INTERLOCKED_FUNCTION( Exchange )( \
00191             QT_INTERLOCKED_REMOVE_VOLATILE( value ), \
00192             newValue )
00193 
00194 #define QT_INTERLOCKED_EXCHANGE_ADD(value, valueToAdd) \
00195     QT_INTERLOCKED_FUNCTION( ExchangeAdd )( \
00196             QT_INTERLOCKED_REMOVE_VOLATILE( value ), \
00197             valueToAdd )
00198 
00199 #if defined(Q_OS_WINCE) || defined(__i386__) || defined(_M_IX86)
00200 
00201 # define QT_INTERLOCKED_COMPARE_EXCHANGE_POINTER(value, newValue, expectedValue) \
00202     reinterpret_cast<void *>( \
00203         QT_INTERLOCKED_FUNCTION( CompareExchange )( \
00204                 QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
00205                 (long)( newValue ), \
00206                 (long)( expectedValue ) ))
00207 
00208 # define QT_INTERLOCKED_EXCHANGE_POINTER(value, newValue) \
00209     QT_INTERLOCKED_FUNCTION( Exchange )( \
00210             QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
00211             (quintptr)( newValue ) )
00212 
00213 # define QT_INTERLOCKED_EXCHANGE_ADD_POINTER(value, valueToAdd) \
00214     QT_INTERLOCKED_FUNCTION( ExchangeAdd )( \
00215             QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
00216             valueToAdd )
00217 
00218 #else // !defined(Q_OS_WINCE) && !defined(__i386__) && !defined(_M_IX86)
00219 
00220 # define QT_INTERLOCKED_COMPARE_EXCHANGE_POINTER(value, newValue, expectedValue) \
00221     QT_INTERLOCKED_FUNCTION( CompareExchangePointer )( \
00222             reinterpret_cast<void * QT_INTERLOCKED_VOLATILE *>( QT_INTERLOCKED_REMOVE_VOLATILE( value ) ), \
00223             newValue, \
00224             expectedValue )
00225 
00226 # define QT_INTERLOCKED_EXCHANGE_POINTER(value, newValue) \
00227     QT_INTERLOCKED_FUNCTION( ExchangePointer )( \
00228             reinterpret_cast<void * QT_INTERLOCKED_VOLATILE *>( QT_INTERLOCKED_REMOVE_VOLATILE( value ) ), \
00229             newValue )
00230 
00231 # define QT_INTERLOCKED_EXCHANGE_ADD_POINTER(value, valueToAdd) \
00232     QT_INTERLOCKED_FUNCTION( ExchangeAdd64 )( \
00233             QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
00234             valueToAdd )
00235 
00236 #endif // !defined(Q_OS_WINCE) && !defined(__i386__) && !defined(_M_IX86)
00237 
00239 
00240 QT_BEGIN_HEADER
00241 
00242 QT_BEGIN_NAMESPACE
00243 
00244 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
00245 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
00246 
00247 inline bool QBasicAtomicInt::isReferenceCountingNative()
00248 { return true; }
00249 inline bool QBasicAtomicInt::isReferenceCountingWaitFree()
00250 { return true; }
00251 
00252 #define Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
00253 #define Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
00254 
00255 inline bool QBasicAtomicInt::isTestAndSetNative()
00256 { return true; }
00257 inline bool QBasicAtomicInt::isTestAndSetWaitFree()
00258 { return true; }
00259 
00260 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
00261 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
00262 
00263 inline bool QBasicAtomicInt::isFetchAndStoreNative()
00264 { return true; }
00265 inline bool QBasicAtomicInt::isFetchAndStoreWaitFree()
00266 { return true; }
00267 
00268 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
00269 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
00270 
00271 inline bool QBasicAtomicInt::isFetchAndAddNative()
00272 { return true; }
00273 inline bool QBasicAtomicInt::isFetchAndAddWaitFree()
00274 { return true; }
00275 
00276 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
00277 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
00278 
00279 template <typename T>
00280 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetNative()
00281 { return true; }
00282 template <typename T>
00283 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetWaitFree()
00284 { return true; }
00285 
00286 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
00287 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
00288 
00289 template <typename T>
00290 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreNative()
00291 { return true; }
00292 template <typename T>
00293 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreWaitFree()
00294 { return true; }
00295 
00296 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
00297 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
00298 template <typename T>
00299 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddNative()
00300 { return true; }
00301 template <typename T>
00302 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddWaitFree()
00303 { return true; }
00304 
00306 
00307 #ifdef QT_INTERLOCKED_NO_VOLATILE
00308 template <class T>
00309 Q_INLINE_TEMPLATE T *qt_interlocked_remove_volatile(T volatile *t)
00310 {
00311     return const_cast<T *>(t);
00312 }
00313 #endif // !QT_INTERLOCKED_NO_VOLATILE
00314 
00316 
00317 inline bool QBasicAtomicInt::ref()
00318 {
00319     return QT_INTERLOCKED_INCREMENT(&_q_value) != 0;
00320 }
00321 
00322 inline bool QBasicAtomicInt::deref()
00323 {
00324     return QT_INTERLOCKED_DECREMENT(&_q_value) != 0;
00325 }
00326 
00327 inline bool QBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
00328 {
00329     return QT_INTERLOCKED_COMPARE_EXCHANGE(&_q_value, newValue, expectedValue)
00330             == expectedValue;
00331 }
00332 
00333 inline int QBasicAtomicInt::fetchAndStoreOrdered(int newValue)
00334 {
00335     return QT_INTERLOCKED_EXCHANGE(&_q_value, newValue);
00336 }
00337 
00338 inline int QBasicAtomicInt::fetchAndAddOrdered(int valueToAdd)
00339 {
00340     return QT_INTERLOCKED_EXCHANGE_ADD(&_q_value, valueToAdd);
00341 }
00342 
00344 
00345 template <typename T>
00346 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
00347 {
00348     return QT_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&_q_value, newValue, expectedValue)
00349             == expectedValue;
00350 }
00351 
00352 template <typename T>
00353 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreOrdered(T* newValue)
00354 {
00355     return reinterpret_cast<T *>(
00356             QT_INTERLOCKED_EXCHANGE_POINTER(&_q_value, newValue));
00357 }
00358 
00359 template <typename T>
00360 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
00361 {
00362     return reinterpret_cast<T *>(
00363             QT_INTERLOCKED_EXCHANGE_ADD_POINTER(&_q_value, valueToAdd * sizeof(T)));
00364 }
00365 
00367 
00368 inline bool QBasicAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
00369 {
00370     return testAndSetOrdered(expectedValue, newValue);
00371 }
00372 
00373 inline bool QBasicAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
00374 {
00375     return testAndSetOrdered(expectedValue, newValue);
00376 }
00377 
00378 inline bool QBasicAtomicInt::testAndSetRelease(int expectedValue, int newValue)
00379 {
00380     return testAndSetOrdered(expectedValue, newValue);
00381 }
00382 
00383 inline int QBasicAtomicInt::fetchAndStoreRelaxed(int newValue)
00384 {
00385     return fetchAndStoreOrdered(newValue);
00386 }
00387 
00388 inline int QBasicAtomicInt::fetchAndStoreAcquire(int newValue)
00389 {
00390     return fetchAndStoreOrdered(newValue);
00391 }
00392 
00393 inline int QBasicAtomicInt::fetchAndStoreRelease(int newValue)
00394 {
00395     return fetchAndStoreOrdered(newValue);
00396 }
00397 
00398 inline int QBasicAtomicInt::fetchAndAddRelaxed(int valueToAdd)
00399 {
00400     return fetchAndAddOrdered(valueToAdd);
00401 }
00402 
00403 inline int QBasicAtomicInt::fetchAndAddAcquire(int valueToAdd)
00404 {
00405     return fetchAndAddOrdered(valueToAdd);
00406 }
00407 
00408 inline int QBasicAtomicInt::fetchAndAddRelease(int valueToAdd)
00409 {
00410     return fetchAndAddOrdered(valueToAdd);
00411 }
00412 
00414 
00415 template <typename T>
00416 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
00417 {
00418     return testAndSetOrdered(expectedValue, newValue);
00419 }
00420 
00421 template <typename T>
00422 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
00423 {
00424     return testAndSetOrdered(expectedValue, newValue);
00425 }
00426 
00427 template <typename T>
00428 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
00429 {
00430     return testAndSetOrdered(expectedValue, newValue);
00431 }
00432 
00433 template <typename T>
00434 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelaxed(T *newValue)
00435 {
00436     return fetchAndStoreOrdered(newValue);
00437 }
00438 
00439 template <typename T>
00440 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreAcquire(T *newValue)
00441 {
00442     return fetchAndStoreOrdered(newValue);
00443 }
00444 
00445 template <typename T>
00446 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelease(T *newValue)
00447 {
00448     return fetchAndStoreOrdered(newValue);
00449 }
00450 
00451 template <typename T>
00452 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelaxed(qptrdiff valueToAdd)
00453 {
00454     return fetchAndAddOrdered(valueToAdd);
00455 }
00456 
00457 template <typename T>
00458 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddAcquire(qptrdiff valueToAdd)
00459 {
00460     return fetchAndAddOrdered(valueToAdd);
00461 }
00462 
00463 template <typename T>
00464 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelease(qptrdiff valueToAdd)
00465 {
00466     return fetchAndAddOrdered(valueToAdd);
00467 }
00468 
00470 // Cleanup
00471 
00472 #undef QT_INTERLOCKED_CONCAT_I
00473 #undef QT_INTERLOCKED_CONCAT
00474 #undef QT_INTERLOCKED_FUNCTION
00475 #undef QT_INTERLOCKED_PREFIX
00476 
00477 #undef QT_INTERLOCKED_NO_VOLATILE
00478 #undef QT_INTERLOCKED_VOLATILE
00479 #undef QT_INTERLOCKED_REMOVE_VOLATILE
00480 
00481 #undef QT_INTERLOCKED_INCREMENT
00482 #undef QT_INTERLOCKED_DECREMENT
00483 #undef QT_INTERLOCKED_COMPARE_EXCHANGE
00484 #undef QT_INTERLOCKED_EXCHANGE
00485 #undef QT_INTERLOCKED_EXCHANGE_ADD
00486 #undef QT_INTERLOCKED_COMPARE_EXCHANGE_POINTER
00487 #undef QT_INTERLOCKED_EXCHANGE_POINTER
00488 #undef QT_INTERLOCKED_EXCHANGE_ADD_POINTER
00489 
00490 QT_END_NAMESPACE
00491 
00492 QT_END_HEADER
00493 
00494 #endif // Q_CC_MSVC
00495 
00496 #endif // QATOMIC_WINDOWS_H