1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Intel Corporation. |
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 QFUTEX_P_H |
41 | #define QFUTEX_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 <qglobal.h> |
55 | |
56 | QT_BEGIN_NAMESPACE |
57 | |
58 | namespace QtDummyFutex { |
59 | constexpr inline bool futexAvailable() { return false; } |
60 | template <typename Atomic> |
61 | inline bool futexWait(Atomic &, typename Atomic::Type, int = 0) |
62 | { Q_UNREACHABLE(); return false; } |
63 | template <typename Atomic> inline void futexWakeOne(Atomic &) |
64 | { Q_UNREACHABLE(); } |
65 | template <typename Atomic> inline void futexWakeAll(Atomic &) |
66 | { Q_UNREACHABLE(); } |
67 | } |
68 | |
69 | QT_END_NAMESPACE |
70 | |
71 | #if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) |
72 | // use Linux mutexes everywhere except for LSB builds |
73 | # include <sys/syscall.h> |
74 | # include <errno.h> |
75 | # include <limits.h> |
76 | # include <unistd.h> |
77 | # include <asm/unistd.h> |
78 | # include <linux/futex.h> |
79 | # define QT_ALWAYS_USE_FUTEX |
80 | |
81 | // if not defined in linux/futex.h |
82 | # define FUTEX_PRIVATE_FLAG 128 // added in v2.6.22 |
83 | |
84 | # if (__has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)) && __has_include(<sanitizer/tsan_interface.h>) |
85 | # include <sanitizer/tsan_interface.h> |
86 | inline void _q_tsan_acquire(void *addr, void *addr2) |
87 | { |
88 | __tsan_acquire(addr); |
89 | if (addr2) |
90 | __tsan_acquire(addr2); |
91 | } |
92 | inline void _q_tsan_release(void *addr, void *addr2) |
93 | { |
94 | if (addr2) |
95 | __tsan_release(addr2); |
96 | __tsan_release(addr); |
97 | } |
98 | # else |
99 | inline void _q_tsan_acquire(void *, void *) {} |
100 | inline void _q_tsan_release(void *, void *) {} |
101 | # endif // __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__) |
102 | |
103 | QT_BEGIN_NAMESPACE |
104 | namespace QtLinuxFutex { |
105 | constexpr inline bool futexAvailable() { return true; } |
106 | inline int _q_futex(int *addr, int op, int val, quintptr val2 = 0, |
107 | int *addr2 = nullptr, int val3 = 0) noexcept |
108 | { |
109 | // A futex call ensures total ordering on the futex words |
110 | // (in either success or failure of the call). Instruct TSAN accordingly, |
111 | // as TSAN does not understand the futex(2) syscall. |
112 | _q_tsan_release(addr, addr2); |
113 | |
114 | // we use __NR_futex because some libcs (like Android's bionic) don't |
115 | // provide SYS_futex etc. |
116 | int result = syscall(__NR_futex, addr, op | FUTEX_PRIVATE_FLAG, val, val2, addr2, val3); |
117 | |
118 | _q_tsan_acquire(addr, addr2); |
119 | |
120 | return result; |
121 | } |
122 | template <typename T> int *addr(T *ptr) |
123 | { |
124 | int *int_addr = reinterpret_cast<int *>(ptr); |
125 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN |
126 | if (sizeof(T) > sizeof(int)) |
127 | int_addr++; //We want a pointer to the least significant half |
128 | #endif |
129 | return int_addr; |
130 | } |
131 | |
132 | template <typename Atomic> |
133 | inline void futexWait(Atomic &futex, typename Atomic::Type expectedValue) |
134 | { |
135 | _q_futex(addr(&futex), FUTEX_WAIT, qintptr(expectedValue)); |
136 | } |
137 | template <typename Atomic> |
138 | inline bool futexWait(Atomic &futex, typename Atomic::Type expectedValue, qint64 nstimeout) |
139 | { |
140 | struct timespec ts; |
141 | ts.tv_sec = nstimeout / 1000 / 1000 / 1000; |
142 | ts.tv_nsec = nstimeout % (1000 * 1000 * 1000); |
143 | int r = _q_futex(addr(&futex), FUTEX_WAIT, qintptr(expectedValue), quintptr(&ts)); |
144 | return r == 0 || errno != ETIMEDOUT; |
145 | } |
146 | template <typename Atomic> inline void futexWakeOne(Atomic &futex) |
147 | { |
148 | _q_futex(addr(&futex), FUTEX_WAKE, 1); |
149 | } |
150 | template <typename Atomic> inline void futexWakeAll(Atomic &futex) |
151 | { |
152 | _q_futex(addr(&futex), FUTEX_WAKE, INT_MAX); |
153 | } |
154 | template <typename Atomic> inline |
155 | void futexWakeOp(Atomic &futex1, int wake1, int wake2, Atomic &futex2, quint32 op) |
156 | { |
157 | _q_futex(addr(&futex1), FUTEX_WAKE_OP, wake1, wake2, addr(&futex2), op); |
158 | } |
159 | } |
160 | namespace QtFutex = QtLinuxFutex; |
161 | QT_END_NAMESPACE |
162 | |
163 | #else |
164 | |
165 | QT_BEGIN_NAMESPACE |
166 | namespace QtFutex = QtDummyFutex; |
167 | QT_END_NAMESPACE |
168 | #endif |
169 | |
170 | #endif // QFUTEX_P_H |
171 | |