Port away from QMutexLocker in public headers
We can't use qt_scoped_lock/qt_unique_lock here, so port to std::unique_lock and std::lock_guard for now. This is in preparation of deprecating QMutexLocker in favor of std::unique_lock and std::scoped_lock. In QFutureInterface, change the return type of mutex() from QMutex* to QMutex&, so we don't need to deref when passing to std::lock_guard. We need to keep the old method around for BC reasons, so the new one needs an artificial function argument for disambiguation. This will vanish come Qt 6. Change-Id: I1a0f0205952a249512ec2dbd3f0f48dd209b1636 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
e913b690b9
commit
97db8e04ac
@ -52,6 +52,8 @@
|
|||||||
#include <QtCore/qthreadpool.h>
|
#include <QtCore/qthreadpool.h>
|
||||||
#include <QtCore/qvector.h>
|
#include <QtCore/qvector.h>
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
|
||||||
@ -147,7 +149,7 @@ public:
|
|||||||
ReduceResultType &r,
|
ReduceResultType &r,
|
||||||
const IntermediateResults<T> &result)
|
const IntermediateResults<T> &result)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&mutex);
|
std::unique_lock<QMutex> locker(mutex);
|
||||||
if (!canReduce(result.begin)) {
|
if (!canReduce(result.begin)) {
|
||||||
++resultsMapSize;
|
++resultsMapSize;
|
||||||
resultsMap.insert(result.begin, result);
|
resultsMap.insert(result.begin, result);
|
||||||
@ -161,7 +163,7 @@ public:
|
|||||||
// reduce this result
|
// reduce this result
|
||||||
locker.unlock();
|
locker.unlock();
|
||||||
reduceResult(reduce, r, result);
|
reduceResult(reduce, r, result);
|
||||||
locker.relock();
|
locker.lock();
|
||||||
|
|
||||||
// reduce all stored results as well
|
// reduce all stored results as well
|
||||||
while (!resultsMap.isEmpty()) {
|
while (!resultsMap.isEmpty()) {
|
||||||
@ -170,7 +172,7 @@ public:
|
|||||||
|
|
||||||
locker.unlock();
|
locker.unlock();
|
||||||
reduceResults(reduce, r, resultsMapCopy);
|
reduceResults(reduce, r, resultsMapCopy);
|
||||||
locker.relock();
|
locker.lock();
|
||||||
|
|
||||||
resultsMapSize -= resultsMapCopy.size();
|
resultsMapSize -= resultsMapCopy.size();
|
||||||
}
|
}
|
||||||
@ -180,7 +182,7 @@ public:
|
|||||||
// reduce this result
|
// reduce this result
|
||||||
locker.unlock();
|
locker.unlock();
|
||||||
reduceResult(reduce, r, result);
|
reduceResult(reduce, r, result);
|
||||||
locker.relock();
|
locker.lock();
|
||||||
|
|
||||||
// OrderedReduce
|
// OrderedReduce
|
||||||
progress += result.end - result.begin;
|
progress += result.end - result.begin;
|
||||||
@ -193,7 +195,7 @@ public:
|
|||||||
|
|
||||||
locker.unlock();
|
locker.unlock();
|
||||||
reduceResult(reduce, r, it.value());
|
reduceResult(reduce, r, it.value());
|
||||||
locker.relock();
|
locker.lock();
|
||||||
|
|
||||||
--resultsMapSize;
|
--resultsMapSize;
|
||||||
progress += it.value().end - it.value().begin;
|
progress += it.value().end - it.value().begin;
|
||||||
|
@ -98,6 +98,7 @@ enum GuardValues {
|
|||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
#include <QtCore/qmutex.h>
|
#include <QtCore/qmutex.h>
|
||||||
|
#include <mutex>
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
#define Q_GLOBAL_STATIC_INTERNAL(ARGS) \
|
#define Q_GLOBAL_STATIC_INTERNAL(ARGS) \
|
||||||
@ -107,7 +108,7 @@ QT_BEGIN_NAMESPACE
|
|||||||
static QBasicMutex mutex; \
|
static QBasicMutex mutex; \
|
||||||
int x = guard.loadAcquire(); \
|
int x = guard.loadAcquire(); \
|
||||||
if (Q_UNLIKELY(x >= QtGlobalStatic::Uninitialized)) { \
|
if (Q_UNLIKELY(x >= QtGlobalStatic::Uninitialized)) { \
|
||||||
QMutexLocker locker(&mutex); \
|
const std::lock_guard<QBasicMutex> locker(mutex); \
|
||||||
if (guard.loadRelaxed() == QtGlobalStatic::Uninitialized) { \
|
if (guard.loadRelaxed() == QtGlobalStatic::Uninitialized) { \
|
||||||
d = new Type ARGS; \
|
d = new Type ARGS; \
|
||||||
static struct Cleanup { \
|
static struct Cleanup { \
|
||||||
|
@ -429,6 +429,11 @@ QMutex *QFutureInterfaceBase::mutex() const
|
|||||||
return &d->m_mutex;
|
return &d->m_mutex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QMutex &QFutureInterfaceBase::mutex(int) const
|
||||||
|
{
|
||||||
|
return d->m_mutex;
|
||||||
|
}
|
||||||
|
|
||||||
QtPrivate::ExceptionStore &QFutureInterfaceBase::exceptionStore()
|
QtPrivate::ExceptionStore &QFutureInterfaceBase::exceptionStore()
|
||||||
{
|
{
|
||||||
return d->m_exceptionStore;
|
return d->m_exceptionStore;
|
||||||
|
@ -45,6 +45,8 @@
|
|||||||
#include <QtCore/qexception.h>
|
#include <QtCore/qexception.h>
|
||||||
#include <QtCore/qresultstore.h>
|
#include <QtCore/qresultstore.h>
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
QT_REQUIRE_CONFIG(future);
|
QT_REQUIRE_CONFIG(future);
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@ -118,6 +120,7 @@ public:
|
|||||||
void waitForResume();
|
void waitForResume();
|
||||||
|
|
||||||
QMutex *mutex() const;
|
QMutex *mutex() const;
|
||||||
|
QMutex &mutex(int) const;
|
||||||
QtPrivate::ExceptionStore &exceptionStore();
|
QtPrivate::ExceptionStore &exceptionStore();
|
||||||
QtPrivate::ResultStoreBase &resultStoreBase();
|
QtPrivate::ResultStoreBase &resultStoreBase();
|
||||||
const QtPrivate::ResultStoreBase &resultStoreBase() const;
|
const QtPrivate::ResultStoreBase &resultStoreBase() const;
|
||||||
@ -188,7 +191,7 @@ public:
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
inline void QFutureInterface<T>::reportResult(const T *result, int index)
|
inline void QFutureInterface<T>::reportResult(const T *result, int index)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(mutex());
|
std::lock_guard<QMutex> locker(mutex(0));
|
||||||
if (this->queryState(Canceled) || this->queryState(Finished)) {
|
if (this->queryState(Canceled) || this->queryState(Finished)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -214,7 +217,7 @@ inline void QFutureInterface<T>::reportResult(const T &result, int index)
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
inline void QFutureInterface<T>::reportResults(const QVector<T> &_results, int beginIndex, int count)
|
inline void QFutureInterface<T>::reportResults(const QVector<T> &_results, int beginIndex, int count)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(mutex());
|
std::lock_guard<QMutex> locker(mutex(0));
|
||||||
if (this->queryState(Canceled) || this->queryState(Finished)) {
|
if (this->queryState(Canceled) || this->queryState(Finished)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -242,14 +245,14 @@ inline void QFutureInterface<T>::reportFinished(const T *result)
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
inline const T &QFutureInterface<T>::resultReference(int index) const
|
inline const T &QFutureInterface<T>::resultReference(int index) const
|
||||||
{
|
{
|
||||||
QMutexLocker lock(mutex());
|
std::lock_guard<QMutex> locker(mutex(0));
|
||||||
return resultStoreBase().resultAt(index).template value<T>();
|
return resultStoreBase().resultAt(index).template value<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline const T *QFutureInterface<T>::resultPointer(int index) const
|
inline const T *QFutureInterface<T>::resultPointer(int index) const
|
||||||
{
|
{
|
||||||
QMutexLocker lock(mutex());
|
std::lock_guard<QMutex> locker(mutex(0));
|
||||||
return resultStoreBase().resultAt(index).template pointer<T>();
|
return resultStoreBase().resultAt(index).template pointer<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,7 +266,7 @@ inline QList<T> QFutureInterface<T>::results()
|
|||||||
QFutureInterfaceBase::waitForResult(-1);
|
QFutureInterfaceBase::waitForResult(-1);
|
||||||
|
|
||||||
QList<T> res;
|
QList<T> res;
|
||||||
QMutexLocker lock(mutex());
|
std::lock_guard<QMutex> locker(mutex(0));
|
||||||
|
|
||||||
QtPrivate::ResultIteratorBase it = resultStoreBase().begin();
|
QtPrivate::ResultIteratorBase it = resultStoreBase().begin();
|
||||||
while (it != resultStoreBase().end()) {
|
while (it != resultStoreBase().end()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user