From c28204066c2d3bae989132ab15e8df437ae38f3d Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 2 Aug 2012 16:15:22 +0200 Subject: Mark mutex locking and unlocking functions with noexcept Unlocking a mutex can never throw an exception. That doesn't make sense and our code should make sure it can't happen. Right now, provided that the system-level functions don't throw, we don't either. Locking a mutex cannot throw on Linux because we use futexes directly. A non-recursive mutex is just a futex, whereas a recursive mutex uses a mutex (a futex) to manage a lock count. However, on other platforms, due to the freelist, there can be memory allocation, which means it might throw std::bad_alloc. Not because of the freelist itself (that uses malloc and will just crash if malloc fails) but because of Q_GLOBAL_STATIC. In 5.1, the global static will be noexcept provided the type's constructor is so too (it is, in this case). Change-Id: I4c562383f48de1be7827b9afb512d73eaf0792d5 Reviewed-by: Marc Mutz Reviewed-by: Thiago Macieira --- src/corelib/thread/qmutex.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/corelib/thread/qmutex.cpp') diff --git a/src/corelib/thread/qmutex.cpp b/src/corelib/thread/qmutex.cpp index a88e3ceef9..1d35b76c92 100644 --- a/src/corelib/thread/qmutex.cpp +++ b/src/corelib/thread/qmutex.cpp @@ -68,7 +68,6 @@ QT_BEGIN_NAMESPACE - Do not use tryLock with timeout > 0, else you can have a leak (see the ~QMutex destructor) */ - /*! \class QMutex \inmodule QtCore @@ -179,7 +178,7 @@ QMutex::~QMutex() \sa unlock() */ -void QMutex::lock() +void QMutex::lock() QT_MUTEX_LOCK_NOEXCEPT { QBasicMutex::lock(); } @@ -207,7 +206,7 @@ void QMutex::lock() \sa lock(), unlock() */ -bool QMutex::tryLock(int timeout) +bool QMutex::tryLock(int timeout) QT_MUTEX_LOCK_NOEXCEPT { return QBasicMutex::tryLock(timeout); } @@ -219,7 +218,7 @@ bool QMutex::tryLock(int timeout) \sa lock() */ -void QMutex::unlock() +void QMutex::unlock() Q_DECL_NOTHROW { QBasicMutex::unlock(); } @@ -332,7 +331,7 @@ bool QBasicMutex::isRecursive() { /*! \internal helper for lock() */ -bool QBasicMutex::lockInternal(int timeout) +bool QBasicMutex::lockInternal(int timeout) QT_MUTEX_LOCK_NOEXCEPT { while (!fastTryLock()) { QMutexData *copy = d_ptr.loadAcquire(); @@ -425,7 +424,7 @@ bool QBasicMutex::lockInternal(int timeout) /*! \internal */ -void QBasicMutex::unlockInternal() +void QBasicMutex::unlockInternal() Q_DECL_NOTHROW { QMutexData *copy = d_ptr.loadAcquire(); Q_ASSERT(copy); //we must be locked @@ -493,7 +492,7 @@ void QMutexPrivate::release() } // atomically subtract "value" to the waiters, and remove the QMutexPrivate::BigNumber flag -void QMutexPrivate::derefWaiters(int value) +void QMutexPrivate::derefWaiters(int value) Q_DECL_NOTHROW { int old_waiters; int new_waiters; @@ -511,7 +510,8 @@ void QMutexPrivate::derefWaiters(int value) /*! \internal */ -bool QRecursiveMutexPrivate::lock(int timeout) { +bool QRecursiveMutexPrivate::lock(int timeout) QT_MUTEX_LOCK_NOEXCEPT +{ Qt::HANDLE self = QThread::currentThreadId(); if (owner == self) { ++count; @@ -533,7 +533,7 @@ bool QRecursiveMutexPrivate::lock(int timeout) { /*! \internal */ -void QRecursiveMutexPrivate::unlock() +void QRecursiveMutexPrivate::unlock() Q_DECL_NOTHROW { if (count > 0) { count--; -- cgit v1.2.3