More fixes for qatomic_windows.h
[qt-netbsd.git] / src / corelib / arch / qatomic_windows.h
blob07b0b25dfee35965a732f70493cf7caa50890627
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Nokia Corporation (qt-info@nokia.com)
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** No Commercial Usage
10 ** This file contains pre-release code and may not be distributed.
11 ** You may use this file in accordance with the terms and conditions
12 ** contained in the Technology Preview License Agreement accompanying
13 ** this package.
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #ifndef QATOMIC_WINDOWS_H
43 #define QATOMIC_WINDOWS_H
45 #ifndef Q_CC_MSVC
47 // Mingw and other GCC platforms get inline assembly
49 # ifdef __i386__
50 # include "QtCore/qatomic_i386.h"
51 # else
52 # include "QtCore/qatomic_x86_64.h"
53 # endif
55 #else // Q_CC_MSVC
57 ////////////////////////////////////////////////////////////////////////////////////////////////////
59 #ifndef Q_OS_WINCE
61 // use compiler intrinsics for all atomic functions
62 # define QT_INTERLOCKED_PREFIX _
63 # define QT_INTERLOCKED_PROTOTYPE __cdecl
64 # define QT_INTERLOCKED_DECLARE_PROTOTYPES
65 # define QT_INTERLOCKED_INTRINSIC
67 #else // Q_OS_WINCE
69 # if _WIN32_WCE < 0x600 && defined(_X86_)
70 // For X86 Windows CE, include winbase.h to catch inline functions which
71 // override the regular definitions inside of coredll.dll.
72 // Though one could use the original version of Increment/Decrement, others are
73 // not exported at all.
74 # include <winbase.h>
76 // It's safer to remove the volatile and let the compiler add it as needed.
77 # define QT_INTERLOCKED_NO_VOLATILE
79 # else // _WIN32_WCE >= 0x600 || !_X86_
81 # define QT_INTERLOCKED_PROTOTYPE __cdecl
82 # define QT_INTERLOCKED_DECLARE_PROTOTYPES
84 # if _WIN32_WCE >= 0x600
85 # if defined(_X86_)
86 # define QT_INTERLOCKED_PREFIX _
87 # define QT_INTERLOCKED_INTRINSIC
88 # endif
89 # else
90 # define QT_INTERLOCKED_NO_VOLATILE
91 # endif
93 # endif // _WIN32_WCE >= 0x600 || !_X86_
95 #endif // Q_OS_WINCE
97 ////////////////////////////////////////////////////////////////////////////////////////////////////
98 // Prototype declaration
100 #define QT_INTERLOCKED_CONCAT_I(prefix, suffix) \
101 prefix ## suffix
102 #define QT_INTERLOCKED_CONCAT(prefix, suffix) \
103 QT_INTERLOCKED_CONCAT_I(prefix, suffix)
105 // MSVC intrinsics prefix function names with an underscore. Also, if platform
106 // SDK headers have been included, the Interlocked names may be defined as
107 // macros.
108 // To avoid double underscores, we paste the prefix with Interlocked first and
109 // then the remainder of the function name.
110 #define QT_INTERLOCKED_FUNCTION(name) \
111 QT_INTERLOCKED_CONCAT( \
112 QT_INTERLOCKED_CONCAT(QT_INTERLOCKED_PREFIX, Interlocked), name)
114 #ifdef QT_INTERLOCKED_NO_VOLATILE
115 # define QT_INTERLOCKED_VOLATILE
116 # define QT_INTERLOCKED_REMOVE_VOLATILE(a) qt_interlocked_remove_volatile(a)
117 #else
118 # define QT_INTERLOCKED_VOLATILE volatile
119 # define QT_INTERLOCKED_REMOVE_VOLATILE(a) a
120 #endif
122 #ifndef QT_INTERLOCKED_PREFIX
123 #define QT_INTERLOCKED_PREFIX
124 #endif
126 #ifndef QT_INTERLOCKED_PROTOTYPE
127 #define QT_INTERLOCKED_PROTOTYPE
128 #endif
130 #ifdef QT_INTERLOCKED_DECLARE_PROTOTYPES
131 #undef QT_INTERLOCKED_DECLARE_PROTOTYPES
133 extern "C" {
135 long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( Increment )(long QT_INTERLOCKED_VOLATILE *);
136 long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( Decrement )(long QT_INTERLOCKED_VOLATILE *);
137 long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( CompareExchange )(long QT_INTERLOCKED_VOLATILE *, long, long);
138 long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( Exchange )(long QT_INTERLOCKED_VOLATILE *, long);
139 long QT_INTERLOCKED_PROTOTYPE QT_INTERLOCKED_FUNCTION( ExchangeAdd )(long QT_INTERLOCKED_VOLATILE *, long);
141 # if !defined(Q_OS_WINCE) && !defined(__i386__) && !defined(_M_IX86)
142 void * QT_INTERLOCKED_FUNCTION( CompareExchangePointer )(void * QT_INTERLOCKED_VOLATILE *, void *, void *);
143 void * QT_INTERLOCKED_FUNCTION( ExchangePointer )(void * QT_INTERLOCKED_VOLATILE *, void *);
144 __int64 QT_INTERLOCKED_FUNCTION( ExchangeAdd64 )(__int64 QT_INTERLOCKED_VOLATILE *, __int64);
145 # endif
149 #endif // QT_INTERLOCKED_DECLARE_PROTOTYPES
151 #undef QT_INTERLOCKED_PROTOTYPE
153 ////////////////////////////////////////////////////////////////////////////////////////////////////
155 #ifdef QT_INTERLOCKED_INTRINSIC
156 #undef QT_INTERLOCKED_INTRINSIC
158 # pragma intrinsic (_InterlockedIncrement)
159 # pragma intrinsic (_InterlockedDecrement)
160 # pragma intrinsic (_InterlockedExchange)
161 # pragma intrinsic (_InterlockedCompareExchange)
162 # pragma intrinsic (_InterlockedExchangeAdd)
164 # if !defined(Q_OS_WINCE) && !defined(_M_IX86)
165 # pragma intrinsic (_InterlockedCompareExchangePointer)
166 # pragma intrinsic (_InterlockedExchangePointer)
167 # pragma intrinsic (_InterlockedExchangeAdd64)
168 # endif
170 #endif // QT_INTERLOCKED_INTRINSIC
172 ////////////////////////////////////////////////////////////////////////////////////////////////////
173 // Interlocked* replacement macros
175 #define QT_INTERLOCKED_INCREMENT(value) \
176 QT_INTERLOCKED_FUNCTION( Increment )( \
177 QT_INTERLOCKED_REMOVE_VOLATILE( value ) )
179 #define QT_INTERLOCKED_DECREMENT(value) \
180 QT_INTERLOCKED_FUNCTION( Decrement )( \
181 QT_INTERLOCKED_REMOVE_VOLATILE( value ) )
183 #define QT_INTERLOCKED_COMPARE_EXCHANGE(value, newValue, expectedValue) \
184 QT_INTERLOCKED_FUNCTION( CompareExchange )( \
185 QT_INTERLOCKED_REMOVE_VOLATILE( value ), \
186 newValue, \
187 expectedValue )
189 #define QT_INTERLOCKED_EXCHANGE(value, newValue) \
190 QT_INTERLOCKED_FUNCTION( Exchange )( \
191 QT_INTERLOCKED_REMOVE_VOLATILE( value ), \
192 newValue )
194 #define QT_INTERLOCKED_EXCHANGE_ADD(value, valueToAdd) \
195 QT_INTERLOCKED_FUNCTION( ExchangeAdd )( \
196 QT_INTERLOCKED_REMOVE_VOLATILE( value ), \
197 valueToAdd )
199 #if defined(Q_OS_WINCE) || defined(__i386__) || defined(_M_IX86)
201 # define QT_INTERLOCKED_COMPARE_EXCHANGE_POINTER(value, newValue, expectedValue) \
202 reinterpret_cast<void *>( \
203 QT_INTERLOCKED_FUNCTION( CompareExchange )( \
204 QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
205 (long)( newValue ), \
206 (long)( expectedValue ) ))
208 # define QT_INTERLOCKED_EXCHANGE_POINTER(value, newValue) \
209 QT_INTERLOCKED_FUNCTION( Exchange )( \
210 QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
211 (quintptr)( newValue ) )
213 # define QT_INTERLOCKED_EXCHANGE_ADD_POINTER(value, valueToAdd) \
214 QT_INTERLOCKED_FUNCTION( ExchangeAdd )( \
215 QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
216 valueToAdd )
218 #else // !defined(Q_OS_WINCE) && !defined(__i386__) && !defined(_M_IX86)
220 # define QT_INTERLOCKED_COMPARE_EXCHANGE_POINTER(value, newValue, expectedValue) \
221 QT_INTERLOCKED_FUNCTION( CompareExchangePointer )( \
222 reinterpret_cast<void * QT_INTERLOCKED_VOLATILE *>( QT_INTERLOCKED_REMOVE_VOLATILE( value ) ), \
223 newValue, \
224 expectedValue )
226 # define QT_INTERLOCKED_EXCHANGE_POINTER(value, newValue) \
227 QT_INTERLOCKED_FUNCTION( ExchangePointer )( \
228 reinterpret_cast<void * QT_INTERLOCKED_VOLATILE *>( QT_INTERLOCKED_REMOVE_VOLATILE( value ) ), \
229 newValue )
231 # define QT_INTERLOCKED_EXCHANGE_ADD_POINTER(value, valueToAdd) \
232 QT_INTERLOCKED_FUNCTION( ExchangeAdd64 )( \
233 QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
234 valueToAdd )
236 #endif // !defined(Q_OS_WINCE) && !defined(__i386__) && !defined(_M_IX86)
238 ////////////////////////////////////////////////////////////////////////////////////////////////////
240 QT_BEGIN_HEADER
242 QT_BEGIN_NAMESPACE
244 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
245 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
247 inline bool QBasicAtomicInt::isReferenceCountingNative()
248 { return true; }
249 inline bool QBasicAtomicInt::isReferenceCountingWaitFree()
250 { return true; }
252 #define Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
253 #define Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
255 inline bool QBasicAtomicInt::isTestAndSetNative()
256 { return true; }
257 inline bool QBasicAtomicInt::isTestAndSetWaitFree()
258 { return true; }
260 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
261 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
263 inline bool QBasicAtomicInt::isFetchAndStoreNative()
264 { return true; }
265 inline bool QBasicAtomicInt::isFetchAndStoreWaitFree()
266 { return true; }
268 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
269 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
271 inline bool QBasicAtomicInt::isFetchAndAddNative()
272 { return true; }
273 inline bool QBasicAtomicInt::isFetchAndAddWaitFree()
274 { return true; }
276 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
277 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
279 template <typename T>
280 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetNative()
281 { return true; }
282 template <typename T>
283 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetWaitFree()
284 { return true; }
286 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
287 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
289 template <typename T>
290 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreNative()
291 { return true; }
292 template <typename T>
293 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreWaitFree()
294 { return true; }
296 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
297 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
298 template <typename T>
299 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddNative()
300 { return true; }
301 template <typename T>
302 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddWaitFree()
303 { return true; }
305 ////////////////////////////////////////////////////////////////////////////////////////////////////
307 #ifdef QT_INTERLOCKED_NO_VOLATILE
308 template <class T>
309 Q_INLINE_TEMPLATE T *qt_interlocked_remove_volatile(T volatile *t)
311 return const_cast<T *>(t);
313 #endif // !QT_INTERLOCKED_NO_VOLATILE
315 ////////////////////////////////////////////////////////////////////////////////////////////////////
317 inline bool QBasicAtomicInt::ref()
319 return QT_INTERLOCKED_INCREMENT(&_q_value) != 0;
322 inline bool QBasicAtomicInt::deref()
324 return QT_INTERLOCKED_DECREMENT(&_q_value) != 0;
327 inline bool QBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
329 return QT_INTERLOCKED_COMPARE_EXCHANGE(&_q_value, newValue, expectedValue)
330 == expectedValue;
333 inline int QBasicAtomicInt::fetchAndStoreOrdered(int newValue)
335 return QT_INTERLOCKED_EXCHANGE(&_q_value, newValue);
338 inline int QBasicAtomicInt::fetchAndAddOrdered(int valueToAdd)
340 return QT_INTERLOCKED_EXCHANGE_ADD(&_q_value, valueToAdd);
343 ////////////////////////////////////////////////////////////////////////////////////////////////////
345 template <typename T>
346 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
348 return QT_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&_q_value, newValue, expectedValue)
349 == expectedValue;
352 template <typename T>
353 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreOrdered(T* newValue)
355 return reinterpret_cast<T *>(
356 QT_INTERLOCKED_EXCHANGE_POINTER(&_q_value, newValue));
359 template <typename T>
360 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
362 return reinterpret_cast<T *>(
363 QT_INTERLOCKED_EXCHANGE_ADD_POINTER(&_q_value, valueToAdd * sizeof(T)));
366 ////////////////////////////////////////////////////////////////////////////////////////////////////
368 inline bool QBasicAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
370 return testAndSetOrdered(expectedValue, newValue);
373 inline bool QBasicAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
375 return testAndSetOrdered(expectedValue, newValue);
378 inline bool QBasicAtomicInt::testAndSetRelease(int expectedValue, int newValue)
380 return testAndSetOrdered(expectedValue, newValue);
383 inline int QBasicAtomicInt::fetchAndStoreRelaxed(int newValue)
385 return fetchAndStoreOrdered(newValue);
388 inline int QBasicAtomicInt::fetchAndStoreAcquire(int newValue)
390 return fetchAndStoreOrdered(newValue);
393 inline int QBasicAtomicInt::fetchAndStoreRelease(int newValue)
395 return fetchAndStoreOrdered(newValue);
398 inline int QBasicAtomicInt::fetchAndAddRelaxed(int valueToAdd)
400 return fetchAndAddOrdered(valueToAdd);
403 inline int QBasicAtomicInt::fetchAndAddAcquire(int valueToAdd)
405 return fetchAndAddOrdered(valueToAdd);
408 inline int QBasicAtomicInt::fetchAndAddRelease(int valueToAdd)
410 return fetchAndAddOrdered(valueToAdd);
413 ////////////////////////////////////////////////////////////////////////////////////////////////////
415 template <typename T>
416 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
418 return testAndSetOrdered(expectedValue, newValue);
421 template <typename T>
422 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
424 return testAndSetOrdered(expectedValue, newValue);
427 template <typename T>
428 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
430 return testAndSetOrdered(expectedValue, newValue);
433 template <typename T>
434 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelaxed(T *newValue)
436 return fetchAndStoreOrdered(newValue);
439 template <typename T>
440 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreAcquire(T *newValue)
442 return fetchAndStoreOrdered(newValue);
445 template <typename T>
446 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelease(T *newValue)
448 return fetchAndStoreOrdered(newValue);
451 template <typename T>
452 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelaxed(qptrdiff valueToAdd)
454 return fetchAndAddOrdered(valueToAdd);
457 template <typename T>
458 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddAcquire(qptrdiff valueToAdd)
460 return fetchAndAddOrdered(valueToAdd);
463 template <typename T>
464 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelease(qptrdiff valueToAdd)
466 return fetchAndAddOrdered(valueToAdd);
469 ////////////////////////////////////////////////////////////////////////////////////////////////////
470 // Cleanup
472 #undef QT_INTERLOCKED_CONCAT_I
473 #undef QT_INTERLOCKED_CONCAT
474 #undef QT_INTERLOCKED_FUNCTION
475 #undef QT_INTERLOCKED_PREFIX
477 #undef QT_INTERLOCKED_NO_VOLATILE
478 #undef QT_INTERLOCKED_VOLATILE
479 #undef QT_INTERLOCKED_REMOVE_VOLATILE
481 #undef QT_INTERLOCKED_INCREMENT
482 #undef QT_INTERLOCKED_DECREMENT
483 #undef QT_INTERLOCKED_COMPARE_EXCHANGE
484 #undef QT_INTERLOCKED_EXCHANGE
485 #undef QT_INTERLOCKED_EXCHANGE_ADD
486 #undef QT_INTERLOCKED_COMPARE_EXCHANGE_POINTER
487 #undef QT_INTERLOCKED_EXCHANGE_POINTER
488 #undef QT_INTERLOCKED_EXCHANGE_ADD_POINTER
490 QT_END_NAMESPACE
492 QT_END_HEADER
494 #endif // Q_CC_MSVC
496 #endif // QATOMIC_WINDOWS_H