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) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Copyright (C) 2012 Intel Corporation | - | ||||||||||||||||||
5 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
6 | ** | - | ||||||||||||||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
8 | ** | - | ||||||||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
16 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
21 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
22 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
23 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
24 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
25 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
26 | ** | - | ||||||||||||||||||
27 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
28 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
29 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
30 | ** | - | ||||||||||||||||||
31 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
32 | ** | - | ||||||||||||||||||
33 | ****************************************************************************/ | - | ||||||||||||||||||
34 | - | |||||||||||||||||||
35 | #include "qplatformdefs.h" | - | ||||||||||||||||||
36 | #include "qmutex.h" | - | ||||||||||||||||||
37 | - | |||||||||||||||||||
38 | #ifndef QT_NO_THREAD | - | ||||||||||||||||||
39 | #include "qatomic.h" | - | ||||||||||||||||||
40 | #include "qmutex_p.h" | - | ||||||||||||||||||
41 | #include "qelapsedtimer.h" | - | ||||||||||||||||||
42 | - | |||||||||||||||||||
43 | #include <linux/futex.h> | - | ||||||||||||||||||
44 | #include <sys/syscall.h> | - | ||||||||||||||||||
45 | #include <unistd.h> | - | ||||||||||||||||||
46 | #include <errno.h> | - | ||||||||||||||||||
47 | #include <asm/unistd.h> | - | ||||||||||||||||||
48 | - | |||||||||||||||||||
49 | #ifndef QT_LINUX_FUTEX | - | ||||||||||||||||||
50 | # error "Qt build is broken: qmutex_linux.cpp is being built but futex support is not wanted" | - | ||||||||||||||||||
51 | #endif | - | ||||||||||||||||||
52 | - | |||||||||||||||||||
53 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
54 | - | |||||||||||||||||||
55 | /* | - | ||||||||||||||||||
56 | * QBasicMutex implementation on Linux with futexes | - | ||||||||||||||||||
57 | * | - | ||||||||||||||||||
58 | * QBasicMutex contains one pointer value, which can contain one of four | - | ||||||||||||||||||
59 | * different values: | - | ||||||||||||||||||
60 | * 0x0 unlocked, non-recursive mutex | - | ||||||||||||||||||
61 | * 0x1 locked non-recursive mutex, no waiters | - | ||||||||||||||||||
62 | * 0x3 locked non-recursive mutex, at least one waiter | - | ||||||||||||||||||
63 | * > 0x3 recursive mutex, points to a QMutexPrivate object | - | ||||||||||||||||||
64 | * | - | ||||||||||||||||||
65 | * LOCKING (non-recursive): | - | ||||||||||||||||||
66 | * | - | ||||||||||||||||||
67 | * A non-recursive mutex starts in the 0x0 state, indicating that it's | - | ||||||||||||||||||
68 | * unlocked. When the first thread attempts to lock it, it will perform a | - | ||||||||||||||||||
69 | * testAndSetAcquire from 0x0 to 0x1. If that succeeds, the caller concludes | - | ||||||||||||||||||
70 | * that it successfully locked the mutex. That happens in fastTryLock(). | - | ||||||||||||||||||
71 | * | - | ||||||||||||||||||
72 | * If that testAndSetAcquire fails, QBasicMutex::lockInternal is called. | - | ||||||||||||||||||
73 | * | - | ||||||||||||||||||
74 | * lockInternal will examine the value of the pointer. Otherwise, it will use | - | ||||||||||||||||||
75 | * futexes to sleep and wait for another thread to unlock. To do that, it needs | - | ||||||||||||||||||
76 | * to set a pointer value of 0x3, which indicates that thread is waiting. It | - | ||||||||||||||||||
77 | * does that by a simple fetchAndStoreAcquire operation. | - | ||||||||||||||||||
78 | * | - | ||||||||||||||||||
79 | * If the pointer value was 0x0, it means we succeeded in acquiring the mutex. | - | ||||||||||||||||||
80 | * For other values, it will then call FUTEX_WAIT and with an expected value of | - | ||||||||||||||||||
81 | * 0x3. | - | ||||||||||||||||||
82 | * | - | ||||||||||||||||||
83 | * If the pointer value changed before futex(2) managed to sleep, it will | - | ||||||||||||||||||
84 | * return -1 / EWOULDBLOCK, in which case we have to start over. And even if we | - | ||||||||||||||||||
85 | * are woken up directly by a FUTEX_WAKE, we need to acquire the mutex, so we | - | ||||||||||||||||||
86 | * start over again. | - | ||||||||||||||||||
87 | * | - | ||||||||||||||||||
88 | * UNLOCKING (non-recursive): | - | ||||||||||||||||||
89 | * | - | ||||||||||||||||||
90 | * To unlock, we need to set a value of 0x0 to indicate it's unlocked. The | - | ||||||||||||||||||
91 | * first attempt is a testAndSetRelease operation from 0x1 to 0x0. If that | - | ||||||||||||||||||
92 | * succeeds, we're done. | - | ||||||||||||||||||
93 | * | - | ||||||||||||||||||
94 | * If it fails, unlockInternal() is called. The only possibility is that the | - | ||||||||||||||||||
95 | * mutex value was 0x3, which indicates some other thread is waiting or was | - | ||||||||||||||||||
96 | * waiting in the past. We then set the mutex to 0x0 and perform a FUTEX_WAKE. | - | ||||||||||||||||||
97 | */ | - | ||||||||||||||||||
98 | - | |||||||||||||||||||
99 | static QBasicAtomicInt futexFlagSupport = Q_BASIC_ATOMIC_INITIALIZER(-1); | - | ||||||||||||||||||
100 | - | |||||||||||||||||||
101 | static int checkFutexPrivateSupport() | - | ||||||||||||||||||
102 | { | - | ||||||||||||||||||
103 | int value = 0; | - | ||||||||||||||||||
104 | #if defined(FUTEX_PRIVATE_FLAG) | - | ||||||||||||||||||
105 | // check if the kernel supports extra futex flags | - | ||||||||||||||||||
106 | // FUTEX_PRIVATE_FLAG appeared in v2.6.22 | - | ||||||||||||||||||
107 | Q_STATIC_ASSERT(FUTEX_PRIVATE_FLAG != 0x80000000); | - | ||||||||||||||||||
108 | - | |||||||||||||||||||
109 | // try an operation that has no side-effects: wake up 42 threads | - | ||||||||||||||||||
110 | // futex will return -1 (errno==ENOSYS) if the flag isn't supported | - | ||||||||||||||||||
111 | // there should be no other error conditions | - | ||||||||||||||||||
112 | value = syscall(__NR_futex, &futexFlagSupport, | - | ||||||||||||||||||
113 | FUTEX_WAKE | FUTEX_PRIVATE_FLAG, | - | ||||||||||||||||||
114 | 42, 0, 0, 0); | - | ||||||||||||||||||
115 | if (value != -1)
| 0-1213 | ||||||||||||||||||
116 | value = FUTEX_PRIVATE_FLAG; executed 1213 times by 539 tests: value = 128; Executed by:
| 1213 | ||||||||||||||||||
117 | else | - | ||||||||||||||||||
118 | value = 0; never executed: value = 0; | 0 | ||||||||||||||||||
119 | - | |||||||||||||||||||
120 | #else | - | ||||||||||||||||||
121 | value = 0; | - | ||||||||||||||||||
122 | #endif | - | ||||||||||||||||||
123 | futexFlagSupport.store(value); | - | ||||||||||||||||||
124 | return value; executed 1213 times by 539 tests: return value; Executed by:
| 1213 | ||||||||||||||||||
125 | } | - | ||||||||||||||||||
126 | - | |||||||||||||||||||
127 | static inline int futexFlags() | - | ||||||||||||||||||
128 | { | - | ||||||||||||||||||
129 | int value = futexFlagSupport.load(); | - | ||||||||||||||||||
130 | if (Q_LIKELY(value != -1))
| 1213-11958923 | ||||||||||||||||||
131 | return value; executed 11958923 times by 1007 tests: return value; Executed by:
| 11958923 | ||||||||||||||||||
132 | return checkFutexPrivateSupport(); executed 1213 times by 539 tests: return checkFutexPrivateSupport(); Executed by:
| 1213 | ||||||||||||||||||
133 | } | - | ||||||||||||||||||
134 | - | |||||||||||||||||||
135 | static inline int _q_futex(void *addr, int op, int val, const struct timespec *timeout) Q_DECL_NOTHROW | - | ||||||||||||||||||
136 | { | - | ||||||||||||||||||
137 | volatile int *int_addr = reinterpret_cast<volatile int *>(addr); | - | ||||||||||||||||||
138 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN && QT_POINTER_SIZE == 8 | - | ||||||||||||||||||
139 | int_addr++; //We want a pointer to the 32 least significant bit of QMutex::d | - | ||||||||||||||||||
140 | #endif | - | ||||||||||||||||||
141 | int *addr2 = 0; | - | ||||||||||||||||||
142 | int val2 = 0; | - | ||||||||||||||||||
143 | - | |||||||||||||||||||
144 | // we use __NR_futex because some libcs (like Android's bionic) don't | - | ||||||||||||||||||
145 | // provide SYS_futex etc. | - | ||||||||||||||||||
146 | return syscall(__NR_futex, int_addr, op | futexFlags(), val, timeout, addr2, val2); executed 11960136 times by 1007 tests: return syscall(202, int_addr, op | futexFlags(), val, timeout, addr2, val2); Executed by:
| 11960136 | ||||||||||||||||||
147 | } | - | ||||||||||||||||||
148 | - | |||||||||||||||||||
149 | static inline QMutexData *dummyFutexValue() | - | ||||||||||||||||||
150 | { | - | ||||||||||||||||||
151 | return reinterpret_cast<QMutexData *>(quintptr(3)); executed 11966133 times by 1007 tests: return reinterpret_cast<QMutexData *>(quintptr(3)); Executed by:
| 11966133 | ||||||||||||||||||
152 | } | - | ||||||||||||||||||
153 | - | |||||||||||||||||||
154 | template <bool IsTimed> static inline | - | ||||||||||||||||||
155 | bool lockInternal_helper(QBasicAtomicPointer<QMutexData> &d_ptr, int timeout = -1, QElapsedTimer *elapsedTimer = 0) Q_DECL_NOTHROW | - | ||||||||||||||||||
156 | { | - | ||||||||||||||||||
157 | if (!IsTimed)
| 102449-3986484 | ||||||||||||||||||
158 | timeout = -1; executed 3986484 times by 1007 tests: timeout = -1; Executed by:
| 3986484 | ||||||||||||||||||
159 | - | |||||||||||||||||||
160 | // we're here because fastTryLock() has just failed | - | ||||||||||||||||||
161 | if (timeout == 0)
| 100170-3988763 | ||||||||||||||||||
162 | return false; executed 100170 times by 12 tests: return false; Executed by:
| 100170 | ||||||||||||||||||
163 | - | |||||||||||||||||||
164 | struct timespec ts, *pts = 0; | - | ||||||||||||||||||
165 | if (IsTimed && timeout > 0) {
| 4-3986484 | ||||||||||||||||||
166 | ts.tv_sec = timeout / 1000; | - | ||||||||||||||||||
167 | ts.tv_nsec = (timeout % 1000) * 1000 * 1000; | - | ||||||||||||||||||
168 | } executed 2275 times by 1 test: end of block Executed by:
| 2275 | ||||||||||||||||||
169 | - | |||||||||||||||||||
170 | // the mutex is locked already, set a bit indicating we're waiting | - | ||||||||||||||||||
171 | while (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) != 0) {
| 3987317-3989428 | ||||||||||||||||||
172 | if (IsTimed && pts == &ts) {
| 61-3987088 | ||||||||||||||||||
173 | // recalculate the timeout | - | ||||||||||||||||||
174 | qint64 xtimeout = qint64(timeout) * 1000 * 1000; | - | ||||||||||||||||||
175 | xtimeout -= elapsedTimer->nsecsElapsed(); | - | ||||||||||||||||||
176 | if (xtimeout <= 0) {
| 21-40 | ||||||||||||||||||
177 | // timer expired after we returned | - | ||||||||||||||||||
178 | return false; executed 40 times by 1 test: return false; Executed by:
| 40 | ||||||||||||||||||
179 | } | - | ||||||||||||||||||
180 | ts.tv_sec = xtimeout / Q_INT64_C(1000) / 1000 / 1000; | - | ||||||||||||||||||
181 | ts.tv_nsec = xtimeout % (Q_INT64_C(1000) * 1000 * 1000); | - | ||||||||||||||||||
182 | } executed 21 times by 1 test: end of block Executed by:
| 21 | ||||||||||||||||||
183 | if (IsTimed && timeout > 0)
| 4-3987088 | ||||||||||||||||||
184 | pts = &ts; executed 2296 times by 1 test: pts = &ts; Executed by:
| 2296 | ||||||||||||||||||
185 | - | |||||||||||||||||||
186 | // successfully set the waiting bit, now sleep | - | ||||||||||||||||||
187 | int r = _q_futex(&d_ptr, FUTEX_WAIT, quintptr(dummyFutexValue()), pts); | - | ||||||||||||||||||
188 | if (IsTimed && r != 0 && errno == ETIMEDOUT)
| 0-3987088 | ||||||||||||||||||
189 | return false; executed 1406 times by 1 test: return false; Executed by:
| 1406 | ||||||||||||||||||
190 | - | |||||||||||||||||||
191 | // we got woken up, so try to acquire the mutex | - | ||||||||||||||||||
192 | // note we must set to dummyFutexValue because there could be other threads | - | ||||||||||||||||||
193 | // also waiting | - | ||||||||||||||||||
194 | } executed 3987982 times by 1007 tests: end of block Executed by:
| 3987982 | ||||||||||||||||||
195 | - | |||||||||||||||||||
196 | Q_ASSERT(d_ptr.load()); | - | ||||||||||||||||||
197 | return true; executed 3987317 times by 1007 tests: return true; Executed by:
| 3987317 | ||||||||||||||||||
198 | } | - | ||||||||||||||||||
199 | - | |||||||||||||||||||
200 | void QBasicMutex::lockInternal() Q_DECL_NOTHROW | - | ||||||||||||||||||
201 | { | - | ||||||||||||||||||
202 | Q_ASSERT(!isRecursive()); | - | ||||||||||||||||||
203 | lockInternal_helper<false>(d_ptr); | - | ||||||||||||||||||
204 | } executed 3986484 times by 1007 tests: end of block Executed by:
| 3986484 | ||||||||||||||||||
205 | - | |||||||||||||||||||
206 | bool QBasicMutex::lockInternal(int timeout) Q_DECL_NOTHROW | - | ||||||||||||||||||
207 | { | - | ||||||||||||||||||
208 | Q_ASSERT(!isRecursive()); | - | ||||||||||||||||||
209 | QElapsedTimer elapsedTimer; | - | ||||||||||||||||||
210 | elapsedTimer.start(); | - | ||||||||||||||||||
211 | return lockInternal_helper<true>(d_ptr, timeout, &elapsedTimer); executed 102449 times by 12 tests: return lockInternal_helper<true>(d_ptr, timeout, &elapsedTimer); Executed by:
| 102449 | ||||||||||||||||||
212 | } | - | ||||||||||||||||||
213 | - | |||||||||||||||||||
214 | void QBasicMutex::unlockInternal() Q_DECL_NOTHROW | - | ||||||||||||||||||
215 | { | - | ||||||||||||||||||
216 | QMutexData *d = d_ptr.load(); | - | ||||||||||||||||||
217 | Q_ASSERT(d); //we must be locked | - | ||||||||||||||||||
218 | Q_ASSERT(d != dummyLocked()); // testAndSetRelease(dummyLocked(), 0) failed | - | ||||||||||||||||||
219 | Q_UNUSED(d); | - | ||||||||||||||||||
220 | Q_ASSERT(!isRecursive()); | - | ||||||||||||||||||
221 | - | |||||||||||||||||||
222 | d_ptr.storeRelease(0); | - | ||||||||||||||||||
223 | _q_futex(&d_ptr, FUTEX_WAKE, 1, 0); | - | ||||||||||||||||||
224 | } executed 7970181 times by 1008 tests: end of block Executed by:
| 7970181 | ||||||||||||||||||
225 | - | |||||||||||||||||||
226 | - | |||||||||||||||||||
227 | QT_END_NAMESPACE | - | ||||||||||||||||||
228 | - | |||||||||||||||||||
229 | #endif // QT_NO_THREAD | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |