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
qqmlthread_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QQMLTHREAD_P_H
6#define QQMLTHREAD_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 purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19
20#include <QtCore/qglobal.h>
21
22#include <private/qintrusivelist_p.h>
23
24QT_BEGIN_NAMESPACE
25
26class QThread;
27class QMutex;
28
31{
32public:
33 QQmlThread();
34 virtual ~QQmlThread();
35
36 void lock();
37 void unlock();
38 void wakeOne();
39 void wait();
40
41 bool isThisThread() const;
42 bool isParentThread() const;
43
44 // Synchronously invoke a method in the thread
45 template<typename Method, typename ...Args>
46 void callMethodInThread(Method &&method, Args &&...args);
47
48 // Synchronously invoke a method in the main thread. If the main thread is
49 // blocked in a callMethodInThread() call, the call is made from within that
50 // call.
51 template<typename Method, typename ...Args>
52 void callMethodInMain(Method &&method, Args &&...args);
53
54 // Asynchronously invoke a method in the thread.
55 template<typename Method, typename ...Args>
56 void postMethodToThread(Method &&method, Args &&...args);
57
58 // Asynchronously invoke a method in the main thread.
59 template<typename Method, typename ...Args>
60 void postMethodToMain(Method &&method, Args &&...args);
61
62 void waitForNextMessage();
63 void discardMessages();
64
65 void startup();
66 void restart();
67 void shutdown();
68 bool isRunning() const;
69
70protected:
71 QThread *thread() const;
72 QObject *threadObject() const;
73
74private:
75 friend class QQmlThreadPrivate;
76
77 struct Message {
78 Message() : next(nullptr) {}
79 virtual ~Message() {}
80 Message *next;
81 virtual void call(QQmlThread *) = 0;
82 };
83 template<typename Method, typename ...Args>
84 Message *createMessageFromMethod(Method &&method, Args &&...args);
85 void internalCallMethodInThread(Message *);
86 void internalCallMethodInMain(Message *);
87 void internalPostMethodToThread(Message *);
88 void internalPostMethodToMain(Message *);
90};
91
92namespace QtPrivate {
93template <typename> struct member_function_traits;
94
95template <typename Return, typename Object, typename... Args>
96struct member_function_traits<Return (Object::*)(Args...)>
97{
98 using class_type = Object;
99};
100}
101
102template<typename Method, typename ...Args>
103QQmlThread::Message *QQmlThread::createMessageFromMethod(Method &&method, Args &&...args)
104{
105 struct I : public Message {
106 Method m;
107 std::tuple<std::decay_t<Args>...> arguments;
108 I(Method &&method, Args&& ...args) : m(std::forward<Method>(method)), arguments(std::forward<Args>(args)...) {}
109 void call(QQmlThread *thread) override {
110 using class_type = typename QtPrivate::member_function_traits<Method>::class_type;
111 class_type *me = static_cast<class_type *>(thread);
112 std::apply(m, std::tuple_cat(std::make_tuple(me), arguments));
113 }
114 };
115 return new I(std::forward<Method>(method), std::forward<Args>(args)...);
116}
117
118template<typename Method, typename ...Args>
119void QQmlThread::callMethodInMain(Method &&method, Args&& ...args)
120{
121 Message *m = createMessageFromMethod(std::forward<Method>(method), std::forward<Args>(args)...);
122 internalCallMethodInMain(m);
123}
124
125template<typename Method, typename ...Args>
126void QQmlThread::callMethodInThread(Method &&method, Args&& ...args)
127{
128 Message *m = createMessageFromMethod(std::forward<Method>(method), std::forward<Args>(args)...);
129 internalCallMethodInThread(m);
130}
131
132template<typename Method, typename ...Args>
133void QQmlThread::postMethodToThread(Method &&method, Args&& ...args)
134{
135 Message *m = createMessageFromMethod(std::forward<Method>(method), std::forward<Args>(args)...);
136 internalPostMethodToThread(m);
137}
138
139template<typename Method, typename ...Args>
140void QQmlThread::postMethodToMain(Method &&method, Args&& ...args)
141{
142 Message *m = createMessageFromMethod(std::forward<Method>(method), std::forward<Args>(args)...);
143 internalPostMethodToMain(m);
144}
145
146QT_END_NAMESPACE
147
148#endif // QQMLTHREAD_P_H
QQmlThreadPrivate(QQmlThread *q)
QFieldList< QQmlThread::Message, &QQmlThread::Message::next > MessageList
bool event(QEvent *e) override
This virtual function receives events to an object and should return true if the event e was recogniz...
\inmodule QtQml
void postMethodToThread(Method &&method, Args &&...args)
QThread * thread() const
bool isRunning() const
bool isThisThread() const
void waitForNextMessage()
void postMethodToMain(Method &&method, Args &&...args)
bool isParentThread() const
void callMethodInMain(Method &&method, Args &&...args)
virtual ~QQmlThread()
void callMethodInThread(Method &&method, Args &&...args)
QObject * threadObject() const
QT_REQUIRE_CONFIG(liburing)