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>
|
|
|
|
|
2016-07-14 14:37:55 +00:00
|
|
|
#if QT_HAS_INCLUDE(<chrono>)
|
|
|
|
# include <chrono>
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
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:
|
2016-07-14 14:37:55 +00:00
|
|
|
// BasicLockable concept
|
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
|
|
|
|
2016-07-14 14:37:55 +00:00
|
|
|
// BasicLockable concept
|
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
|
|
|
|
2016-07-14 14:37:55 +00:00
|
|
|
// Lockable concept
|
|
|
|
bool try_lock() Q_DECL_NOTHROW { return tryLock(); }
|
|
|
|
|
2016-04-25 23:42:11 +00:00
|
|
|
bool isRecursive() Q_DECL_NOTHROW; //### Qt6: remove me
|
|
|
|
bool isRecursive() const Q_DECL_NOTHROW;
|
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
|
|
|
};
|
|
|
|
|
2016-04-25 23:42:11 +00:00
|
|
|
class Q_CORE_EXPORT QMutex : public QBasicMutex
|
|
|
|
{
|
2011-07-02 13:13:12 +00:00
|
|
|
public:
|
|
|
|
enum RecursionMode { NonRecursive, Recursive };
|
|
|
|
explicit QMutex(RecursionMode mode = NonRecursive);
|
|
|
|
~QMutex();
|
2012-05-23 08:26:07 +00:00
|
|
|
|
2016-07-14 14:37:55 +00:00
|
|
|
// BasicLockable concept
|
2012-08-02 14:15:22 +00:00
|
|
|
void lock() QT_MUTEX_LOCK_NOEXCEPT;
|
|
|
|
bool tryLock(int timeout = 0) QT_MUTEX_LOCK_NOEXCEPT;
|
2016-07-14 14:37:55 +00:00
|
|
|
// BasicLockable concept
|
2012-08-02 14:15:22 +00:00
|
|
|
void unlock() Q_DECL_NOTHROW;
|
2012-05-23 08:26:07 +00:00
|
|
|
|
2016-07-14 14:37:55 +00:00
|
|
|
// Lockable concept
|
|
|
|
bool try_lock() QT_MUTEX_LOCK_NOEXCEPT { return tryLock(); }
|
|
|
|
|
|
|
|
#if QT_HAS_INCLUDE(<chrono>)
|
|
|
|
// TimedLockable concept
|
|
|
|
template <class Rep, class Period>
|
|
|
|
bool try_lock_for(std::chrono::duration<Rep, Period> duration)
|
|
|
|
{
|
|
|
|
// § 30.4.1.3.5 [thread.timedmutex.requirements] specifies that a
|
|
|
|
// duration less than or equal to duration.zero() shall result in a
|
|
|
|
// try_lock, unlike QMutex's tryLock with a negative duration which
|
|
|
|
// results in a lock.
|
|
|
|
|
|
|
|
if (duration <= duration.zero())
|
|
|
|
return tryLock(0);
|
|
|
|
return tryLock(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count());
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimedLockable concept
|
|
|
|
template<class Clock, class Duration>
|
|
|
|
bool try_lock_until(std::chrono::time_point<Clock, Duration> timePoint)
|
|
|
|
{
|
|
|
|
// Implemented in terms of try_lock_for to honor the similar
|
|
|
|
// requirement in § 30.4.1.3.12 [thread.timedmutex.requirements]
|
|
|
|
|
|
|
|
return try_lock_for(timePoint - Clock::now());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-25 23:42:11 +00:00
|
|
|
bool isRecursive() const Q_DECL_NOTHROW
|
|
|
|
{ return QBasicMutex::isRecursive(); }
|
2012-05-23 08:26:07 +00:00
|
|
|
|
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; }
|
2016-07-14 14:37:55 +00:00
|
|
|
inline bool try_lock() Q_DECL_NOTHROW { return true; }
|
2015-01-08 14:03:39 +00:00
|
|
|
inline void unlock() Q_DECL_NOTHROW {}
|
2016-04-25 23:42:11 +00:00
|
|
|
inline bool isRecursive() const Q_DECL_NOTHROW { return true; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-07-14 14:37:55 +00:00
|
|
|
#if QT_HAS_INCLUDE(<chrono>) || defined(Q_QDOC)
|
|
|
|
template <class Rep, class Period>
|
|
|
|
inline bool try_lock_for(std::chrono::duration<Rep, Period> duration) Q_DECL_NOTHROW
|
|
|
|
{
|
|
|
|
Q_UNUSED(duration);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Clock, class Duration>
|
|
|
|
inline bool try_lock_until(std::chrono::time_point<Clock, Duration> timePoint) Q_DECL_NOTHROW
|
|
|
|
{
|
|
|
|
Q_UNUSED(timePoint);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|