| 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 | |
| 9 | #include <sys/eventfd.h> |
| 10 | #include <cstdint> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | namespace QtPrivate { |
| 15 | |
| 16 | QAutoResetEventEventFD::QAutoResetEventEventFD(QObject *parent) |
| 17 | : QObject{ |
| 18 | parent, |
| 19 | }, |
| 20 | m_notifier{ |
| 21 | QSocketNotifier::Type::Read, |
| 22 | } |
| 23 | { |
| 24 | m_fd = eventfd(/*initval=*/count: 0, EFD_NONBLOCK); |
| 25 | if (m_fd == -1) { |
| 26 | qCritical() << "eventfd failed:" << qt_error_string(errno); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | connect(sender: &m_notifier, signal: &QSocketNotifier::activated, context: this, slot: [this] { |
| 31 | uint64_t payload; |
| 32 | |
| 33 | qt_safe_read(fd: m_fd, data: &payload, maxlen: sizeof(payload)); |
| 34 | |
| 35 | emit activated(); |
| 36 | }); |
| 37 | m_notifier.setSocket(m_fd); |
| 38 | m_notifier.setEnabled(true); |
| 39 | } |
| 40 | |
| 41 | QAutoResetEventEventFD::~QAutoResetEventEventFD() |
| 42 | { |
| 43 | if (m_fd != -1) |
| 44 | qt_safe_close(fd: m_fd); |
| 45 | } |
| 46 | |
| 47 | void QAutoResetEventEventFD::set() |
| 48 | { |
| 49 | Q_ASSERT(isValid()); |
| 50 | |
| 51 | constexpr uint64_t increment{ 1 }; |
| 52 | |
| 53 | qint64 bytesWritten = qt_safe_write(fd: m_fd, data: &increment, len: sizeof(increment)); |
| 54 | if (bytesWritten == -1) |
| 55 | qCritical(msg: "QAutoResetEvent::set failed" ); |
| 56 | } |
| 57 | |
| 58 | } // namespace QtPrivate |
| 59 | |
| 60 | QT_END_NAMESPACE |
| 61 | |
| 62 | #include "moc_qautoresetevent_linux_p.cpp" |
| 63 | |