Long live futexes for FreeBSD!
Like commit 91f6460aff0a6ab5142f16d5f4fc1f559ca1c325 which added support for Windows. This API is documented and has apparently been present in FreeBSD for a long while. The DragonflyBSD API is very similar, but I don't have one to confirm that I've coded correctly. OpenBSD and NetBSD may have similar APIs, but I haven't even researched them. We're open to contributions, though. Change-Id: I63b988479db546dabffcfffd1766bc431fed614b Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
parent
96c76839f9
commit
b86f368441
@ -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
|
||||
|
82
src/corelib/thread/qfutex_freebsd_p.h
Normal file
82
src/corelib/thread/qfutex_freebsd_p.h
Normal file
@ -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
|
@ -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
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user