Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfutex_freebsd_p.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#ifndef QFUTEX_FREEBSD_P_H
6#define QFUTEX_FREEBSD_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qcore_unix_p.h>
20#include <qdeadlinetimer.h>
21
22// https://man.freebsd.org/cgi/man.cgi?query=_umtx_op
23#include <sys/umtx.h>
24
25#define QT_ALWAYS_USE_FUTEX
26
27QT_BEGIN_NAMESPACE
28
29namespace QtFreeBSDFutex {
30constexpr inline bool futexAvailable() { return true; }
31
32template <typename Atomic>
33inline int do_wait(Atomic &futex, typename Atomic::Type expectedValue, _umtx_time *tmp = nullptr)
34{
35 // FreeBSD UMTX_OP_WAIT does not apply acquire or release memory barriers,
36 // so there are no QtTsan calls here.
37
38 int op = UMTX_OP_WAIT_UINT_PRIVATE;
39 if (sizeof(futex) > sizeof(quint32))
40 op = UMTX_OP_WAIT; // no _PRIVATE version
41
42 // The timeout is passed in uaddr2, with its size in uaddr
43 void *uaddr = reinterpret_cast<void *>(tmp ? sizeof(*tmp) : 0);
44 void *uaddr2 = tmp;
45 int ret = _umtx_op(&futex, op, u_long(expectedValue), uaddr, uaddr2);
46
47 return ret;
48}
49
50template <typename Atomic>
51inline void futexWait(Atomic &futex, typename Atomic::Type expectedValue)
52{
53 do_wait(futex, expectedValue);
54}
55
56template <typename Atomic>
57inline bool futexWait(Atomic &futex, typename Atomic::Type expectedValue, QDeadlineTimer timer)
58{
59 struct _umtx_time tm = {};
60 auto deadline = timer.deadline<std::chrono::steady_clock>();
61 tm._timeout = durationToTimespec(deadline.time_since_epoch());
62 tm._flags = UMTX_ABSTIME;
63 tm._clockid = CLOCK_MONOTONIC;
64 int r = do_wait(futex, expectedValue, &tm);
65 return r == 0 || errno != ETIMEDOUT;
66}
67
68template <typename Atomic> inline void futexWakeOne(Atomic &futex)
69{
70 _umtx_op(&futex, UMTX_OP_WAKE_PRIVATE, 1, nullptr, nullptr);
71}
72
73template <typename Atomic> inline void futexWakeAll(Atomic &futex)
74{
75 _umtx_op(&futex, UMTX_OP_WAKE_PRIVATE, INT_MAX, nullptr, nullptr);
76}
77} //namespace QtFreeBSDFutex
78
79namespace QtFutex = QtFreeBSDFutex;
80
81QT_END_NAMESPACE
82
83#endif // QFUTEX_FREEBSD_P_H
bool futexWait(Atomic &futex, typename Atomic::Type expectedValue, QDeadlineTimer timer)
void futexWakeAll(Atomic &futex)
void futexWait(Atomic &futex, typename Atomic::Type expectedValue)
void futexWakeOne(Atomic &futex)
int do_wait(Atomic &futex, typename Atomic::Type expectedValue, _umtx_time *tmp=nullptr)
constexpr bool futexAvailable()