1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtCore/qglobal.h>
46 #include <QtCore/qatomic.h>
60 class Q_CORE_EXPORT QMutex
62 friend class QWaitCondition;
63 friend class QWaitConditionPrivate;
66 enum RecursionMode { NonRecursive, Recursive };
68 explicit QMutex(RecursionMode mode = NonRecursive);
71 void lock(); //### Qt5: make inline;
72 inline void lockInline();
73 bool tryLock(); //### Qt5: make inline;
74 bool tryLock(int timeout);
75 inline bool tryLockInline();
76 void unlock(); //### Qt5: make inline;
77 inline void unlockInline();
79 #if defined(QT3_SUPPORT)
80 inline QT3_SUPPORT bool locked()
87 inline QT3_SUPPORT_CONSTRUCTOR QMutex(bool recursive)
89 new (this) QMutex(recursive ? Recursive : NonRecursive);
95 void unlockInternal();
96 Q_DISABLE_COPY(QMutex)
101 class Q_CORE_EXPORT QMutexLocker
104 inline explicit QMutexLocker(QMutex *m)
106 Q_ASSERT_X((reinterpret_cast<quintptr>(m) & quintptr(1u)) == quintptr(0),
107 "QMutexLocker", "QMutex pointer is misaligned");
110 val = reinterpret_cast<quintptr>(m) | quintptr(1u);
115 inline ~QMutexLocker() { unlock(); }
119 if ((val & quintptr(1u)) == quintptr(1u)) {
120 val &= ~quintptr(1u);
121 mutex()->unlockInline();
128 if ((val & quintptr(1u)) == quintptr(0u)) {
129 mutex()->lockInline();
135 #if defined(Q_CC_MSVC)
136 #pragma warning( push )
137 #pragma warning( disable : 4312 ) // ignoring the warning from /Wp64
140 inline QMutex *mutex() const
142 return reinterpret_cast<QMutex *>(val & ~quintptr(1u));
145 #if defined(Q_CC_MSVC)
146 #pragma warning( pop )
150 Q_DISABLE_COPY(QMutexLocker)
158 QAtomicInt contenders;
159 const uint recursive : 1;
162 QMutexData(QMutex::RecursionMode mode);
167 inline void QMutex::unlockInline()
171 } else if (!d->contenders.testAndSetRelease(1, 0)) {
176 inline bool QMutex::tryLockInline()
181 return d->contenders.testAndSetAcquire(0, 1);
185 inline void QMutex::lockInline()
189 } else if(!tryLockInline()) {
194 //in debug we do not use inline calls in order to allow debugging tools
195 // to hook the mutex locking functions.
196 inline void QMutex::unlockInline() { unlock(); }
197 inline bool QMutex::tryLockInline() { return tryLock(); }
198 inline void QMutex::lockInline() { lock(); }
199 #endif // QT_NO_DEBUG
202 #else // QT_NO_THREAD
205 class Q_CORE_EXPORT QMutex
208 enum RecursionMode { NonRecursive, Recursive };
210 inline explicit QMutex(RecursionMode mode = NonRecursive) { Q_UNUSED(mode); }
213 static inline void lock() {}
214 static inline void lockInline() {}
215 static inline bool tryLock(int timeout = 0) { Q_UNUSED(timeout); return true; }
216 static inline bool tryLockInline() { return true; }
217 static inline void unlock() {}
218 static inline void unlockInline() {}
220 #if defined(QT3_SUPPORT)
221 static inline QT3_SUPPORT bool locked() { return false; }
225 Q_DISABLE_COPY(QMutex)
228 class Q_CORE_EXPORT QMutexLocker
231 inline explicit QMutexLocker(QMutex *) {}
232 inline ~QMutexLocker() {}
234 static inline void unlock() {}
235 static void relock() {}
236 static inline QMutex *mutex() { return 0; }
239 Q_DISABLE_COPY(QMutexLocker)
242 #endif // QT_NO_THREAD