Qt 4.8
qatomic_windows.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QATOMIC_WINDOWS_H
43 #define QATOMIC_WINDOWS_H
44 
45 #ifndef Q_CC_MSVC
46 
47 // Mingw and other GCC platforms get inline assembly
48 
49 # ifdef __i386__
50 # include "QtCore/qatomic_i386.h"
51 # else
52 # include "QtCore/qatomic_x86_64.h"
53 # endif
54 
55 #else // Q_CC_MSVC
56 
58 
59 #ifndef Q_OS_WINCE
60 
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
66 
67 #else // Q_OS_WINCE
68 
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>
75 
76 // It's safer to remove the volatile and let the compiler add it as needed.
77 # define QT_INTERLOCKED_NO_VOLATILE
78 
79 # else // _WIN32_WCE >= 0x600 || !_X86_
80 
81 # define QT_INTERLOCKED_PROTOTYPE __cdecl
82 # define QT_INTERLOCKED_DECLARE_PROTOTYPES
83 
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
92 
93 # endif // _WIN32_WCE >= 0x600 || !_X86_
94 
95 #endif // Q_OS_WINCE
96 
98 // Prototype declaration
99 
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)
104 
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)
113 
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
121 
122 #ifndef QT_INTERLOCKED_PREFIX
123 #define QT_INTERLOCKED_PREFIX
124 #endif
125 
126 #ifndef QT_INTERLOCKED_PROTOTYPE
127 #define QT_INTERLOCKED_PROTOTYPE
128 #endif
129 
130 #ifdef QT_INTERLOCKED_DECLARE_PROTOTYPES
131 #undef QT_INTERLOCKED_DECLARE_PROTOTYPES
132 
133 extern "C" {
134 
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);
140 
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
146 
147 }
148 
149 #endif // QT_INTERLOCKED_DECLARE_PROTOTYPES
150 
151 #undef QT_INTERLOCKED_PROTOTYPE
152 
154 
155 #ifdef QT_INTERLOCKED_INTRINSIC
156 #undef QT_INTERLOCKED_INTRINSIC
157 
158 # pragma intrinsic (_InterlockedIncrement)
159 # pragma intrinsic (_InterlockedDecrement)
160 # pragma intrinsic (_InterlockedExchange)
161 # pragma intrinsic (_InterlockedCompareExchange)
162 # pragma intrinsic (_InterlockedExchangeAdd)
163 
164 # if !defined(Q_OS_WINCE) && !defined(_M_IX86)
165 # pragma intrinsic (_InterlockedCompareExchangePointer)
166 # pragma intrinsic (_InterlockedExchangePointer)
167 # pragma intrinsic (_InterlockedExchangeAdd64)
168 # endif
169 
170 #endif // QT_INTERLOCKED_INTRINSIC
171 
173 // Interlocked* replacement macros
174 
175 #define QT_INTERLOCKED_INCREMENT(value) \
176  QT_INTERLOCKED_FUNCTION( Increment )( \
177  QT_INTERLOCKED_REMOVE_VOLATILE( value ) )
178 
179 #define QT_INTERLOCKED_DECREMENT(value) \
180  QT_INTERLOCKED_FUNCTION( Decrement )( \
181  QT_INTERLOCKED_REMOVE_VOLATILE( value ) )
182 
183 #define QT_INTERLOCKED_COMPARE_EXCHANGE(value, newValue, expectedValue) \
184  QT_INTERLOCKED_FUNCTION( CompareExchange )( \
185  QT_INTERLOCKED_REMOVE_VOLATILE( value ), \
186  newValue, \
187  expectedValue )
188 
189 #define QT_INTERLOCKED_EXCHANGE(value, newValue) \
190  QT_INTERLOCKED_FUNCTION( Exchange )( \
191  QT_INTERLOCKED_REMOVE_VOLATILE( value ), \
192  newValue )
193 
194 #define QT_INTERLOCKED_EXCHANGE_ADD(value, valueToAdd) \
195  QT_INTERLOCKED_FUNCTION( ExchangeAdd )( \
196  QT_INTERLOCKED_REMOVE_VOLATILE( value ), \
197  valueToAdd )
198 
199 #if defined(Q_OS_WINCE) || defined(__i386__) || defined(_M_IX86)
200 
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 ) ))
207 
208 # define QT_INTERLOCKED_EXCHANGE_POINTER(value, newValue) \
209  QT_INTERLOCKED_FUNCTION( Exchange )( \
210  QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
211  (quintptr)( newValue ) )
212 
213 # define QT_INTERLOCKED_EXCHANGE_ADD_POINTER(value, valueToAdd) \
214  QT_INTERLOCKED_FUNCTION( ExchangeAdd )( \
215  QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
216  valueToAdd )
217 
218 #else // !defined(Q_OS_WINCE) && !defined(__i386__) && !defined(_M_IX86)
219 
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 )
225 
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 )
230 
231 # define QT_INTERLOCKED_EXCHANGE_ADD_POINTER(value, valueToAdd) \
232  QT_INTERLOCKED_FUNCTION( ExchangeAdd64 )( \
233  QT_INTERLOCKED_REMOVE_VOLATILE( value ## _integral ), \
234  valueToAdd )
235 
236 #endif // !defined(Q_OS_WINCE) && !defined(__i386__) && !defined(_M_IX86)
237 
239 
241 
243 
244 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
245 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
246 
248 { return true; }
250 { return true; }
251 
252 #define Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
253 #define Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
254 
256 { return true; }
258 { return true; }
259 
260 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
261 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
262 
264 { return true; }
266 { return true; }
267 
268 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
269 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
270 
272 { return true; }
274 { return true; }
275 
276 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
277 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
278 
279 template <typename T>
281 { return true; }
282 template <typename T>
284 { return true; }
285 
286 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
287 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
288 
289 template <typename T>
291 { return true; }
292 template <typename T>
294 { return true; }
295 
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>
300 { return true; }
301 template <typename T>
303 { return true; }
304 
306 
307 #ifdef QT_INTERLOCKED_NO_VOLATILE
308 template <class T>
309 Q_INLINE_TEMPLATE T *qt_interlocked_remove_volatile(T volatile *t)
310 {
311  return const_cast<T *>(t);
312 }
313 #endif // !QT_INTERLOCKED_NO_VOLATILE
314 
316 
317 inline bool QBasicAtomicInt::ref()
318 {
319  return QT_INTERLOCKED_INCREMENT(&_q_value) != 0;
320 }
321 
322 inline bool QBasicAtomicInt::deref()
323 {
324  return QT_INTERLOCKED_DECREMENT(&_q_value) != 0;
325 }
326 
327 inline bool QBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
328 {
329  return QT_INTERLOCKED_COMPARE_EXCHANGE(&_q_value, newValue, expectedValue)
330  == expectedValue;
331 }
332 
333 inline int QBasicAtomicInt::fetchAndStoreOrdered(int newValue)
334 {
335  return QT_INTERLOCKED_EXCHANGE(&_q_value, newValue);
336 }
337 
338 inline int QBasicAtomicInt::fetchAndAddOrdered(int valueToAdd)
339 {
340  return QT_INTERLOCKED_EXCHANGE_ADD(&_q_value, valueToAdd);
341 }
342 
344 
345 template <typename T>
346 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
347 {
348  return QT_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&_q_value, newValue, expectedValue)
349  == expectedValue;
350 }
351 
352 template <typename T>
354 {
355  return reinterpret_cast<T *>(
356  QT_INTERLOCKED_EXCHANGE_POINTER(&_q_value, newValue));
357 }
358 
359 template <typename T>
361 {
362  return reinterpret_cast<T *>(
363  QT_INTERLOCKED_EXCHANGE_ADD_POINTER(&_q_value, valueToAdd * sizeof(T)));
364 }
365 
367 
368 inline bool QBasicAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
369 {
370  return testAndSetOrdered(expectedValue, newValue);
371 }
372 
373 inline bool QBasicAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
374 {
375  return testAndSetOrdered(expectedValue, newValue);
376 }
377 
378 inline bool QBasicAtomicInt::testAndSetRelease(int expectedValue, int newValue)
379 {
380  return testAndSetOrdered(expectedValue, newValue);
381 }
382 
383 inline int QBasicAtomicInt::fetchAndStoreRelaxed(int newValue)
384 {
385  return fetchAndStoreOrdered(newValue);
386 }
387 
388 inline int QBasicAtomicInt::fetchAndStoreAcquire(int newValue)
389 {
390  return fetchAndStoreOrdered(newValue);
391 }
392 
393 inline int QBasicAtomicInt::fetchAndStoreRelease(int newValue)
394 {
395  return fetchAndStoreOrdered(newValue);
396 }
397 
398 inline int QBasicAtomicInt::fetchAndAddRelaxed(int valueToAdd)
399 {
400  return fetchAndAddOrdered(valueToAdd);
401 }
402 
403 inline int QBasicAtomicInt::fetchAndAddAcquire(int valueToAdd)
404 {
405  return fetchAndAddOrdered(valueToAdd);
406 }
407 
408 inline int QBasicAtomicInt::fetchAndAddRelease(int valueToAdd)
409 {
410  return fetchAndAddOrdered(valueToAdd);
411 }
412 
414 
415 template <typename T>
416 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
417 {
418  return testAndSetOrdered(expectedValue, newValue);
419 }
420 
421 template <typename T>
422 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
423 {
424  return testAndSetOrdered(expectedValue, newValue);
425 }
426 
427 template <typename T>
428 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
429 {
430  return testAndSetOrdered(expectedValue, newValue);
431 }
432 
433 template <typename T>
435 {
436  return fetchAndStoreOrdered(newValue);
437 }
438 
439 template <typename T>
441 {
442  return fetchAndStoreOrdered(newValue);
443 }
444 
445 template <typename T>
447 {
448  return fetchAndStoreOrdered(newValue);
449 }
450 
451 template <typename T>
453 {
454  return fetchAndAddOrdered(valueToAdd);
455 }
456 
457 template <typename T>
459 {
460  return fetchAndAddOrdered(valueToAdd);
461 }
462 
463 template <typename T>
465 {
466  return fetchAndAddOrdered(valueToAdd);
467 }
468 
470 // Cleanup
471 
472 #undef QT_INTERLOCKED_CONCAT_I
473 #undef QT_INTERLOCKED_CONCAT
474 #undef QT_INTERLOCKED_FUNCTION
475 #undef QT_INTERLOCKED_PREFIX
476 
477 #undef QT_INTERLOCKED_NO_VOLATILE
478 #undef QT_INTERLOCKED_VOLATILE
479 #undef QT_INTERLOCKED_REMOVE_VOLATILE
480 
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
489 
491 
493 
494 #endif // Q_CC_MSVC
495 
496 #endif // QATOMIC_WINDOWS_H
static bool isFetchAndStoreNative()
Definition: qatomic_alpha.h:65
static bool isTestAndSetNative()
Definition: qatomic_alpha.h:58
int fetchAndStoreRelease(int newValue)
static bool isReferenceCountingNative()
Definition: qatomic_alpha.h:51
T * fetchAndAddRelaxed(qptrdiff valueToAdd)
volatile int _q_value
Definition: qbasicatomic.h:64
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static bool isFetchAndAddNative()
Definition: qatomic_alpha.h:98
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
static bool isTestAndSetNative()
Definition: qatomic_alpha.h:80
static bool isFetchAndAddNative()
Definition: qatomic_alpha.h:72
T * fetchAndStoreRelease(T *newValue)
static bool isTestAndSetWaitFree()
Definition: qatomic_alpha.h:83
T * fetchAndAddRelease(qptrdiff valueToAdd)
bool testAndSetOrdered(T *expectedValue, T *newValue)
int fetchAndAddAcquire(int valueToAdd)
T * fetchAndAddAcquire(qptrdiff valueToAdd)
int fetchAndStoreRelaxed(int newValue)
T * fetchAndAddOrdered(qptrdiff valueToAdd)
bool testAndSetAcquire(int expectedValue, int newValue)
bool testAndSetRelaxed(int expectedValue, int newValue)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int fetchAndStoreAcquire(int newValue)
QIntegerForSizeof< void * >::Signed qptrdiff
Definition: qglobal.h:987
T * fetchAndStoreOrdered(T *newValue)
#define Q_INLINE_TEMPLATE
Definition: qglobal.h:1713
int fetchAndAddRelease(int valueToAdd)
bool testAndSetOrdered(int expectedValue, int newValue)
T * fetchAndStoreAcquire(T *newValue)
static bool isFetchAndStoreWaitFree()
Definition: qatomic_alpha.h:67
static bool isTestAndSetWaitFree()
Definition: qatomic_alpha.h:60
static bool isReferenceCountingWaitFree()
Definition: qatomic_alpha.h:53
int fetchAndAddOrdered(int valueToAdd)
static bool isFetchAndStoreWaitFree()
Definition: qatomic_alpha.h:92
static bool isFetchAndAddWaitFree()
Definition: qatomic_alpha.h:74
static bool isFetchAndAddWaitFree()
bool testAndSetRelease(int expectedValue, int newValue)
int fetchAndAddRelaxed(int valueToAdd)
int fetchAndStoreOrdered(int newValue)
bool testAndSetRelaxed(T *expectedValue, T *newValue)
bool testAndSetRelease(T *expectedValue, T *newValue)
#define QT_END_HEADER
Definition: qglobal.h:137
T * fetchAndStoreRelaxed(T *newValue)
static bool isFetchAndStoreNative()
Definition: qatomic_alpha.h:89
bool testAndSetAcquire(T *expectedValue, T *newValue)