0f3a72d43f6a0f1ad6602f1da08f4e43187ba8de
[profile/ivi/qtbase.git] / src / corelib / thread / qmutex.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QMUTEX_H
43 #define QMUTEX_H
44
45 #include <QtCore/qglobal.h>
46 #include <QtCore/qatomic.h>
47 #include <new>
48
49 QT_BEGIN_HEADER
50
51 QT_BEGIN_NAMESPACE
52
53 QT_MODULE(Core)
54
55 #ifndef QT_NO_THREAD
56
57 class QAtomicInt;
58 class QMutexData;
59
60 class Q_CORE_EXPORT QMutex
61 {
62     friend class QWaitCondition;
63     friend class QWaitConditionPrivate;
64
65 public:
66     enum RecursionMode { NonRecursive, Recursive };
67
68     explicit QMutex(RecursionMode mode = NonRecursive);
69     ~QMutex();
70
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();
78
79 #if defined(QT3_SUPPORT)
80     inline QT3_SUPPORT bool locked()
81     {
82         if (!tryLock())
83             return true;
84         unlock();
85         return false;
86     }
87     inline QT3_SUPPORT_CONSTRUCTOR QMutex(bool recursive)
88     {
89         new (this) QMutex(recursive ? Recursive : NonRecursive);
90     }
91 #endif
92
93 private:
94     void lockInternal();
95     void unlockInternal();
96     Q_DISABLE_COPY(QMutex)
97
98     QMutexData *d;
99 };
100
101 class Q_CORE_EXPORT QMutexLocker
102 {
103 public:
104     inline explicit QMutexLocker(QMutex *m)
105     {
106         Q_ASSERT_X((reinterpret_cast<quintptr>(m) & quintptr(1u)) == quintptr(0),
107                    "QMutexLocker", "QMutex pointer is misaligned");
108         if (m) {
109             m->lockInline();
110             val = reinterpret_cast<quintptr>(m) | quintptr(1u);
111         } else {
112             val = 0;
113         }
114     }
115     inline ~QMutexLocker() { unlock(); }
116
117     inline void unlock()
118     {
119         if ((val & quintptr(1u)) == quintptr(1u)) {
120             val &= ~quintptr(1u);
121             mutex()->unlockInline();
122         }
123     }
124
125     inline void relock()
126     {
127         if (val) {
128             if ((val & quintptr(1u)) == quintptr(0u)) {
129                 mutex()->lockInline();
130                 val |= quintptr(1u);
131             }
132         }
133     }
134
135 #if defined(Q_CC_MSVC)
136 #pragma warning( push )
137 #pragma warning( disable : 4312 ) // ignoring the warning from /Wp64
138 #endif
139
140     inline QMutex *mutex() const
141     {
142         return reinterpret_cast<QMutex *>(val & ~quintptr(1u));
143     }
144
145 #if defined(Q_CC_MSVC)
146 #pragma warning( pop )
147 #endif
148
149 private:
150     Q_DISABLE_COPY(QMutexLocker)
151
152     quintptr val;
153 };
154
155 class QMutexData
156 {
157     public:
158         QAtomicInt contenders;
159         const uint recursive : 1;
160         uint reserved : 31;
161     protected:
162         QMutexData(QMutex::RecursionMode mode);
163         ~QMutexData();
164 };
165
166 #ifdef QT_NO_DEBUG
167 inline void QMutex::unlockInline()
168 {
169     if (d->recursive) {
170         unlock();
171     } else if (!d->contenders.testAndSetRelease(1, 0)) {
172         unlockInternal();
173     }
174 }
175
176 inline bool QMutex::tryLockInline()
177 {
178     if (d->recursive) {
179         return tryLock();
180     } else {
181         return d->contenders.testAndSetAcquire(0, 1);
182     }
183 }
184
185 inline void QMutex::lockInline()
186 {
187     if (d->recursive) {
188         lock();
189     } else if(!tryLockInline()) {
190         lockInternal();
191     }
192 }
193 #else // QT_NO_DEBUG
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
200
201
202 #else // QT_NO_THREAD
203
204
205 class Q_CORE_EXPORT QMutex
206 {
207 public:
208     enum RecursionMode { NonRecursive, Recursive };
209
210     inline explicit QMutex(RecursionMode mode = NonRecursive) { Q_UNUSED(mode); }
211     inline ~QMutex() {}
212
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() {}
219
220 #if defined(QT3_SUPPORT)
221     static inline QT3_SUPPORT bool locked() { return false; }
222 #endif
223
224 private:
225     Q_DISABLE_COPY(QMutex)
226 };
227
228 class Q_CORE_EXPORT QMutexLocker
229 {
230 public:
231     inline explicit QMutexLocker(QMutex *) {}
232     inline ~QMutexLocker() {}
233
234     static inline void unlock() {}
235     static void relock() {}
236     static inline QMutex *mutex() { return 0; }
237
238 private:
239     Q_DISABLE_COPY(QMutexLocker)
240 };
241
242 #endif // QT_NO_THREAD
243
244 QT_END_NAMESPACE
245
246 QT_END_HEADER
247
248 #endif // QMUTEX_H