OSDN Git Service

inline QMutex
authorIvailo Monev <xakepa10@laimg.moc>
Fri, 7 Jun 2019 13:44:10 +0000 (13:44 +0000)
committerIvailo Monev <xakepa10@laimg.moc>
Fri, 7 Jun 2019 13:44:10 +0000 (13:44 +0000)
Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
src/core/thread/qmutex.cpp
src/core/thread/qmutex.h
src/core/thread/qmutex_p.h [deleted file]
src/core/thread/qwaitcondition_unix.cpp
src/core/thread/thread.cmake

index 0b6b622..afac20e 100644 (file)
 **
 ****************************************************************************/
 
-#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.
@@ -114,6 +106,8 @@ QMutexPrivate::QMutexPrivate(QMutex::RecursionMode mode)
 */
 
 /*!
+    \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
@@ -123,19 +117,18 @@ QMutexPrivate::QMutexPrivate(QMutex::RecursionMode mode)
 
     \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.
 
@@ -147,16 +140,10 @@ QMutex::~QMutex()
 
     \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.
@@ -173,15 +160,10 @@ void QMutex::lock()
 
     \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
@@ -204,30 +186,16 @@ bool QMutex::tryLock()
 
     \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()
index 366f732..ae46514 100644 (file)
 #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
diff --git a/src/core/thread/qmutex_p.h b/src/core/thread/qmutex_p.h
deleted file mode 100644 (file)
index 01a87b4..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/****************************************************************************
-**
-** 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
index 5418a88..af83e40 100644 (file)
@@ -45,8 +45,6 @@
 #include "qreadwritelock.h"
 #include "qatomic.h"
 #include "qstring.h"
-
-#include "qmutex_p.h"
 #include "qreadwritelock_p.h"
 #include "qcorecommon_p.h"
 
@@ -139,9 +137,9 @@ void QWaitCondition::wakeAll()
 
 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;
     }
index 9346e1d..e477ba3 100644 (file)
@@ -7,7 +7,6 @@ set(CORE_HEADERS
     ${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