diff options
-rw-r--r-- | src/corelib/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/corelib/thread/qfutex_freebsd_p.h | 82 | ||||
-rw-r--r-- | src/corelib/thread/qfutex_p.h | 5 | ||||
-rw-r--r-- | tests/auto/corelib/thread/qmutex/tst_qmutex.cpp | 14 |
4 files changed, 97 insertions, 9 deletions
diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index 5808885179..5c5d6beaff 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -690,6 +690,11 @@ qt_internal_extend_target(Core CONDITION QT_FEATURE_thread AND UNIX AND NOT APPL thread/qmutex_unix.cpp ) +qt_internal_extend_target(Core CONDITION QT_FEATURE_thread AND FREEBSD + SOURCES + thread/qfutex_freebsd_p.h +) + qt_internal_extend_target(Core CONDITION QT_FEATURE_thread AND LINUX SOURCES thread/qfutex_linux_p.h diff --git a/src/corelib/thread/qfutex_freebsd_p.h b/src/corelib/thread/qfutex_freebsd_p.h new file mode 100644 index 0000000000..b31774d28d --- /dev/null +++ b/src/corelib/thread/qfutex_freebsd_p.h @@ -0,0 +1,82 @@ +// Copyright (C) 2023 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QFUTEX_FREEBSD_P_H +#define QFUTEX_FREEBSD_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <private/qcore_unix_p.h> +#include <qdeadlinetimer.h> + +// https://man.freebsd.org/cgi/man.cgi?query=_umtx_op +#include <sys/umtx.h> + +#define QT_ALWAYS_USE_FUTEX + +QT_BEGIN_NAMESPACE + +namespace QtFreeBSDFutex { +constexpr inline bool futexAvailable() { return true; } + +template <typename Atomic> +inline int do_wait(Atomic &futex, typename Atomic::Type expectedValue, _umtx_time *tmp = nullptr) +{ + // FreeBSD UMTX_OP_WAIT does not apply acquire or release memory barriers, + // so there are no QtTsan calls here. + + int op = UMTX_OP_WAIT_UINT_PRIVATE; + if (sizeof(futex) > sizeof(quint32)) + op = UMTX_OP_WAIT; // no _PRIVATE version + + // The timeout is passed in uaddr2, with its size in uaddr + void *uaddr = reinterpret_cast<void *>(tmp ? sizeof(*tmp) : 0); + void *uaddr2 = tmp; + int ret = _umtx_op(&futex, op, u_long(expectedValue), uaddr, uaddr2); + + return ret; +} + +template <typename Atomic> +inline void futexWait(Atomic &futex, typename Atomic::Type expectedValue) +{ + do_wait(futex, expectedValue); +} + +template <typename Atomic> +inline bool futexWait(Atomic &futex, typename Atomic::Type expectedValue, QDeadlineTimer timer) +{ + struct _umtx_time tm = {}; + auto deadline = timer.deadline<std::chrono::steady_clock>(); + tm._timeout = durationToTimespec(deadline.time_since_epoch()); + tm._flags = UMTX_ABSTIME; + tm._clockid = CLOCK_MONOTONIC; + int r = do_wait(futex, expectedValue, &tm); + return r == 0 || errno != ETIMEDOUT; +} + +template <typename Atomic> inline void futexWakeOne(Atomic &futex) +{ + _umtx_op(&futex, UMTX_OP_WAKE_PRIVATE, 1, nullptr, nullptr); +} + +template <typename Atomic> inline void futexWakeAll(Atomic &futex) +{ + _umtx_op(&futex, UMTX_OP_WAKE_PRIVATE, INT_MAX, nullptr, nullptr); +} +} //namespace QtFreeBSDFutex + +namespace QtFutex = QtFreeBSDFutex; + +QT_END_NAMESPACE + +#endif // QFUTEX_FREEBSD_P_H diff --git a/src/corelib/thread/qfutex_p.h b/src/corelib/thread/qfutex_p.h index f7130ebad2..d7b91f6d2d 100644 --- a/src/corelib/thread/qfutex_p.h +++ b/src/corelib/thread/qfutex_p.h @@ -33,13 +33,14 @@ namespace QtDummyFutex { QT_END_NAMESPACE -#if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) +#if defined(Q_OS_FREEBSD) +# include "qfutex_freebsd_p.h" +#elif defined(Q_OS_LINUX) && !defined(QT_LINUXBASE) // use Linux mutexes everywhere except for LSB builds # include "qfutex_linux_p.h" #elif defined(Q_OS_WIN) # include "qfutex_win_p.h" #else - QT_BEGIN_NAMESPACE namespace QtFutex = QtDummyFutex; QT_END_NAMESPACE diff --git a/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp b/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp index d2c0a38fd0..a679c9afdf 100644 --- a/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp +++ b/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp @@ -145,43 +145,43 @@ void tst_QMutex::tryLock_non_recursive() Thread thread; thread.start(); - // TEST 1: thread can't acquire lock + qDebug("TEST 1: thread can't acquire lock"); testsTurn.acquire(); normalMutex.lock(); QVERIFY(lockCount.testAndSetRelaxed(0, 1)); threadsTurn.release(); - // TEST 2: thread can acquire lock + qDebug("TEST 2: thread can acquire lock"); testsTurn.acquire(); QVERIFY(lockCount.testAndSetRelaxed(1, 0)); normalMutex.unlock(); threadsTurn.release(); - // TEST 3: thread can't acquire lock, timeout = waitTime + qDebug("TEST 3: thread can't acquire lock, timeout = waitTime"); testsTurn.acquire(); normalMutex.lock(); QVERIFY(lockCount.testAndSetRelaxed(0, 1)); threadsTurn.release(); - // TEST 4: thread can acquire lock, timeout = waitTime + qDebug("TEST 4: thread can acquire lock, timeout = waitTime"); testsTurn.acquire(); QVERIFY(lockCount.testAndSetRelaxed(1, 0)); normalMutex.unlock(); threadsTurn.release(); - // TEST 5: thread can't acquire lock, timeout = 0 + qDebug("TEST 5: thread can't acquire lock, timeout = 0"); testsTurn.acquire(); normalMutex.lock(); QVERIFY(lockCount.testAndSetRelaxed(0, 1)); threadsTurn.release(); - // TEST 6: thread can acquire lock, timeout = 0 + qDebug("TEST 6: thread can acquire lock, timeout = 0"); testsTurn.acquire(); QVERIFY(lockCount.testAndSetRelaxed(1, 0)); normalMutex.unlock(); threadsTurn.release(); - // TEST 7: thread can acquire lock, timeout = 3000 (QTBUG-24795) + qDebug("TEST 7: thread can acquire lock, timeout = 3000 (QTBUG-24795)"); testsTurn.acquire(); normalMutex.lock(); threadsTurn.release(); |