source: trunk/src/corelib/thread/qbasicatomic.h@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
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.
22**
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.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QBASICATOMIC_H
43#define QBASICATOMIC_H
44
45#include <QtCore/qglobal.h>
46
47QT_BEGIN_HEADER
48
49QT_BEGIN_NAMESPACE
50
51QT_MODULE(Core)
52
53class Q_CORE_EXPORT QBasicAtomicInt
54{
55public:
56#ifdef QT_ARCH_PARISC
57 int _q_lock[4];
58#endif
59 volatile int _q_value;
60
61 // Non-atomic API
62 inline bool operator==(int value) const
63 {
64 return _q_value == value;
65 }
66
67 inline bool operator!=(int value) const
68 {
69 return _q_value != value;
70 }
71
72 inline bool operator!() const
73 {
74 return _q_value == 0;
75 }
76
77 inline operator int() const
78 {
79 return _q_value;
80 }
81
82 inline QBasicAtomicInt &operator=(int value)
83 {
84#ifdef QT_ARCH_PARISC
85 this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
86#endif
87 _q_value = value;
88 return *this;
89 }
90
91 // Atomic API, implemented in qatomic_XXX.h
92
93 static bool isReferenceCountingNative();
94 static bool isReferenceCountingWaitFree();
95
96 bool ref();
97 bool deref();
98
99 static bool isTestAndSetNative();
100 static bool isTestAndSetWaitFree();
101
102 bool testAndSetRelaxed(int expectedValue, int newValue);
103 bool testAndSetAcquire(int expectedValue, int newValue);
104 bool testAndSetRelease(int expectedValue, int newValue);
105 bool testAndSetOrdered(int expectedValue, int newValue);
106
107 static bool isFetchAndStoreNative();
108 static bool isFetchAndStoreWaitFree();
109
110 int fetchAndStoreRelaxed(int newValue);
111 int fetchAndStoreAcquire(int newValue);
112 int fetchAndStoreRelease(int newValue);
113 int fetchAndStoreOrdered(int newValue);
114
115 static bool isFetchAndAddNative();
116 static bool isFetchAndAddWaitFree();
117
118 int fetchAndAddRelaxed(int valueToAdd);
119 int fetchAndAddAcquire(int valueToAdd);
120 int fetchAndAddRelease(int valueToAdd);
121 int fetchAndAddOrdered(int valueToAdd);
122};
123
124template <typename T>
125class QBasicAtomicPointer
126{
127public:
128#ifdef QT_ARCH_PARISC
129 int _q_lock[4];
130#endif
131 T * volatile _q_value;
132
133 // Non-atomic API
134 inline bool operator==(T *value) const
135 {
136 return _q_value == value;
137 }
138
139 inline bool operator!=(T *value) const
140 {
141 return !operator==(value);
142 }
143
144 inline bool operator!() const
145 {
146 return operator==(0);
147 }
148
149 inline operator T *() const
150 {
151 return _q_value;
152 }
153
154 inline T *operator->() const
155 {
156 return _q_value;
157 }
158
159 inline QBasicAtomicPointer<T> &operator=(T *value)
160 {
161#ifdef QT_ARCH_PARISC
162 this->_q_lock[0] = this->_q_lock[1] = this->_q_lock[2] = this->_q_lock[3] = -1;
163#endif
164 _q_value = value;
165 return *this;
166 }
167
168 // Atomic API, implemented in qatomic_XXX.h
169
170 static bool isTestAndSetNative();
171 static bool isTestAndSetWaitFree();
172
173 bool testAndSetRelaxed(T *expectedValue, T *newValue);
174 bool testAndSetAcquire(T *expectedValue, T *newValue);
175 bool testAndSetRelease(T *expectedValue, T *newValue);
176 bool testAndSetOrdered(T *expectedValue, T *newValue);
177
178 static bool isFetchAndStoreNative();
179 static bool isFetchAndStoreWaitFree();
180
181 T *fetchAndStoreRelaxed(T *newValue);
182 T *fetchAndStoreAcquire(T *newValue);
183 T *fetchAndStoreRelease(T *newValue);
184 T *fetchAndStoreOrdered(T *newValue);
185
186 static bool isFetchAndAddNative();
187 static bool isFetchAndAddWaitFree();
188
189 T *fetchAndAddRelaxed(qptrdiff valueToAdd);
190 T *fetchAndAddAcquire(qptrdiff valueToAdd);
191 T *fetchAndAddRelease(qptrdiff valueToAdd);
192 T *fetchAndAddOrdered(qptrdiff valueToAdd);
193};
194
195#ifdef QT_ARCH_PARISC
196# define Q_BASIC_ATOMIC_INITIALIZER(a) {{-1,-1,-1,-1},(a)}
197#else
198# define Q_BASIC_ATOMIC_INITIALIZER(a) { (a) }
199#endif
200
201QT_END_NAMESPACE
202QT_END_HEADER
203
204#if defined(QT_MOC) || defined(QT_BUILD_QMAKE) || defined(QT_RCC) || defined(QT_UIC) || defined(QT_BOOTSTRAPPED)
205# include <QtCore/qatomic_bootstrap.h>
206#else
207# include <QtCore/qatomic_arch.h>
208#endif
209
210#endif // QBASIC_ATOMIC
Note: See TracBrowser for help on using the repository browser.