author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 12:15:23 +0300 | |
branch | RCL_3 |
changeset 12 | cc75c76972ee |
parent 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
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 <new> |
|
47 |
||
48 |
QT_BEGIN_HEADER |
|
49 |
||
50 |
QT_BEGIN_NAMESPACE |
|
51 |
||
52 |
QT_MODULE(Core) |
|
53 |
||
54 |
#ifndef QT_NO_THREAD |
|
55 |
||
56 |
class QMutexPrivate; |
|
57 |
||
58 |
class Q_CORE_EXPORT QMutex |
|
59 |
{ |
|
60 |
friend class QWaitCondition; |
|
61 |
friend class QWaitConditionPrivate; |
|
62 |
||
63 |
public: |
|
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 |
||
88 |
private: |
|
89 |
Q_DISABLE_COPY(QMutex) |
|
90 |
||
91 |
QMutexPrivate *d; |
|
92 |
}; |
|
93 |
||
94 |
class Q_CORE_EXPORT QMutexLocker |
|
95 |
{ |
|
96 |
public: |
|
97 |
inline explicit QMutexLocker(QMutex *m) |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
98 |
: val(reinterpret_cast<quintptr>(m)) |
0 | 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 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
108 |
if (val) { |
0 | 109 |
if ((val & quintptr(1u)) == quintptr(1u)) { |
110 |
val &= ~quintptr(1u); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
111 |
mutex()->unlock(); |
0 | 112 |
} |
113 |
} |
|
114 |
} |
|
115 |
||
116 |
inline void relock() |
|
117 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
118 |
if (val) { |
0 | 119 |
if ((val & quintptr(1u)) == quintptr(0u)) { |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
120 |
mutex()->lock(); |
0 | 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 |
||
140 |
private: |
|
141 |
Q_DISABLE_COPY(QMutexLocker) |
|
142 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
143 |
quintptr val; |
0 | 144 |
}; |
145 |
||
146 |
#else // QT_NO_THREAD |
|
147 |
||
148 |
||
149 |
class Q_CORE_EXPORT QMutex |
|
150 |
{ |
|
151 |
public: |
|
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 |
||
166 |
private: |
|
167 |
Q_DISABLE_COPY(QMutex) |
|
168 |
}; |
|
169 |
||
170 |
class Q_CORE_EXPORT QMutexLocker |
|
171 |
{ |
|
172 |
public: |
|
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 |
||
180 |
private: |
|
181 |
Q_DISABLE_COPY(QMutexLocker) |
|
182 |
}; |
|
183 |
||
184 |
#endif // QT_NO_THREAD |
|
185 |
||
186 |
QT_END_NAMESPACE |
|
187 |
||
188 |
QT_END_HEADER |
|
189 |
||
190 |
#endif // QMUTEX_H |