1 | // Copyright (C) 2019 The Qt Company Ltd. |
2 | // Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com> |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #ifndef QOBJECT_P_P_H |
6 | #define QOBJECT_P_P_H |
7 | |
8 | // |
9 | // W A R N I N G |
10 | // ------------- |
11 | // |
12 | // This file is not part of the Qt API. It exists for the convenience |
13 | // of qobject.cpp. This header file may change from version to version |
14 | // without notice, or even be removed. |
15 | // |
16 | // We mean it. |
17 | // |
18 | |
19 | // Even though this file is only used by qobject.cpp, the only reason this |
20 | // code lives here is that some special apps/libraries for e.g., QtJambi, |
21 | // Gammaray need access to the structs in this file. |
22 | |
23 | #include <QtCore/qobject.h> |
24 | #include <QtCore/private/qobject_p.h> |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | // ConnectionList is a singly-linked list |
29 | struct QObjectPrivate::ConnectionList |
30 | { |
31 | QAtomicPointer<Connection> first; |
32 | QAtomicPointer<Connection> last; |
33 | }; |
34 | static_assert(std::is_trivially_destructible_v<QObjectPrivate::ConnectionList>); |
35 | Q_DECLARE_TYPEINFO(QObjectPrivate::ConnectionList, Q_RELOCATABLE_TYPE); |
36 | |
37 | struct QObjectPrivate::TaggedSignalVector |
38 | { |
39 | quintptr c; |
40 | |
41 | TaggedSignalVector() = default; |
42 | TaggedSignalVector(std::nullptr_t) noexcept : c(0) {} |
43 | TaggedSignalVector(Connection *v) noexcept : c(reinterpret_cast<quintptr>(v)) { Q_ASSERT(v && (reinterpret_cast<quintptr>(v) & 0x1) == 0); } |
44 | TaggedSignalVector(SignalVector *v) noexcept : c(reinterpret_cast<quintptr>(v) | quintptr(1u)) { Q_ASSERT(v); } |
45 | explicit operator SignalVector *() const noexcept |
46 | { |
47 | if (c & 0x1) |
48 | return reinterpret_cast<SignalVector *>(c & ~quintptr(1u)); |
49 | return nullptr; |
50 | } |
51 | explicit operator Connection *() const noexcept |
52 | { |
53 | return reinterpret_cast<Connection *>(c); |
54 | } |
55 | operator uintptr_t() const noexcept { return c; } |
56 | }; |
57 | |
58 | struct QObjectPrivate::ConnectionOrSignalVector |
59 | { |
60 | union { |
61 | // linked list of orphaned connections that need cleaning up |
62 | TaggedSignalVector nextInOrphanList; |
63 | // linked list of connections connected to slots in this object |
64 | Connection *next; |
65 | }; |
66 | }; |
67 | static_assert(std::is_trivial_v<QObjectPrivate::ConnectionOrSignalVector>); |
68 | |
69 | struct QObjectPrivate::Connection : public ConnectionOrSignalVector |
70 | { |
71 | // linked list of connections connected to slots in this object, next is in base class |
72 | Connection **prev; |
73 | // linked list of connections connected to signals in this object |
74 | QAtomicPointer<Connection> nextConnectionList; |
75 | Connection *prevConnectionList; |
76 | |
77 | QObject *sender; |
78 | QAtomicPointer<QObject> receiver; |
79 | QAtomicPointer<QThreadData> receiverThreadData; |
80 | union { |
81 | StaticMetaCallFunction callFunction; |
82 | QtPrivate::QSlotObjectBase *slotObj; |
83 | }; |
84 | QAtomicPointer<const int> argumentTypes; |
85 | QAtomicInt ref_{ |
86 | 2 |
87 | }; // ref_ is 2 for the use in the internal lists, and for the use in QMetaObject::Connection |
88 | uint id = 0; |
89 | ushort method_offset; |
90 | ushort method_relative; |
91 | signed int signal_index : 27; // In signal range (see QObjectPrivate::signalIndex()) |
92 | ushort connectionType : 2; // 0 == auto, 1 == direct, 2 == queued, 3 == blocking |
93 | ushort isSlotObject : 1; |
94 | ushort ownArgumentTypes : 1; |
95 | ushort isSingleShot : 1; |
96 | Connection() : ownArgumentTypes(true) { } |
97 | ~Connection(); |
98 | int method() const |
99 | { |
100 | Q_ASSERT(!isSlotObject); |
101 | return method_offset + method_relative; |
102 | } |
103 | void ref() { ref_.ref(); } |
104 | void freeSlotObject() |
105 | { |
106 | if (isSlotObject) { |
107 | slotObj->destroyIfLastRef(); |
108 | isSlotObject = false; |
109 | } |
110 | } |
111 | void deref() |
112 | { |
113 | if (!ref_.deref()) { |
114 | Q_ASSERT(!receiver.loadRelaxed()); |
115 | Q_ASSERT(!isSlotObject); |
116 | delete this; |
117 | } |
118 | } |
119 | }; |
120 | Q_DECLARE_TYPEINFO(QObjectPrivate::Connection, Q_RELOCATABLE_TYPE); |
121 | |
122 | struct QObjectPrivate::SignalVector : public ConnectionOrSignalVector |
123 | { |
124 | quintptr allocated; |
125 | // ConnectionList signals[] |
126 | ConnectionList &at(int i) { return reinterpret_cast<ConnectionList *>(this + 1)[i + 1]; } |
127 | const ConnectionList &at(int i) const |
128 | { |
129 | return reinterpret_cast<const ConnectionList *>(this + 1)[i + 1]; |
130 | } |
131 | int count() const { return static_cast<int>(allocated); } |
132 | }; |
133 | static_assert( |
134 | std::is_trivial_v<QObjectPrivate::SignalVector>); // it doesn't need to be, but it helps |
135 | |
136 | struct QObjectPrivate::ConnectionData |
137 | { |
138 | // the id below is used to avoid activating new connections. When the object gets |
139 | // deleted it's set to 0, so that signal emission stops |
140 | QAtomicInteger<uint> currentConnectionId; |
141 | QAtomicInt ref; |
142 | QAtomicPointer<SignalVector> signalVector; |
143 | Connection *senders = nullptr; |
144 | Sender *currentSender = nullptr; // object currently activating the object |
145 | std::atomic<TaggedSignalVector> orphaned = {}; |
146 | |
147 | ~ConnectionData() |
148 | { |
149 | Q_ASSERT(ref.loadRelaxed() == 0); |
150 | TaggedSignalVector c = orphaned.exchange(i: nullptr, m: std::memory_order_relaxed); |
151 | if (c) |
152 | deleteOrphaned(o: c); |
153 | SignalVector *v = signalVector.loadRelaxed(); |
154 | if (v) { |
155 | v->~SignalVector(); |
156 | free(ptr: v); |
157 | } |
158 | } |
159 | |
160 | // must be called on the senders connection data |
161 | // assumes the senders and receivers lock are held |
162 | void removeConnection(Connection *c); |
163 | enum LockPolicy { |
164 | NeedToLock, |
165 | // Beware that we need to temporarily release the lock |
166 | // and thus calling code must carefully consider whether |
167 | // invariants still hold. |
168 | AlreadyLockedAndTemporarilyReleasingLock |
169 | }; |
170 | void cleanOrphanedConnections(QObject *sender, LockPolicy lockPolicy = NeedToLock) |
171 | { |
172 | if (orphaned.load(m: std::memory_order_relaxed) && ref.loadAcquire() == 1) |
173 | cleanOrphanedConnectionsImpl(sender, lockPolicy); |
174 | } |
175 | void cleanOrphanedConnectionsImpl(QObject *sender, LockPolicy lockPolicy); |
176 | |
177 | ConnectionList &connectionsForSignal(int signal) |
178 | { |
179 | return signalVector.loadRelaxed()->at(i: signal); |
180 | } |
181 | |
182 | void resizeSignalVector(uint size) |
183 | { |
184 | SignalVector *vector = this->signalVector.loadRelaxed(); |
185 | if (vector && vector->allocated > size) |
186 | return; |
187 | size = (size + 7) & ~7; |
188 | void *ptr = malloc(size: sizeof(SignalVector) + (size + 1) * sizeof(ConnectionList)); |
189 | auto newVector = new (ptr) SignalVector; |
190 | |
191 | int start = -1; |
192 | if (vector) { |
193 | // not (yet) existing trait: |
194 | // static_assert(std::is_relocatable_v<SignalVector>); |
195 | // static_assert(std::is_relocatable_v<ConnectionList>); |
196 | memcpy(dest: newVector, src: vector, |
197 | n: sizeof(SignalVector) + (vector->allocated + 1) * sizeof(ConnectionList)); |
198 | start = vector->count(); |
199 | } |
200 | for (int i = start; i < int(size); ++i) |
201 | new (&newVector->at(i)) ConnectionList(); |
202 | newVector->next = nullptr; |
203 | newVector->allocated = size; |
204 | |
205 | signalVector.storeRelaxed(newValue: newVector); |
206 | if (vector) { |
207 | TaggedSignalVector o = nullptr; |
208 | /* No ABA issue here: When adding a node, we only care about the list head, it doesn't |
209 | * matter if the tail changes. |
210 | */ |
211 | o = orphaned.load(m: std::memory_order_acquire); |
212 | do { |
213 | vector->nextInOrphanList = o; |
214 | } while (!orphaned.compare_exchange_strong(e&: o, i: TaggedSignalVector(vector), m: std::memory_order_release)); |
215 | } |
216 | } |
217 | int signalVectorCount() const |
218 | { |
219 | return signalVector.loadAcquire() ? signalVector.loadRelaxed()->count() : -1; |
220 | } |
221 | |
222 | static void deleteOrphaned(TaggedSignalVector o); |
223 | }; |
224 | |
225 | struct QObjectPrivate::Sender |
226 | { |
227 | Sender(QObject *receiver, QObject *sender, int signal) |
228 | : receiver(receiver), sender(sender), signal(signal) |
229 | { |
230 | if (receiver) { |
231 | ConnectionData *cd = receiver->d_func()->connections.loadRelaxed(); |
232 | previous = cd->currentSender; |
233 | cd->currentSender = this; |
234 | } |
235 | } |
236 | ~Sender() |
237 | { |
238 | if (receiver) |
239 | receiver->d_func()->connections.loadRelaxed()->currentSender = previous; |
240 | } |
241 | void receiverDeleted() |
242 | { |
243 | Sender *s = this; |
244 | while (s) { |
245 | s->receiver = nullptr; |
246 | s = s->previous; |
247 | } |
248 | } |
249 | Sender *previous; |
250 | QObject *receiver; |
251 | QObject *sender; |
252 | int signal; |
253 | }; |
254 | Q_DECLARE_TYPEINFO(QObjectPrivate::Sender, Q_RELOCATABLE_TYPE); |
255 | |
256 | QT_END_NAMESPACE |
257 | |
258 | #endif |
259 | |