From 87d32424de2f471a520c1f3ba0c3035fbff7ee06 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Tue, 2 Jun 2020 10:59:21 +0200 Subject: Do not multithread if already in a global threadpool thread This can lead to a deadlock if we block all the worker threads, waiting for the worker threads to finish. Pick-to: 5.15 Fixes: QTBUG-84619 Change-Id: I92b7f96007897d86ece0c34223bab0df4ccbed9a Reviewed-by: Sona Kurazyan --- src/corelib/thread/qthreadpool.cpp | 24 ++++++++++++++++++++++++ src/corelib/thread/qthreadpool.h | 2 ++ 2 files changed, 26 insertions(+) (limited to 'src/corelib/thread') diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 1f99bad247..d2dcc32280 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -52,6 +52,7 @@ Q_GLOBAL_STATIC(QThreadPool, theInstance) */ class QThreadPoolThread : public QThread { + Q_OBJECT public: QThreadPoolThread(QThreadPoolPrivate *manager); void run() override; @@ -763,6 +764,28 @@ void QThreadPool::clear() d->clear(); } +#if QT_VERSION < QT_VERSION_CHECK(6,0,0) +/*! + \internal + + Returns \c true if \a thread is a thread managed by this thread pool. +*/ +#else +/*! + \since 6.0 + + Returns \c true if \a thread is a thread managed by this thread pool. +*/ +#endif +bool QThreadPool::contains(const QThread *thread) const +{ + Q_D(const QThreadPool); + const QThreadPoolThread *poolThread = qobject_cast(thread); + if (!poolThread) + return false; + return d->allThreads.contains(const_cast(poolThread)); +} + #if QT_DEPRECATED_SINCE(5, 9) /*! \since 5.5 @@ -784,3 +807,4 @@ void QThreadPool::cancel(QRunnable *runnable) QT_END_NAMESPACE #include "moc_qthreadpool.cpp" +#include "qthreadpool.moc" diff --git a/src/corelib/thread/qthreadpool.h b/src/corelib/thread/qthreadpool.h index e3691ab010..004f76a240 100644 --- a/src/corelib/thread/qthreadpool.h +++ b/src/corelib/thread/qthreadpool.h @@ -93,6 +93,8 @@ public: void clear(); + bool contains(const QThread *thread) const; + #if QT_DEPRECATED_SINCE(5, 9) QT_DEPRECATED_X("use tryTake(), but note the different deletion rules") void cancel(QRunnable *runnable); -- cgit v1.2.3