**
****************************************************************************/
-#include "qplatformdefs.h"
#include "qmutex.h"
-#include <qdebug.h>
#ifndef QT_NO_THREAD
-#include "qmutex_p.h"
QT_BEGIN_NAMESPACE
-QMutexPrivate::QMutexPrivate(QMutex::RecursionMode mode)
- : recursive(mode == QMutex::Recursive)
-{
-}
-
/*!
\class QMutex
\brief The QMutex class provides access serialization between threads.
*/
/*!
+ \fn QMutex::QMutex(RecursionMode mode)
+
Constructs a new mutex. The mutex is created in an unlocked state.
If \a mode is QMutex::Recursive, a thread can lock the same mutex
\sa lock(), unlock()
*/
-QMutex::QMutex(RecursionMode mode)
- : d(new QMutexPrivate(mode))
-{ }
/*!
+ \fn QMutex::~QMutex()
+
Destroys the mutex.
\warning Destroying a locked mutex may result in undefined behavior.
*/
-QMutex::~QMutex()
-{ delete d; }
/*!
+ \fn void QMutex::lock()
+
Locks the mutex. If another thread has locked the mutex then this
call will block until that thread has unlocked it.
\sa unlock()
*/
-void QMutex::lock()
-{
- if (d->recursive) {
- d->mutex2.lock();
- } else {
- d->mutex1.lock();
- }
-}
/*!
+ \fn bool QMutex::tryLock()
+
Attempts to lock the mutex. If the lock was obtained, this function
returns true. If another thread has locked the mutex, this
function returns false immediately.
\sa lock(), unlock()
*/
-bool QMutex::tryLock()
-{
- if (d->recursive) {
- return d->mutex2.try_lock();
- }
- return d->mutex1.try_lock();
-}
-/*! \overload
+/*!
+ \fn bool QMutex::tryLock(int timeout)
+ \overload
Attempts to lock the mutex. This function returns true if the lock
was obtained; otherwise it returns false. If another thread has
\sa lock(), unlock()
*/
-bool QMutex::tryLock(int timeout)
-{
- if (d->recursive) {
- return d->mutex2.try_lock_for(std::chrono::milliseconds(timeout));
- }
- return d->mutex1.try_lock_for(std::chrono::milliseconds(timeout));
-}
-
/*!
+ \fn void QMutex::unlock()
+
Unlocks the mutex. Attempting to unlock a mutex in a different
thread to the one that locked it results in an error. Unlocking a
mutex that is not locked results in undefined behavior.
\sa lock()
*/
-void QMutex::unlock()
-{
- if (d->recursive) {
- d->mutex2.unlock();
- } else {
- d->mutex1.unlock();
- }
-}
/*!
\fn bool QMutex::locked()
#ifndef QMUTEX_H
#define QMUTEX_H
-#include <QtCore/qatomic.h>
+#include <QtCore/qglobal.h>
+
+#include <mutex>
QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
-
#ifndef QT_NO_THREAD
-class QMutexPrivate;
-
class Q_CORE_EXPORT QMutex
{
- friend class QWaitCondition;
- friend class QWaitConditionPrivate;
-
public:
enum RecursionMode {
NonRecursive,
Recursive
};
- explicit QMutex(RecursionMode mode = NonRecursive);
- ~QMutex();
+ explicit QMutex(RecursionMode mode = NonRecursive) : recursive(mode == Recursive) { }
+ ~QMutex() {}
+
+ inline void lock() {
+ if (recursive) {
+ recursive_mutex.lock();
+ } else {
+ mutex.lock();
+ }
+ }
+
+ inline bool tryLock() {
+ if (recursive) {
+ return recursive_mutex.try_lock();
+ }
+ return mutex.try_lock();
+ }
+
+ inline bool tryLock(int timeout) {
+ if (recursive) {
+ return recursive_mutex.try_lock_for(std::chrono::milliseconds(timeout));
+ }
+ return mutex.try_lock_for(std::chrono::milliseconds(timeout));
+ }
- void lock();
- bool tryLock();
- bool tryLock(int timeout);
- void unlock();
+ inline void unlock() {
+ if (recursive) {
+ recursive_mutex.unlock();
+ } else {
+ mutex.unlock();
+ }
+ }
private:
Q_DISABLE_COPY(QMutex)
- QMutexPrivate *d;
+ const bool recursive;
+ std::timed_mutex mutex;
+ std::recursive_timed_mutex recursive_mutex;
+
+ friend class QWaitCondition;
};
class Q_CORE_EXPORT QMutexLocker
+++ /dev/null
-/****************************************************************************
-**
-** Copyright (C) 2015 The Qt Company Ltd.
-** Copyright (C) 2016-2019 Ivailo Monev
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 or version 3 as published by the Free
-** Software Foundation and appearing in the file LICENSE.LGPLv21 and
-** LICENSE.LGPLv3 included in the packaging of this file. Please review the
-** following information to ensure the GNU Lesser General Public License
-** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
-** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
-**
-** As a special exception, The Qt Company gives you certain additional
-** rights. These rights are described in The Qt Company LGPL Exception
-** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3.0 as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU General Public License version 3.0 requirements will be
-** met: http://www.gnu.org/copyleft/gpl.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#ifndef QMUTEX_P_H
-#define QMUTEX_P_H
-
-//
-// W A R N I N G
-// -------------
-//
-// This file is not part of the Katie API. It exists for the convenience
-// of qmutex.cpp and qmutex_unix.cpp. This header file may change
-// from version to version without notice, or even be removed.
-//
-// We mean it.
-//
-
-#include <QtCore/qnamespace.h>
-#include <QtCore/qmutex.h>
-
-#include <mutex>
-
-QT_BEGIN_NAMESPACE
-
-class QMutexPrivate {
-public:
- QMutexPrivate(QMutex::RecursionMode mode);
-
- const bool recursive;
- std::timed_mutex mutex1;
- std::recursive_timed_mutex mutex2;
-};
-
-QT_END_NAMESPACE
-
-#endif // QMUTEX_P_H
#include "qreadwritelock.h"
#include "qatomic.h"
#include "qstring.h"
-
-#include "qmutex_p.h"
#include "qreadwritelock_p.h"
#include "qcorecommon_p.h"
bool QWaitCondition::wait(QMutex *mutex, unsigned long time)
{
- if (! mutex)
+ if (!mutex)
return false;
- if (mutex->d->recursive) {
+ if (mutex->recursive) {
qWarning("QWaitCondition: cannot wait on recursive mutexes");
return false;
}
${CMAKE_CURRENT_SOURCE_DIR}/thread/qwaitcondition.h
${CMAKE_CURRENT_SOURCE_DIR}/thread/qatomic.h
- ${CMAKE_CURRENT_SOURCE_DIR}/thread/qmutex_p.h
${CMAKE_CURRENT_SOURCE_DIR}/thread/qmutexpool_p.h
${CMAKE_CURRENT_SOURCE_DIR}/thread/qorderedmutexlocker_p.h
${CMAKE_CURRENT_SOURCE_DIR}/thread/qreadwritelock_p.h