1// Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QLOCKING_P_H
5#define QLOCKING_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience of
12// qmutex.cpp and qmutex_unix.cpp. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qmutex.h>
19#include <QtCore/private/qglobal_p.h>
20
21#include <mutex>
22
23QT_BEGIN_NAMESPACE
24
25//
26// This API is bridging the time until we can depend on C++17:
27//
28// - qt_scoped_lock returns a lock that cannot be unlocked again before the end of the scope
29// - qt_unique_lock returns a lock that can be unlock()ed and moved around
30// - for compat with QMutexLocker, qt_unique_lock supports passing by pointer.
31// Do NOT use this overload lightly; it's only for cases such as where a Q_GLOBAL_STATIC
32// may have already been deleted. In particular, do NOT port from
33// QMutexLocker locker(&mutex);
34// to
35// auto locker = qt_unique_lock(&mutex);
36// as this will not port automatically to std::unique_lock come C++17!
37//
38// The intent, come C++17, is to replace
39// qt_scoped_lock(mutex);
40// qt_unique_lock(mutex); // except qt_unique_lock(&mutex)
41// with
42// std::scoped_lock(mutex);
43// std::unique_lock(mutex);
44// resp. (C++17 meaning CTAD, guaranteed copy elision + scoped_lock available on all platforms),
45// so please use these functions only in ways which don't break this mechanical search & replace.
46//
47
48namespace {
49
50template <typename Mutex, typename Lock =
51# if defined(__cpp_lib_scoped_lock) && __cpp_lib_scoped_lock >= 201703L
52 std::scoped_lock
53# else
54 std::lock_guard
55# endif
56 <typename std::decay<Mutex>::type>
57>
58Lock qt_scoped_lock(Mutex &mutex)
59{
60 return Lock(mutex);
61}
62
63template <typename Mutex, typename Lock = std::unique_lock<typename std::decay<Mutex>::type>>
64Lock qt_unique_lock(Mutex &mutex)
65{
66 return Lock(mutex);
67}
68
69template <typename Mutex, typename Lock = std::unique_lock<typename std::decay<Mutex>::type>>
70Lock qt_unique_lock(Mutex *mutex)
71{
72 return mutex ? Lock(*mutex) : Lock() ;
73}
74
75} // unnamed namespace
76
77QT_END_NAMESPACE
78
79#endif // QLOCKING_P_H
80

source code of qtbase/src/corelib/thread/qlocking_p.h