summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2023-07-04 22:28:50 +0200
committerMarc Mutz <marc.mutz@qt.io>2023-07-09 21:23:17 +0000
commit416e07e0575922323b76e4e7768409b203292837 (patch)
tree7d56ad80d6c4966165d8e9c8c208200969549623 /src
parent536696196c4f954ffd2848bb89ea0404938262ad (diff)
QEventLoopLocker: rewrite to hold public classes
... instead of Private ones, at the cost of having to befriend of all the lockable classes, because we need access to their d_func()'s. This simplifies the code, because we don't need the manual QClass to QClassPrivate mapping (o2p) anymore, we can just use d_func(). This also paves the way to make QEventLoopLocker almost completely inline and use a 3-pointer form of QBiPointer, once available, to hide the bit fiddling. We couldn't make such a change if the class continued to hold pointers to QClassPrivate's. Pick-to: 6.6 Task-number: QTBUG-114793 Change-Id: Id300e4d45d6cacabe090a46cd6433c5ead3c8b0c Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/kernel/qcoreapplication.h2
-rw-r--r--src/corelib/kernel/qeventloop.cpp32
-rw-r--r--src/corelib/kernel/qeventloop.h2
-rw-r--r--src/corelib/thread/qthread.h2
4 files changed, 17 insertions, 21 deletions
diff --git a/src/corelib/kernel/qcoreapplication.h b/src/corelib/kernel/qcoreapplication.h
index 5a7dc46a3f..eb34c9aa9b 100644
--- a/src/corelib/kernel/qcoreapplication.h
+++ b/src/corelib/kernel/qcoreapplication.h
@@ -33,6 +33,7 @@ class QTranslator;
class QPostEventList;
class QAbstractEventDispatcher;
class QAbstractNativeEventFilter;
+class QEventLoopLocker;
#if QT_CONFIG(permissions) || defined(Q_QDOC)
class QPermission;
@@ -59,6 +60,7 @@ class Q_CORE_EXPORT QCoreApplication
#endif
Q_DECLARE_PRIVATE(QCoreApplication)
+ friend class QEventLoopLocker;
public:
enum { ApplicationFlags = QT_VERSION
};
diff --git a/src/corelib/kernel/qeventloop.cpp b/src/corelib/kernel/qeventloop.cpp
index bdc448fe56..f754a975b9 100644
--- a/src/corelib/kernel/qeventloop.cpp
+++ b/src/corelib/kernel/qeventloop.cpp
@@ -309,19 +309,10 @@ bool QEventLoop::event(QEvent *event)
void QEventLoop::quit()
{ exit(0); }
-
-namespace {
// If any of these trigger, the Type bits will interfere with the pointer values:
-static_assert(alignof(QEventLoopPrivate) >= 4);
-static_assert(alignof(QThreadPrivate) >= 4);
-static_assert(alignof(QCoreApplicationPrivate) >= 4);
-
-template <typename Private>
-Private *o2p(QObject *o)
-{
- return static_cast<Private*>(QObjectPrivate::get(o));
-}
-} // unnamed namespace
+static_assert(alignof(QEventLoop) >= 4);
+static_assert(alignof(QThread) >= 4);
+static_assert(alignof(QCoreApplication) >= 4);
/*!
\class QEventLoopLocker
@@ -351,8 +342,7 @@ Private *o2p(QObject *o)
\sa QCoreApplication::quit(), QCoreApplication::isQuitLockEnabled()
*/
QEventLoopLocker::QEventLoopLocker() noexcept
- : QEventLoopLocker{o2p<QCoreApplicationPrivate>(QCoreApplication::instance()),
- Type::Application}
+ : QEventLoopLocker{QCoreApplication::instance(), Type::Application}
{
}
@@ -365,7 +355,7 @@ QEventLoopLocker::QEventLoopLocker() noexcept
\sa QEventLoop::quit()
*/
QEventLoopLocker::QEventLoopLocker(QEventLoop *loop) noexcept
- : QEventLoopLocker{o2p<QEventLoopPrivate>(loop), Type::EventLoop}
+ : QEventLoopLocker{loop, Type::EventLoop}
{
}
@@ -378,7 +368,7 @@ QEventLoopLocker::QEventLoopLocker(QEventLoop *loop) noexcept
\sa QThread::quit()
*/
QEventLoopLocker::QEventLoopLocker(QThread *thread) noexcept
- : QEventLoopLocker{o2p<QThreadPrivate>(thread), Type::Thread}
+ : QEventLoopLocker{thread, Type::Thread}
{
}
@@ -425,7 +415,7 @@ QEventLoopLocker::QEventLoopLocker(QThread *thread) noexcept
*/
QEventLoopLocker::~QEventLoopLocker()
{
- visit([](auto p) { p->deref(); });
+ visit([](auto p) { p->d_func()->deref(); });
}
/*!
@@ -434,7 +424,7 @@ QEventLoopLocker::~QEventLoopLocker()
QEventLoopLocker::QEventLoopLocker(void *ptr, Type t) noexcept
: p{quintptr(ptr) | quintptr(t)}
{
- visit([](auto p) { p->ref(); });
+ visit([](auto p) { p->d_func()->ref(); });
}
/*!
@@ -447,9 +437,9 @@ void QEventLoopLocker::visit(Func f) const
if (!ptr)
return;
switch (type()) {
- case Type::EventLoop: return f(static_cast<QEventLoopPrivate *>(ptr));
- case Type::Thread: return f(static_cast<QThreadPrivate *>(ptr));
- case Type::Application: return f(static_cast<QCoreApplicationPrivate *>(ptr));
+ case Type::EventLoop: return f(static_cast<QEventLoop *>(ptr));
+ case Type::Thread: return f(static_cast<QThread *>(ptr));
+ case Type::Application: return f(static_cast<QCoreApplication *>(ptr));
}
Q_UNREACHABLE();
}
diff --git a/src/corelib/kernel/qeventloop.h b/src/corelib/kernel/qeventloop.h
index e2629caaca..68d4023361 100644
--- a/src/corelib/kernel/qeventloop.h
+++ b/src/corelib/kernel/qeventloop.h
@@ -9,12 +9,14 @@
QT_BEGIN_NAMESPACE
+class QEventLoopLocker;
class QEventLoopPrivate;
class Q_CORE_EXPORT QEventLoop : public QObject
{
Q_OBJECT
Q_DECLARE_PRIVATE(QEventLoop)
+ friend class QEventLoopLocker;
public:
explicit QEventLoop(QObject *parent = nullptr);
diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h
index cea5aeb73c..40f363ee5a 100644
--- a/src/corelib/thread/qthread.h
+++ b/src/corelib/thread/qthread.h
@@ -24,6 +24,7 @@ QT_BEGIN_NAMESPACE
class QThreadData;
class QThreadPrivate;
class QAbstractEventDispatcher;
+class QEventLoopLocker;
class Q_CORE_EXPORT QThread : public QObject
{
@@ -109,6 +110,7 @@ protected:
private:
Q_DECLARE_PRIVATE(QThread)
+ friend class QEventLoopLocker;
#if QT_CONFIG(cxx11_future)
[[nodiscard]] static QThread *createThreadImpl(std::future<void> &&future);