2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtCore module of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 07:08:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-15 07:08:27 +00:00
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 3 requirements
|
|
|
|
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 2.0 or (at your option) the GNU General
|
|
|
|
** Public license version 3 or any later version approved by the KDE Free
|
|
|
|
** Qt Foundation. The licenses are as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
|
|
|
** https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef QMUTEX_H
|
|
|
|
#define QMUTEX_H
|
|
|
|
|
|
|
|
#include <QtCore/qglobal.h>
|
|
|
|
#include <QtCore/qatomic.h>
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
|
2012-12-28 06:18:53 +00:00
|
|
|
#if !defined(QT_NO_THREAD) && !defined(Q_QDOC)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-02 14:15:22 +00:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
# define QT_MUTEX_LOCK_NOEXCEPT Q_DECL_NOTHROW
|
|
|
|
#else
|
|
|
|
# define QT_MUTEX_LOCK_NOEXCEPT
|
|
|
|
#endif
|
|
|
|
|
2011-10-20 15:11:18 +00:00
|
|
|
class QMutexData;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-02 13:13:12 +00:00
|
|
|
class Q_CORE_EXPORT QBasicMutex
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-08-02 14:15:22 +00:00
|
|
|
inline void lock() QT_MUTEX_LOCK_NOEXCEPT {
|
2011-07-02 13:13:12 +00:00
|
|
|
if (!fastTryLock())
|
|
|
|
lockInternal();
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-02 14:15:22 +00:00
|
|
|
inline void unlock() Q_DECL_NOTHROW {
|
2011-10-28 09:51:06 +00:00
|
|
|
Q_ASSERT(d_ptr.load()); //mutex must be locked
|
2012-08-11 14:45:14 +00:00
|
|
|
if (!fastTryUnlock())
|
2011-07-02 13:13:12 +00:00
|
|
|
unlockInternal();
|
|
|
|
}
|
|
|
|
|
2012-08-11 16:18:27 +00:00
|
|
|
bool tryLock() Q_DECL_NOTHROW {
|
|
|
|
return fastTryLock();
|
2011-07-02 13:13:12 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-06-26 10:54:33 +00:00
|
|
|
bool isRecursive(); //### Qt6: mark const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
2012-08-02 14:15:22 +00:00
|
|
|
inline bool fastTryLock() Q_DECL_NOTHROW {
|
2015-06-30 20:11:15 +00:00
|
|
|
return d_ptr.testAndSetAcquire(Q_NULLPTR, dummyLocked());
|
2011-07-02 13:13:12 +00:00
|
|
|
}
|
2012-08-11 14:45:14 +00:00
|
|
|
inline bool fastTryUnlock() Q_DECL_NOTHROW {
|
2015-06-30 20:11:15 +00:00
|
|
|
return d_ptr.testAndSetRelease(dummyLocked(), Q_NULLPTR);
|
2012-08-11 14:45:14 +00:00
|
|
|
}
|
2012-08-16 15:15:33 +00:00
|
|
|
inline bool fastTryLock(QMutexData *¤t) Q_DECL_NOTHROW {
|
2015-06-30 20:11:15 +00:00
|
|
|
return d_ptr.testAndSetAcquire(Q_NULLPTR, dummyLocked(), current);
|
2012-08-16 15:15:33 +00:00
|
|
|
}
|
|
|
|
inline bool fastTryUnlock(QMutexData *¤t) Q_DECL_NOTHROW {
|
2015-06-30 20:11:15 +00:00
|
|
|
return d_ptr.testAndSetRelease(dummyLocked(), Q_NULLPTR, current);
|
2012-08-16 15:15:33 +00:00
|
|
|
}
|
2012-08-11 14:45:14 +00:00
|
|
|
|
2012-08-11 10:18:45 +00:00
|
|
|
void lockInternal() QT_MUTEX_LOCK_NOEXCEPT;
|
|
|
|
bool lockInternal(int timeout) QT_MUTEX_LOCK_NOEXCEPT;
|
2012-08-02 14:15:22 +00:00
|
|
|
void unlockInternal() Q_DECL_NOTHROW;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-10-20 15:11:18 +00:00
|
|
|
QBasicAtomicPointer<QMutexData> d_ptr;
|
|
|
|
static inline QMutexData *dummyLocked() {
|
|
|
|
return reinterpret_cast<QMutexData *>(quintptr(1));
|
2011-07-02 13:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
friend class QMutex;
|
2011-10-20 15:11:18 +00:00
|
|
|
friend class QMutexData;
|
2011-07-02 13:13:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Q_CORE_EXPORT QMutex : public QBasicMutex {
|
|
|
|
public:
|
|
|
|
enum RecursionMode { NonRecursive, Recursive };
|
|
|
|
explicit QMutex(RecursionMode mode = NonRecursive);
|
|
|
|
~QMutex();
|
2012-05-23 08:26:07 +00:00
|
|
|
|
2012-08-02 14:15:22 +00:00
|
|
|
void lock() QT_MUTEX_LOCK_NOEXCEPT;
|
|
|
|
bool tryLock(int timeout = 0) QT_MUTEX_LOCK_NOEXCEPT;
|
|
|
|
void unlock() Q_DECL_NOTHROW;
|
2012-05-23 08:26:07 +00:00
|
|
|
|
|
|
|
using QBasicMutex::isRecursive;
|
|
|
|
|
2011-07-02 13:13:12 +00:00
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QMutex)
|
2012-05-23 08:26:07 +00:00
|
|
|
friend class QMutexLocker;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Q_CORE_EXPORT QMutexLocker
|
|
|
|
{
|
|
|
|
public:
|
2012-08-02 14:15:22 +00:00
|
|
|
inline explicit QMutexLocker(QBasicMutex *m) QT_MUTEX_LOCK_NOEXCEPT
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
Q_ASSERT_X((reinterpret_cast<quintptr>(m) & quintptr(1u)) == quintptr(0),
|
|
|
|
"QMutexLocker", "QMutex pointer is misaligned");
|
2012-05-23 08:26:07 +00:00
|
|
|
val = quintptr(m);
|
2012-08-01 15:54:32 +00:00
|
|
|
if (Q_LIKELY(m)) {
|
|
|
|
// call QMutex::lock() instead of QBasicMutex::lock()
|
|
|
|
static_cast<QMutex *>(m)->lock();
|
|
|
|
val |= 1;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
inline ~QMutexLocker() { unlock(); }
|
|
|
|
|
2012-08-02 14:15:22 +00:00
|
|
|
inline void unlock() Q_DECL_NOTHROW
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if ((val & quintptr(1u)) == quintptr(1u)) {
|
|
|
|
val &= ~quintptr(1u);
|
2011-07-02 13:13:12 +00:00
|
|
|
mutex()->unlock();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 14:15:22 +00:00
|
|
|
inline void relock() QT_MUTEX_LOCK_NOEXCEPT
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (val) {
|
|
|
|
if ((val & quintptr(1u)) == quintptr(0u)) {
|
2011-07-02 13:13:12 +00:00
|
|
|
mutex()->lock();
|
2011-04-27 10:05:43 +00:00
|
|
|
val |= quintptr(1u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(Q_CC_MSVC)
|
|
|
|
#pragma warning( push )
|
|
|
|
#pragma warning( disable : 4312 ) // ignoring the warning from /Wp64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
inline QMutex *mutex() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<QMutex *>(val & ~quintptr(1u));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(Q_CC_MSVC)
|
|
|
|
#pragma warning( pop )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QMutexLocker)
|
|
|
|
|
|
|
|
quintptr val;
|
|
|
|
};
|
|
|
|
|
2012-12-28 06:18:53 +00:00
|
|
|
#else // QT_NO_THREAD or Q_QDOC
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
class Q_CORE_EXPORT QMutex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum RecursionMode { NonRecursive, Recursive };
|
|
|
|
|
2015-01-08 14:03:39 +00:00
|
|
|
inline explicit QMutex(RecursionMode mode = NonRecursive) Q_DECL_NOTHROW { Q_UNUSED(mode); }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-01-08 14:03:39 +00:00
|
|
|
inline void lock() Q_DECL_NOTHROW {}
|
|
|
|
inline bool tryLock(int timeout = 0) Q_DECL_NOTHROW { Q_UNUSED(timeout); return true; }
|
|
|
|
inline void unlock() Q_DECL_NOTHROW {}
|
|
|
|
inline bool isRecursive() Q_DECL_NOTHROW { return true; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QMutex)
|
|
|
|
};
|
|
|
|
|
|
|
|
class Q_CORE_EXPORT QMutexLocker
|
|
|
|
{
|
|
|
|
public:
|
2015-01-08 14:03:39 +00:00
|
|
|
inline explicit QMutexLocker(QMutex *) Q_DECL_NOTHROW {}
|
|
|
|
inline ~QMutexLocker() Q_DECL_NOTHROW {}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-01-08 14:03:39 +00:00
|
|
|
inline void unlock() Q_DECL_NOTHROW {}
|
|
|
|
void relock() Q_DECL_NOTHROW {}
|
|
|
|
inline QMutex *mutex() const Q_DECL_NOTHROW { return Q_NULLPTR; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QMutexLocker)
|
|
|
|
};
|
|
|
|
|
2011-07-02 13:13:12 +00:00
|
|
|
typedef QMutex QBasicMutex;
|
|
|
|
|
2012-12-28 06:18:53 +00:00
|
|
|
#endif // QT_NO_THREAD or Q_QDOC
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
#endif // QMUTEX_H
|