qmutex.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QMUTEX_H
00043 #define QMUTEX_H
00044 
00045 #include <QtCore/qglobal.h>
00046 #include <new>
00047 
00048 QT_BEGIN_HEADER
00049 
00050 QT_BEGIN_NAMESPACE
00051 
00052 QT_MODULE(Core)
00053 
00054 #ifndef QT_NO_THREAD
00055 
00056 class QMutexPrivate;
00057 
00058 class Q_CORE_EXPORT QMutex
00059 {
00060     friend class QWaitCondition;
00061     friend class QWaitConditionPrivate;
00062 
00063 public:
00064     enum RecursionMode { NonRecursive, Recursive };
00065 
00066     explicit QMutex(RecursionMode mode = NonRecursive);
00067     ~QMutex();
00068 
00069     void lock();
00070     bool tryLock();
00071     bool tryLock(int timeout);
00072     void unlock();
00073 
00074 #if defined(QT3_SUPPORT)
00075     inline QT3_SUPPORT bool locked()
00076     {
00077         if (!tryLock())
00078             return true;
00079         unlock();
00080         return false;
00081     }
00082     inline QT3_SUPPORT_CONSTRUCTOR QMutex(bool recursive)
00083     {
00084         new (this) QMutex(recursive ? Recursive : NonRecursive);
00085     }
00086 #endif
00087 
00088 private:
00089     Q_DISABLE_COPY(QMutex)
00090 
00091     QMutexPrivate *d;
00092 };
00093 
00094 class Q_CORE_EXPORT QMutexLocker
00095 {
00096 public:
00097     inline explicit QMutexLocker(QMutex *m)
00098     {
00099         Q_ASSERT_X((reinterpret_cast<quintptr>(m) & quintptr(1u)) == quintptr(0),
00100                    "QMutexLocker", "QMutex pointer is misaligned");
00101         if (m) {
00102             m->lock();
00103             val = reinterpret_cast<quintptr>(m) | quintptr(1u);
00104         } else {
00105             val = 0;
00106         }
00107     }
00108     inline ~QMutexLocker() { unlock(); }
00109 
00110     inline void unlock()
00111     {
00112         if ((val & quintptr(1u)) == quintptr(1u)) {
00113             val &= ~quintptr(1u);
00114             mutex()->unlock();
00115         }
00116     }
00117 
00118     inline void relock()
00119     {
00120         if (val) {
00121             if ((val & quintptr(1u)) == quintptr(0u)) {
00122                 mutex()->lock();
00123                 val |= quintptr(1u);
00124             }
00125         }
00126     }
00127 
00128 #if defined(Q_CC_MSVC)
00129 #pragma warning( push )
00130 #pragma warning( disable : 4312 ) // ignoring the warning from /Wp64
00131 #endif
00132 
00133     inline QMutex *mutex() const
00134     {
00135         return reinterpret_cast<QMutex *>(val & ~quintptr(1u));
00136     }
00137 
00138 #if defined(Q_CC_MSVC)
00139 #pragma warning( pop )
00140 #endif
00141 
00142 private:
00143     Q_DISABLE_COPY(QMutexLocker)
00144 
00145     quintptr val;
00146 };
00147 
00148 #else // QT_NO_THREAD
00149 
00150 
00151 class Q_CORE_EXPORT QMutex
00152 {
00153 public:
00154     enum RecursionMode { NonRecursive, Recursive };
00155 
00156     inline explicit QMutex(RecursionMode mode = NonRecursive) { Q_UNUSED(mode); }
00157     inline ~QMutex() {}
00158 
00159     static inline void lock() {}
00160     static inline bool tryLock() { return true; }
00161     static inline bool tryLock(int timeout) { Q_UNUSED(timeout); return true; }
00162     static void unlock() {}
00163 
00164 #if defined(QT3_SUPPORT)
00165     static inline QT3_SUPPORT bool locked() { return false; }
00166 #endif
00167 
00168 private:
00169     Q_DISABLE_COPY(QMutex)
00170 };
00171 
00172 class Q_CORE_EXPORT QMutexLocker
00173 {
00174 public:
00175     inline explicit QMutexLocker(QMutex *) {}
00176     inline ~QMutexLocker() {}
00177 
00178     static inline void unlock() {}
00179     static void relock() {}
00180     static inline QMutex *mutex() { return 0; }
00181 
00182 private:
00183     Q_DISABLE_COPY(QMutexLocker)
00184 };
00185 
00186 #endif // QT_NO_THREAD
00187 
00188 QT_END_NAMESPACE
00189 
00190 QT_END_HEADER
00191 
00192 #endif // QMUTEX_H