diff --git a/src/libs/utils/runextensions.cpp b/src/libs/utils/runextensions.cpp new file mode 100644 index 00000000000..4fff2612a60 --- /dev/null +++ b/src/libs/utils/runextensions.cpp @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +****************************************************************************/ + +#include "runextensions.h" + +namespace Utils { +namespace Internal { + +RunnableThread::RunnableThread(QRunnable *runnable, QObject *parent) + : QThread(parent), + m_runnable(runnable) +{ +} + +void RunnableThread::run() +{ + m_runnable->run(); + if (m_runnable->autoDelete()) + delete m_runnable; + deleteLater(); +} + +} // Internal +} // Utils diff --git a/src/libs/utils/runextensions.h b/src/libs/utils/runextensions.h index 433ac523a9f..775d61af30f 100644 --- a/src/libs/utils/runextensions.h +++ b/src/libs/utils/runextensions.h @@ -27,11 +27,13 @@ #define RUNEXTENSIONS_H #include "qtcassert.h" +#include "utils_global.h" #include #include #include #include +#include #include #include @@ -618,6 +620,18 @@ private: QThread::Priority priority = QThread::InheritPriority; }; +class QTCREATOR_UTILS_EXPORT RunnableThread : public QThread +{ +public: + explicit RunnableThread(QRunnable *runnable, QObject *parent = 0); + +protected: + void run(); + +private: + QRunnable *m_runnable; +}; + } // Internal template ::type> QFuture runAsync(Function &&function, Args&&... args) { - QFutureInterface futureInterface; - futureInterface.reportStarted(); - std::thread(Internal::runAsyncImpl::type,typename std::decay::type...>, - futureInterface, std::forward(function), std::forward(args)...).detach(); - return futureInterface.future(); + auto job = new Internal::AsyncJob + (std::forward(function), std::forward(args)...); + QFuture future = job->future(); + (new Internal::RunnableThread(job))->start(); // automatically deletes itself + return future; } template diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index 015cc32b99b..6057b116410 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -99,7 +99,8 @@ SOURCES += $$PWD/environment.cpp \ $$PWD/categorysortfiltermodel.cpp \ $$PWD/dropsupport.cpp \ $$PWD/icon.cpp \ - $$PWD/port.cpp + $$PWD/port.cpp \ + $$PWD/runextensions.cpp win32:SOURCES += $$PWD/consoleprocess_win.cpp else:SOURCES += $$PWD/consoleprocess_unix.cpp diff --git a/src/libs/utils/utils.qbs b/src/libs/utils/utils.qbs index 3978ff127b6..13473050e26 100644 --- a/src/libs/utils/utils.qbs +++ b/src/libs/utils/utils.qbs @@ -166,6 +166,7 @@ QtcLibrary { "qtcprocess.h", "reloadpromptutils.cpp", "reloadpromptutils.h", + "runextensions.cpp", "runextensions.h", "savedaction.cpp", "savedaction.h", diff --git a/tests/auto/runextensions/runextensions.pro b/tests/auto/runextensions/runextensions.pro index 9aa9bb4a971..ad6bae4e3b3 100644 --- a/tests/auto/runextensions/runextensions.pro +++ b/tests/auto/runextensions/runextensions.pro @@ -1,3 +1,4 @@ +QTC_LIB_DEPENDS = utils include(../qttest.pri) # Input diff --git a/tests/auto/runextensions/runextensions.qbs b/tests/auto/runextensions/runextensions.qbs index c63b71f8234..26e6bf5d84a 100644 --- a/tests/auto/runextensions/runextensions.qbs +++ b/tests/auto/runextensions/runextensions.qbs @@ -2,7 +2,7 @@ import qbs QtcAutotest { name: "Run extensions autotest" - cpp.includePaths: base.concat(project.ide_source_tree + "/src/libs") + Depends { name: "Utils" } files: [ "tst_runextensions.cpp",