1 | // Copyright (C) 2023 Intel Corporation. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QFUTEX_LINUX_P_H |
5 | #define QFUTEX_LINUX_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <private/qcore_unix_p.h> |
19 | #include <qdeadlinetimer.h> |
20 | #include <qtsan_impl.h> |
21 | |
22 | #include <asm/unistd.h> |
23 | #include <errno.h> |
24 | #include <limits.h> |
25 | #include <linux/futex.h> |
26 | #include <sys/syscall.h> |
27 | #include <unistd.h> |
28 | |
29 | // RISC-V does not supply __NR_futex |
30 | #ifndef __NR_futex |
31 | # define __NR_futex __NR_futex_time64 |
32 | #endif |
33 | |
34 | #define QT_ALWAYS_USE_FUTEX |
35 | |
36 | QT_BEGIN_NAMESPACE |
37 | |
38 | namespace QtLinuxFutex { |
39 | constexpr inline bool futexAvailable() { return true; } |
40 | |
41 | inline long _q_futex(int *addr, int op, int val, quintptr val2 = 0, |
42 | int *addr2 = nullptr, int val3 = 0) noexcept |
43 | { |
44 | QtTsan::futexRelease(addr, addr2); |
45 | |
46 | // we use __NR_futex because some libcs (like Android's bionic) don't |
47 | // provide SYS_futex etc. |
48 | long result = syscall(__NR_futex, addr, op | FUTEX_PRIVATE_FLAG, val, val2, addr2, val3); |
49 | |
50 | QtTsan::futexAcquire(addr, addr2); |
51 | |
52 | return result; |
53 | } |
54 | template <typename T> int *addr(T *ptr) |
55 | { |
56 | int *int_addr = reinterpret_cast<int *>(ptr); |
57 | #if Q_BYTE_ORDER == Q_BIG_ENDIAN |
58 | if (sizeof(T) > sizeof(int)) |
59 | int_addr++; //We want a pointer to the least significant half |
60 | #endif |
61 | return int_addr; |
62 | } |
63 | |
64 | template <typename Atomic> |
65 | inline void futexWait(Atomic &futex, typename Atomic::Type expectedValue) |
66 | { |
67 | _q_futex(addr(&futex), FUTEX_WAIT, qintptr(expectedValue)); |
68 | } |
69 | template <typename Atomic> |
70 | inline bool futexWait(Atomic &futex, typename Atomic::Type expectedValue, QDeadlineTimer deadline) |
71 | { |
72 | auto timeout = deadline.deadline<std::chrono::steady_clock>().time_since_epoch(); |
73 | struct timespec ts = durationToTimespec(timeout); |
74 | long r = _q_futex(addr(&futex), FUTEX_WAIT_BITSET, qintptr(expectedValue), quintptr(&ts), |
75 | nullptr, FUTEX_BITSET_MATCH_ANY); |
76 | return r == 0 || errno != ETIMEDOUT; |
77 | } |
78 | template <typename Atomic> inline void futexWakeOne(Atomic &futex) |
79 | { |
80 | _q_futex(addr(&futex), FUTEX_WAKE, 1); |
81 | } |
82 | template <typename Atomic> inline void futexWakeAll(Atomic &futex) |
83 | { |
84 | _q_futex(addr(&futex), FUTEX_WAKE, INT_MAX); |
85 | } |
86 | template <typename Atomic> inline |
87 | void futexWakeOp(Atomic &futex1, int wake1, int wake2, Atomic &futex2, quint32 op) |
88 | { |
89 | _q_futex(addr(&futex1), FUTEX_WAKE_OP, wake1, wake2, addr(&futex2), op); |
90 | }} // namespace QtLinuxFutex |
91 | namespace QtFutex = QtLinuxFutex; |
92 | |
93 | QT_END_NAMESPACE |
94 | |
95 | #endif // QFUTEX_LINUX_P_H |
96 | |