From 3a29976eac1ff95e663d49886dc82611a175fed9 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 27 Aug 2012 12:24:48 +0200 Subject: Move QtConcurrent::Exception as QException back to QtCore Leave the old name as a deprecated typedef; adapt users. This is a prerequisite for moving QFuture back to QtCore. Change-Id: I81dcee2c7e6eb234c16f3f42e2415ca0da3dc4f8 Reviewed-by: Thiago Macieira --- dist/changes-5.0.0 | 3 + src/concurrent/concurrent.pro | 1 - .../code/src_concurrent_qtconcurrentexception.cpp | 75 -------- src/concurrent/qfutureinterface.cpp | 4 +- src/concurrent/qfutureinterface.h | 6 +- src/concurrent/qfutureinterface_p.h | 2 +- src/concurrent/qtconcurrentexception.cpp | 211 --------------------- src/concurrent/qtconcurrentexception.h | 64 +------ src/concurrent/qtconcurrentmap.cpp | 8 - src/concurrent/qtconcurrentrunbase.h | 8 +- src/concurrent/qtconcurrentthreadengine.cpp | 10 +- src/concurrent/qtconcurrentthreadengine.h | 6 +- .../code/src_corelib_thread_qexception.cpp | 75 ++++++++ src/corelib/thread/qexception.cpp | 206 ++++++++++++++++++++ src/corelib/thread/qexception.h | 123 ++++++++++++ src/corelib/thread/thread.pri | 2 + tests/auto/concurrent/qfuture/tst_qfuture.cpp | 22 +-- .../qtconcurrentmap/tst_qtconcurrentmap.cpp | 4 +- .../qtconcurrentrun/tst_qtconcurrentrun.cpp | 8 +- .../tst_qtconcurrentthreadengine.cpp | 14 +- 20 files changed, 455 insertions(+), 397 deletions(-) delete mode 100644 src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentexception.cpp delete mode 100644 src/concurrent/qtconcurrentexception.cpp create mode 100644 src/corelib/doc/snippets/code/src_corelib_thread_qexception.cpp create mode 100644 src/corelib/thread/qexception.cpp create mode 100644 src/corelib/thread/qexception.h diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index 5f2781c1b6..b5f63c3600 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -563,6 +563,9 @@ QtConcurrent QT += concurrent to include the new library. +* QtConcurrent::Exception has been renamed to QException, and is still in QtCore. + Ditto QtConcurrent::UnhandledException. + QtOpenGL -------- diff --git a/src/concurrent/concurrent.pro b/src/concurrent/concurrent.pro index 190da83e9c..772146f682 100644 --- a/src/concurrent/concurrent.pro +++ b/src/concurrent/concurrent.pro @@ -21,7 +21,6 @@ SOURCES += \ qtconcurrentresultstore.cpp \ qtconcurrentthreadengine.cpp \ qtconcurrentiteratekernel.cpp \ - qtconcurrentexception.cpp HEADERS += \ qtconcurrent_global.h \ diff --git a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentexception.cpp b/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentexception.cpp deleted file mode 100644 index 002d669493..0000000000 --- a/src/concurrent/doc/snippets/code/src_concurrent_qtconcurrentexception.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names -** of its contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -//! [0] - -class MyException : public QtConcurrent::Exception -{ -public: - void raise() const { throw *this; } - Exception *clone() const { return new MyException(*this); } -}; - -//! [0] - - -//! [1] - -try { - QtConcurrent::blockingMap(list, throwFunction); // throwFunction throws MyException -} catch (MyException &e) { - // handle exception -} - -//! [1] - - -//! [2] - -void MyException::raise() const { throw *this; } - -//! [2] - - -//! [3] - -MyException *MyException::clone() const { return new MyException(*this); } - -//! [3] diff --git a/src/concurrent/qfutureinterface.cpp b/src/concurrent/qfutureinterface.cpp index 5fc08c9366..1f675525a5 100644 --- a/src/concurrent/qfutureinterface.cpp +++ b/src/concurrent/qfutureinterface.cpp @@ -238,7 +238,7 @@ void QFutureInterfaceBase::reportCanceled() } #ifndef QT_NO_EXCEPTIONS -void QFutureInterfaceBase::reportException(const QtConcurrent::Exception &exception) +void QFutureInterfaceBase::reportException(const QException &exception) { QMutexLocker locker(&d->m_mutex); if ((d->state & Canceled) || (d->state & Finished)) @@ -390,7 +390,7 @@ QMutex *QFutureInterfaceBase::mutex() const return &d->m_mutex; } -QtConcurrent::internal::ExceptionStore &QFutureInterfaceBase::exceptionStore() +QtPrivate::ExceptionStore &QFutureInterfaceBase::exceptionStore() { return d->m_exceptionStore; } diff --git a/src/concurrent/qfutureinterface.h b/src/concurrent/qfutureinterface.h index 55112ab68d..615b9fbe6a 100644 --- a/src/concurrent/qfutureinterface.h +++ b/src/concurrent/qfutureinterface.h @@ -49,7 +49,7 @@ #ifndef QT_NO_QFUTURE #include -#include +#include #include QT_BEGIN_HEADER @@ -83,7 +83,7 @@ public: void reportFinished(); void reportCanceled(); #ifndef QT_NO_EXCEPTIONS - void reportException(const QtConcurrent::Exception &e); + void reportException(const QException &e); #endif void reportResultsReady(int beginIndex, int endIndex); @@ -122,7 +122,7 @@ public: void waitForResume(); QMutex *mutex() const; - QtConcurrent::internal::ExceptionStore &exceptionStore(); + QtPrivate::ExceptionStore &exceptionStore(); QtConcurrent::ResultStoreBase &resultStoreBase(); const QtConcurrent::ResultStoreBase &resultStoreBase() const; diff --git a/src/concurrent/qfutureinterface_p.h b/src/concurrent/qfutureinterface_p.h index e4c5d369aa..734584adad 100644 --- a/src/concurrent/qfutureinterface_p.h +++ b/src/concurrent/qfutureinterface_p.h @@ -145,7 +145,7 @@ public: QtConcurrent::ResultStoreBase m_results; bool manualProgress; int m_expectedResultCount; - QtConcurrent::internal::ExceptionStore m_exceptionStore; + QtPrivate::ExceptionStore m_exceptionStore; QString m_progressText; QRunnable *runnable; diff --git a/src/concurrent/qtconcurrentexception.cpp b/src/concurrent/qtconcurrentexception.cpp deleted file mode 100644 index 82fa5ab243..0000000000 --- a/src/concurrent/qtconcurrentexception.cpp +++ /dev/null @@ -1,211 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qtconcurrentexception.h" -#include "QtCore/qshareddata.h" - -#ifndef QT_NO_QFUTURE -#ifndef QT_NO_EXCEPTIONS - -QT_BEGIN_NAMESPACE - -/*! - \class QtConcurrent::Exception - \brief The Exception class provides a base class for exceptions that can transferred across threads. - \since 4.4 - - Qt Concurrent supports throwing and catching exceptions across thread - boundaries, provided that the exception inherit from QtConcurrent::Exception - and implement two helper functions: - - \snippet code/src_concurrent_qtconcurrentexception.cpp 0 - - QtConcurrent::Exception subclasses must be thrown by value and - caught by reference: - - \snippet code/src_concurrent_qtconcurrentexception.cpp 1 - - If you throw an exception that is not a subclass of QtConcurrent::Exception, - the Qt Concurrent functions will throw a QtConcurrent::UnhandledException - in the receiver thread. - - When using QFuture, transferred exceptions will be thrown when calling the following functions: - \list - \li QFuture::waitForFinished() - \li QFuture::result() - \li QFuture::resultAt() - \li QFuture::results() - \endlist -*/ - -/*! - \fn QtConcurrent::Exception::raise() const - In your QtConcurrent::Exception subclass, reimplement raise() like this: - - \snippet code/src_concurrent_qtconcurrentexception.cpp 2 -*/ - -/*! - \fn QtConcurrent::Exception::clone() const - In your QtConcurrent::Exception subclass, reimplement clone() like this: - - \snippet code/src_concurrent_qtconcurrentexception.cpp 3 -*/ - -/*! - \class QtConcurrent::UnhandledException - - \brief The UnhandledException class represents an unhandled exception in a worker thread. - \since 4.4 - - If a worker thread throws an exception that is not a subclass of QtConcurrent::Exception, - the Qt Concurrent functions will throw a QtConcurrent::UnhandledException - on the receiver thread side. - - Inheriting from this class is not supported. -*/ - -/*! - \fn QtConcurrent::UnhandledException::raise() const - \internal -*/ - -/*! - \fn QtConcurrent::UnhandledException::clone() const - \internal -*/ - -namespace QtConcurrent -{ - -void Exception::raise() const -{ - Exception e = *this; - throw e; -} - -Exception *Exception::clone() const -{ - return new Exception(*this); -} - -void UnhandledException::raise() const -{ - UnhandledException e = *this; - throw e; -} - -Exception *UnhandledException::clone() const -{ - return new UnhandledException(*this); -} - -#ifndef qdoc - -namespace internal { - -class Base : public QSharedData -{ -public: - Base(Exception *exception) - : exception(exception), hasThrown(false) { } - ~Base() { delete exception; } - - Exception *exception; - bool hasThrown; -}; - -ExceptionHolder::ExceptionHolder(Exception *exception) -: base(new Base(exception)) {} - -ExceptionHolder::ExceptionHolder(const ExceptionHolder &other) -: base(other.base) -{} - -void ExceptionHolder::operator=(const ExceptionHolder &other) -{ - base = other.base; -} - -ExceptionHolder::~ExceptionHolder() -{} - -Exception *ExceptionHolder::exception() const -{ - return base->exception; -} - -void ExceptionStore::setException(const Exception &e) -{ - if (hasException() == false) - exceptionHolder = ExceptionHolder(e.clone()); -} - -bool ExceptionStore::hasException() const -{ - return (exceptionHolder.exception() != 0); -} - -ExceptionHolder ExceptionStore::exception() -{ - return exceptionHolder; -} - -void ExceptionStore::throwPossibleException() -{ - if (hasException() ) { - exceptionHolder.base->hasThrown = true; - exceptionHolder.exception()->raise(); - } -} - -bool ExceptionStore::hasThrown() const { return exceptionHolder.base->hasThrown; } - -} // namespace internal - -#endif //qdoc - -} // namespace QtConcurrent - -QT_END_NAMESPACE - -#endif // QT_NO_EXCEPTIONS -#endif // QT_NO_CONCURRENT diff --git a/src/concurrent/qtconcurrentexception.h b/src/concurrent/qtconcurrentexception.h index 0c86f08d75..373d42ee64 100644 --- a/src/concurrent/qtconcurrentexception.h +++ b/src/concurrent/qtconcurrentexception.h @@ -46,12 +46,7 @@ #ifndef QT_NO_QFUTURE -#include -#include - -#ifndef QT_NO_EXCEPTIONS -# include -#endif +#include QT_BEGIN_HEADER QT_BEGIN_NAMESPACE @@ -62,59 +57,8 @@ namespace QtConcurrent #ifndef QT_NO_EXCEPTIONS -class Q_CONCURRENT_EXPORT Exception : public std::exception -{ -public: - virtual void raise() const; - virtual Exception *clone() const; -}; - -class Q_CONCURRENT_EXPORT UnhandledException : public Exception -{ -public: - void raise() const; - Exception *clone() const; -}; - -namespace internal { - -class Base; -class ExceptionHolder -{ -public: - ExceptionHolder(Exception *exception = 0); - ExceptionHolder(const ExceptionHolder &other); - void operator=(const ExceptionHolder &other); - ~ExceptionHolder(); - Exception *exception() const; - QExplicitlySharedDataPointer base; -}; - -class Q_CONCURRENT_EXPORT ExceptionStore -{ -public: - void setException(const Exception &e); - bool hasException() const; - ExceptionHolder exception(); - void throwPossibleException(); - bool hasThrown() const; - ExceptionHolder exceptionHolder; -}; - -} // namespace internal - -#else // QT_NO_EXCEPTIONS - -namespace internal { - -class Q_CONCURRENT_EXPORT ExceptionStore -{ -public: - ExceptionStore() { } - inline void throwPossibleException() const {} -}; - -} // namespace internal +typedef Q_DECL_DEPRECATED QException Exception; +typedef Q_DECL_DEPRECATED QUnhandledException UnhandledException; #endif @@ -123,6 +67,6 @@ public: QT_END_NAMESPACE QT_END_HEADER -#endif // QT_NO_CONCURRENT +#endif // QT_NO_QFUTURE #endif diff --git a/src/concurrent/qtconcurrentmap.cpp b/src/concurrent/qtconcurrentmap.cpp index b26930cf0c..f1e46177eb 100644 --- a/src/concurrent/qtconcurrentmap.cpp +++ b/src/concurrent/qtconcurrentmap.cpp @@ -54,14 +54,6 @@ \ingroup thread */ -/*! - \namespace QtConcurrent::internal - \internal - - \brief The QtConcurrent::internal namespace contains QtConcurrent - implementation details. -*/ - /*! \enum QtConcurrent::ReduceOption This enum specifies the order of which results from the map or filter diff --git a/src/concurrent/qtconcurrentrunbase.h b/src/concurrent/qtconcurrentrunbase.h index 02ce30a7f5..897345f239 100644 --- a/src/concurrent/qtconcurrentrunbase.h +++ b/src/concurrent/qtconcurrentrunbase.h @@ -104,10 +104,10 @@ public: #endif this->runFunctor(); #ifndef QT_NO_EXCEPTIONS - } catch (QtConcurrent::Exception &e) { + } catch (QException &e) { QFutureInterface::reportException(e); } catch (...) { - QFutureInterface::reportException(QtConcurrent::UnhandledException()); + QFutureInterface::reportException(QUnhandledException()); } #endif @@ -132,10 +132,10 @@ public: #endif this->runFunctor(); #ifndef QT_NO_EXCEPTIONS - } catch (QtConcurrent::Exception &e) { + } catch (QException &e) { QFutureInterface::reportException(e); } catch (...) { - QFutureInterface::reportException(QtConcurrent::UnhandledException()); + QFutureInterface::reportException(QUnhandledException()); } #endif this->reportFinished(); diff --git a/src/concurrent/qtconcurrentthreadengine.cpp b/src/concurrent/qtconcurrentthreadengine.cpp index 886fb09ea6..56592965fd 100644 --- a/src/concurrent/qtconcurrentthreadengine.cpp +++ b/src/concurrent/qtconcurrentthreadengine.cpp @@ -155,10 +155,10 @@ void ThreadEngineBase::startBlocking() } } #ifndef QT_NO_EXCEPTIONS - } catch (QtConcurrent::Exception &e) { + } catch (QException &e) { handleException(e); } catch (...) { - handleException(QtConcurrent::UnhandledException()); + handleException(QUnhandledException()); } #endif @@ -271,10 +271,10 @@ void ThreadEngineBase::run() // implements QRunnable. } #ifndef QT_NO_EXCEPTIONS - } catch (QtConcurrent::Exception &e) { + } catch (QException &e) { handleException(e); } catch (...) { - handleException(QtConcurrent::UnhandledException()); + handleException(QUnhandledException()); } #endif threadExit(); @@ -282,7 +282,7 @@ void ThreadEngineBase::run() // implements QRunnable. #ifndef QT_NO_EXCEPTIONS -void ThreadEngineBase::handleException(const QtConcurrent::Exception &exception) +void ThreadEngineBase::handleException(const QException &exception) { if (futureInterface) futureInterface->reportException(exception); diff --git a/src/concurrent/qtconcurrentthreadengine.h b/src/concurrent/qtconcurrentthreadengine.h index 971348be97..2a5187502f 100644 --- a/src/concurrent/qtconcurrentthreadengine.h +++ b/src/concurrent/qtconcurrentthreadengine.h @@ -49,7 +49,7 @@ #include #include #include -#include +#include #include #include #include @@ -119,13 +119,13 @@ private: void run(); virtual void asynchronousFinish() = 0; #ifndef QT_NO_EXCEPTIONS - void handleException(const QtConcurrent::Exception &exception); + void handleException(const QException &exception); #endif protected: QFutureInterfaceBase *futureInterface; QThreadPool *threadPool; ThreadEngineBarrier barrier; - QtConcurrent::internal::ExceptionStore exceptionStore; + QtPrivate::ExceptionStore exceptionStore; }; diff --git a/src/corelib/doc/snippets/code/src_corelib_thread_qexception.cpp b/src/corelib/doc/snippets/code/src_corelib_thread_qexception.cpp new file mode 100644 index 0000000000..d2bc6363ed --- /dev/null +++ b/src/corelib/doc/snippets/code/src_corelib_thread_qexception.cpp @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] + +class MyException : public QException +{ +public: + void raise() const { throw *this; } + MyException *clone() const { return new MyException(*this); } +}; + +//! [0] + + +//! [1] + +try { + QtConcurrent::blockingMap(list, throwFunction); // throwFunction throws MyException +} catch (MyException &e) { + // handle exception +} + +//! [1] + + +//! [2] + +void MyException::raise() const { throw *this; } + +//! [2] + + +//! [3] + +MyException *MyException::clone() const { return new MyException(*this); } + +//! [3] diff --git a/src/corelib/thread/qexception.cpp b/src/corelib/thread/qexception.cpp new file mode 100644 index 0000000000..090cbf4e85 --- /dev/null +++ b/src/corelib/thread/qexception.cpp @@ -0,0 +1,206 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qexception.h" +#include "QtCore/qshareddata.h" + +#ifndef QT_NO_QFUTURE +#ifndef QT_NO_EXCEPTIONS + +QT_BEGIN_NAMESPACE + +/*! + \class QException + \brief The QException class provides a base class for exceptions that can transferred across threads. + \since 5.0 + + Qt Concurrent supports throwing and catching exceptions across thread + boundaries, provided that the exception inherit from QException + and implement two helper functions: + + \snippet code/src_corelib_thread_qexception.cpp 0 + + QException subclasses must be thrown by value and + caught by reference: + + \snippet code/src_corelib_thread_qexception.cpp 1 + + If you throw an exception that is not a subclass of QException, + the Qt functions will throw a QUnhandledException + in the receiver thread. + + When using QFuture, transferred exceptions will be thrown when calling the following functions: + \list + \li QFuture::waitForFinished() + \li QFuture::result() + \li QFuture::resultAt() + \li QFuture::results() + \endlist +*/ + +/*! + \fn QException::raise() const + In your QException subclass, reimplement raise() like this: + + \snippet code/src_corelib_thread_qexception.cpp 2 +*/ + +/*! + \fn QException::clone() const + In your QException subclass, reimplement clone() like this: + + \snippet code/src_corelib_thread_qexception.cpp 3 +*/ + +/*! + \class QUnhandledException + + \brief The UnhandledException class represents an unhandled exception in a worker thread. + \since 5.0 + + If a worker thread throws an exception that is not a subclass of QException, + the Qt functions will throw a QUnhandledException + on the receiver thread side. + + Inheriting from this class is not supported. +*/ + +/*! + \fn QUnhandledException::raise() const + \internal +*/ + +/*! + \fn QUnhandledException::clone() const + \internal +*/ + +void QException::raise() const +{ + QException e = *this; + throw e; +} + +QException *QException::clone() const +{ + return new QException(*this); +} + +void QUnhandledException::raise() const +{ + QUnhandledException e = *this; + throw e; +} + +QUnhandledException *QUnhandledException::clone() const +{ + return new QUnhandledException(*this); +} + +#ifndef qdoc + +namespace QtPrivate { + +class Base : public QSharedData +{ +public: + Base(QException *exception) + : exception(exception), hasThrown(false) { } + ~Base() { delete exception; } + + QException *exception; + bool hasThrown; +}; + +ExceptionHolder::ExceptionHolder(QException *exception) +: base(new Base(exception)) {} + +ExceptionHolder::ExceptionHolder(const ExceptionHolder &other) +: base(other.base) +{} + +void ExceptionHolder::operator=(const ExceptionHolder &other) +{ + base = other.base; +} + +ExceptionHolder::~ExceptionHolder() +{} + +QException *ExceptionHolder::exception() const +{ + return base->exception; +} + +void ExceptionStore::setException(const QException &e) +{ + if (hasException() == false) + exceptionHolder = ExceptionHolder(e.clone()); +} + +bool ExceptionStore::hasException() const +{ + return (exceptionHolder.exception() != 0); +} + +ExceptionHolder ExceptionStore::exception() +{ + return exceptionHolder; +} + +void ExceptionStore::throwPossibleException() +{ + if (hasException() ) { + exceptionHolder.base->hasThrown = true; + exceptionHolder.exception()->raise(); + } +} + +bool ExceptionStore::hasThrown() const { return exceptionHolder.base->hasThrown; } + +} // namespace QtPrivate + +#endif //qdoc + +QT_END_NAMESPACE + +#endif // QT_NO_EXCEPTIONS +#endif // QT_NO_QFUTURE diff --git a/src/corelib/thread/qexception.h b/src/corelib/thread/qexception.h new file mode 100644 index 0000000000..7106bb57df --- /dev/null +++ b/src/corelib/thread/qexception.h @@ -0,0 +1,123 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTCORE_QEXCEPTION_H +#define QTCORE_QEXCEPTION_H + +#include + +#ifndef QT_NO_QFUTURE + +#include +#include + +#ifndef QT_NO_EXCEPTIONS +# include +#endif + +QT_BEGIN_HEADER +QT_BEGIN_NAMESPACE + + +#ifndef QT_NO_EXCEPTIONS + +class Q_CORE_EXPORT QException : public std::exception +{ +public: + virtual void raise() const; + virtual QException *clone() const; +}; + +class Q_CORE_EXPORT QUnhandledException : public QException +{ +public: + void raise() const; + QUnhandledException *clone() const; +}; + +namespace QtPrivate { + +class Base; +class Q_CORE_EXPORT ExceptionHolder +{ +public: + ExceptionHolder(QException *exception = 0); + ExceptionHolder(const ExceptionHolder &other); + void operator=(const ExceptionHolder &other); + ~ExceptionHolder(); + QException *exception() const; + QExplicitlySharedDataPointer base; +}; + +class Q_CORE_EXPORT ExceptionStore +{ +public: + void setException(const QException &e); + bool hasException() const; + ExceptionHolder exception(); + void throwPossibleException(); + bool hasThrown() const; + ExceptionHolder exceptionHolder; +}; + +} // namespace QtPrivate + +#else // QT_NO_EXCEPTIONS + +namespace QtPrivate { + +class Q_CORE_EXPORT ExceptionStore +{ +public: + ExceptionStore() { } + inline void throwPossibleException() const {} +}; + +} // namespace QtPrivate + +#endif // QT_NO_EXCEPTIONS + +QT_END_NAMESPACE +QT_END_HEADER + +#endif // QT_NO_QFUTURE + +#endif diff --git a/src/corelib/thread/thread.pri b/src/corelib/thread/thread.pri index 7f383d6a4c..215e56628c 100644 --- a/src/corelib/thread/thread.pri +++ b/src/corelib/thread/thread.pri @@ -10,6 +10,7 @@ HEADERS += thread/qmutex.h \ thread/qthreadstorage.h \ thread/qwaitcondition.h \ thread/qatomic.h \ + thread/qexception.h \ thread/qbasicatomic.h \ thread/qgenericatomic.h \ thread/qoldbasicatomic.h @@ -23,6 +24,7 @@ HEADERS += thread/qmutex_p.h \ thread/qthreadpool_p.h SOURCES += thread/qatomic.cpp \ + thread/qexception.cpp \ thread/qmutex.cpp \ thread/qreadwritelock.cpp \ thread/qrunnable.cpp \ diff --git a/tests/auto/concurrent/qfuture/tst_qfuture.cpp b/tests/auto/concurrent/qfuture/tst_qfuture.cpp index 4e681e79ae..aea35e80b2 100644 --- a/tests/auto/concurrent/qfuture/tst_qfuture.cpp +++ b/tests/auto/concurrent/qfuture/tst_qfuture.cpp @@ -1276,7 +1276,7 @@ QFuture createExceptionFuture() i.reportStarted(); QFuture f = i.future(); - Exception e; + QException e; i.reportException(e); i.reportFinished(); return f; @@ -1290,17 +1290,17 @@ QFuture createExceptionResultFuture() int r = 0; i.reportResult(r); - Exception e; + QException e; i.reportException(e); i.reportFinished(); return f; } -class DerivedException : public Exception +class DerivedException : public QException { public: void raise() const { throw *this; } - Exception *clone() const { return new DerivedException(*this); } + DerivedException *clone() const { return new DerivedException(*this); } }; QFuture createDerivedExceptionFuture() @@ -1323,7 +1323,7 @@ void tst_QFuture::exceptions() bool caught = false; try { f.waitForFinished(); - } catch (Exception &) { + } catch (QException &) { caught = true; } QVERIFY(caught); @@ -1335,7 +1335,7 @@ void tst_QFuture::exceptions() bool caught = false; try { f.result(); - } catch (Exception &) { + } catch (QException &) { caught = true; } QVERIFY(caught); @@ -1346,7 +1346,7 @@ void tst_QFuture::exceptions() bool caught = false; try { createExceptionResultFuture().result(); - } catch (Exception &) { + } catch (QException &) { caught = true; } QVERIFY(caught); @@ -1358,7 +1358,7 @@ void tst_QFuture::exceptions() bool caught = false; try { f.results(); - } catch (Exception &) { + } catch (QException &) { caught = true; } QVERIFY(caught); @@ -1373,7 +1373,7 @@ void tst_QFuture::exceptions() Q_UNUSED(e); QFAIL("did not get exception"); } - } catch (Exception &) { + } catch (QException &) { caught = true; } QVERIFY(caught); @@ -1384,7 +1384,7 @@ void tst_QFuture::exceptions() bool caught = false; try { createDerivedExceptionFuture().waitForFinished(); - } catch (Exception &) { + } catch (QException &) { caught = true; } QVERIFY(caught); @@ -1409,7 +1409,7 @@ public: QFuture f = createExceptionFuture(); try { f.waitForFinished(); - } catch (Exception &) { + } catch (QException &) { caught = true; } } diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp index 48837d3ee0..186ee826cf 100644 --- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp +++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp @@ -2205,7 +2205,7 @@ void tst_QtConcurrentMap::throttling() void throwMapper(int &e) { Q_UNUSED(e); - throw QtConcurrent::Exception(); + throw QException(); } void tst_QtConcurrentMap::exceptions() @@ -2214,7 +2214,7 @@ void tst_QtConcurrentMap::exceptions() try { QList list = QList() << 1 << 2 << 3; QtConcurrent::map(list, throwMapper).waitForFinished(); - } catch (const Exception &) { + } catch (const QException &) { caught = true; } if (!caught) diff --git a/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp b/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp index af63c25240..f3f4be3f3f 100644 --- a/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp +++ b/tests/auto/concurrent/qtconcurrentrun/tst_qtconcurrentrun.cpp @@ -367,12 +367,12 @@ int fn2(double, int *) #ifndef QT_NO_EXCEPTIONS void throwFunction() { - throw QtConcurrent::Exception(); + throw QException(); } int throwFunctionReturn() { - throw QtConcurrent::Exception(); + throw QException(); return 0; } @@ -381,7 +381,7 @@ void tst_QtConcurrentRun::exceptions() bool caught = false; try { QtConcurrent::run(throwFunction).waitForFinished(); - } catch (Exception &) { + } catch (QException &) { caught = true; } if (!caught) @@ -390,7 +390,7 @@ void tst_QtConcurrentRun::exceptions() caught = false; try { QtConcurrent::run(throwFunctionReturn).waitForFinished(); - } catch (Exception &) { + } catch (QException &) { caught = true; } if (!caught) diff --git a/tests/auto/concurrent/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp b/tests/auto/concurrent/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp index d2e4c74be8..edf9104571 100644 --- a/tests/auto/concurrent/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp +++ b/tests/auto/concurrent/qtconcurrentthreadengine/tst_qtconcurrentthreadengine.cpp @@ -425,7 +425,7 @@ public: ThreadFunctionResult threadFunction() { QTest::qSleep(50); - throw QtConcurrent::Exception(); + throw QException(); return ThreadFinished; } QThread *blockThread; @@ -457,7 +457,7 @@ void tst_QtConcurrentThreadEngine::exceptions() QtConcurrentExceptionThrower *e = new QtConcurrentExceptionThrower(); QFuture f = e->startAsynchronously(); f.waitForFinished(); - } catch (const Exception &) { + } catch (const QException &) { caught = true; } QVERIFY2(caught, "did not get exception"); @@ -470,7 +470,7 @@ void tst_QtConcurrentThreadEngine::exceptions() try { QtConcurrentExceptionThrower e(QThread::currentThread()); e.startBlocking(); - } catch (const Exception &) { + } catch (const QException &) { caught = true; } QVERIFY2(caught, "did not get exception"); @@ -482,7 +482,7 @@ void tst_QtConcurrentThreadEngine::exceptions() try { QtConcurrentExceptionThrower e(0); e.startBlocking(); - } catch (const Exception &) { + } catch (const QException &) { caught = true; } QVERIFY2(caught, "did not get exception"); @@ -495,7 +495,7 @@ void tst_QtConcurrentThreadEngine::exceptions() UnrelatedExceptionThrower *e = new UnrelatedExceptionThrower(); QFuture f = e->startAsynchronously(); f.waitForFinished(); - } catch (const QtConcurrent::UnhandledException &) { + } catch (const QUnhandledException &) { caught = true; } QVERIFY2(caught, "did not get exception"); @@ -508,7 +508,7 @@ void tst_QtConcurrentThreadEngine::exceptions() try { UnrelatedExceptionThrower e(QThread::currentThread()); e.startBlocking(); - } catch (const QtConcurrent::UnhandledException &) { + } catch (const QUnhandledException &) { caught = true; } QVERIFY2(caught, "did not get exception"); @@ -520,7 +520,7 @@ void tst_QtConcurrentThreadEngine::exceptions() try { UnrelatedExceptionThrower e(0); e.startBlocking(); - } catch (const QtConcurrent::UnhandledException &) { + } catch (const QUnhandledException &) { caught = true; } QVERIFY2(caught, "did not get exception"); -- cgit v1.2.3