|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 ** 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) |
|
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 |
|
140 private: |
|
141 Q_DISABLE_COPY(QMutexLocker) |
|
142 |
|
143 union { |
|
144 QMutex *mtx; |
|
145 quintptr val; |
|
146 }; |
|
147 }; |
|
148 |
|
149 #else // QT_NO_THREAD |
|
150 |
|
151 |
|
152 class Q_CORE_EXPORT QMutex |
|
153 { |
|
154 public: |
|
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 |
|
169 private: |
|
170 Q_DISABLE_COPY(QMutex) |
|
171 }; |
|
172 |
|
173 class Q_CORE_EXPORT QMutexLocker |
|
174 { |
|
175 public: |
|
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 |
|
183 private: |
|
184 Q_DISABLE_COPY(QMutexLocker) |
|
185 }; |
|
186 |
|
187 #endif // QT_NO_THREAD |
|
188 |
|
189 QT_END_NAMESPACE |
|
190 |
|
191 QT_END_HEADER |
|
192 |
|
193 #endif // QMUTEX_H |