| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QREFCOUNT_H |
| 5 | #define QREFCOUNT_H |
| 6 | |
| 7 | #include <QtCore/qatomic.h> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | |
| 12 | namespace QtPrivate |
| 13 | { |
| 14 | |
| 15 | class RefCount |
| 16 | { |
| 17 | public: |
| 18 | inline bool ref() noexcept { |
| 19 | int count = atomic.loadRelaxed(); |
| 20 | if (count != -1) // !isStatic |
| 21 | atomic.ref(); |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | inline bool deref() noexcept { |
| 26 | int count = atomic.loadRelaxed(); |
| 27 | if (count == -1) // isStatic |
| 28 | return true; |
| 29 | return atomic.deref(); |
| 30 | } |
| 31 | |
| 32 | bool isStatic() const noexcept |
| 33 | { |
| 34 | // Persistent object, never deleted |
| 35 | return atomic.loadRelaxed() == -1; |
| 36 | } |
| 37 | |
| 38 | bool isShared() const noexcept |
| 39 | { |
| 40 | int count = atomic.loadRelaxed(); |
| 41 | return (count != 1) && (count != 0); |
| 42 | } |
| 43 | |
| 44 | void initializeOwned() noexcept { atomic.storeRelaxed(newValue: 1); } |
| 45 | void initializeUnsharable() noexcept { atomic.storeRelaxed(newValue: 0); } |
| 46 | |
| 47 | QBasicAtomicInt atomic; |
| 48 | }; |
| 49 | |
| 50 | } |
| 51 | |
| 52 | #define Q_REFCOUNT_INITIALIZE_STATIC { Q_BASIC_ATOMIC_INITIALIZER(-1) } |
| 53 | |
| 54 | QT_END_NAMESPACE |
| 55 | |
| 56 | #endif |
| 57 | |