1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qautoresetevent_linux_p.h"
5
6#include <QtCore/private/qcore_unix_p.h>
7#include <QtCore/qdebug.h>
8#include <QtMultimedia/private/qaudio_rtsan_support_p.h>
9
10#include <sys/eventfd.h>
11#include <cstdint>
12
13QT_BEGIN_NAMESPACE
14
15namespace QtPrivate {
16
17QAutoResetEventEventFD::QAutoResetEventEventFD(QObject *parent)
18 : QObject{
19 parent,
20 },
21 m_notifier{
22 QSocketNotifier::Type::Read,
23 }
24{
25 m_fd = eventfd(/*initval=*/count: 0, EFD_NONBLOCK);
26 if (m_fd == -1) {
27 qCritical() << "eventfd failed:" << qt_error_string(errno);
28 return;
29 }
30
31 connect(sender: &m_notifier, signal: &QSocketNotifier::activated, context: this, slot: [this] {
32 uint64_t payload;
33
34 qt_safe_read(fd: m_fd, data: &payload, maxlen: sizeof(payload));
35
36 emit activated();
37 });
38 m_notifier.setSocket(m_fd);
39 m_notifier.setEnabled(true);
40}
41
42QAutoResetEventEventFD::~QAutoResetEventEventFD()
43{
44 if (m_fd != -1)
45 qt_safe_close(fd: m_fd);
46}
47
48void QAutoResetEventEventFD::set()
49{
50 Q_ASSERT(isValid());
51
52 constexpr uint64_t increment{ 1 };
53
54 ScopedRTSanDisabler disabler; // opened via EFD_NONBLOCK
55 qint64 bytesWritten = qt_safe_write(fd: m_fd, data: &increment, len: sizeof(increment));
56 if (bytesWritten == -1)
57 qCritical(msg: "QAutoResetEvent::set failed");
58}
59
60} // namespace QtPrivate
61
62QT_END_NAMESPACE
63
64#include "moc_qautoresetevent_linux_p.cpp"
65

source code of qtmultimedia/src/multimedia/audio/qautoresetevent_linux.cpp