src/corelib/thread/qmutex.h
changeset 7 f7bc934e204c
parent 0 1918ee327afb
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the QtCore module of the Qt Toolkit.
     7 ** This file is part of the QtCore module of the Qt Toolkit.
     8 **
     8 **
    93 
    93 
    94 class Q_CORE_EXPORT QMutexLocker
    94 class Q_CORE_EXPORT QMutexLocker
    95 {
    95 {
    96 public:
    96 public:
    97     inline explicit QMutexLocker(QMutex *m)
    97     inline explicit QMutexLocker(QMutex *m)
    98         : mtx(m)
    98         : val(reinterpret_cast<quintptr>(m))
    99     {
    99     {
   100         Q_ASSERT_X((val & quintptr(1u)) == quintptr(0),
   100         Q_ASSERT_X((val & quintptr(1u)) == quintptr(0),
   101                    "QMutexLocker", "QMutex pointer is misaligned");
   101                    "QMutexLocker", "QMutex pointer is misaligned");
   102         relock();
   102         relock();
   103     }
   103     }
   104     inline ~QMutexLocker() { unlock(); }
   104     inline ~QMutexLocker() { unlock(); }
   105 
   105 
   106     inline void unlock()
   106     inline void unlock()
   107     {
   107     {
   108         if (mtx) {
   108         if (val) {
   109             if ((val & quintptr(1u)) == quintptr(1u)) {
   109             if ((val & quintptr(1u)) == quintptr(1u)) {
   110                 val &= ~quintptr(1u);
   110                 val &= ~quintptr(1u);
   111                 mtx->unlock();
   111                 mutex()->unlock();
   112             }
   112             }
   113         }
   113         }
   114     }
   114     }
   115 
   115 
   116     inline void relock()
   116     inline void relock()
   117     {
   117     {
   118         if (mtx) {
   118         if (val) {
   119             if ((val & quintptr(1u)) == quintptr(0u)) {
   119             if ((val & quintptr(1u)) == quintptr(0u)) {
   120                 mtx->lock();
   120                 mutex()->lock();
   121                 val |= quintptr(1u);
   121                 val |= quintptr(1u);
   122             }
   122             }
   123         }
   123         }
   124     }
   124     }
   125 
   125 
   138 #endif
   138 #endif
   139 
   139 
   140 private:
   140 private:
   141     Q_DISABLE_COPY(QMutexLocker)
   141     Q_DISABLE_COPY(QMutexLocker)
   142 
   142 
   143     union {
   143     quintptr val;
   144         QMutex *mtx;
       
   145         quintptr val;
       
   146     };
       
   147 };
   144 };
   148 
   145 
   149 #else // QT_NO_THREAD
   146 #else // QT_NO_THREAD
   150 
   147 
   151 
   148