1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QMUTEX_H
41#define QMUTEX_H
42
43#include <QtCore/qglobal.h>
44#include <QtCore/qatomic.h>
45#include <new>
46
47#if QT_HAS_INCLUDE(<chrono>)
48# include <chrono>
49# include <limits>
50#endif
51
52class tst_QMutex;
53
54QT_BEGIN_NAMESPACE
55
56
57#if !defined(QT_NO_THREAD) || defined(Q_CLANG_QDOC)
58
59#ifdef Q_OS_LINUX
60# define QT_MUTEX_LOCK_NOEXCEPT Q_DECL_NOTHROW
61#else
62# define QT_MUTEX_LOCK_NOEXCEPT
63#endif
64
65class QMutexData;
66
67class Q_CORE_EXPORT QBasicMutex
68{
69public:
70 // BasicLockable concept
71 inline void lock() QT_MUTEX_LOCK_NOEXCEPT {
72 if (!fastTryLock())
73 lockInternal();
74 }
75
76 // BasicLockable concept
77 inline void unlock() Q_DECL_NOTHROW {
78 Q_ASSERT(d_ptr.load()); //mutex must be locked
79 if (!fastTryUnlock())
80 unlockInternal();
81 }
82
83 bool tryLock() Q_DECL_NOTHROW {
84 return fastTryLock();
85 }
86
87 // Lockable concept
88 bool try_lock() Q_DECL_NOTHROW { return tryLock(); }
89
90 bool isRecursive() Q_DECL_NOTHROW; //### Qt6: remove me
91 bool isRecursive() const Q_DECL_NOTHROW;
92
93private:
94 inline bool fastTryLock() Q_DECL_NOTHROW {
95 return d_ptr.testAndSetAcquire(nullptr, dummyLocked());
96 }
97 inline bool fastTryUnlock() Q_DECL_NOTHROW {
98 return d_ptr.testAndSetRelease(dummyLocked(), nullptr);
99 }
100 inline bool fastTryLock(QMutexData *&current) Q_DECL_NOTHROW {
101 return d_ptr.testAndSetAcquire(nullptr, dummyLocked(), current);
102 }
103 inline bool fastTryUnlock(QMutexData *&current) Q_DECL_NOTHROW {
104 return d_ptr.testAndSetRelease(dummyLocked(), nullptr, current);
105 }
106
107 void lockInternal() QT_MUTEX_LOCK_NOEXCEPT;
108 bool lockInternal(int timeout) QT_MUTEX_LOCK_NOEXCEPT;
109 void unlockInternal() Q_DECL_NOTHROW;
110
111 QBasicAtomicPointer<QMutexData> d_ptr;
112 static inline QMutexData *dummyLocked() {
113 return reinterpret_cast<QMutexData *>(quintptr(1));
114 }
115
116 friend class QMutex;
117 friend class QMutexData;
118};
119
120class Q_CORE_EXPORT QMutex : public QBasicMutex
121{
122public:
123 enum RecursionMode { NonRecursive, Recursive };
124 explicit QMutex(RecursionMode mode = NonRecursive);
125 ~QMutex();
126
127 // BasicLockable concept
128 void lock() QT_MUTEX_LOCK_NOEXCEPT;
129 bool tryLock(int timeout = 0) QT_MUTEX_LOCK_NOEXCEPT;
130 // BasicLockable concept
131 void unlock() Q_DECL_NOTHROW;
132
133 // Lockable concept
134 bool try_lock() QT_MUTEX_LOCK_NOEXCEPT { return tryLock(); }
135
136#if QT_HAS_INCLUDE(<chrono>)
137 // TimedLockable concept
138 template <class Rep, class Period>
139 bool try_lock_for(std::chrono::duration<Rep, Period> duration)
140 {
141 return tryLock(convertToMilliseconds(duration));
142 }
143
144 // TimedLockable concept
145 template<class Clock, class Duration>
146 bool try_lock_until(std::chrono::time_point<Clock, Duration> timePoint)
147 {
148 // Implemented in terms of try_lock_for to honor the similar
149 // requirement in N4606 § 30.4.1.3 [thread.timedmutex.requirements]/12.
150
151 return try_lock_for(timePoint - Clock::now());
152 }
153#endif
154
155 bool isRecursive() const Q_DECL_NOTHROW
156 { return QBasicMutex::isRecursive(); }
157
158private:
159 Q_DISABLE_COPY(QMutex)
160 friend class QMutexLocker;
161 friend class ::tst_QMutex;
162
163#if QT_HAS_INCLUDE(<chrono>)
164 template<class Rep, class Period>
165 static int convertToMilliseconds(std::chrono::duration<Rep, Period> duration)
166 {
167 // N4606 § 30.4.1.3.5 [thread.timedmutex.requirements] specifies that a
168 // duration less than or equal to duration.zero() shall result in a
169 // try_lock, unlike QMutex's tryLock with a negative duration which
170 // results in a lock.
171
172 if (duration <= duration.zero())
173 return 0;
174
175 // when converting from 'duration' to milliseconds, make sure that
176 // the result is not shorter than 'duration':
177 std::chrono::milliseconds wait = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
178 if (wait < duration)
179 wait += std::chrono::milliseconds(1);
180 Q_ASSERT(wait >= duration);
181 const auto ms = wait.count();
182 const auto maxInt = (std::numeric_limits<int>::max)();
183
184 return ms < maxInt ? int(ms) : maxInt;
185 }
186#endif
187};
188
189class Q_CORE_EXPORT QMutexLocker
190{
191public:
192#ifndef Q_CLANG_QDOC
193 inline explicit QMutexLocker(QBasicMutex *m) QT_MUTEX_LOCK_NOEXCEPT
194 {
195 Q_ASSERT_X((reinterpret_cast<quintptr>(m) & quintptr(1u)) == quintptr(0),
196 "QMutexLocker", "QMutex pointer is misaligned");
197 val = quintptr(m);
198 if (Q_LIKELY(m)) {
199 // call QMutex::lock() instead of QBasicMutex::lock()
200 static_cast<QMutex *>(m)->lock();
201 val |= 1;
202 }
203 }
204#else
205 QMutexLocker(QMutex *) { }
206#endif
207 inline ~QMutexLocker() { unlock(); }
208
209 inline void unlock() Q_DECL_NOTHROW
210 {
211 if ((val & quintptr(1u)) == quintptr(1u)) {
212 val &= ~quintptr(1u);
213 mutex()->unlock();
214 }
215 }
216
217 inline void relock() QT_MUTEX_LOCK_NOEXCEPT
218 {
219 if (val) {
220 if ((val & quintptr(1u)) == quintptr(0u)) {
221 mutex()->lock();
222 val |= quintptr(1u);
223 }
224 }
225 }
226
227#if defined(Q_CC_MSVC)
228#pragma warning( push )
229#pragma warning( disable : 4312 ) // ignoring the warning from /Wp64
230#endif
231
232 inline QMutex *mutex() const
233 {
234 return reinterpret_cast<QMutex *>(val & ~quintptr(1u));
235 }
236
237#if defined(Q_CC_MSVC)
238#pragma warning( pop )
239#endif
240
241private:
242 Q_DISABLE_COPY(QMutexLocker)
243
244 quintptr val;
245};
246
247#else // QT_NO_THREAD && !Q_CLANG_QDOC
248
249class Q_CORE_EXPORT QMutex
250{
251public:
252 enum RecursionMode { NonRecursive, Recursive };
253
254 inline Q_DECL_CONSTEXPR explicit QMutex(RecursionMode = NonRecursive) Q_DECL_NOTHROW { }
255
256 inline void lock() Q_DECL_NOTHROW {}
257 inline bool tryLock(int timeout = 0) Q_DECL_NOTHROW { Q_UNUSED(timeout); return true; }
258 inline bool try_lock() Q_DECL_NOTHROW { return true; }
259 inline void unlock() Q_DECL_NOTHROW {}
260 inline bool isRecursive() const Q_DECL_NOTHROW { return true; }
261
262#if QT_HAS_INCLUDE(<chrono>)
263 template <class Rep, class Period>
264 inline bool try_lock_for(std::chrono::duration<Rep, Period> duration) Q_DECL_NOTHROW
265 {
266 Q_UNUSED(duration);
267 return true;
268 }
269
270 template<class Clock, class Duration>
271 inline bool try_lock_until(std::chrono::time_point<Clock, Duration> timePoint) Q_DECL_NOTHROW
272 {
273 Q_UNUSED(timePoint);
274 return true;
275 }
276#endif
277
278private:
279 Q_DISABLE_COPY(QMutex)
280};
281
282class Q_CORE_EXPORT QMutexLocker
283{
284public:
285 inline explicit QMutexLocker(QMutex *) Q_DECL_NOTHROW {}
286 inline ~QMutexLocker() Q_DECL_NOTHROW {}
287
288 inline void unlock() Q_DECL_NOTHROW {}
289 void relock() Q_DECL_NOTHROW {}
290 inline QMutex *mutex() const Q_DECL_NOTHROW { return nullptr; }
291
292private:
293 Q_DISABLE_COPY(QMutexLocker)
294};
295
296typedef QMutex QBasicMutex;
297
298#endif // QT_NO_THREAD && !Q_CLANG_QDOC
299
300QT_END_NAMESPACE
301
302#endif // QMUTEX_H
303