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

Last change on this file since 788 was 769, checked in by Dmitry A. Kuminov, 15 years ago

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

File size: 4.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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 : val(reinterpret_cast<quintptr>(m))
99 {
100 Q_ASSERT_X((val & quintptr(1u)) == quintptr(0),
101 "QMutexLocker", "QMutex pointer is misaligned");
102 relock();
103 }
104 inline ~QMutexLocker() { unlock(); }
105
106 inline void unlock()
107 {
108 if (val) {
109 if ((val & quintptr(1u)) == quintptr(1u)) {
110 val &= ~quintptr(1u);
111 mutex()->unlock();
112 }
113 }
114 }
115
116 inline void relock()
117 {
118 if (val) {
119 if ((val & quintptr(1u)) == quintptr(0u)) {
120 mutex()->lock();
121 val |= quintptr(1u);
122 }
123 }
124 }
125
126#if defined(Q_CC_MSVC)
127#pragma warning( push )
128#pragma warning( disable : 4312 ) // ignoring the warning from /Wp64
129#endif
130
131 inline QMutex *mutex() const
132 {
133 return reinterpret_cast<QMutex *>(val & ~quintptr(1u));
134 }
135
136#if defined(Q_CC_MSVC)
137#pragma warning( pop )
138#endif
139
140private:
141 Q_DISABLE_COPY(QMutexLocker)
142
143 quintptr val;
144};
145
146#else // QT_NO_THREAD
147
148
149class Q_CORE_EXPORT QMutex
150{
151public:
152 enum RecursionMode { NonRecursive, Recursive };
153
154 inline explicit QMutex(RecursionMode mode = NonRecursive) { Q_UNUSED(mode); }
155 inline ~QMutex() {}
156
157 static inline void lock() {}
158 static inline bool tryLock() { return true; }
159 static inline bool tryLock(int timeout) { Q_UNUSED(timeout); return true; }
160 static void unlock() {}
161
162#if defined(QT3_SUPPORT)
163 static inline QT3_SUPPORT bool locked() { return false; }
164#endif
165
166private:
167 Q_DISABLE_COPY(QMutex)
168};
169
170class Q_CORE_EXPORT QMutexLocker
171{
172public:
173 inline explicit QMutexLocker(QMutex *) {}
174 inline ~QMutexLocker() {}
175
176 static inline void unlock() {}
177 static void relock() {}
178 static inline QMutex *mutex() { return 0; }
179
180private:
181 Q_DISABLE_COPY(QMutexLocker)
182};
183
184#endif // QT_NO_THREAD
185
186QT_END_NAMESPACE
187
188QT_END_HEADER
189
190#endif // QMUTEX_H
Note: See TracBrowser for help on using the repository browser.