1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml 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 https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://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 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QQMLTHREAD_P_H |
41 | #define QQMLTHREAD_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | |
55 | #include <QtCore/qglobal.h> |
56 | |
57 | #include <private/qintrusivelist_p.h> |
58 | |
59 | QT_BEGIN_NAMESPACE |
60 | |
61 | class QThread; |
62 | class QMutex; |
63 | |
64 | class QQmlThreadPrivate; |
65 | class QQmlThread |
66 | { |
67 | public: |
68 | QQmlThread(); |
69 | virtual ~QQmlThread(); |
70 | |
71 | void startup(); |
72 | void shutdown(); |
73 | bool isShutdown() const; |
74 | |
75 | QMutex &mutex(); |
76 | void lock(); |
77 | void unlock(); |
78 | void wakeOne(); |
79 | void wakeAll(); |
80 | void wait(); |
81 | |
82 | QThread *thread() const; |
83 | bool isThisThread() const; |
84 | |
85 | // Synchronously invoke a method in the thread |
86 | template<class O> |
87 | inline void callMethodInThread(void (O::*Member)()); |
88 | template<typename T, class V, class O> |
89 | inline void callMethodInThread(void (O::*Member)(V), const T &); |
90 | template<typename T, typename T2, class V, class V2, class O> |
91 | inline void callMethodInThread(void (O::*Member)(V, V2), const T &, const T2 &); |
92 | |
93 | // Synchronously invoke a method in the main thread. If the main thread is |
94 | // blocked in a callMethodInThread() call, the call is made from within that |
95 | // call. |
96 | template<class O> |
97 | inline void callMethodInMain(void (O::*Member)()); |
98 | template<typename T, class V, class O> |
99 | inline void callMethodInMain(void (O::*Member)(V), const T &); |
100 | template<typename T, typename T2, class V, class V2, class O> |
101 | inline void callMethodInMain(void (O::*Member)(V, V2), const T &, const T2 &); |
102 | |
103 | // Asynchronously invoke a method in the thread. |
104 | template<class O> |
105 | inline void postMethodToThread(void (O::*Member)()); |
106 | template<typename T, class V, class O> |
107 | inline void postMethodToThread(void (O::*Member)(V), const T &); |
108 | template<typename T, typename T2, class V, class V2, class O> |
109 | inline void postMethodToThread(void (O::*Member)(V, V2), const T &, const T2 &); |
110 | |
111 | // Asynchronously invoke a method in the main thread. |
112 | template<class O> |
113 | inline void postMethodToMain(void (O::*Member)()); |
114 | template<typename T, class V, class O> |
115 | inline void postMethodToMain(void (O::*Member)(V), const T &); |
116 | template<typename T, typename T2, class V, class V2, class O> |
117 | inline void postMethodToMain(void (O::*Member)(V, V2), const T &, const T2 &); |
118 | |
119 | void waitForNextMessage(); |
120 | |
121 | protected: |
122 | virtual void startupThread(); |
123 | virtual void shutdownThread(); |
124 | |
125 | private: |
126 | friend class QQmlThreadPrivate; |
127 | |
128 | struct Message { |
129 | Message() : next(nullptr) {} |
130 | virtual ~Message() {} |
131 | Message *next; |
132 | virtual void call(QQmlThread *) = 0; |
133 | }; |
134 | void internalCallMethodInThread(Message *); |
135 | void internalCallMethodInMain(Message *); |
136 | void internalPostMethodToThread(Message *); |
137 | void internalPostMethodToMain(Message *); |
138 | QQmlThreadPrivate *d; |
139 | }; |
140 | |
141 | template<class O> |
142 | void QQmlThread::callMethodInThread(void (O::*Member)()) |
143 | { |
144 | struct I : public Message { |
145 | void (O::*Member)(); |
146 | I(void (O::*Member)()) : Member(Member) {} |
147 | void call(QQmlThread *thread) override { |
148 | O *me = static_cast<O *>(thread); |
149 | (me->*Member)(); |
150 | } |
151 | }; |
152 | internalCallMethodInThread(new I(Member)); |
153 | } |
154 | |
155 | template<typename T, class V, class O> |
156 | void QQmlThread::callMethodInThread(void (O::*Member)(V), const T &arg) |
157 | { |
158 | struct I : public Message { |
159 | void (O::*Member)(V); |
160 | T arg; |
161 | I(void (O::*Member)(V), const T &arg) : Member(Member), arg(arg) {} |
162 | void call(QQmlThread *thread) override { |
163 | O *me = static_cast<O *>(thread); |
164 | (me->*Member)(arg); |
165 | } |
166 | }; |
167 | internalCallMethodInThread(new I(Member, arg)); |
168 | } |
169 | |
170 | template<typename T, typename T2, class V, class V2, class O> |
171 | void QQmlThread::callMethodInThread(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) |
172 | { |
173 | struct I : public Message { |
174 | void (O::*Member)(V, V2); |
175 | T arg; |
176 | T2 arg2; |
177 | I(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) : Member(Member), arg(arg), arg2(arg2) {} |
178 | void call(QQmlThread *thread) override { |
179 | O *me = static_cast<O *>(thread); |
180 | (me->*Member)(arg, arg2); |
181 | } |
182 | }; |
183 | internalCallMethodInThread(new I(Member, arg, arg2)); |
184 | } |
185 | |
186 | template<class O> |
187 | void QQmlThread::callMethodInMain(void (O::*Member)()) |
188 | { |
189 | struct I : public Message { |
190 | void (O::*Member)(); |
191 | I(void (O::*Member)()) : Member(Member) {} |
192 | void call(QQmlThread *thread) override { |
193 | O *me = static_cast<O *>(thread); |
194 | (me->*Member)(); |
195 | } |
196 | }; |
197 | internalCallMethodInMain(new I(Member)); |
198 | } |
199 | |
200 | template<typename T, class V, class O> |
201 | void QQmlThread::callMethodInMain(void (O::*Member)(V), const T &arg) |
202 | { |
203 | struct I : public Message { |
204 | void (O::*Member)(V); |
205 | T arg; |
206 | I(void (O::*Member)(V), const T &arg) : Member(Member), arg(arg) {} |
207 | void call(QQmlThread *thread) override { |
208 | O *me = static_cast<O *>(thread); |
209 | (me->*Member)(arg); |
210 | } |
211 | }; |
212 | internalCallMethodInMain(new I(Member, arg)); |
213 | } |
214 | |
215 | template<typename T, typename T2, class V, class V2, class O> |
216 | void QQmlThread::callMethodInMain(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) |
217 | { |
218 | struct I : public Message { |
219 | void (O::*Member)(V, V2); |
220 | T arg; |
221 | T2 arg2; |
222 | I(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) : Member(Member), arg(arg), arg2(arg2) {} |
223 | void call(QQmlThread *thread) override { |
224 | O *me = static_cast<O *>(thread); |
225 | (me->*Member)(arg, arg2); |
226 | } |
227 | }; |
228 | internalCallMethodInMain(new I(Member, arg, arg2)); |
229 | } |
230 | |
231 | template<class O> |
232 | void QQmlThread::postMethodToThread(void (O::*Member)()) |
233 | { |
234 | struct I : public Message { |
235 | void (O::*Member)(); |
236 | I(void (O::*Member)()) : Member(Member) {} |
237 | void call(QQmlThread *thread) override { |
238 | O *me = static_cast<O *>(thread); |
239 | (me->*Member)(); |
240 | } |
241 | }; |
242 | internalPostMethodToThread(new I(Member)); |
243 | } |
244 | |
245 | template<typename T, class V, class O> |
246 | void QQmlThread::postMethodToThread(void (O::*Member)(V), const T &arg) |
247 | { |
248 | struct I : public Message { |
249 | void (O::*Member)(V); |
250 | T arg; |
251 | I(void (O::*Member)(V), const T &arg) : Member(Member), arg(arg) {} |
252 | void call(QQmlThread *thread) override { |
253 | O *me = static_cast<O *>(thread); |
254 | (me->*Member)(arg); |
255 | } |
256 | }; |
257 | internalPostMethodToThread(new I(Member, arg)); |
258 | } |
259 | |
260 | template<typename T, typename T2, class V, class V2, class O> |
261 | void QQmlThread::postMethodToThread(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) |
262 | { |
263 | struct I : public Message { |
264 | void (O::*Member)(V, V2); |
265 | T arg; |
266 | T2 arg2; |
267 | I(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) : Member(Member), arg(arg), arg2(arg2) {} |
268 | void call(QQmlThread *thread) override { |
269 | O *me = static_cast<O *>(thread); |
270 | (me->*Member)(arg, arg2); |
271 | } |
272 | }; |
273 | internalPostMethodToThread(new I(Member, arg, arg2)); |
274 | } |
275 | |
276 | template<class O> |
277 | void QQmlThread::postMethodToMain(void (O::*Member)()) |
278 | { |
279 | struct I : public Message { |
280 | void (O::*Member)(); |
281 | I(void (O::*Member)()) : Member(Member) {} |
282 | void call(QQmlThread *thread) override { |
283 | O *me = static_cast<O *>(thread); |
284 | (me->*Member)(); |
285 | } |
286 | }; |
287 | internalPostMethodToMain(new I(Member)); |
288 | } |
289 | |
290 | template<typename T, class V, class O> |
291 | void QQmlThread::postMethodToMain(void (O::*Member)(V), const T &arg) |
292 | { |
293 | struct I : public Message { |
294 | void (O::*Member)(V); |
295 | T arg; |
296 | I(void (O::*Member)(V), const T &arg) : Member(Member), arg(arg) {} |
297 | void call(QQmlThread *thread) override { |
298 | O *me = static_cast<O *>(thread); |
299 | (me->*Member)(arg); |
300 | } |
301 | }; |
302 | internalPostMethodToMain(new I(Member, arg)); |
303 | } |
304 | |
305 | template<typename T, typename T2, class V, class V2, class O> |
306 | void QQmlThread::postMethodToMain(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) |
307 | { |
308 | struct I : public Message { |
309 | void (O::*Member)(V, V2); |
310 | T arg; |
311 | T2 arg2; |
312 | I(void (O::*Member)(V, V2), const T &arg, const T2 &arg2) : Member(Member), arg(arg), arg2(arg2) {} |
313 | void call(QQmlThread *thread) override { |
314 | O *me = static_cast<O *>(thread); |
315 | (me->*Member)(arg, arg2); |
316 | } |
317 | }; |
318 | internalPostMethodToMain(new I(Member, arg, arg2)); |
319 | } |
320 | |
321 | QT_END_NAMESPACE |
322 | |
323 | #endif // QQMLTHREAD_P_H |
324 | |