From 30bea611df2d95b53b63273ac867d9bd0a181edf Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sat, 11 Aug 2012 16:45:14 +0200 Subject: Make QBasicMutex be exclusively non-recursive Dispatch to the recursive mutex functions from QMutex::lock, tryLock and unlock. This has the benefit that those using QBasicMutex will not go through the testing for recursive mutexes. It simplifies a little the code for those users. For the users of QMutex, the code required to perform a lock does not appear to change. Change-Id: I0ca9965e012b283c30f1fab8e9f6d9b3288c2247 Reviewed-by: Lars Knoll --- src/corelib/thread/qmutex.cpp | 60 ++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 20 deletions(-) (limited to 'src/corelib/thread/qmutex.cpp') diff --git a/src/corelib/thread/qmutex.cpp b/src/corelib/thread/qmutex.cpp index fa4fddca56..2906a5a198 100644 --- a/src/corelib/thread/qmutex.cpp +++ b/src/corelib/thread/qmutex.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (C) 2012 Intel Corporation ** Contact: http://www.qt-project.org/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -55,6 +56,19 @@ QT_BEGIN_NAMESPACE +static inline bool isRecursive(QMutexData *d) +{ + register quintptr u = quintptr(d); + if (Q_LIKELY(u <= 0x3)) + return false; +#ifdef QT_LINUX_FUTEX + Q_ASSERT(d->recursive); + return true; +#else + return d->recursive; +#endif +} + /* \class QBasicMutex \inmodule QtCore @@ -180,7 +194,13 @@ QMutex::~QMutex() */ void QMutex::lock() QT_MUTEX_LOCK_NOEXCEPT { - QBasicMutex::lock(); + if (fastTryLock()) + return; + QMutexData *current = d_ptr.loadAcquire(); + if (QT_PREPEND_NAMESPACE(isRecursive)(current)) + static_cast(current)->lock(-1); + else + lockInternal(); } /*! \fn bool QMutex::tryLock(int timeout) @@ -208,7 +228,13 @@ void QMutex::lock() QT_MUTEX_LOCK_NOEXCEPT */ bool QMutex::tryLock(int timeout) QT_MUTEX_LOCK_NOEXCEPT { - return QBasicMutex::tryLock(timeout); + if (fastTryLock()) + return true; + QMutexData *current = d_ptr.loadAcquire(); + if (QT_PREPEND_NAMESPACE(isRecursive)(current)) + return static_cast(current)->lock(timeout); + else + return lockInternal(timeout); } /*! \fn void QMutex::unlock() @@ -220,7 +246,13 @@ bool QMutex::tryLock(int timeout) QT_MUTEX_LOCK_NOEXCEPT */ void QMutex::unlock() Q_DECL_NOTHROW { - QBasicMutex::unlock(); + if (fastTryUnlock()) + return; + QMutexData *current = d_ptr.loadAcquire(); + if (QT_PREPEND_NAMESPACE(isRecursive)(current)) + static_cast(current)->unlock(); + else + unlockInternal(); } /*! @@ -230,16 +262,9 @@ void QMutex::unlock() Q_DECL_NOTHROW Returns true if the mutex is recursive */ -bool QBasicMutex::isRecursive() { - QMutexData *d = d_ptr.load(); - if (quintptr(d) <= 0x3) - return false; -#ifdef QT_LINUX_FUTEX - Q_ASSERT(d->recursive); - return true; -#else - return d->recursive; -#endif +bool QBasicMutex::isRecursive() +{ + return QT_PREPEND_NAMESPACE(isRecursive)(d_ptr.loadAcquire()); } @@ -338,8 +363,7 @@ bool QBasicMutex::isRecursive() { */ bool QBasicMutex::lockInternal(int timeout) QT_MUTEX_LOCK_NOEXCEPT { - if (isRecursive()) - return static_cast(d_ptr.load())->lock(timeout); + Q_ASSERT(!isRecursive()); while (!fastTryLock()) { QMutexData *copy = d_ptr.loadAcquire(); @@ -435,11 +459,7 @@ void QBasicMutex::unlockInternal() Q_DECL_NOTHROW QMutexData *copy = d_ptr.loadAcquire(); Q_ASSERT(copy); //we must be locked Q_ASSERT(copy != dummyLocked()); // testAndSetRelease(dummyLocked(), 0) failed - - if (copy->recursive) { - static_cast(copy)->unlock(); - return; - } + Q_ASSERT(!isRecursive()); QMutexPrivate *d = reinterpret_cast(copy); -- cgit v1.2.3