Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/thread/qmutex_linux.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||||||||
5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
6 | ** | - | ||||||||||||||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
8 | ** | - | ||||||||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||
17 | ** | - | ||||||||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
22 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** GNU General Public License Usage | - | ||||||||||||||||||
27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
36 | ** | - | ||||||||||||||||||
37 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
38 | ** | - | ||||||||||||||||||
39 | ****************************************************************************/ | - | ||||||||||||||||||
40 | - | |||||||||||||||||||
41 | #include "qplatformdefs.h" | - | ||||||||||||||||||
42 | #include "qmutex.h" | - | ||||||||||||||||||
43 | - | |||||||||||||||||||
44 | #ifndef QT_NO_THREAD | - | ||||||||||||||||||
45 | #include "qatomic.h" | - | ||||||||||||||||||
46 | #include "qmutex_p.h" | - | ||||||||||||||||||
47 | #include "qelapsedtimer.h" | - | ||||||||||||||||||
48 | - | |||||||||||||||||||
49 | #include <linux/futex.h> | - | ||||||||||||||||||
50 | #include <sys/syscall.h> | - | ||||||||||||||||||
51 | #include <unistd.h> | - | ||||||||||||||||||
52 | #include <errno.h> | - | ||||||||||||||||||
53 | #include <asm/unistd.h> | - | ||||||||||||||||||
54 | - | |||||||||||||||||||
55 | #ifndef QT_LINUX_FUTEX | - | ||||||||||||||||||
56 | # error "Qt build is broken: qmutex_linux.cpp is being built but futex support is not wanted" | - | ||||||||||||||||||
57 | #endif | - | ||||||||||||||||||
58 | - | |||||||||||||||||||
59 | #ifndef FUTEX_PRIVATE_FLAG | - | ||||||||||||||||||
60 | # define FUTEX_PRIVATE_FLAG 0 | - | ||||||||||||||||||
61 | #endif | - | ||||||||||||||||||
62 | - | |||||||||||||||||||
63 | - | |||||||||||||||||||
64 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
65 | - | |||||||||||||||||||
66 | /* | - | ||||||||||||||||||
67 | * QBasicMutex implementation on Linux with futexes | - | ||||||||||||||||||
68 | * | - | ||||||||||||||||||
69 | * QBasicMutex contains one pointer value, which can contain one of four | - | ||||||||||||||||||
70 | * different values: | - | ||||||||||||||||||
71 | * 0x0 unlocked, non-recursive mutex | - | ||||||||||||||||||
72 | * 0x1 locked non-recursive mutex, no waiters | - | ||||||||||||||||||
73 | * 0x3 locked non-recursive mutex, at least one waiter | - | ||||||||||||||||||
74 | * > 0x3 recursive mutex, points to a QMutexPrivate object | - | ||||||||||||||||||
75 | * | - | ||||||||||||||||||
76 | * LOCKING (non-recursive): | - | ||||||||||||||||||
77 | * | - | ||||||||||||||||||
78 | * A non-recursive mutex starts in the 0x0 state, indicating that it's | - | ||||||||||||||||||
79 | * unlocked. When the first thread attempts to lock it, it will perform a | - | ||||||||||||||||||
80 | * testAndSetAcquire from 0x0 to 0x1. If that succeeds, the caller concludes | - | ||||||||||||||||||
81 | * that it successfully locked the mutex. That happens in fastTryLock(). | - | ||||||||||||||||||
82 | * | - | ||||||||||||||||||
83 | * If that testAndSetAcquire fails, QBasicMutex::lockInternal is called. | - | ||||||||||||||||||
84 | * | - | ||||||||||||||||||
85 | * lockInternal will examine the value of the pointer. Otherwise, it will use | - | ||||||||||||||||||
86 | * futexes to sleep and wait for another thread to unlock. To do that, it needs | - | ||||||||||||||||||
87 | * to set a pointer value of 0x3, which indicates that thread is waiting. It | - | ||||||||||||||||||
88 | * does that by a simple fetchAndStoreAcquire operation. | - | ||||||||||||||||||
89 | * | - | ||||||||||||||||||
90 | * If the pointer value was 0x0, it means we succeeded in acquiring the mutex. | - | ||||||||||||||||||
91 | * For other values, it will then call FUTEX_WAIT and with an expected value of | - | ||||||||||||||||||
92 | * 0x3. | - | ||||||||||||||||||
93 | * | - | ||||||||||||||||||
94 | * If the pointer value changed before futex(2) managed to sleep, it will | - | ||||||||||||||||||
95 | * return -1 / EWOULDBLOCK, in which case we have to start over. And even if we | - | ||||||||||||||||||
96 | * are woken up directly by a FUTEX_WAKE, we need to acquire the mutex, so we | - | ||||||||||||||||||
97 | * start over again. | - | ||||||||||||||||||
98 | * | - | ||||||||||||||||||
99 | * UNLOCKING (non-recursive): | - | ||||||||||||||||||
100 | * | - | ||||||||||||||||||
101 | * To unlock, we need to set a value of 0x0 to indicate it's unlocked. The | - | ||||||||||||||||||
102 | * first attempt is a testAndSetRelease operation from 0x1 to 0x0. If that | - | ||||||||||||||||||
103 | * succeeds, we're done. | - | ||||||||||||||||||
104 | * | - | ||||||||||||||||||
105 | * If it fails, unlockInternal() is called. The only possibility is that the | - | ||||||||||||||||||
106 | * mutex value was 0x3, which indicates some other thread is waiting or was | - | ||||||||||||||||||
107 | * waiting in the past. We then set the mutex to 0x0 and perform a FUTEX_WAKE. | - | ||||||||||||||||||
108 | */ | - | ||||||||||||||||||
109 | - | |||||||||||||||||||
110 | static QBasicAtomicInt futexFlagSupport = Q_BASIC_ATOMIC_INITIALIZER(-1); | - | ||||||||||||||||||
111 | - | |||||||||||||||||||
112 | static inline int _q_futex(void *addr, int op, int val, const struct timespec *timeout) Q_DECL_NOTHROW | - | ||||||||||||||||||
113 | { | - | ||||||||||||||||||
114 | volatile int *int_addr = reinterpret_cast<volatile int *>(addr); | - | ||||||||||||||||||
115 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN && QT_POINTER_SIZE == 8 | - | ||||||||||||||||||
116 | int_addr++; //We want a pointer to the 32 least significant bit of QMutex::d | - | ||||||||||||||||||
117 | #endif | - | ||||||||||||||||||
118 | int *addr2 = 0; | - | ||||||||||||||||||
119 | int val2 = 0; | - | ||||||||||||||||||
120 | - | |||||||||||||||||||
121 | // we use __NR_futex because some libcs (like Android's bionic) don't | - | ||||||||||||||||||
122 | // provide SYS_futex etc. | - | ||||||||||||||||||
123 | return syscall(__NR_futex, int_addr, op | FUTEX_PRIVATE_FLAG, val, timeout, addr2, val2); executed 11649591 times by 934 tests: return syscall(202, int_addr, op | 128, val, timeout, addr2, val2); Executed by:
| 11649591 | ||||||||||||||||||
124 | } | - | ||||||||||||||||||
125 | - | |||||||||||||||||||
126 | static inline QMutexData *dummyFutexValue() | - | ||||||||||||||||||
127 | { | - | ||||||||||||||||||
128 | return reinterpret_cast<QMutexData *>(quintptr(3)); executed 11655234 times by 934 tests: return reinterpret_cast<QMutexData *>(quintptr(3)); Executed by:
| 11655234 | ||||||||||||||||||
129 | } | - | ||||||||||||||||||
130 | - | |||||||||||||||||||
131 | template <bool IsTimed> static inline | - | ||||||||||||||||||
132 | bool lockInternal_helper(QBasicAtomicPointer<QMutexData> &d_ptr, int timeout = -1, QElapsedTimer *elapsedTimer = 0) Q_DECL_NOTHROW | - | ||||||||||||||||||
133 | { | - | ||||||||||||||||||
134 | if (!IsTimed)
| 264473-3882900 | ||||||||||||||||||
135 | timeout = -1; executed 3882900 times by 934 tests: timeout = -1; Executed by:
| 3882900 | ||||||||||||||||||
136 | - | |||||||||||||||||||
137 | // we're here because fastTryLock() has just failed | - | ||||||||||||||||||
138 | if (timeout == 0)
| 262259-3885114 | ||||||||||||||||||
139 | return false; executed 262259 times by 22 tests: return false; Executed by:
| 262259 | ||||||||||||||||||
140 | - | |||||||||||||||||||
141 | struct timespec ts, *pts = 0; | - | ||||||||||||||||||
142 | if (IsTimed && timeout > 0) {
| 4-3882900 | ||||||||||||||||||
143 | ts.tv_sec = timeout / 1000; | - | ||||||||||||||||||
144 | ts.tv_nsec = (timeout % 1000) * 1000 * 1000; | - | ||||||||||||||||||
145 | } executed 2210 times by 1 test: end of block Executed by:
| 2210 | ||||||||||||||||||
146 | - | |||||||||||||||||||
147 | // the mutex is locked already, set a bit indicating we're waiting | - | ||||||||||||||||||
148 | while (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) != 0) {
| 3883721-3885773 | ||||||||||||||||||
149 | if (IsTimed && pts == &ts) {
| 58-3883501 | ||||||||||||||||||
150 | // recalculate the timeout | - | ||||||||||||||||||
151 | qint64 xtimeout = qint64(timeout) * 1000 * 1000; | - | ||||||||||||||||||
152 | xtimeout -= elapsedTimer->nsecsElapsed(); | - | ||||||||||||||||||
153 | if (xtimeout <= 0) {
| 25-33 | ||||||||||||||||||
154 | // timer expired after we returned | - | ||||||||||||||||||
155 | return false; executed 33 times by 1 test: return false; Executed by:
| 33 | ||||||||||||||||||
156 | } | - | ||||||||||||||||||
157 | ts.tv_sec = xtimeout / Q_INT64_C(1000) / 1000 / 1000; | - | ||||||||||||||||||
158 | ts.tv_nsec = xtimeout % (Q_INT64_C(1000) * 1000 * 1000); | - | ||||||||||||||||||
159 | } executed 25 times by 1 test: end of block Executed by:
| 25 | ||||||||||||||||||
160 | if (IsTimed && timeout > 0)
| 4-3883501 | ||||||||||||||||||
161 | pts = &ts; executed 2235 times by 1 test: pts = &ts; Executed by:
| 2235 | ||||||||||||||||||
162 | - | |||||||||||||||||||
163 | // successfully set the waiting bit, now sleep | - | ||||||||||||||||||
164 | int r = _q_futex(&d_ptr, FUTEX_WAIT, quintptr(dummyFutexValue()), pts); | - | ||||||||||||||||||
165 | if (IsTimed && r != 0 && errno == ETIMEDOUT)
| 0-3883501 | ||||||||||||||||||
166 | return false; executed 1360 times by 1 test: return false; Executed by:
| 1360 | ||||||||||||||||||
167 | - | |||||||||||||||||||
168 | // we got woken up, so try to acquire the mutex | - | ||||||||||||||||||
169 | // note we must set to dummyFutexValue because there could be other threads | - | ||||||||||||||||||
170 | // also waiting | - | ||||||||||||||||||
171 | } executed 3884380 times by 934 tests: end of block Executed by:
| 3884380 | ||||||||||||||||||
172 | - | |||||||||||||||||||
173 | Q_ASSERT(d_ptr.load()); | - | ||||||||||||||||||
174 | return true; executed 3883721 times by 934 tests: return true; Executed by:
| 3883721 | ||||||||||||||||||
175 | } | - | ||||||||||||||||||
176 | - | |||||||||||||||||||
177 | void QBasicMutex::lockInternal() Q_DECL_NOTHROW | - | ||||||||||||||||||
178 | { | - | ||||||||||||||||||
179 | Q_ASSERT(!isRecursive()); | - | ||||||||||||||||||
180 | lockInternal_helper<false>(d_ptr); | - | ||||||||||||||||||
181 | } executed 3882900 times by 934 tests: end of block Executed by:
| 3882900 | ||||||||||||||||||
182 | - | |||||||||||||||||||
183 | bool QBasicMutex::lockInternal(int timeout) Q_DECL_NOTHROW | - | ||||||||||||||||||
184 | { | - | ||||||||||||||||||
185 | Q_ASSERT(!isRecursive()); | - | ||||||||||||||||||
186 | QElapsedTimer elapsedTimer; | - | ||||||||||||||||||
187 | elapsedTimer.start(); | - | ||||||||||||||||||
188 | return lockInternal_helper<true>(d_ptr, timeout, &elapsedTimer); executed 264473 times by 22 tests: return lockInternal_helper<true>(d_ptr, timeout, &elapsedTimer); Executed by:
| 264473 | ||||||||||||||||||
189 | } | - | ||||||||||||||||||
190 | - | |||||||||||||||||||
191 | void QBasicMutex::unlockInternal() Q_DECL_NOTHROW | - | ||||||||||||||||||
192 | { | - | ||||||||||||||||||
193 | QMutexData *d = d_ptr.load(); | - | ||||||||||||||||||
194 | Q_ASSERT(d); //we must be locked | - | ||||||||||||||||||
195 | Q_ASSERT(d != dummyLocked()); // testAndSetRelease(dummyLocked(), 0) failed | - | ||||||||||||||||||
196 | Q_UNUSED(d); | - | ||||||||||||||||||
197 | Q_ASSERT(!isRecursive()); | - | ||||||||||||||||||
198 | - | |||||||||||||||||||
199 | d_ptr.storeRelease(0); | - | ||||||||||||||||||
200 | _q_futex(&d_ptr, FUTEX_WAKE, 1, 0); | - | ||||||||||||||||||
201 | } executed 7763193 times by 935 tests: end of block Executed by:
| 7763193 | ||||||||||||||||||
202 | - | |||||||||||||||||||
203 | - | |||||||||||||||||||
204 | QT_END_NAMESPACE | - | ||||||||||||||||||
205 | - | |||||||||||||||||||
206 | #endif // QT_NO_THREAD | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |