From 90c436dd1ea8ed0f86827d6e119ee25377970041 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 15 Oct 2024 09:41:05 -0700 Subject: [PATCH] QThread: make finish() and cleanup() non-static members MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Looks cleaner. Change-Id: I77148a15c316ea0eaf63fffd9358d952c5b9e1d0 Reviewed-by: Edward Welbourne Reviewed-by: Volker Hilsheimer (cherry picked from commit f94c9b0f3a060fa98c270d5ccab3055e77ebc35f) Reviewed-by: MÃ¥rten Nordheim --- src/corelib/thread/qthread_p.h | 6 +++--- src/corelib/thread/qthread_unix.cpp | 21 ++++++++++----------- src/corelib/thread/qthread_win.cpp | 16 ++++++++-------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h index eaeca568f18..69cad23f99a 100644 --- a/src/corelib/thread/qthread_p.h +++ b/src/corelib/thread/qthread_p.h @@ -202,13 +202,13 @@ public: QWaitCondition thread_done; static void *start(void *arg); - static void finish(void *); // happens early (before thread-local dtors) - static void cleanup(void *); // happens late (as a thread-local dtor, if possible) + void finish(); // happens early (before thread-local dtors) + void cleanup(); // happens late (as a thread-local dtor, if possible) #endif // Q_OS_UNIX #ifdef Q_OS_WIN static unsigned int __stdcall start(void *) noexcept; - static void finish(void *, bool lockAnyway = true) noexcept; + void finish(bool lockAnyway = true) noexcept; Qt::HANDLE handle; unsigned int id; diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index 3a754d3be5b..4dac10baade 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -112,15 +112,15 @@ static void destroy_current_thread_data(void *p) // this is very likely the last reference. These pointers cannot be // null and there is no race. QThreadPrivate *thread_p = static_cast(QObjectPrivate::get(thread)); - thread_p->finish(thread); + thread_p->finish(); if constexpr (!QT_CONFIG(broken_threadlocal_dtors)) - thread_p->cleanup(thread); + thread_p->cleanup(); } else if constexpr (!QT_CONFIG(broken_threadlocal_dtors)) { // We may be racing the QThread destructor in another thread. With // two-phase clean-up enabled, there's also no race because it will // stop in a call to QThread::wait() until we call cleanup(). QThreadPrivate *thread_p = static_cast(QObjectPrivate::get(thread)); - thread_p->cleanup(thread); + thread_p->cleanup(); } else { // We may be racing the QThread destructor in another thread and it may // have begun destruction; we must not dereference the QThread pointer. @@ -314,7 +314,7 @@ void *QThreadPrivate::start(void *arg) // this ensures the thread-local is created as early as possible set_thread_data(data); - pthread_cleanup_push(QThreadPrivate::finish, arg); + pthread_cleanup_push([](void *arg) { static_cast(arg)->d_func()->finish(); }, arg); terminate_on_exception([&] { { QMutexLocker locker(&thr->d_func()->mutex); @@ -361,11 +361,11 @@ void *QThreadPrivate::start(void *arg) return nullptr; } -void QThreadPrivate::finish(void *arg) +void QThreadPrivate::finish() { terminate_on_exception([&] { - QThread *thr = reinterpret_cast(arg); - QThreadPrivate *d = thr->d_func(); + QThreadPrivate *d = this; + QThread *thr = q_func(); // Disable cancellation; we're already in the finishing touches of this // thread, and we don't want cleanup to be disturbed by @@ -388,14 +388,13 @@ void QThreadPrivate::finish(void *arg) }); if constexpr (QT_CONFIG(broken_threadlocal_dtors)) - cleanup(arg); + cleanup(); } -void QThreadPrivate::cleanup(void *arg) +void QThreadPrivate::cleanup() { terminate_on_exception([&] { - QThread *thr = reinterpret_cast(arg); - QThreadPrivate *d = thr->d_func(); + QThreadPrivate *d = this; // Disable cancellation again: we did it above, but some user code // running between finish() and cleanup() may have turned them back on. diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp index 1c5b5bf965a..712a6f07041 100644 --- a/src/corelib/thread/qthread_win.cpp +++ b/src/corelib/thread/qthread_win.cpp @@ -209,7 +209,7 @@ DWORD WINAPI qt_adopted_thread_watcher_function(LPVOID) auto thread_p = static_cast(QObjectPrivate::get(thread)); Q_UNUSED(thread_p); Q_ASSERT(!thread_p->finished); - QThreadPrivate::finish(thread); + thread_p->finish(); } data->deref(); @@ -269,7 +269,7 @@ unsigned int __stdcall QT_ENSURE_STACK_ALIGNED_FOR_SSE QThreadPrivate::start(voi QThread::setTerminationEnabled(true); thr->run(); - finish(arg); + thr->d_func()->finish(); return 0; } @@ -284,10 +284,10 @@ unsigned int __stdcall QT_ENSURE_STACK_ALIGNED_FOR_SSE QThreadPrivate::start(voi In those cases, \a arg will not be the current thread. */ -void QThreadPrivate::finish(void *arg, bool lockAnyway) noexcept +void QThreadPrivate::finish(bool lockAnyway) noexcept { - QThread *thr = reinterpret_cast(arg); - QThreadPrivate *d = thr->d_func(); + QThreadPrivate *d = this; + QThread *thr = q_func(); QMutexLocker locker(lockAnyway ? &d->mutex : nullptr); d->isInFinish = true; @@ -481,7 +481,7 @@ void QThread::terminate() } TerminateThread(d->handle, 0); - QThreadPrivate::finish(this, false); + d->finish(false); } bool QThread::wait(QDeadlineTimer deadline) @@ -519,7 +519,7 @@ bool QThread::wait(QDeadlineTimer deadline) if (ret && !d->finished) { // thread was terminated by someone else - QThreadPrivate::finish(this, false); + d->finish(false); } if (d->finished && !d->waiters) { @@ -539,7 +539,7 @@ void QThread::setTerminationEnabled(bool enabled) QMutexLocker locker(&d->mutex); d->terminationEnabled = enabled; if (enabled && d->terminatePending) { - QThreadPrivate::finish(thr, false); + d->finish(false); locker.unlock(); // don't leave the mutex locked! _endthreadex(0); }