1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
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 <QtCore/qatomic.h> |
47 | #include <new> |
48 | |
49 | QT_BEGIN_HEADER |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | QT_MODULE(Core) |
54 | |
55 | #ifndef QT_NO_THREAD |
56 | |
57 | class QAtomicInt; |
58 | class QMutexData; |
59 | |
60 | class Q_CORE_EXPORT QMutex |
61 | { |
62 | friend class QWaitCondition; |
63 | friend class QWaitConditionPrivate; |
64 | |
65 | public: |
66 | enum RecursionMode { NonRecursive, Recursive }; |
67 | |
68 | explicit QMutex(RecursionMode mode = NonRecursive); |
69 | ~QMutex(); |
70 | |
71 | void lock(); //### Qt5: make inline; |
72 | inline void lockInline(); |
73 | bool tryLock(); //### Qt5: make inline; |
74 | bool tryLock(int timeout); |
75 | inline bool tryLockInline(); |
76 | void unlock(); //### Qt5: make inline; |
77 | inline void unlockInline(); |
78 | |
79 | #if defined(QT3_SUPPORT) |
80 | inline QT3_SUPPORT bool locked() |
81 | { |
82 | if (!tryLock()) |
83 | return true; |
84 | unlock(); |
85 | return false; |
86 | } |
87 | inline QT3_SUPPORT_CONSTRUCTOR QMutex(bool recursive) |
88 | { |
89 | new (this) QMutex(recursive ? Recursive : NonRecursive); |
90 | } |
91 | #endif |
92 | |
93 | private: |
94 | void lockInternal(); |
95 | void unlockInternal(); |
96 | Q_DISABLE_COPY(QMutex) |
97 | |
98 | QMutexData *d; |
99 | }; |
100 | |
101 | class Q_CORE_EXPORT QMutexLocker |
102 | { |
103 | public: |
104 | inline explicit QMutexLocker(QMutex *m) |
105 | { |
106 | Q_ASSERT_X((reinterpret_cast<quintptr>(m) & quintptr(1u)) == quintptr(0), |
107 | "QMutexLocker" , "QMutex pointer is misaligned" ); |
108 | if (m) { |
109 | m->lockInline(); |
110 | val = reinterpret_cast<quintptr>(m) | quintptr(1u); |
111 | } else { |
112 | val = 0; |
113 | } |
114 | } |
115 | inline ~QMutexLocker() { unlock(); } |
116 | |
117 | inline void unlock() |
118 | { |
119 | if ((val & quintptr(1u)) == quintptr(1u)) { |
120 | val &= ~quintptr(1u); |
121 | mutex()->unlockInline(); |
122 | } |
123 | } |
124 | |
125 | inline void relock() |
126 | { |
127 | if (val) { |
128 | if ((val & quintptr(1u)) == quintptr(0u)) { |
129 | mutex()->lockInline(); |
130 | val |= quintptr(1u); |
131 | } |
132 | } |
133 | } |
134 | |
135 | #if defined(Q_CC_MSVC) |
136 | #pragma warning( push ) |
137 | #pragma warning( disable : 4312 ) // ignoring the warning from /Wp64 |
138 | #endif |
139 | |
140 | inline QMutex *mutex() const |
141 | { |
142 | return reinterpret_cast<QMutex *>(val & ~quintptr(1u)); |
143 | } |
144 | |
145 | #if defined(Q_CC_MSVC) |
146 | #pragma warning( pop ) |
147 | #endif |
148 | |
149 | private: |
150 | Q_DISABLE_COPY(QMutexLocker) |
151 | |
152 | quintptr val; |
153 | }; |
154 | |
155 | class QMutexData |
156 | { |
157 | public: |
158 | QAtomicInt contenders; |
159 | const uint recursive : 1; |
160 | uint reserved : 31; |
161 | protected: |
162 | QMutexData(QMutex::RecursionMode mode); |
163 | ~QMutexData(); |
164 | }; |
165 | |
166 | #ifdef QT_NO_DEBUG |
167 | inline void QMutex::unlockInline() |
168 | { |
169 | if (d->recursive) { |
170 | unlock(); |
171 | } else if (!d->contenders.testAndSetRelease(1, 0)) { |
172 | unlockInternal(); |
173 | } |
174 | } |
175 | |
176 | inline bool QMutex::tryLockInline() |
177 | { |
178 | if (d->recursive) { |
179 | return tryLock(); |
180 | } else { |
181 | return d->contenders.testAndSetAcquire(0, 1); |
182 | } |
183 | } |
184 | |
185 | inline void QMutex::lockInline() |
186 | { |
187 | if (d->recursive) { |
188 | lock(); |
189 | } else if(!tryLockInline()) { |
190 | lockInternal(); |
191 | } |
192 | } |
193 | #else // QT_NO_DEBUG |
194 | //in debug we do not use inline calls in order to allow debugging tools |
195 | // to hook the mutex locking functions. |
196 | inline void QMutex::unlockInline() { unlock(); } |
197 | inline bool QMutex::tryLockInline() { return tryLock(); } |
198 | inline void QMutex::lockInline() { lock(); } |
199 | #endif // QT_NO_DEBUG |
200 | |
201 | |
202 | #else // QT_NO_THREAD |
203 | |
204 | |
205 | class Q_CORE_EXPORT QMutex |
206 | { |
207 | public: |
208 | enum RecursionMode { NonRecursive, Recursive }; |
209 | |
210 | inline explicit QMutex(RecursionMode mode = NonRecursive) { Q_UNUSED(mode); } |
211 | inline ~QMutex() {} |
212 | |
213 | static inline void lock() {} |
214 | static inline void lockInline() {} |
215 | static inline bool tryLock(int timeout = 0) { Q_UNUSED(timeout); return true; } |
216 | static inline bool tryLockInline() { return true; } |
217 | static inline void unlock() {} |
218 | static inline void unlockInline() {} |
219 | |
220 | #if defined(QT3_SUPPORT) |
221 | static inline QT3_SUPPORT bool locked() { return false; } |
222 | #endif |
223 | |
224 | private: |
225 | Q_DISABLE_COPY(QMutex) |
226 | }; |
227 | |
228 | class Q_CORE_EXPORT QMutexLocker |
229 | { |
230 | public: |
231 | inline explicit QMutexLocker(QMutex *) {} |
232 | inline ~QMutexLocker() {} |
233 | |
234 | static inline void unlock() {} |
235 | static void relock() {} |
236 | static inline QMutex *mutex() { return 0; } |
237 | |
238 | private: |
239 | Q_DISABLE_COPY(QMutexLocker) |
240 | }; |
241 | |
242 | #endif // QT_NO_THREAD |
243 | |
244 | QT_END_NAMESPACE |
245 | |
246 | QT_END_HEADER |
247 | |
248 | #endif // QMUTEX_H |
249 | |