1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui 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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QATOMIC_INTEGRITY_H
43 #define QATOMIC_INTEGRITY_H
44 
45 #include <INTEGRITY.h>
46 
47 QT_BEGIN_HEADER
48 
49 QT_BEGIN_NAMESPACE
50 
51 #define qt_pp2addrp(a)  reinterpret_cast<Address *>(const_cast<T **>(a))
52 #define qt_ip2addrp(a) reinterpret_cast<Address *>(const_cast<int *>(a))
53 #define qt_p2addrp(a) reinterpret_cast<Address *>(a)
54 #define qt_addr(a) reinterpret_cast<Address>(a)
55 
56 
57 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_ALWAYS_NATIVE
58 
isReferenceCountingNative()59 inline bool QBasicAtomicInt::isReferenceCountingNative()
60 { return true; }
61 
62 #define Q_ATOMIC_INT_REFERENCE_COUNTING_IS_WAIT_FREE
63 
isReferenceCountingWaitFree()64 inline bool QBasicAtomicInt::isReferenceCountingWaitFree()
65 { return true; }
66 
67 #define Q_ATOMIC_INT_TEST_AND_SET_IS_ALWAYS_NATIVE
68 
isTestAndSetNative()69 inline bool QBasicAtomicInt::isTestAndSetNative()
70 { return true; }
71 
72 #define Q_ATOMIC_INT_TEST_AND_SET_IS_WAIT_FREE
73 
isTestAndSetWaitFree()74 inline bool QBasicAtomicInt::isTestAndSetWaitFree()
75 { return true; }
76 
77 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_ALWAYS_NATIVE
78 
isFetchAndStoreNative()79 inline bool QBasicAtomicInt::isFetchAndStoreNative()
80 { return true; }
81 
82 #define Q_ATOMIC_INT_FETCH_AND_STORE_IS_WAIT_FREE
83 
isFetchAndStoreWaitFree()84 inline bool QBasicAtomicInt::isFetchAndStoreWaitFree()
85 { return true; }
86 
87 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_ALWAYS_NATIVE
88 
isFetchAndAddNative()89 inline bool QBasicAtomicInt::isFetchAndAddNative()
90 { return true; }
91 
92 #define Q_ATOMIC_INT_FETCH_AND_ADD_IS_WAIT_FREE
93 
isFetchAndAddWaitFree()94 inline bool QBasicAtomicInt::isFetchAndAddWaitFree()
95 { return true; }
96 
97 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
98 
99 template <typename T>
isTestAndSetNative()100 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetNative()
101 { return true; }
102 
103 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_WAIT_FREE
104 
105 template <typename T>
isTestAndSetWaitFree()106 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isTestAndSetWaitFree()
107 { return true; }
108 
109 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_ALWAYS_NATIVE
110 
111 template <typename T>
isFetchAndStoreNative()112 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreNative()
113 { return true; }
114 
115 #define Q_ATOMIC_POINTER_FETCH_AND_STORE_IS_WAIT_FREE
116 
117 template <typename T>
isFetchAndStoreWaitFree()118 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndStoreWaitFree()
119 { return true; }
120 
121 #define Q_ATOMIC_POINTER_FETCH_AND_ADD_IS_ALWAYS_NATIVE
122 
123 template <typename T>
isFetchAndAddNative()124 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddNative()
125 { return true; }
126 
127 #define Q_ATOMIC_POINTER_TEST_AND_SET_IS_WAIT_FREE
128 
129 template <typename T>
isFetchAndAddWaitFree()130 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::isFetchAndAddWaitFree()
131 { return true; }
132 
133 // Reference counting
134 
ref()135 inline bool QBasicAtomicInt::ref()
136 {
137     int old_value;
138     AtomicModify(qt_ip2addrp(&_q_value), qt_ip2addrp(&old_value), 0, 1U);
139     return _q_value != 0;
140 }
141 
deref()142 inline bool QBasicAtomicInt::deref()
143 {
144     int old_value;
145     AtomicModify(qt_ip2addrp(&_q_value), qt_ip2addrp(&old_value), 0, -1U);
146     return _q_value != 0;
147 }
148 
149 // Test and set for integers
150 
testAndSetOrdered(int expectedValue,int newValue)151 inline bool QBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
152 {
153     return TestAndSet(qt_ip2addrp(&_q_value), expectedValue, newValue) == Success;
154 }
155 
testAndSetRelaxed(int expectedValue,int newValue)156 inline bool QBasicAtomicInt::testAndSetRelaxed(int expectedValue, int newValue)
157 {
158     return testAndSetOrdered(expectedValue, newValue);
159 }
160 
testAndSetAcquire(int expectedValue,int newValue)161 inline bool QBasicAtomicInt::testAndSetAcquire(int expectedValue, int newValue)
162 {
163     return testAndSetOrdered(expectedValue, newValue);
164 }
165 
testAndSetRelease(int expectedValue,int newValue)166 inline bool QBasicAtomicInt::testAndSetRelease(int expectedValue, int newValue)
167 {
168     return testAndSetOrdered(expectedValue, newValue);
169 }
170 
171 // Fetch and store for integers
172 
fetchAndStoreOrdered(int newValue)173 inline int QBasicAtomicInt::fetchAndStoreOrdered(int newValue)
174 {
175     int old_value;
176     do {
177         old_value = _q_value;
178     } while (TestAndSet(qt_ip2addrp(&_q_value), old_value, newValue) != Success);
179     return old_value;
180 }
181 
fetchAndStoreRelaxed(int newValue)182 inline int QBasicAtomicInt::fetchAndStoreRelaxed(int newValue)
183 {
184     return fetchAndStoreOrdered(newValue);
185 }
186 
fetchAndStoreAcquire(int newValue)187 inline int QBasicAtomicInt::fetchAndStoreAcquire(int newValue)
188 {
189     return fetchAndStoreOrdered(newValue);
190 }
191 
fetchAndStoreRelease(int newValue)192 inline int QBasicAtomicInt::fetchAndStoreRelease(int newValue)
193 {
194     return fetchAndStoreOrdered(newValue);
195 }
196 
197 // Fetch and add for integers
198 
fetchAndAddOrdered(int valueToAdd)199 inline int QBasicAtomicInt::fetchAndAddOrdered(int valueToAdd)
200 {
201     int old_value;
202     AtomicModify(qt_ip2addrp(&_q_value), qt_ip2addrp(&old_value), 0, valueToAdd);
203 
204     return old_value;
205 }
206 
fetchAndAddRelaxed(int valueToAdd)207 inline int QBasicAtomicInt::fetchAndAddRelaxed(int valueToAdd)
208 {
209     return fetchAndAddOrdered(valueToAdd);
210 }
211 
fetchAndAddAcquire(int valueToAdd)212 inline int QBasicAtomicInt::fetchAndAddAcquire(int valueToAdd)
213 {
214     return fetchAndAddOrdered(valueToAdd);
215 }
216 
fetchAndAddRelease(int valueToAdd)217 inline int QBasicAtomicInt::fetchAndAddRelease(int valueToAdd)
218 {
219     return fetchAndAddOrdered(valueToAdd);
220 }
221 
222 // Test and set for pointers
223 
224 template <typename T>
testAndSetOrdered(T * expectedValue,T * newValue)225 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetOrdered(T *expectedValue, T *newValue)
226 {
227     return TestAndSet(qt_pp2addrp(&_q_value), qt_addr(expectedValue), qt_addr(newValue)) == Success;
228 }
229 
230 template <typename T>
testAndSetRelaxed(T * expectedValue,T * newValue)231 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelaxed(T *expectedValue, T *newValue)
232 {
233     return testAndSetOrdered(expectedValue, newValue);
234 }
235 
236 template <typename T>
testAndSetAcquire(T * expectedValue,T * newValue)237 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetAcquire(T *expectedValue, T *newValue)
238 {
239     return testAndSetOrdered(expectedValue, newValue);
240 }
241 
242 template <typename T>
testAndSetRelease(T * expectedValue,T * newValue)243 Q_INLINE_TEMPLATE bool QBasicAtomicPointer<T>::testAndSetRelease(T *expectedValue, T *newValue)
244 {
245     return testAndSetOrdered(expectedValue, newValue);
246 }
247 
248 // Fetch and store for pointers
249 
250 template <typename T>
fetchAndStoreOrdered(T * newValue)251 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreOrdered(T *newValue)
252 {
253     Address old_value;
254 
255     do {
256         old_value = qt_addr(_q_value);
257     } while (TestAndSet(qt_pp2addrp(&_q_value), old_value, qt_addr(newValue)) != Success);
258 
259     return reinterpret_cast<T *>(old_value);
260 }
261 
262 template <typename T>
fetchAndStoreRelaxed(T * newValue)263 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelaxed(T *newValue)
264 {
265     return fetchAndStoreOrdered(newValue);
266 }
267 
268 template <typename T>
fetchAndStoreAcquire(T * newValue)269 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreAcquire(T *newValue)
270 {
271     return fetchAndStoreOrdered(newValue);
272 }
273 
274 template <typename T>
fetchAndStoreRelease(T * newValue)275 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndStoreRelease(T *newValue)
276 {
277     return fetchAndStoreOrdered(newValue);
278 }
279 
280 // Fetch and add for pointers
281 
282 template <typename T>
fetchAndAddOrdered(qptrdiff valueToAdd)283 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddOrdered(qptrdiff valueToAdd)
284 {
285     Address old_value;
286     AtomicModify(qt_pp2addrp(&_q_value), &old_value, 0, valueToAdd * sizeof(T));
287 
288     return reinterpret_cast<T *>(old_value);
289 }
290 
291 template <typename T>
fetchAndAddRelaxed(qptrdiff valueToAdd)292 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelaxed(qptrdiff valueToAdd)
293 {
294     return fetchAndAddOrdered(valueToAdd);
295 }
296 
297 template <typename T>
fetchAndAddAcquire(qptrdiff valueToAdd)298 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddAcquire(qptrdiff valueToAdd)
299 {
300     return fetchAndAddOrdered(valueToAdd);
301 }
302 
303 template <typename T>
fetchAndAddRelease(qptrdiff valueToAdd)304 Q_INLINE_TEMPLATE T *QBasicAtomicPointer<T>::fetchAndAddRelease(qptrdiff valueToAdd)
305 {
306     return fetchAndAddOrdered(valueToAdd);
307 }
308 
309 QT_END_NAMESPACE
310 
311 QT_END_HEADER
312 
313 #endif // QATOMIC_INTEGRITY_H
314