2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2012-01-05 04:03:39 +00:00
|
|
|
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2012-01-20 03:06:31 +00:00
|
|
|
** Contact: http://www.qt-project.org/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtCore module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** GNU Lesser General Public License Usage
|
2011-05-24 09:34:08 +00:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this
|
|
|
|
** file. Please review the following information to ensure the GNU Lesser
|
|
|
|
** General Public License version 2.1 requirements will be met:
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-05-24 09:34:08 +00:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-04-27 10:05:43 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-05-24 09:34:08 +00:00
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
** Other Usage
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
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_HEADER
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
|
2011-07-02 13:13:12 +00:00
|
|
|
#if !defined(QT_NO_THREAD) && !defined(qdoc)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
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:
|
2011-07-02 13:13:12 +00:00
|
|
|
inline void lock() {
|
|
|
|
if (!fastTryLock())
|
|
|
|
lockInternal();
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-02 13:13:12 +00:00
|
|
|
inline void unlock() {
|
2011-10-28 09:51:06 +00:00
|
|
|
Q_ASSERT(d_ptr.load()); //mutex must be locked
|
|
|
|
if (!d_ptr.testAndSetRelease(dummyLocked(), 0))
|
2011-07-02 13:13:12 +00:00
|
|
|
unlockInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tryLock(int timeout = 0) {
|
|
|
|
return fastTryLock() || lockInternal(timeout);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-02 13:13:12 +00:00
|
|
|
bool isRecursive();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
2011-07-02 13:13:12 +00:00
|
|
|
inline bool fastTryLock() {
|
2011-10-28 09:51:06 +00:00
|
|
|
return d_ptr.testAndSetAcquire(0, dummyLocked());
|
2011-07-02 13:13:12 +00:00
|
|
|
}
|
|
|
|
bool lockInternal(int timeout = -1);
|
2011-04-27 10:05:43 +00:00
|
|
|
void unlockInternal();
|
|
|
|
|
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
|
|
|
|
|
|
|
void lock();
|
|
|
|
bool tryLock(int timeout = 0);
|
|
|
|
void unlock();
|
|
|
|
|
|
|
|
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:
|
2011-07-02 13:13:12 +00:00
|
|
|
inline explicit QMutexLocker(QBasicMutex *m)
|
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(); }
|
|
|
|
|
|
|
|
inline void unlock()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void relock()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-02 13:13:12 +00:00
|
|
|
#else // QT_NO_THREAD or qdoc
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
class Q_CORE_EXPORT QMutex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum RecursionMode { NonRecursive, Recursive };
|
|
|
|
|
|
|
|
inline explicit QMutex(RecursionMode mode = NonRecursive) { Q_UNUSED(mode); }
|
|
|
|
|
|
|
|
static inline void lock() {}
|
|
|
|
static inline bool tryLock(int timeout = 0) { Q_UNUSED(timeout); return true; }
|
|
|
|
static inline void unlock() {}
|
2011-07-02 13:13:12 +00:00
|
|
|
static inline bool isRecursive() { return true; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QMutex)
|
|
|
|
};
|
|
|
|
|
|
|
|
class Q_CORE_EXPORT QMutexLocker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline explicit QMutexLocker(QMutex *) {}
|
|
|
|
inline ~QMutexLocker() {}
|
|
|
|
|
|
|
|
static inline void unlock() {}
|
|
|
|
static void relock() {}
|
|
|
|
static inline QMutex *mutex() { return 0; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QMutexLocker)
|
|
|
|
};
|
|
|
|
|
2011-07-02 13:13:12 +00:00
|
|
|
typedef QMutex QBasicMutex;
|
|
|
|
|
|
|
|
#endif // QT_NO_THREAD or qdoc
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
QT_END_HEADER
|
|
|
|
|
|
|
|
#endif // QMUTEX_H
|