2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2016-01-29 16:17:26 +01:00
|
|
|
#include "functiontraits.h"
|
2018-06-01 15:58:34 +02:00
|
|
|
#include "optional.h"
|
2016-01-28 09:54:39 +01:00
|
|
|
#include "utils_global.h"
|
2015-12-16 14:58:04 +01:00
|
|
|
|
2016-01-27 15:44:31 +01:00
|
|
|
#include <QCoreApplication>
|
2016-01-26 16:48:21 +01:00
|
|
|
#include <QFuture>
|
|
|
|
|
#include <QFutureInterface>
|
2016-10-27 16:44:45 +02:00
|
|
|
#include <QFutureWatcher>
|
2016-01-26 16:48:21 +01:00
|
|
|
#include <QRunnable>
|
2016-01-28 09:54:39 +01:00
|
|
|
#include <QThread>
|
2016-01-26 16:48:21 +01:00
|
|
|
#include <QThreadPool>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-09-04 11:55:36 +02:00
|
|
|
#include <functional>
|
|
|
|
|
|
2016-04-05 10:11:48 +02:00
|
|
|
// hasCallOperator & Co must be outside of any namespace
|
|
|
|
|
// because of internal compiler error with MSVC2015 Update 2
|
|
|
|
|
|
|
|
|
|
using testCallOperatorYes = char;
|
|
|
|
|
using testCallOperatorNo = struct { char foo[2]; };
|
|
|
|
|
|
|
|
|
|
template<typename C>
|
|
|
|
|
static testCallOperatorYes testCallOperator(decltype(&C::operator()));
|
|
|
|
|
|
|
|
|
|
template<typename>
|
|
|
|
|
static testCallOperatorNo testCallOperator(...);
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct hasCallOperator
|
|
|
|
|
{
|
2018-05-07 17:33:02 +02:00
|
|
|
static const bool value = (sizeof(testCallOperator<T>(nullptr)) == sizeof(testCallOperatorYes));
|
2016-04-05 10:11:48 +02:00
|
|
|
};
|
|
|
|
|
|
2015-12-16 14:58:04 +01:00
|
|
|
namespace Utils {
|
2018-06-01 15:58:34 +02:00
|
|
|
|
2018-07-17 23:21:05 +03:00
|
|
|
using StackSizeInBytes = optional<uint>;
|
2018-06-01 15:58:34 +02:00
|
|
|
|
2015-12-16 14:58:04 +01:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2016-02-08 16:26:19 +01:00
|
|
|
/*
|
|
|
|
|
resultType<F>::type
|
|
|
|
|
|
|
|
|
|
Returns the type of results that would be reported by a callable of type F
|
|
|
|
|
when called through the runAsync methods. I.e. the ResultType in
|
|
|
|
|
|
|
|
|
|
void f(QFutureInterface<Result> &fi, ...)
|
|
|
|
|
ResultType f(...)
|
|
|
|
|
|
|
|
|
|
Returns void if F is not callable, and if F is a callable that does not take
|
|
|
|
|
a QFutureInterface& as its first parameter and returns void.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultType;
|
|
|
|
|
|
|
|
|
|
template <typename Function, typename Arg>
|
|
|
|
|
struct resultTypeWithArgument;
|
|
|
|
|
|
|
|
|
|
template <typename Function, int index, bool>
|
|
|
|
|
struct resultTypeTakesArguments;
|
|
|
|
|
|
|
|
|
|
template <typename Function, bool>
|
|
|
|
|
struct resultTypeIsMemberFunction;
|
|
|
|
|
|
|
|
|
|
template <typename Function, bool>
|
|
|
|
|
struct resultTypeIsFunctionLike;
|
|
|
|
|
|
|
|
|
|
template <typename Function, bool>
|
|
|
|
|
struct resultTypeHasCallOperator;
|
|
|
|
|
|
|
|
|
|
template <typename Function, typename ResultType>
|
|
|
|
|
struct resultTypeWithArgument<Function, QFutureInterface<ResultType>&>
|
|
|
|
|
{
|
|
|
|
|
using type = ResultType;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function, typename Arg>
|
|
|
|
|
struct resultTypeWithArgument
|
|
|
|
|
{
|
2016-07-07 11:28:24 +02:00
|
|
|
using type = functionResult_t<Function>;
|
2016-02-08 16:26:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function, int index>
|
|
|
|
|
struct resultTypeTakesArguments<Function, index, true>
|
|
|
|
|
: public resultTypeWithArgument<Function, typename functionTraits<Function>::template argument<index>::type>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function, int index>
|
|
|
|
|
struct resultTypeTakesArguments<Function, index, false>
|
|
|
|
|
{
|
2016-07-07 11:28:24 +02:00
|
|
|
using type = functionResult_t<Function>;
|
2016-02-08 16:26:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultTypeIsFunctionLike<Function, true>
|
|
|
|
|
: public resultTypeTakesArguments<Function, 0, (functionTraits<Function>::arity > 0)>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultTypeIsMemberFunction<Function, true>
|
|
|
|
|
: public resultTypeTakesArguments<Function, 1, (functionTraits<Function>::arity > 1)>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultTypeIsMemberFunction<Function, false>
|
|
|
|
|
{
|
|
|
|
|
using type = void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultTypeIsFunctionLike<Function, false>
|
|
|
|
|
: public resultTypeIsMemberFunction<Function, std::is_member_function_pointer<Function>::value>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultTypeHasCallOperator<Function, false>
|
2017-01-11 17:04:23 +01:00
|
|
|
: public resultTypeIsFunctionLike<Function, std::is_function<std::remove_pointer_t<std::decay_t<Function>>>::value>
|
2016-02-08 16:26:19 +01:00
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Callable>
|
|
|
|
|
struct resultTypeHasCallOperator<Callable, true>
|
2016-02-25 11:38:14 +01:00
|
|
|
: public resultTypeTakesArguments<Callable, 0, (functionTraits<Callable>::arity > 0)>
|
2016-02-08 16:26:19 +01:00
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultType
|
|
|
|
|
: public resultTypeHasCallOperator<Function, hasCallOperator<Function>::value>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultType<Function&> : public resultType<Function>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultType<const Function&> : public resultType<Function>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultType<Function &&> : public resultType<Function>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-25 11:38:14 +01:00
|
|
|
template <typename Function>
|
|
|
|
|
struct resultType<std::reference_wrapper<Function>> : public resultType<Function>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
template <typename Function>
|
|
|
|
|
struct resultType<std::reference_wrapper<const Function>> : public resultType<Function>
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-08 16:26:19 +01:00
|
|
|
/*
|
|
|
|
|
Callable object that wraps a member function pointer with the object it
|
|
|
|
|
will be called on.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-01-29 16:17:26 +01:00
|
|
|
template <typename Function>
|
|
|
|
|
class MemberCallable;
|
|
|
|
|
|
|
|
|
|
template <typename Result, typename Obj, typename... Args>
|
|
|
|
|
class MemberCallable<Result(Obj::*)(Args...) const>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
MemberCallable(Result(Obj::* function)(Args...) const, const Obj *obj)
|
|
|
|
|
: m_function(function),
|
|
|
|
|
m_obj(obj)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result operator()(Args&&... args) const
|
|
|
|
|
{
|
|
|
|
|
return ((*m_obj).*m_function)(std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Result(Obj::* m_function)(Args...) const;
|
|
|
|
|
const Obj *m_obj;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Result, typename Obj, typename... Args>
|
|
|
|
|
class MemberCallable<Result(Obj::*)(Args...)>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
MemberCallable(Result(Obj::* function)(Args...), Obj *obj)
|
|
|
|
|
: m_function(function),
|
|
|
|
|
m_obj(obj)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result operator()(Args&&... args) const
|
|
|
|
|
{
|
|
|
|
|
return ((*m_obj).*m_function)(std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Result(Obj::* m_function)(Args...);
|
|
|
|
|
Obj *m_obj;
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-08 16:26:19 +01:00
|
|
|
/*
|
|
|
|
|
Helper functions for runAsync that run in the started thread.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-01-29 16:17:26 +01:00
|
|
|
// void function that does not take QFutureInterface
|
|
|
|
|
template <typename ResultType, typename Function, typename... Args>
|
2016-02-08 11:03:28 +01:00
|
|
|
void runAsyncReturnVoidDispatch(std::true_type, QFutureInterface<ResultType>, Function &&function, Args&&... args)
|
2016-01-18 12:48:09 +01:00
|
|
|
{
|
2016-01-29 16:17:26 +01:00
|
|
|
function(std::forward<Args>(args)...);
|
2016-01-18 12:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-29 16:17:26 +01:00
|
|
|
// non-void function that does not take QFutureInterface
|
2016-01-12 14:05:05 +01:00
|
|
|
template <typename ResultType, typename Function, typename... Args>
|
2016-01-29 16:17:26 +01:00
|
|
|
void runAsyncReturnVoidDispatch(std::false_type, QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
futureInterface.reportResult(function(std::forward<Args>(args)...));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// function that takes QFutureInterface
|
|
|
|
|
template <typename ResultType, typename Function, typename... Args>
|
|
|
|
|
void runAsyncQFutureInterfaceDispatch(std::true_type, QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
|
2016-01-12 14:05:05 +01:00
|
|
|
{
|
2016-02-02 08:25:55 +01:00
|
|
|
function(futureInterface, std::forward<Args>(args)...);
|
2016-01-12 14:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-29 16:17:26 +01:00
|
|
|
// function that does not take QFutureInterface
|
|
|
|
|
template <typename ResultType, typename Function, typename... Args>
|
|
|
|
|
void runAsyncQFutureInterfaceDispatch(std::false_type, QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
|
|
|
|
|
{
|
2017-01-11 17:04:23 +01:00
|
|
|
runAsyncReturnVoidDispatch(std::is_void<std::result_of_t<Function(Args...)>>(),
|
2016-01-29 16:17:26 +01:00
|
|
|
futureInterface, std::forward<Function>(function), std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// function, function pointer, or other callable object that is no member pointer
|
|
|
|
|
template <typename ResultType, typename Function, typename... Args,
|
2017-01-11 17:04:23 +01:00
|
|
|
typename = std::enable_if_t<!std::is_member_pointer<std::decay_t<Function>>::value>
|
|
|
|
|
>
|
2016-02-21 17:34:36 +01:00
|
|
|
void runAsyncMemberDispatch(QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
|
2016-01-29 16:17:26 +01:00
|
|
|
{
|
2016-02-25 10:08:07 +01:00
|
|
|
runAsyncQFutureInterfaceDispatch(functionTakesArgument<Function, 0, QFutureInterface<ResultType>&>(),
|
|
|
|
|
futureInterface, std::forward<Function>(function), std::forward<Args>(args)...);
|
2016-01-29 16:17:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Function = member function
|
|
|
|
|
template <typename ResultType, typename Function, typename Obj, typename... Args,
|
2017-01-11 17:04:23 +01:00
|
|
|
typename = std::enable_if_t<std::is_member_pointer<std::decay_t<Function>>::value>
|
|
|
|
|
>
|
2016-02-21 17:34:36 +01:00
|
|
|
void runAsyncMemberDispatch(QFutureInterface<ResultType> futureInterface, Function &&function, Obj &&obj, Args&&... args)
|
2016-01-29 16:17:26 +01:00
|
|
|
{
|
|
|
|
|
// Wrap member function with object into callable
|
|
|
|
|
runAsyncImpl(futureInterface,
|
2017-01-11 17:04:23 +01:00
|
|
|
MemberCallable<std::decay_t<Function>>(std::forward<Function>(function), std::forward<Obj>(obj)),
|
2016-01-29 16:17:26 +01:00
|
|
|
std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-21 17:34:36 +01:00
|
|
|
// cref to function/callable
|
|
|
|
|
template <typename ResultType, typename Function, typename... Args>
|
|
|
|
|
void runAsyncImpl(QFutureInterface<ResultType> futureInterface,
|
|
|
|
|
std::reference_wrapper<Function> functionWrapper, Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
runAsyncMemberDispatch(futureInterface, functionWrapper.get(), std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// function/callable, no cref
|
|
|
|
|
template <typename ResultType, typename Function, typename... Args>
|
|
|
|
|
void runAsyncImpl(QFutureInterface<ResultType> futureInterface,
|
|
|
|
|
Function &&function, Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
runAsyncMemberDispatch(futureInterface, std::forward<Function>(function),
|
|
|
|
|
std::forward<Args>(args)...);
|
|
|
|
|
}
|
2016-02-08 16:26:19 +01:00
|
|
|
/*
|
|
|
|
|
AsyncJob is a QRunnable that wraps a function with the
|
|
|
|
|
arguments that are passed to it when it is run in a thread.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-01-26 16:48:21 +01:00
|
|
|
template <class T>
|
2017-01-11 17:04:23 +01:00
|
|
|
std::decay_t<T>
|
2016-01-26 16:48:21 +01:00
|
|
|
decayCopy(T&& v)
|
|
|
|
|
{
|
|
|
|
|
return std::forward<T>(v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename ResultType, typename Function, typename... Args>
|
|
|
|
|
class AsyncJob : public QRunnable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
AsyncJob(Function &&function, Args&&... args)
|
|
|
|
|
// decay copy like std::thread
|
|
|
|
|
: data(decayCopy(std::forward<Function>(function)), decayCopy(std::forward<Args>(args))...)
|
|
|
|
|
{
|
|
|
|
|
// we need to report it as started even though it isn't yet, because someone might
|
|
|
|
|
// call waitForFinished on the future, which does _not_ block if the future is not started
|
|
|
|
|
futureInterface.setRunnable(this);
|
|
|
|
|
futureInterface.reportStarted();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 15:07:21 +02:00
|
|
|
~AsyncJob() override
|
2016-01-26 16:48:21 +01:00
|
|
|
{
|
|
|
|
|
// QThreadPool can delete runnables even if they were never run (e.g. QThreadPool::clear).
|
|
|
|
|
// Since we reported them as started, we make sure that we always report them as finished.
|
|
|
|
|
// reportFinished only actually sends the signal if it wasn't already finished.
|
|
|
|
|
futureInterface.reportFinished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFuture<ResultType> future() { return futureInterface.future(); }
|
|
|
|
|
|
|
|
|
|
void run() override
|
|
|
|
|
{
|
2016-01-27 15:44:31 +01:00
|
|
|
if (priority != QThread::InheritPriority)
|
|
|
|
|
if (QThread *thread = QThread::currentThread())
|
|
|
|
|
if (thread != qApp->thread())
|
|
|
|
|
thread->setPriority(priority);
|
2016-01-26 16:48:21 +01:00
|
|
|
if (futureInterface.isCanceled()) {
|
|
|
|
|
futureInterface.reportFinished();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-01-13 10:04:07 +01:00
|
|
|
runHelper(std::make_index_sequence<std::tuple_size<Data>::value>());
|
2016-01-26 16:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setThreadPool(QThreadPool *pool)
|
|
|
|
|
{
|
|
|
|
|
futureInterface.setThreadPool(pool);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-27 15:44:31 +01:00
|
|
|
void setThreadPriority(QThread::Priority p)
|
|
|
|
|
{
|
|
|
|
|
priority = p;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 16:48:21 +01:00
|
|
|
private:
|
2017-01-11 17:04:23 +01:00
|
|
|
using Data = std::tuple<std::decay_t<Function>, std::decay_t<Args>...>;
|
2016-01-26 16:48:21 +01:00
|
|
|
|
|
|
|
|
template <std::size_t... index>
|
2017-01-13 10:04:07 +01:00
|
|
|
void runHelper(std::index_sequence<index...>)
|
2016-01-26 16:48:21 +01:00
|
|
|
{
|
|
|
|
|
// invalidates data, which is moved into the call
|
|
|
|
|
runAsyncImpl(futureInterface, std::move(std::get<index>(data))...);
|
2016-02-08 11:03:28 +01:00
|
|
|
if (futureInterface.isPaused())
|
|
|
|
|
futureInterface.waitForResume();
|
|
|
|
|
futureInterface.reportFinished();
|
2016-01-26 16:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Data data;
|
|
|
|
|
QFutureInterface<ResultType> futureInterface;
|
2016-01-27 15:44:31 +01:00
|
|
|
QThread::Priority priority = QThread::InheritPriority;
|
2016-01-26 16:48:21 +01:00
|
|
|
};
|
|
|
|
|
|
2016-01-28 09:54:39 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT RunnableThread : public QThread
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-05-07 17:33:02 +02:00
|
|
|
explicit RunnableThread(QRunnable *runnable, QObject *parent = nullptr);
|
2016-01-28 09:54:39 +01:00
|
|
|
|
|
|
|
|
protected:
|
2018-05-07 15:07:21 +02:00
|
|
|
void run() override;
|
2016-01-28 09:54:39 +01:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QRunnable *m_runnable;
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-01 15:58:34 +02:00
|
|
|
template<typename Function,
|
|
|
|
|
typename... Args,
|
|
|
|
|
typename ResultType = typename Internal::resultType<Function>::type>
|
|
|
|
|
QFuture<ResultType> runAsync_internal(QThreadPool *pool,
|
|
|
|
|
StackSizeInBytes stackSize,
|
|
|
|
|
QThread::Priority priority,
|
|
|
|
|
Function &&function,
|
|
|
|
|
Args &&... args)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(!(pool && stackSize)); // stack size cannot be changed once a thread is started
|
|
|
|
|
auto job = new Internal::AsyncJob<ResultType,Function,Args...>
|
|
|
|
|
(std::forward<Function>(function), std::forward<Args>(args)...);
|
|
|
|
|
job->setThreadPriority(priority);
|
|
|
|
|
QFuture<ResultType> future = job->future();
|
|
|
|
|
if (pool) {
|
|
|
|
|
job->setThreadPool(pool);
|
|
|
|
|
pool->start(job);
|
|
|
|
|
} else {
|
|
|
|
|
auto thread = new Internal::RunnableThread(job);
|
|
|
|
|
if (stackSize)
|
|
|
|
|
thread->setStackSize(stackSize.value());
|
|
|
|
|
thread->moveToThread(qApp->thread()); // make sure thread gets deleteLater on main thread
|
|
|
|
|
QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
|
|
|
|
|
thread->start(priority);
|
|
|
|
|
}
|
|
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-16 14:58:04 +01:00
|
|
|
} // Internal
|
|
|
|
|
|
2016-01-18 12:48:09 +01:00
|
|
|
/*!
|
|
|
|
|
The interface of \c {runAsync} is similar to the std::thread constructor and \c {std::invoke}.
|
|
|
|
|
|
2016-02-05 11:17:40 +01:00
|
|
|
The \a function argument can be a member function,
|
|
|
|
|
an object with \c {operator()} (with no overloads),
|
|
|
|
|
a \c {std::function}, lambda, function pointer or function reference.
|
|
|
|
|
The \a args are passed to the function call after they are copied/moved to the thread.
|
|
|
|
|
|
|
|
|
|
The \a function can take a \c {QFutureInterface<ResultType>&} as its first argument, followed by
|
2016-01-18 12:48:09 +01:00
|
|
|
other custom arguments which need to be passed to this function.
|
2016-02-05 11:17:40 +01:00
|
|
|
If it does not take a \c {QFutureInterface<ResultType>&} as its first argument
|
|
|
|
|
and its return type is not void, the function call's result is reported to the QFuture.
|
2016-01-18 12:48:09 +01:00
|
|
|
If \a function is a (non-static) member function, the first argument in \a args is expected
|
|
|
|
|
to be the object that the function is called on.
|
|
|
|
|
|
2016-02-05 11:17:40 +01:00
|
|
|
If a thread \a pool is given, the function is run there. Otherwise a new, independent thread
|
|
|
|
|
is started.
|
|
|
|
|
|
2016-01-18 12:48:09 +01:00
|
|
|
\sa std::thread
|
|
|
|
|
\sa std::invoke
|
2016-02-05 11:17:40 +01:00
|
|
|
\sa QThreadPool
|
|
|
|
|
\sa QThread::Priority
|
2016-01-18 12:48:09 +01:00
|
|
|
*/
|
2016-02-08 16:26:19 +01:00
|
|
|
template <typename Function, typename... Args,
|
|
|
|
|
typename ResultType = typename Internal::resultType<Function>::type>
|
|
|
|
|
QFuture<ResultType>
|
|
|
|
|
runAsync(QThreadPool *pool, QThread::Priority priority, Function &&function, Args&&... args)
|
2016-01-12 14:05:05 +01:00
|
|
|
{
|
2018-06-01 15:58:34 +02:00
|
|
|
return Internal::runAsync_internal(pool,
|
|
|
|
|
StackSizeInBytes(),
|
|
|
|
|
priority,
|
|
|
|
|
std::forward<Function>(function),
|
|
|
|
|
std::forward<Args>(args)...);
|
2016-01-12 14:05:05 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 11:17:40 +01:00
|
|
|
/*!
|
|
|
|
|
Runs \a function with \a args in a new thread with given thread \a priority.
|
|
|
|
|
\sa runAsync(QThreadPool*,QThread::Priority,Function&&,Args&&...)
|
|
|
|
|
\sa QThread::Priority
|
|
|
|
|
*/
|
2016-02-08 16:26:19 +01:00
|
|
|
template <typename Function, typename... Args,
|
|
|
|
|
typename ResultType = typename Internal::resultType<Function>::type>
|
|
|
|
|
QFuture<ResultType>
|
|
|
|
|
runAsync(QThread::Priority priority, Function &&function, Args&&... args)
|
2016-02-05 11:17:40 +01:00
|
|
|
{
|
2016-02-08 16:26:19 +01:00
|
|
|
return runAsync(static_cast<QThreadPool *>(nullptr), priority,
|
|
|
|
|
std::forward<Function>(function), std::forward<Args>(args)...);
|
2016-02-05 11:17:40 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-01 15:58:34 +02:00
|
|
|
/*!
|
|
|
|
|
Runs \a function with \a args in a new thread with given thread \a stackSize and
|
|
|
|
|
thread priority QThread::InheritPriority .
|
|
|
|
|
\sa runAsync(QThreadPool*,QThread::Priority,Function&&,Args&&...)
|
|
|
|
|
\sa QThread::Priority
|
|
|
|
|
\sa QThread::setStackSize
|
|
|
|
|
*/
|
|
|
|
|
template<typename Function,
|
|
|
|
|
typename... Args,
|
|
|
|
|
typename ResultType = typename Internal::resultType<Function>::type>
|
|
|
|
|
QFuture<ResultType> runAsync(Utils::StackSizeInBytes stackSize, Function &&function, Args &&... args)
|
|
|
|
|
{
|
|
|
|
|
return Internal::runAsync_internal(static_cast<QThreadPool *>(nullptr),
|
|
|
|
|
stackSize,
|
|
|
|
|
QThread::InheritPriority,
|
|
|
|
|
std::forward<Function>(function),
|
|
|
|
|
std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Runs \a function with \a args in a new thread with given thread \a stackSize and
|
|
|
|
|
given thread \a priority.
|
|
|
|
|
\sa runAsync(QThreadPool*,QThread::Priority,Function&&,Args&&...)
|
|
|
|
|
\sa QThread::Priority
|
|
|
|
|
\sa QThread::setStackSize
|
|
|
|
|
*/
|
|
|
|
|
template<typename Function,
|
|
|
|
|
typename... Args,
|
|
|
|
|
typename ResultType = typename Internal::resultType<Function>::type>
|
|
|
|
|
QFuture<ResultType> runAsync(Utils::StackSizeInBytes stackSize,
|
|
|
|
|
QThread::Priority priority,
|
|
|
|
|
Function &&function,
|
|
|
|
|
Args &&... args)
|
|
|
|
|
{
|
|
|
|
|
return Internal::runAsync_internal(static_cast<QThreadPool *>(nullptr),
|
|
|
|
|
stackSize,
|
|
|
|
|
priority,
|
|
|
|
|
std::forward<Function>(function),
|
|
|
|
|
std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-05 11:17:40 +01:00
|
|
|
/*!
|
|
|
|
|
Runs \a function with \a args in a new thread with thread priority QThread::InheritPriority.
|
|
|
|
|
\sa runAsync(QThreadPool*,QThread::Priority,Function&&,Args&&...)
|
|
|
|
|
\sa QThread::Priority
|
|
|
|
|
*/
|
2016-02-08 16:26:19 +01:00
|
|
|
template <typename Function, typename... Args,
|
2017-01-11 17:04:23 +01:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
|
!std::is_same<std::decay_t<Function>, QThreadPool>::value
|
|
|
|
|
&& !std::is_same<std::decay_t<Function>, QThread::Priority>::value
|
|
|
|
|
>,
|
2016-02-08 16:26:19 +01:00
|
|
|
typename ResultType = typename Internal::resultType<Function>::type>
|
|
|
|
|
QFuture<ResultType>
|
|
|
|
|
runAsync(Function &&function, Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
return runAsync(static_cast<QThreadPool *>(nullptr),
|
|
|
|
|
QThread::InheritPriority, std::forward<Function>(function),
|
|
|
|
|
std::forward<Args>(args)...);
|
2016-02-02 13:46:08 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 11:17:40 +01:00
|
|
|
/*!
|
|
|
|
|
Runs \a function with \a args in a thread \a pool with thread priority QThread::InheritPriority.
|
|
|
|
|
\sa runAsync(QThreadPool*,QThread::Priority,Function&&,Args&&...)
|
|
|
|
|
\sa QThread::Priority
|
|
|
|
|
*/
|
2016-02-08 16:26:19 +01:00
|
|
|
template <typename Function, typename... Args,
|
2017-01-11 17:04:23 +01:00
|
|
|
typename = std::enable_if_t<!std::is_same<std::decay_t<Function>, QThread::Priority>::value>,
|
2016-02-08 16:26:19 +01:00
|
|
|
typename ResultType = typename Internal::resultType<Function>::type>
|
|
|
|
|
QFuture<ResultType>
|
|
|
|
|
runAsync(QThreadPool *pool, Function &&function, Args&&... args)
|
2016-01-27 15:44:31 +01:00
|
|
|
{
|
2016-02-08 16:26:19 +01:00
|
|
|
return runAsync(pool, QThread::InheritPriority, std::forward<Function>(function),
|
|
|
|
|
std::forward<Args>(args)...);
|
2016-01-27 15:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
2016-10-27 16:44:45 +02:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Adds a handler for when a result is ready.
|
|
|
|
|
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
|
|
|
|
|
or create a QFutureWatcher already for other reasons.
|
|
|
|
|
*/
|
|
|
|
|
template <typename R, typename T>
|
|
|
|
|
const QFuture<T> &onResultReady(const QFuture<T> &future, R *receiver, void(R::*member)(const T &))
|
|
|
|
|
{
|
|
|
|
|
auto watcher = new QFutureWatcher<T>();
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::resultReadyAt, receiver,
|
|
|
|
|
[receiver, member, watcher](int index) {
|
|
|
|
|
(receiver->*member)(watcher->future().resultAt(index));
|
|
|
|
|
});
|
2016-11-28 16:09:34 +01:00
|
|
|
watcher->setFuture(future);
|
2016-10-27 16:44:45 +02:00
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Adds a handler for when a result is ready. The guard object determines the lifetime of
|
|
|
|
|
the connection.
|
|
|
|
|
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
|
|
|
|
|
or create a QFutureWatcher already for other reasons.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T, typename Function>
|
|
|
|
|
const QFuture<T> &onResultReady(const QFuture<T> &future, QObject *guard, Function f)
|
|
|
|
|
{
|
|
|
|
|
auto watcher = new QFutureWatcher<T>();
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::resultReadyAt, guard, [f, watcher](int index) {
|
|
|
|
|
f(watcher->future().resultAt(index));
|
|
|
|
|
});
|
2016-11-28 16:09:34 +01:00
|
|
|
watcher->setFuture(future);
|
2016-10-27 16:44:45 +02:00
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Adds a handler for when a result is ready.
|
|
|
|
|
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
|
|
|
|
|
or create a QFutureWatcher already for other reasons.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T, typename Function>
|
|
|
|
|
const QFuture<T> &onResultReady(const QFuture<T> &future, Function f)
|
|
|
|
|
{
|
|
|
|
|
auto watcher = new QFutureWatcher<T>();
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::resultReadyAt, [f, watcher](int index) {
|
|
|
|
|
f(watcher->future().resultAt(index));
|
|
|
|
|
});
|
2016-11-28 16:09:34 +01:00
|
|
|
watcher->setFuture(future);
|
2016-10-27 16:44:45 +02:00
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 15:32:55 +02:00
|
|
|
/*!
|
|
|
|
|
Adds a handler for when the future is finished.
|
|
|
|
|
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
|
|
|
|
|
or create a QFutureWatcher already for other reasons.
|
|
|
|
|
*/
|
|
|
|
|
template<typename R, typename T>
|
|
|
|
|
const QFuture<T> &onFinished(const QFuture<T> &future,
|
|
|
|
|
R *receiver,
|
|
|
|
|
void (R::*member)(const QFuture<T> &))
|
|
|
|
|
{
|
|
|
|
|
auto watcher = new QFutureWatcher<T>();
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
|
|
|
|
|
QObject::connect(watcher,
|
|
|
|
|
&QFutureWatcherBase::finished,
|
|
|
|
|
receiver,
|
|
|
|
|
[receiver, member, watcher]() { (receiver->*member)(watcher->future()); });
|
|
|
|
|
watcher->setFuture(future);
|
|
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Adds a handler for when the future is finished. The guard object determines the lifetime of
|
|
|
|
|
the connection.
|
|
|
|
|
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
|
|
|
|
|
or create a QFutureWatcher already for other reasons.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T, typename Function>
|
|
|
|
|
const QFuture<T> &onFinished(const QFuture<T> &future, QObject *guard, Function f)
|
|
|
|
|
{
|
|
|
|
|
auto watcher = new QFutureWatcher<T>();
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::finished, guard, [f, watcher]() {
|
|
|
|
|
f(watcher->future());
|
|
|
|
|
});
|
|
|
|
|
watcher->setFuture(future);
|
|
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Adds a handler for when the future is finished.
|
|
|
|
|
This creates a new QFutureWatcher. Do not use if you intend to react on multiple conditions
|
|
|
|
|
or create a QFutureWatcher already for other reasons.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T, typename Function>
|
|
|
|
|
const QFuture<T> &onFinished(const QFuture<T> &future, Function f)
|
|
|
|
|
{
|
|
|
|
|
auto watcher = new QFutureWatcher<T>();
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater);
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcherBase::finished, [f, watcher]() {
|
|
|
|
|
f(watcher->future());
|
|
|
|
|
});
|
|
|
|
|
watcher->setFuture(future);
|
|
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Utils
|