Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qatomic.h
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#ifndef QATOMIC_H
7#define QATOMIC_H
8
9#include <QtCore/qbasicatomic.h>
10
12
13QT_WARNING_PUSH
14QT_WARNING_DISABLE_GCC("-Wextra")
15
16// High-level atomic integer operations
17template <typename T>
18class QAtomicInteger : public QBasicAtomicInteger<T>
19{
20public:
21 // Non-atomic API
22 constexpr QAtomicInteger(T value = 0) noexcept : QBasicAtomicInteger<T>(value) {}
23
24 inline QAtomicInteger(const QAtomicInteger &other) noexcept
25 : QBasicAtomicInteger<T>()
26 {
27 this->storeRelease(other.loadAcquire());
28 }
29
30 inline QAtomicInteger &operator=(const QAtomicInteger &other) noexcept
31 {
32 this->storeRelease(other.loadAcquire());
33 return *this;
34 }
35
36#ifdef Q_QDOC
37 T loadRelaxed() const;
38 T loadAcquire() const;
39 void storeRelaxed(T newValue);
40 void storeRelease(T newValue);
41
42 operator T() const;
43 QAtomicInteger &operator=(T);
44
45 static constexpr bool isReferenceCountingNative();
46 static constexpr bool isReferenceCountingWaitFree();
47
48 bool ref();
49 void refRelaxed();
50 bool deref();
51
52 static constexpr bool isTestAndSetNative();
53 static constexpr bool isTestAndSetWaitFree();
54
55 bool testAndSetRelaxed(T expectedValue, T newValue);
56 bool testAndSetAcquire(T expectedValue, T newValue);
57 bool testAndSetRelease(T expectedValue, T newValue);
58 bool testAndSetOrdered(T expectedValue, T newValue);
59
60 bool testAndSetRelaxed(T expectedValue, T newValue, T &currentValue);
61 bool testAndSetAcquire(T expectedValue, T newValue, T &currentValue);
62 bool testAndSetRelease(T expectedValue, T newValue, T &currentValue);
63 bool testAndSetOrdered(T expectedValue, T newValue, T &currentValue);
64
65 static constexpr bool isFetchAndStoreNative();
66 static constexpr bool isFetchAndStoreWaitFree();
67
68 T fetchAndStoreRelaxed(T newValue);
69 T fetchAndStoreAcquire(T newValue);
70 T fetchAndStoreRelease(T newValue);
71 T fetchAndStoreOrdered(T newValue);
72
73 static constexpr bool isFetchAndAddNative();
74 static constexpr bool isFetchAndAddWaitFree();
75
76 T fetchAndAddRelaxed(T valueToAdd);
77 T fetchAndAddAcquire(T valueToAdd);
78 T fetchAndAddRelease(T valueToAdd);
79 T fetchAndAddOrdered(T valueToAdd);
80
81 T fetchAndSubRelaxed(T valueToSub);
82 T fetchAndSubAcquire(T valueToSub);
83 T fetchAndSubRelease(T valueToSub);
84 T fetchAndSubOrdered(T valueToSub);
85
86 T fetchAndOrRelaxed(T valueToOr);
87 T fetchAndOrAcquire(T valueToOr);
88 T fetchAndOrRelease(T valueToOr);
89 T fetchAndOrOrdered(T valueToOr);
90
91 T fetchAndAndRelaxed(T valueToAnd);
92 T fetchAndAndAcquire(T valueToAnd);
93 T fetchAndAndRelease(T valueToAnd);
94 T fetchAndAndOrdered(T valueToAnd);
95
96 T fetchAndXorRelaxed(T valueToXor);
97 T fetchAndXorAcquire(T valueToXor);
98 T fetchAndXorRelease(T valueToXor);
99 T fetchAndXorOrdered(T valueToXor);
100
101 T operator++();
102 T operator++(int);
103 T operator--();
104 T operator--(int);
105 T operator+=(T value);
106 T operator-=(T value);
107 T operator|=(T value);
108 T operator&=(T value);
109 T operator^=(T value);
110#endif
111};
112
113class QAtomicInt : public QAtomicInteger<int>
114{
115public:
116 // Non-atomic API
117 // We could use QT_COMPILER_INHERITING_CONSTRUCTORS, but we need only one;
118 // the implicit definition for all the others is fine.
119 constexpr QAtomicInt(int value = 0) noexcept : QAtomicInteger<int>(value) {}
120};
121
122// High-level atomic pointer operations
123template <typename T>
125{
126public:
127 constexpr QAtomicPointer(T *value = nullptr) noexcept : QBasicAtomicPointer<T>(value) {}
128
129 inline QAtomicPointer(const QAtomicPointer<T> &other) noexcept
131 {
132 this->storeRelease(other.loadAcquire());
133 }
134
135 inline QAtomicPointer<T> &operator=(const QAtomicPointer<T> &other) noexcept
136 {
137 this->storeRelease(other.loadAcquire());
138 return *this;
139 }
140
141#ifdef Q_QDOC
142 T *loadAcquire() const;
143 T *loadRelaxed() const;
144 void storeRelaxed(T *newValue);
145 void storeRelease(T *newValue);
146
147 static constexpr bool isTestAndSetNative();
148 static constexpr bool isTestAndSetWaitFree();
149
154
155 static constexpr bool isFetchAndStoreNative();
156 static constexpr bool isFetchAndStoreWaitFree();
157
162
163 static constexpr bool isFetchAndAddNative();
164 static constexpr bool isFetchAndAddWaitFree();
165
170#endif
171};
172
173QT_WARNING_POP
174
175/*!
176 This is a helper for the assignment operators of implicitly
177 shared classes. Your assignment operator should look like this:
178
179 \snippet code/src.corelib.thread.qatomic.h 0
180*/
181template <typename T>
182inline void qAtomicAssign(T *&d, T *x)
183{
184 if (d == x)
185 return;
186 x->ref.ref();
187 if (!d->ref.deref())
188 delete d;
189 d = x;
190}
191
192/*!
193 This is a helper for the detach method of implicitly shared
194 classes. Your private class needs a copy constructor which copies
195 the members and sets the refcount to 1. After that, your detach
196 function should look like this:
197
198 \snippet code/src.corelib.thread.qatomic.h 1
199*/
200template <typename T>
201inline void qAtomicDetach(T *&d)
202{
203 if (d->ref.loadRelaxed() == 1)
204 return;
205 T *x = d;
206 d = new T(*d);
207 if (!x->ref.deref())
208 delete x;
209}
210
211QT_END_NAMESPACE
212#endif // QATOMIC_H
\inmodule QtCore
Definition qatomic.h:114
constexpr QAtomicInt(int value=0) noexcept
Constructs a QAtomicInt with the given value.
Definition qatomic.h:119
\macro Q_ATOMIC_INTnn_IS_SUPPORTED
Definition qatomic.h:125
QAtomicPointer(const QAtomicPointer< T > &other) noexcept
Constructs a copy of other.
Definition qatomic.h:129
QAtomicPointer< T > & operator=(const QAtomicPointer< T > &other) noexcept
Assigns other to this QAtomicPointer and returns a reference to this QAtomicPointer.
Definition qatomic.h:135
constexpr QAtomicPointer(T *value=nullptr) noexcept
Constructs a QAtomicPointer with the given value.
Definition qatomic.h:127
QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data, std::unique_ptr< QAbstractFileEngine > engine)
Definition qfileinfo_p.h:98
bool const isDefaultConstructed
QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data)
Definition qfileinfo_p.h:83
Ret checkAttribute(QFileSystemMetaData::MetaDataFlags fsFlags, FSLambda fsLambda, EngineLambda engineLambda) const
QFileSystemMetaData metaData
QDateTime & getFileTime(QFile::FileTime) const
std::unique_ptr< QAbstractFileEngine > const fileEngine
QDateTime fileTimes[4]
uint getFileFlags(QAbstractFileEngine::FileFlags) const
void clearFlags() const
void setCachedFlag(uint c) const
QString getFileOwner(QAbstractFileEngine::FileOwner own) const
Definition qfileinfo.cpp:76
QString fileOwners[2]
bool getCachedFlag(uint c) const
QFileInfoPrivate(const QFileInfoPrivate &copy)
Definition qfileinfo_p.h:57
QFileSystemEntry fileEntry
static QFileInfoPrivate * get(QFileInfo *fi)
Definition qfileinfo_p.h:49
QString getFileName(QAbstractFileEngine::FileName) const
Definition qfileinfo.cpp:18
QString fileNames[QAbstractFileEngine::NFileNames]
QFileInfoPrivate(const QString &file)
Definition qfileinfo_p.h:70
Ret checkAttribute(Ret defaultValue, QFileSystemMetaData::MetaDataFlags fsFlags, FSLambda fsLambda, EngineLambda engineLambda) const
\inmodule QtCore
Definition qshareddata.h:38
QSharedData() noexcept
Constructs a QSharedData object with a reference count of 0.
Definition qshareddata.h:42
Combined button and popup list for selecting options.
void qAtomicDetach(T *&d)
This is a helper for the detach method of implicitly shared classes.
Definition qatomic.h:201
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)