Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qfutex_mac_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
4#ifndef QFUTEX_MAC_P_H
5#define QFUTEX_MAC_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 <qdeadlinetimer.h>
19#include <qtsan_impl.h>
20#include <private/qglobal_p.h>
21
22// The Darwin kernel exposes a set of __ulock_{wait,wait2,wake} APIs in
23// https://github.com/apple-oss-distributions/xnu/blob/xnu-8792.81.2/bsd/sys/ulock.h,
24// but these APIs are marked as private, so we cannot rely on them being
25// stable, nor we can use these APIs in builds of Qt intended for
26// the Apple App Store. By wholesale disabling the use of the APIs
27// in App Store compliant builds, and runtime checking availability
28// of the APIs when we do build them in, we should be safe, unless
29// the semantics of the APIs change in ways we haven't accounted for,
30// but that's a risk we're willing to take.
31
32#if QT_CONFIG(appstore_compliant)
34namespace QtFutex = QtDummyFutex;
36#else
37
38extern "C" {
39// -------- BEGIN OS Declarations --------
40// Source: https://github.com/apple-oss-distributions/xnu/blob/xnu-8792.81.2/bsd/sys/ulock.h
41// Modification: added __attribute((__weak__))
42// Copyright (c) 2015 Apple Inc. All rights reserved.
43
44__attribute((__weak__))
45extern int __ulock_wait2(uint32_t operation, void *addr, uint64_t value,
46 uint64_t timeout, uint64_t value2);
47__attribute((__weak__))
48extern int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);
49
50/*
51 * operation bits [7, 0] contain the operation code.
52 */
53#define UL_COMPARE_AND_WAIT 1
54#define UL_COMPARE_AND_WAIT_SHARED 3
55#define UL_COMPARE_AND_WAIT64 5
56#define UL_COMPARE_AND_WAIT64_SHARED 6
57
58/*
59 * operation bits [15, 8] contain the flags for __ulock_wake
60 */
61#define ULF_WAKE_ALL 0x00000100
62#define ULF_WAKE_THREAD 0x00000200
63#define ULF_WAKE_ALLOW_NON_OWNER 0x00000400
64
65/*
66 * operation bits [15, 8] contain the flags for __ulock_wake
67 */
68#define ULF_WAKE_ALL 0x00000100
69#define ULF_WAKE_THREAD 0x00000200
70#define ULF_WAKE_ALLOW_NON_OWNER 0x00000400
71
72/*
73 * operation bits [31, 24] contain the generic flags
74 */
75#define ULF_NO_ERRNO 0x01000000
76
77// -------- END OS Declarations --------
78} // extern "C"
79
81
82namespace QtDarwinFutex {
83
84/*not constexpr*/ inline bool futexAvailable() { return __ulock_wake && __ulock_wait2; }
85
86template <typename Atomic>
87inline uint32_t baseOperation(Atomic &)
88{
89 static_assert(sizeof(Atomic) >= sizeof(quint32), "Can only operate on 32- or 64-bit atomics");
90
91 uint32_t operation = ULF_NO_ERRNO;
92 if (sizeof(Atomic) == sizeof(quint32))
93 operation |= UL_COMPARE_AND_WAIT;
94 else
95 operation |= UL_COMPARE_AND_WAIT64;
96 return operation;
97}
98
99template <typename Atomic> inline int
100do_wait(Atomic &futex, typename Atomic::Type expectedValue, QDeadlineTimer timer)
101{
102 // source code inspection shows __ulock_wait2 uses nanoseconds for timeout
103 QtTsan::futexRelease(&futex);
104 int ret = __ulock_wait2(baseOperation(futex), &futex, uint64_t(expectedValue),
105 timer.remainingTimeNSecs(), 0);
106 QtTsan::futexAcquire(&futex);
107 return ret;
108}
109
110template <typename Atomic>
111inline void futexWait(Atomic &futex, typename Atomic::Type expectedValue)
112{
113 do_wait(futex, expectedValue, {});
114}
115
116template <typename Atomic>
117inline bool futexWait(Atomic &futex, typename Atomic::Type expectedValue, QDeadlineTimer timer)
118{
119 int r = do_wait(futex, expectedValue, timer);
120 return r == 0 || r != -ETIMEDOUT;
121}
122
123template <typename Atomic> inline void futexWakeAll(Atomic &futex)
124{
125 __ulock_wake(baseOperation(futex) | ULF_WAKE_ALL, &futex, 0);
126}
127
128template <typename Atomic> inline void futexWakeOne(Atomic &futex)
129{
130 __ulock_wake(baseOperation(futex), &futex, 0);
131}
132} //namespace QtDarwinMutex
133
134namespace QtFutex = QtDarwinFutex;
135
137
138#endif // QT_CONFIG(appstore_compliant)
139
140#endif // QFUTEX_MAC_P_H
\inmodule QtCore
Combined button and popup list for selecting options.
void futexWakeOne(Atomic &futex)
void futexWait(Atomic &futex, typename Atomic::Type expectedValue)
uint32_t baseOperation(Atomic &)
bool futexAvailable()
void futexWakeAll(Atomic &futex)
int do_wait(Atomic &futex, typename Atomic::Type expectedValue, QDeadlineTimer timer)
void futexRelease(void *, void *=nullptr)
Definition qtsan_impl.h:63
void futexAcquire(void *, void *=nullptr)
Definition qtsan_impl.h:62
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define ULF_WAKE_ALL
#define ULF_NO_ERRNO
void uint64_t wake_value
__attribute((__weak__)) extern int __ulock_wait2(uint32_t operation
void uint64_t uint64_t uint64_t value2
#define UL_COMPARE_AND_WAIT
#define UL_COMPARE_AND_WAIT64
return ret
GLboolean r
[2]
GLbitfield GLuint64 timeout
[4]
GLenum const void * addr
unsigned int quint32
Definition qtypes.h:50
QTimer * timer
[3]