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 QORDEREDMUTEXLOCKER_P_H
41#define QORDEREDMUTEXLOCKER_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtCore/private/qglobal_p.h>
55#include <QtCore/qmutex.h>
56
57#include <functional>
58
59QT_BEGIN_NAMESPACE
60
61#if QT_CONFIG(thread)
62
63/*
64 Locks 2 mutexes in a defined order, avoiding a recursive lock if
65 we're trying to lock the same mutex twice.
66*/
67class QOrderedMutexLocker
68{
69public:
70 QOrderedMutexLocker(QBasicMutex *m1, QBasicMutex *m2)
71 : mtx1((m1 == m2) ? m1 : (std::less<QBasicMutex *>()(m1, m2) ? m1 : m2)),
72 mtx2((m1 == m2) ? nullptr : (std::less<QBasicMutex *>()(m1, m2) ? m2 : m1)),
73 locked(false)
74 {
75 relock();
76 }
77 ~QOrderedMutexLocker()
78 {
79 unlock();
80 }
81
82 void relock()
83 {
84 if (!locked) {
85 if (mtx1) mtx1->lock();
86 if (mtx2) mtx2->lock();
87 locked = true;
88 }
89 }
90
91 void unlock()
92 {
93 if (locked) {
94 if (mtx2) mtx2->unlock();
95 if (mtx1) mtx1->unlock();
96 locked = false;
97 }
98 }
99
100 static bool relock(QBasicMutex *mtx1, QBasicMutex *mtx2)
101 {
102 // mtx1 is already locked, mtx2 not... do we need to unlock and relock?
103 if (mtx1 == mtx2)
104 return false;
105 if (std::less<QBasicMutex *>()(mtx1, mtx2)) {
106 mtx2->lock();
107 return true;
108 }
109 if (!mtx2->tryLock()) {
110 mtx1->unlock();
111 mtx2->lock();
112 mtx1->lock();
113 }
114 return true;
115 }
116
117private:
118 QBasicMutex *mtx1, *mtx2;
119 bool locked;
120};
121
122class QBasicMutexLocker
123{
124public:
125 inline explicit QBasicMutexLocker(QBasicMutex *m) QT_MUTEX_LOCK_NOEXCEPT
126 : m(m), isLocked(true)
127 {
128 m->lock();
129 }
130 inline ~QBasicMutexLocker() { if (isLocked) unlock(); }
131
132 inline void unlock() noexcept
133 {
134 isLocked = false;
135 m->unlock();
136 }
137
138 inline void relock() QT_MUTEX_LOCK_NOEXCEPT
139 {
140 isLocked = true;
141 m->lock();
142 }
143
144private:
145 Q_DISABLE_COPY(QBasicMutexLocker)
146
147 QBasicMutex *m;
148 bool isLocked;
149};
150
151#else
152
153class QOrderedMutexLocker
154{
155public:
156 QOrderedMutexLocker(QBasicMutex *, QBasicMutex *) {}
157 ~QOrderedMutexLocker() {}
158
159 void relock() {}
160 void unlock() {}
161
162 static bool relock(QBasicMutex *, QBasicMutex *) { return false; }
163};
164
165using QBasicMutexLocker = QMutexLocker<QBasicMutex>;
166
167#endif
168
169
170QT_END_NAMESPACE
171
172#endif
173