source: trunk/src/corelib/thread/qatomic.cpp@ 641

Last change on this file since 641 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 37.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \class QAtomicInt
44 \brief The QAtomicInt class provides platform-independent atomic operations on integers.
45 \since 4.4
46
47 \ingroup thread
48
49 For atomic operations on pointers, see the QAtomicPointer class.
50
51 An complex operation that completes without interruption is said
52 to be \e atomic. The QAtomicInt class provides atomic reference
53 counting, test-and-set, fetch-and-store, and fetch-and-add for
54 integers.
55
56 \section1 Non-atomic convenience operators
57
58 For convenience, QAtomicInt provides integer comparison, cast, and
59 assignment operators. Note that a combination of these operators
60 is \e not an atomic operation.
61
62 \section1 The Atomic API
63
64 \section2 Reference counting
65
66 The ref() and deref() functions provide an efficient reference
67 counting API. The return value of these functions are used to
68 indicate when the last reference has been released. These
69 functions allow you to implement your own implicitly shared
70 classes.
71
72 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 0
73
74 \section2 Memory ordering
75
76 QAtomicInt provides several implementations of the atomic
77 test-and-set, fetch-and-store, and fetch-and-add functions. Each
78 implementation defines a memory ordering semantic that describes
79 how memory accesses surrounding the atomic instruction are
80 executed by the processor. Since many modern architectures allow
81 out-of-order execution and memory ordering, using the correct
82 semantic is necessary to ensure that your application functions
83 properly on all processors.
84
85 \list
86
87 \o Relaxed - memory ordering is unspecified, leaving the compiler
88 and processor to freely reorder memory accesses.
89
90 \o Acquire - memory access following the atomic operation (in
91 program order) may not be re-ordered before the atomic operation.
92
93 \o Release - memory access before the atomic operation (in program
94 order) may not be re-ordered after the atomic operation.
95
96 \o Ordered - the same Acquire and Release semantics combined.
97
98 \endlist
99
100 \section2 Test-and-set
101
102 If the current value of the QAtomicInt is an expected value, the
103 test-and-set functions assign a new value to the QAtomicInt and
104 return true. If values are \a not the same, these functions do
105 nothing and return false. This operation equates to the following
106 code:
107
108 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 1
109
110 There are 4 test-and-set functions: testAndSetRelaxed(),
111 testAndSetAcquire(), testAndSetRelease(), and
112 testAndSetOrdered(). See above for an explanation of the different
113 memory ordering semantics.
114
115 \section2 Fetch-and-store
116
117 The atomic fetch-and-store functions read the current value of the
118 QAtomicInt and then assign a new value, returning the original
119 value. This operation equates to the following code:
120
121 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 2
122
123 There are 4 fetch-and-store functions: fetchAndStoreRelaxed(),
124 fetchAndStoreAcquire(), fetchAndStoreRelease(), and
125 fetchAndStoreOrdered(). See above for an explanation of the
126 different memory ordering semantics.
127
128 \section2 Fetch-and-add
129
130 The atomic fetch-and-add functions read the current value of the
131 QAtomicInt and then add the given value to the current value,
132 returning the original value. This operation equates to the
133 following code:
134
135 \snippet doc/src/snippets/code/src_corelib_thread_qatomic.cpp 3
136
137 There are 4 fetch-and-add functions: fetchAndAddRelaxed(),
138 fetchAndAddAcquire(), fetchAndAddRelease(), and
139 fetchAndAddOrdered(). See above for an explanation of the
140 different memory ordering semantics.
141
142 \section1 Feature Tests for the Atomic API
143
144 Providing a platform-independent atomic API that works on all
145 processors is challenging. The API provided by QAtomicInt is
146 guaranteed to work atomically on all processors. However, since
147 not all processors implement support for every operation provided
148 by QAtomicInt, it is necessary to expose information about the
149 processor.
150
151 You can check at compile time which features are supported on your
152 hardware using various macros. These will tell you if your
153 hardware always, sometimes, or does not support a particular
154 operation. The macros have the form
155 Q_ATOMIC_INT_\e{OPERATION}_IS_\e{HOW}_NATIVE. \e{OPERATION}
156 is one of REFERENCE_COUNTING, TEST_AND_SET,
157 FETCH_AND_STORE, or FETCH_AND_ADD, and \e{HOW} is one of
158 ALWAYS, SOMETIMES, or NOT. There will always be exactly one
159 defined macro per operation. For example, if
160 Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE is defined,
161 neither Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE nor
162 Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE will be defined.
163
164 An operation that completes in constant time is said to be
165 wait-free. Such operations are not implemented using locks or
166 loops of any kind. For atomic operations that are always
167 supported, and that are wait-free, Qt defines the
168 Q_ATOMIC_INT_\e{OPERATION}_IS_WAIT_FREE in addition to the
169 Q_ATOMIC_INT_\e{OPERATION}_IS_ALWAYS_NATIVE.
170
171 In cases where an atomic operation is only supported in newer
172 generations of the processor, QAtomicInt also provides a way to
173 check at runtime what your hardware supports with the
174 isReferenceCountingNative(), isTestAndSetNative(),
175 isFetchAndStoreNative(), and isFetchAndAddNative()
176 functions. Wait-free implementations can be detected using the
177 isReferenceCountingWaitFree(), isTestAndSetWaitFree(),
178 isFetchAndStoreWaitFree(), and isFetchAndAddWaitFree() functions.
179
180 Below is a complete list of all feature macros for QAtomicInt:
181
182 \list
183
184 \o Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
185 \o Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
186 \o Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE
187 \o Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
188
189 \o Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
190 \o Q_ATOMIC_INT_TEST_AND_SET_IS_SOMETIMES_NATIVE
191 \o Q_ATOMIC_INT_TEST_AND_SET_IS_NOT_NATIVE
192 \o Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
193
194 \o Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
195 \o Q_ATOMIC_INT_FETCH_AND_STORE_IS_SOMETIMES_NATIVE
196 \o Q_ATOMIC_INT_FETCH_AND_STORE_IS_NOT_NATIVE
197 \o Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
198
199 \o Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
200 \o Q_ATOMIC_INT_FETCH_AND_ADD_IS_SOMETIMES_NATIVE
201 \o Q_ATOMIC_INT_FETCH_AND_ADD_IS_NOT_NATIVE
202 \o Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
203
204 \endlist
205
206 \sa QAtomicPointer
207*/
208
209/*! \fn QAtomicInt::QAtomicInt(int value)
210
211 Constructs a QAtomicInt with the given \a value.
212*/
213
214/*! \fn QAtomicInt::QAtomicInt(const QAtomicInt &other)
215
216 Constructs a copy of \a other.
217*/
218
219/*! \fn QAtomicInt &QAtomicInt::operator=(int value)
220
221 Assigns the \a value to this QAtomicInt and returns a reference to
222 this QAtomicInt.
223*/
224
225/*! \fn QAtomicInt &QAtomicInt::operator=(const QAtomicInt &other)
226
227 Assigns \a other to this QAtomicInt and returns a reference to
228 this QAtomicInt.
229*/
230
231/*! \fn bool QAtomicInt::operator==(int value) const
232
233 Returns true if the \a value is equal to the value in this
234 QAtomicInt; otherwise returns false.
235*/
236
237/*! \fn bool QAtomicInt::operator!=(int value) const
238
239 Returns true if the value of this QAtomicInt is not equal to \a
240 value; otherwise returns false.
241*/
242
243/*! \fn bool QAtomicInt::operator!() const
244
245 Returns true is the value of this QAtomicInt is zero; otherwise
246 returns false.
247*/
248
249/*! \fn QAtomicInt::operator int() const
250
251 Returns the value stored by the QAtomicInt object as an integer.
252*/
253
254/*! \fn bool QAtomicInt::isReferenceCountingNative()
255
256 Returns true if reference counting is implemented using atomic
257 processor instructions, false otherwise.
258*/
259
260/*! \fn bool QAtomicInt::isReferenceCountingWaitFree()
261
262 Returns true if atomic reference counting is wait-free, false
263 otherwise.
264*/
265
266/*! \fn bool QAtomicInt::ref()
267 Atomically increments the value of this QAtomicInt. Returns true
268 if the new value is non-zero, false otherwise.
269
270 This function uses \e ordered \l {QAtomicInt#Memory
271 ordering}{memory ordering} semantics, which ensures that memory
272 access before and after the atomic operation (in program order)
273 may not be re-ordered.
274
275 \sa deref()
276*/
277
278/*! \fn bool QAtomicInt::deref()
279 Atomically decrements the value of this QAtomicInt. Returns true
280 if the new value is non-zero, false otherwise.
281
282 This function uses \e ordered \l {QAtomicInt#Memory
283 ordering}{memory ordering} semantics, which ensures that memory
284 access before and after the atomic operation (in program order)
285 may not be re-ordered.
286
287 \sa ref()
288*/
289
290/*! \fn bool QAtomicInt::isTestAndSetNative()
291
292 Returns true if test-and-set is implemented using atomic processor
293 instructions, false otherwise.
294*/
295
296/*! \fn bool QAtomicInt::isTestAndSetWaitFree()
297
298 Returns true if atomic test-and-set is wait-free, false otherwise.
299*/
300
301/*! \fn bool QAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
302
303 Atomic test-and-set.
304
305 If the current value of this QAtomicInt is the \a expectedValue,
306 the test-and-set functions assign the \a newValue to this
307 QAtomicInt and return true. If the values are \e not the same,
308 this function does nothing and returns false.
309
310 This function uses \e relaxed \l {QAtomicInt#Memory
311 ordering}{memory ordering} semantics, leaving the compiler and
312 processor to freely reorder memory accesses.
313*/
314
315/*! \fn bool QAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
316
317 Atomic test-and-set.
318
319 If the current value of this QAtomicInt is the \a expectedValue,
320 the test-and-set functions assign the \a newValue to this
321 QAtomicInt and return true. If the values are \e not the same,
322 this function does nothing and returns false.
323
324 This function uses \e acquire \l {QAtomicInt#Memory
325 ordering}{memory ordering} semantics, which ensures that memory
326 access following the atomic operation (in program order) may not
327 be re-ordered before the atomic operation.
328*/
329
330/*! \fn bool QAtomicInt::testAndSetRelease(int expectedValue, int newValue)
331
332 Atomic test-and-set.
333
334 If the current value of this QAtomicInt is the \a expectedValue,
335 the test-and-set functions assign the \a newValue to this
336 QAtomicInt and return true. If the values are \e not the same,
337 this function does nothing and returns false.
338
339 This function uses \e release \l {QAtomicInt#Memory
340 ordering}{memory ordering} semantics, which ensures that memory
341 access before the atomic operation (in program order) may not be
342 re-ordered after the atomic operation.
343*/
344
345/*! \fn bool QAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
346
347 Atomic test-and-set.
348
349 If the current value of this QAtomicInt is the \a expectedValue,
350 the test-and-set functions assign the \a newValue to this
351 QAtomicInt and return true. If the values are \e not the same,
352 this function does nothing and returns false.
353
354 This function uses \e ordered \l {QAtomicInt#Memory
355 ordering}{memory ordering} semantics, which ensures that memory
356 access before and after the atomic operation (in program order)
357 may not be re-ordered.
358*/
359
360/*! \fn bool QAtomicInt::isFetchAndStoreNative()
361
362 Returns true if fetch-and-store is implemented using atomic
363 processor instructions, false otherwise.
364*/
365
366/*! \fn bool QAtomicInt::isFetchAndStoreWaitFree()
367
368 Returns true if atomic fetch-and-store is wait-free, false
369 otherwise.
370*/
371
372/*! \fn int QAtomicInt::fetchAndStoreRelaxed(int newValue)
373
374 Atomic fetch-and-store.
375
376 Reads the current value of this QAtomicInt and then assigns it the
377 \a newValue, returning the original value.
378
379 This function uses \e relaxed \l {QAtomicInt#Memory
380 ordering}{memory ordering} semantics, leaving the compiler and
381 processor to freely reorder memory accesses.
382*/
383
384/*! \fn int QAtomicInt::fetchAndStoreAcquire(int newValue)
385
386 Atomic fetch-and-store.
387
388 Reads the current value of this QAtomicInt and then assigns it the
389 \a newValue, returning the original value.
390
391 This function uses \e acquire \l {QAtomicInt#Memory
392 ordering}{memory ordering} semantics, which ensures that memory
393 access following the atomic operation (in program order) may not
394 be re-ordered before the atomic operation.
395*/
396
397/*! \fn int QAtomicInt::fetchAndStoreRelease(int newValue)
398
399 Atomic fetch-and-store.
400
401 Reads the current value of this QAtomicInt and then assigns it the
402 \a newValue, returning the original value.
403
404 This function uses \e release \l {QAtomicInt#Memory
405 ordering}{memory ordering} semantics, which ensures that memory
406 access before the atomic operation (in program order) may not be
407 re-ordered after the atomic operation.
408*/
409
410/*! \fn int QAtomicInt::fetchAndStoreOrdered(int newValue)
411
412 Atomic fetch-and-store.
413
414 Reads the current value of this QAtomicInt and then assigns it the
415 \a newValue, returning the original value.
416
417 This function uses \e ordered \l {QAtomicInt#Memory
418 ordering}{memory ordering} semantics, which ensures that memory
419 access before and after the atomic operation (in program order)
420 may not be re-ordered.
421*/
422
423/*! \fn bool QAtomicInt::isFetchAndAddNative()
424
425 Returns true if fetch-and-add is implemented using atomic
426 processor instructions, false otherwise.
427*/
428
429/*! \fn bool QAtomicInt::isFetchAndAddWaitFree()
430
431 Returns true if atomic fetch-and-add is wait-free, false
432 otherwise.
433*/
434
435/*! \fn int QAtomicInt::fetchAndAddRelaxed(int valueToAdd)
436
437 Atomic fetch-and-add.
438
439 Reads the current value of this QAtomicInt and then adds
440 \a valueToAdd to the current value, returning the original value.
441
442 This function uses \e relaxed \l {QAtomicInt#Memory
443 ordering}{memory ordering} semantics, leaving the compiler and
444 processor to freely reorder memory accesses.
445*/
446
447/*! \fn int QAtomicInt::fetchAndAddAcquire(int valueToAdd)
448
449 Atomic fetch-and-add.
450
451 Reads the current value of this QAtomicInt and then adds
452 \a valueToAdd to the current value, returning the original value.
453
454 This function uses \e acquire \l {QAtomicInt#Memory
455 ordering}{memory ordering} semantics, which ensures that memory
456 access following the atomic operation (in program order) may not
457 be re-ordered before the atomic operation.
458*/
459
460/*! \fn int QAtomicInt::fetchAndAddRelease(int valueToAdd)
461
462 Atomic fetch-and-add.
463
464 Reads the current value of this QAtomicInt and then adds
465 \a valueToAdd to the current value, returning the original value.
466
467 This function uses \e release \l {QAtomicInt#Memory
468 ordering}{memory ordering} semantics, which ensures that memory
469 access before the atomic operation (in program order) may not be
470 re-ordered after the atomic operation.
471*/
472
473/*! \fn int QAtomicInt::fetchAndAddOrdered(int valueToAdd)
474
475 Atomic fetch-and-add.
476
477 Reads the current value of this QAtomicInt and then adds
478 \a valueToAdd to the current value, returning the original value.
479
480 This function uses \e ordered \l {QAtomicInt#Memory
481 ordering}{memory ordering} semantics, which ensures that memory
482 access before and after the atomic operation (in program order)
483 may not be re-ordered.
484*/
485
486/*!
487 \macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
488 \relates QAtomicInt
489
490 This macro is defined if and only if all generations of your
491 processor support atomic reference counting.
492*/
493
494/*!
495 \macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_SOMETIMES_NATIVE
496 \relates QAtomicInt
497
498 This macro is defined when only certain generations of the
499 processor support atomic reference counting. Use the
500 QAtomicInt::isReferenceCountingNative() function to check what
501 your processor supports.
502*/
503
504/*!
505 \macro Q_ATOMIC_INT_REFERENCE_COUNTING_IS_NOT_NATIVE
506 \relates QAtomicInt
507
508 This macro is defined when the hardware does not support atomic
509 reference counting.
510*/
511
512/*!