From e5890084dcc70ae9c5b87f9d4fd209a830a17c7c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 11 Jun 2024 14:20:48 +0200 Subject: [PATCH] QThread: mark is(Main|Current)Thread() noexcept The rationale given for the addition of these functions was that they are a more efficient way to check these things in public API. If so, they shouldn't cause compilers to insert exception handling around their calls, so mark them noexcept. This is safe, as both conceptually and practically, these functions should not and cannot fail, so we'll never need to remove noexcept again. Found in API-Review. Amends 7a374c38d288435b3c0a76b82a1c2ca53ea9c003 and a3d50112e44bc42b310d9d3a8e6c7805ef31ef53. These two commits also lacked each the changelog entry, so adding them here: [ChangeLog][QtCore][QThread] Added isMainThread() static member function. [ChangeLog][QtCore][QThread] Added isCurrentThread() member function. Change-Id: Iaf39ce2cc0abd45049bff60b24693e84bf76d9e0 Reviewed-by: Fabian Kosmale Reviewed-by: Thiago Macieira (cherry picked from commit d73a2bf0fb3e0a00691c858645a5a5bb2b55b40a) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/thread/qthread.cpp | 8 ++++---- src/corelib/thread/qthread.h | 4 ++-- src/corelib/thread/qthread_p.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index ea76a2ccada..738c3846c53 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -438,7 +438,7 @@ QThread *QThread::currentThread() \sa currentThread(), QCoreApplication::instance() */ -bool QThread::isMainThread() +bool QThread::isMainThread() noexcept { return currentThreadId() == QCoreApplicationPrivate::theMainThreadId.loadRelaxed(); } @@ -952,7 +952,7 @@ int QThread::loopLevel() const therefore give surprising results if it outlives the QThread object (threads claimed to be the same even if they aren't). */ -Qt::HANDLE QThreadPrivate::threadId() const +Qt::HANDLE QThreadPrivate::threadId() const noexcept { return data->threadId.loadRelaxed(); } @@ -963,7 +963,7 @@ Qt::HANDLE QThreadPrivate::threadId() const \sa currentThreadId() */ -bool QThread::isCurrentThread() const +bool QThread::isCurrentThread() const noexcept { Q_D(const QThread); return QThread::currentThreadId() == d->threadId(); @@ -1041,7 +1041,7 @@ QThread *QThread::currentThread() return QThreadData::current()->thread.loadAcquire(); } -bool QThread::isCurrentThread() const +bool QThread::isCurrentThread() const noexcept { return true; } diff --git a/src/corelib/thread/qthread.h b/src/corelib/thread/qthread.h index 641c8ef68a0..ae060ecf1cf 100644 --- a/src/corelib/thread/qthread.h +++ b/src/corelib/thread/qthread.h @@ -30,7 +30,7 @@ class Q_CORE_EXPORT QThread : public QObject public: static Qt::HANDLE currentThreadId() noexcept Q_DECL_PURE_FUNCTION; static QThread *currentThread(); - static bool isMainThread(); + static bool isMainThread() noexcept; static int idealThreadCount() noexcept; static void yieldCurrentThread(); @@ -69,7 +69,7 @@ public: bool event(QEvent *event) override; int loopLevel() const; - bool isCurrentThread() const; + bool isCurrentThread() const noexcept; template [[nodiscard]] static QThread *create(Function &&f, Args &&... args); diff --git a/src/corelib/thread/qthread_p.h b/src/corelib/thread/qthread_p.h index c39e21ec9a2..d6fd99e5ba2 100644 --- a/src/corelib/thread/qthread_p.h +++ b/src/corelib/thread/qthread_p.h @@ -179,7 +179,7 @@ public: ~QThreadPrivate(); void setPriority(QThread::Priority prio); - Qt::HANDLE threadId() const; + Qt::HANDLE threadId() const noexcept; mutable QMutex mutex; QAtomicInt quitLockRef;