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 <fabian.kosmale@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit d73a2bf0fb3e0a00691c858645a5a5bb2b55b40a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-06-11 14:20:48 +02:00 committed by Qt Cherry-pick Bot
parent 7b65e0395f
commit e5890084dc
3 changed files with 7 additions and 7 deletions

View File

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

View File

@ -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 <typename Function, typename... Args>
[[nodiscard]] static QThread *create(Function &&f, Args &&... args);

View File

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