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

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

trunk: Merged in qt 4.6.1 sources.

File size: 4.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 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 : mtx(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 (mtx) {
109 if ((val & quintptr(1u)) == quintptr(1u)) {
110 val &= ~quintptr(1u);
111 mtx->unlock();
112 }
113 }
114 }
115
116 inline void relock()
117 {
118 if (mtx) {
119 if ((val & quintptr(1u)) == quintptr(0u)) {
120 mtx->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 union {
144 QMutex *mtx;
145 quintptr val;
146 };
147};
148
149#else // QT_NO_THREAD
150
151
152class Q_CORE_EXPORT QMutex
153{
154public:
155 enum RecursionMode { NonRecursive, Recursive };
156
157 inline explicit QMutex(RecursionMode mode = NonRecursive) { Q_UNUSED(mode); }
158 inline ~QMutex() {}
159
160 static inline void lock() {}
161 static inline bool tryLock() { return true; }
162 static inline bool tryLock(int timeout) { Q_UNUSED(timeout); return true; }
163 static void unlock() {}
164
165#if defined(QT3_SUPPORT)
166 static inline QT3_SUPPORT bool locked() { return false; }
167#endif
168
169private:
170 Q_DISABLE_COPY(QMutex)
171};
172
173class Q_CORE_EXPORT QMutexLocker
174{
175public:
176 inline explicit QMutexLocker(QMutex *) {}
177 inline ~QMutexLocker() {}
178
179 static inline void unlock() {}
180 static void relock() {}
181 static inline QMutex *mutex() { return 0; }
182
183private:
184 Q_DISABLE_COPY(QMutexLocker)
185};
186
187#endif // QT_NO_THREAD
188
189QT_END_NAMESPACE
190
191QT_END_HEADER
192
193#endif // QMUTEX_H
Note: See TracBrowser for help on using the repository browser.