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>
This commit is contained in:
Marc Mutz 2023-07-04 22:28:50 +02:00
parent 536696196c
commit 416e07e057
4 changed files with 17 additions and 21 deletions

View File

@ -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
};

View File

@ -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();
}

View File

@ -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);

View File

@ -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);