source: trunk/src/corelib/thread/qmutex.h@ 1050

Last change on this file since 1050 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 4.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QMUTEX_H
43#define QMUTEX_H
44
45#include <QtCore/qglobal.h>
46#include <new>
47
48QT_BEGIN_HEADER
49
50QT_BEGIN_NAMESPACE
51
52QT_MODULE(Core)
53
54#ifndef QT_NO_THREAD
55
56class QMutexPrivate;
57
58class Q_CORE_EXPORT QMutex
59{
60 friend class QWaitCondition;
61 friend class QWaitConditionPrivate;
62
63public:
64 enum RecursionMode { NonRecursive, Recursive };
65
66 explicit QMutex(RecursionMode mode = NonRecursive);
67 ~QMutex();
68
69 void lock();
70 bool tryLock();
71 bool tryLock(int timeout);
72 void unlock();
73
74#if defined(QT3_SUPPORT)
75 inline QT3_SUPPORT bool locked()
76 {
77 if (!tryLock())
78 return true;
79 unlock();
80 return false;
81 }
82 inline QT3_SUPPORT_CONSTRUCTOR QMutex(bool recursive)
83 {
84 new (this) QMutex(recursive ? Recursive : NonRecursive);
85 }
86#endif
87
88private:
89 Q_DISABLE_COPY(QMutex)
90
91 QMutexPrivate *d;
92};
93
94class Q_CORE_EXPORT QMutexLocker
95{
96public:
97 inline explicit QMutexLocker(QMutex *m)
98 {
99 Q_ASSERT_X((reinterpret_cast<quintptr>(m) & quintptr(1u)) == quintptr(0),
100 "QMutexLocker", "QMutex pointer is misaligned");
101 if (m) {
102 m->lock();
103 val = reinterpret_cast<quintptr>(m) | quintptr(1u);
104 } else {
105 val = 0;
106 }
107 }
108 inline ~QMutexLocker() { unlock(); }
109
110 inline void unlock()
111 {
112 if ((val & quintptr(1u)) == quintptr(1u)) {
113 val &= ~quintptr(1u);
114 mutex()->unlock();
115 }
116 }
117
118 inline void relock()
119 {
120 if (val) {
121 if ((val & quintptr(1u)) == quintptr(0u)) {
122 mutex()->lock();
123 val |= quintptr(1u);
124 }
125 }
126 }
127
128#if defined(Q_CC_MSVC)
129#pragma warning( push )
130#pragma warning( disable : 4312 ) // ignoring the warning from /Wp64
131#endif
132
133 inline QMutex *mutex() const
134 {
135 return reinterpret_cast<QMutex *>(val & ~quintptr(1u));
136 }
137
138#if defined(Q_CC_MSVC)
139#pragma warning( pop )
140#endif
141
142private:
143 Q_DISABLE_COPY(QMutexLocker)
144
145 quintptr val;
146};
147
148#else // QT_NO_THREAD
149
150
151class Q_CORE_EXPORT QMutex
152{
153public:
154 enum RecursionMode { NonRecursive, Recursive };
155
156 inline explicit QMutex(RecursionMode mode = NonRecursive) { Q_UNUSED(mode); }
157 inline ~QMutex() {}
158
159 static inline void lock() {}
160 static inline bool tryLock() { return true; }
161 static inline bool tryLock(int timeout) { Q_UNUSED(timeout); return true; }
162 static void unlock() {}
163
164#if defined(QT3_SUPPORT)
165 static inline QT3_SUPPORT bool locked() { return false; }
166#endif
167
168private:
169 Q_DISABLE_COPY(QMutex)
170};
171
172class Q_CORE_EXPORT QMutexLocker
173{
174public:
175 inline explicit QMutexLocker(QMutex *) {}
176 inline ~QMutexLocker() {}
177
178 static inline void unlock() {}
179 static void relock() {}
180 static inline QMutex *mutex() { return 0; }
181
182private:
183 Q_DISABLE_COPY(QMutexLocker)
184};
185
186#endif // QT_NO_THREAD
187
188QT_END_NAMESPACE
189
190QT_END_HEADER
191
192#endif // QMUTEX_H
Note: See TracBrowser for help on using the repository browser.