summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qorderedmutexlocker_p.h
blob: 730ba6127317e6c232faa82054bd8972ba05787c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QORDEREDMUTEXLOCKER_P_H
#define QORDEREDMUTEXLOCKER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/private/qglobal_p.h>
#include <QtCore/qmutex.h>

#include <functional>

QT_BEGIN_NAMESPACE

#if QT_CONFIG(thread)

/*
  Locks 2 mutexes in a defined order, avoiding a recursive lock if
  we're trying to lock the same mutex twice.
*/
class QOrderedMutexLocker
{
public:
    QOrderedMutexLocker(QBasicMutex *m1, QBasicMutex *m2)
        : mtx1((m1 == m2) ? m1 : (std::less<QBasicMutex *>()(m1, m2) ? m1 : m2)),
          mtx2((m1 == m2) ?  nullptr : (std::less<QBasicMutex *>()(m1, m2) ? m2 : m1)),
          locked(false)
    {
        relock();
    }

    Q_DISABLE_COPY(QOrderedMutexLocker)

    void swap(QOrderedMutexLocker &other) noexcept
    {
        qSwap(this->mtx1, other.mtx1);
        qSwap(this->mtx2, other.mtx2);
        qSwap(this->locked, other.locked);
    }

    QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QOrderedMutexLocker)

    QOrderedMutexLocker(QOrderedMutexLocker &&other) noexcept
        : mtx1(std::exchange(other.mtx1, nullptr))
        , mtx2(std::exchange(other.mtx2, nullptr))
        , locked(std::exchange(other.locked, false))
    {}

    ~QOrderedMutexLocker()
    {
        unlock();
    }

    void relock()
    {
        if (!locked) {
            if (mtx1) mtx1->lock();
            if (mtx2) mtx2->lock();
            locked = true;
        }
    }

    /*!
        \internal
        Can be called if the mutexes have been unlocked manually, and sets the
        state of the QOrderedMutexLocker to unlocked.
        The caller is expected to have unlocked both of them if they
        are not the same. Calling this method when the QOrderedMutexLocker is
        unlocked or when the provided mutexes have not actually been unlocked is
        UB.
     */
    void dismiss()
    {
        Q_ASSERT(locked);
        locked = false;
    }

    void unlock()
    {
        if (locked) {
            if (mtx2) mtx2->unlock();
            if (mtx1) mtx1->unlock();
            locked = false;
        }
    }

    static bool relock(QBasicMutex *mtx1, QBasicMutex *mtx2)
    {
        // mtx1 is already locked, mtx2 not... do we need to unlock and relock?
        if (mtx1 == mtx2)
            return false;
        if (std::less<QBasicMutex *>()(mtx1, mtx2)) {
            mtx2->lock();
            return true;
        }
        if (!mtx2->tryLock()) {
            mtx1->unlock();
            mtx2->lock();
            mtx1->lock();
        }
        return true;
    }

private:
    QBasicMutex *mtx1, *mtx2;
    bool locked;
};

class QBasicMutexLocker
{
public:
    inline explicit QBasicMutexLocker(QBasicMutex *m) QT_MUTEX_LOCK_NOEXCEPT
        : m(m), isLocked(true)
    {
        m->lock();
    }
    inline ~QBasicMutexLocker() { if (isLocked) unlock(); }

    inline void unlock() noexcept
    {
        isLocked = false;
        m->unlock();
    }

    inline void relock() QT_MUTEX_LOCK_NOEXCEPT
    {
        isLocked = true;
        m->lock();
    }

private:
    Q_DISABLE_COPY(QBasicMutexLocker)

    QBasicMutex *m;
    bool isLocked;
};

#else

class QOrderedMutexLocker
{
public:
    Q_DISABLE_COPY(QOrderedMutexLocker)
    QOrderedMutexLocker(QBasicMutex *, QBasicMutex *) {}
    QOrderedMutexLocker(QOrderedMutexLocker &&) = default;
    QOrderedMutexLocker& operator=(QOrderedMutexLocker &&other) = default;
    ~QOrderedMutexLocker() {}

    void relock() {}
    void unlock() {}
    void dismiss() {}

    static bool relock(QBasicMutex *, QBasicMutex *) { return false; }
};

using QBasicMutexLocker = QMutexLocker<QBasicMutex>;

#endif


QT_END_NAMESPACE

#endif