Move the futex implementation to a header
So I can use it in QSemaphore and provide a Windows implementation. Change-Id: I6e9274c1e7444ad48c81fffd14dbc0a8e2201302 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
6011e8a9eb
commit
5b5153fd5b
128
src/corelib/thread/qfutex_p.h
Normal file
128
src/corelib/thread/qfutex_p.h
Normal file
@ -0,0 +1,128 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2017 Intel Corporation.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the QtCore module of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 3 requirements
|
||||
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 2.0 or (at your option) the GNU General
|
||||
** Public license version 3 or any later version approved by the KDE Free
|
||||
** Qt Foundation. The licenses are as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
||||
** https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef QFUTEX_P_H
|
||||
#define QFUTEX_P_H
|
||||
|
||||
#include <qglobal.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
namespace QtDummyFutex {
|
||||
Q_DECL_CONSTEXPR inline bool futexAvailable() { return false; }
|
||||
template <typename Atomic>
|
||||
inline bool futexWait(Atomic &, typename Atomic::Type, int = 0)
|
||||
{ Q_UNREACHABLE(); return false; }
|
||||
template <typename Atomic> inline void futexWakeOne(Atomic &)
|
||||
{ Q_UNREACHABLE(); }
|
||||
template <typename Atomic> inline void futexWakeAll(Atomic &)
|
||||
{ Q_UNREACHABLE(); }
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
|
||||
// use Linux mutexes everywhere except for LSB builds
|
||||
# include <sys/syscall.h>
|
||||
# include <errno.h>
|
||||
# include <limits.h>
|
||||
# include <unistd.h>
|
||||
# include <asm/unistd.h>
|
||||
# include <linux/futex.h>
|
||||
# define QT_ALWAYS_USE_FUTEX
|
||||
|
||||
// if not defined in linux/futex.h
|
||||
# define FUTEX_PRIVATE_FLAG 128 // added in v2.6.22
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace QtLinuxFutex {
|
||||
constexpr inline bool futexAvailable() { return true; }
|
||||
inline int _q_futex(int *addr, int op, int val, const struct timespec *timeout) Q_DECL_NOTHROW
|
||||
{
|
||||
int *addr2 = 0;
|
||||
int val2 = 0;
|
||||
|
||||
// we use __NR_futex because some libcs (like Android's bionic) don't
|
||||
// provide SYS_futex etc.
|
||||
return syscall(__NR_futex, addr, op | FUTEX_PRIVATE_FLAG, val, timeout, addr2, val2);
|
||||
}
|
||||
template <typename T> int *addr(T *ptr)
|
||||
{
|
||||
int *int_addr = reinterpret_cast<int *>(ptr);
|
||||
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
|
||||
if (sizeof(T) > sizeof(int))
|
||||
int_addr++; //We want a pointer to the least significant half
|
||||
#endif
|
||||
return int_addr;
|
||||
}
|
||||
|
||||
template <typename Atomic>
|
||||
inline void futexWait(Atomic &futex, typename Atomic::Type expectedValue)
|
||||
{
|
||||
_q_futex(addr(&futex), FUTEX_WAIT, qintptr(expectedValue), nullptr);
|
||||
}
|
||||
template <typename Atomic>
|
||||
inline bool futexWait(Atomic &futex, typename Atomic::Type expectedValue, qint64 nstimeout)
|
||||
{
|
||||
struct timespec ts;
|
||||
ts.tv_sec = nstimeout / 1000 / 1000 / 1000;
|
||||
ts.tv_nsec = nstimeout % (1000 * 1000 * 1000);
|
||||
int r = _q_futex(addr(&futex), FUTEX_WAIT, qintptr(expectedValue), &ts);
|
||||
return r == 0 || errno != ETIMEDOUT;
|
||||
}
|
||||
template <typename Atomic> inline void futexWakeOne(Atomic &futex)
|
||||
{
|
||||
_q_futex(addr(&futex), FUTEX_WAKE, 1, nullptr);
|
||||
}
|
||||
template <typename Atomic> inline void futexWakeAll(Atomic &futex)
|
||||
{
|
||||
_q_futex(addr(&futex), FUTEX_WAKE, INT_MAX, nullptr);
|
||||
}
|
||||
}
|
||||
namespace QtFutex = QtLinuxFutex;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#else
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace QtFutex = QtDummyFutex;
|
||||
QT_END_NAMESPACE
|
||||
#endif
|
||||
|
||||
#endif // QFUTEX_P_H
|
@ -44,15 +44,9 @@
|
||||
#ifndef QT_NO_THREAD
|
||||
#include "qatomic.h"
|
||||
#include "qmutex_p.h"
|
||||
#include "qelapsedtimer.h"
|
||||
#include "qfutex_p.h"
|
||||
|
||||
#include <linux/futex.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <asm/unistd.h>
|
||||
|
||||
#ifndef QT_LINUX_FUTEX
|
||||
#ifndef QT_ALWAYS_USE_FUTEX
|
||||
# error "Qt build is broken: qmutex_linux.cpp is being built but futex support is not wanted"
|
||||
#endif
|
||||
|
||||
@ -63,6 +57,8 @@
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
using namespace QtFutex;
|
||||
|
||||
/*
|
||||
* QBasicMutex implementation on Linux with futexes
|
||||
*
|
||||
@ -107,20 +103,6 @@ QT_BEGIN_NAMESPACE
|
||||
* waiting in the past. We then set the mutex to 0x0 and perform a FUTEX_WAKE.
|
||||
*/
|
||||
|
||||
static inline int _q_futex(void *addr, int op, int val, const struct timespec *timeout) Q_DECL_NOTHROW
|
||||
{
|
||||
volatile int *int_addr = reinterpret_cast<volatile int *>(addr);
|
||||
#if Q_BYTE_ORDER == Q_BIG_ENDIAN && QT_POINTER_SIZE == 8
|
||||
int_addr++; //We want a pointer to the 32 least significant bit of QMutex::d
|
||||
#endif
|
||||
int *addr2 = 0;
|
||||
int val2 = 0;
|
||||
|
||||
// we use __NR_futex because some libcs (like Android's bionic) don't
|
||||
// provide SYS_futex etc.
|
||||
return syscall(__NR_futex, int_addr, op | FUTEX_PRIVATE_FLAG, val, timeout, addr2, val2);
|
||||
}
|
||||
|
||||
static inline QMutexData *dummyFutexValue()
|
||||
{
|
||||
return reinterpret_cast<QMutexData *>(quintptr(3));
|
||||
@ -136,36 +118,38 @@ bool lockInternal_helper(QBasicAtomicPointer<QMutexData> &d_ptr, int timeout = -
|
||||
if (timeout == 0)
|
||||
return false;
|
||||
|
||||
struct timespec ts, *pts = 0;
|
||||
if (IsTimed && timeout > 0) {
|
||||
ts.tv_sec = timeout / 1000;
|
||||
ts.tv_nsec = (timeout % 1000) * 1000 * 1000;
|
||||
}
|
||||
|
||||
// the mutex is locked already, set a bit indicating we're waiting
|
||||
while (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) != 0) {
|
||||
if (IsTimed && pts == &ts) {
|
||||
// recalculate the timeout
|
||||
qint64 xtimeout = qint64(timeout) * 1000 * 1000;
|
||||
xtimeout -= elapsedTimer->nsecsElapsed();
|
||||
if (xtimeout <= 0) {
|
||||
// timer expired after we returned
|
||||
return false;
|
||||
}
|
||||
ts.tv_sec = xtimeout / Q_INT64_C(1000) / 1000 / 1000;
|
||||
ts.tv_nsec = xtimeout % (Q_INT64_C(1000) * 1000 * 1000);
|
||||
}
|
||||
if (IsTimed && timeout > 0)
|
||||
pts = &ts;
|
||||
if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) == nullptr)
|
||||
return true;
|
||||
|
||||
qint64 nstimeout = timeout * Q_INT64_C(1000) * 1000;
|
||||
qint64 remainingTime = nstimeout;
|
||||
forever {
|
||||
// successfully set the waiting bit, now sleep
|
||||
int r = _q_futex(&d_ptr, FUTEX_WAIT, quintptr(dummyFutexValue()), pts);
|
||||
if (IsTimed && r != 0 && errno == ETIMEDOUT)
|
||||
return false;
|
||||
if (IsTimed && nstimeout >= 0) {
|
||||
bool r = futexWait(d_ptr, dummyFutexValue(), remainingTime);
|
||||
if (!r)
|
||||
return false;
|
||||
|
||||
// we got woken up, so try to acquire the mutex
|
||||
// note we must set to dummyFutexValue because there could be other threads
|
||||
// also waiting
|
||||
// we got woken up, so try to acquire the mutex
|
||||
// note we must set to dummyFutexValue because there could be other threads
|
||||
// also waiting
|
||||
if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) == nullptr)
|
||||
return true;
|
||||
|
||||
// recalculate the timeout
|
||||
remainingTime = nstimeout - elapsedTimer->nsecsElapsed();
|
||||
if (remainingTime <= 0)
|
||||
return false;
|
||||
} else {
|
||||
futexWait(d_ptr, dummyFutexValue());
|
||||
|
||||
// we got woken up, so try to acquire the mutex
|
||||
// note we must set to dummyFutexValue because there could be other threads
|
||||
// also waiting
|
||||
if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) == nullptr)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Q_ASSERT(d_ptr.load());
|
||||
@ -195,10 +179,9 @@ void QBasicMutex::unlockInternal() Q_DECL_NOTHROW
|
||||
Q_ASSERT(!isRecursive());
|
||||
|
||||
d_ptr.storeRelease(0);
|
||||
_q_futex(&d_ptr, FUTEX_WAKE, 1, 0);
|
||||
futexWakeOne(d_ptr);
|
||||
}
|
||||
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif // QT_NO_THREAD
|
||||
|
@ -22,6 +22,7 @@ HEADERS += thread/qmutex.h \
|
||||
# private headers
|
||||
HEADERS += thread/qmutex_p.h \
|
||||
thread/qmutexpool_p.h \
|
||||
thread/qfutex_p.h \
|
||||
thread/qfutureinterface_p.h \
|
||||
thread/qfuturewatcher_p.h \
|
||||
thread/qorderedmutexlocker_p.h \
|
||||
|
Loading…
x
Reference in New Issue
Block a user