Go to the documentation of this file.
39 #ifndef CGU_TASK_MANAGER_H
40 #define CGU_TASK_MANAGER_H
47 #include <type_traits>
64 virtual const char*
what()
const throw() {
return "TaskError\n";}
450 #ifndef DOXYGEN_PARSING
456 namespace TaskManagerHelper2 {
458 template <
class Ret,
class FType>
459 struct FunctorResultWrapper {
460 static void exec(FType& f,
464 static void do_fail(
const SharedLockPtr<AsyncResult<Ret>>& ret) {
484 template <
class Ret,
class FType>
486 mutable FType functor;
487 SharedLockPtr<AsyncResult<Ret>> ret;
492 template <
class FunctorArg>
493 FunctorResultExec(FunctorArg&& functor_,
494 const SharedLockPtr<AsyncResult<Ret>>& ret_): functor(std::forward<FunctorArg>(functor_)),
499 #endif // DOXYGEN_PARSING
507 typedef std::pair<std::unique_ptr<const Callback::Callback>,
508 std::unique_ptr<const Callback::Callback>> QueueItemType;
518 static unsigned int init_max_tasks();
700 static unsigned int max_tasks = init_max_tasks();
888 add_task(std::unique_ptr<const Callback::Callback>(task),
889 std::unique_ptr<const Callback::Callback>());
953 void add_task(std::unique_ptr<const Callback::Callback> task,
954 std::unique_ptr<const Callback::Callback> fail);
1012 template <
class Task,
1013 class =
typename std::enable_if<!std::is_convertible<typename std::remove_reference<Task>::type,
1016 add_task(std::unique_ptr<const Callback::Callback>(Callback::lambda<>(std::forward<Task>(task))),
1017 std::unique_ptr<const Callback::Callback>());
1095 template <
class Task,
class Fail,
1096 class =
typename std::enable_if<!std::is_convertible<Task, std::unique_ptr<const Callback::Callback>>::value
1097 && !std::is_convertible<Fail, std::unique_ptr<const Callback::Callback>>::value>::type>
1099 std::unique_ptr<const Callback::Callback> task_cb(
1100 Callback::lambda<>(std::forward<Task>(task))
1102 std::unique_ptr<const Callback::Callback> fail_cb(
1103 Callback::lambda<>(std::forward<Fail>(fail))
1105 add_task(std::move(task_cb), std::move(fail_cb));
1182 template <
class Ret,
class... Params,
class... Args,
class T>
1184 Ret (T::*func)(Params...),
1319 template <
class Ret,
class... Params,
class... Args,
class T>
1322 std::unique_ptr<const Cgu::Callback::Callback> fail,
1325 GMainContext* context,
1327 Ret (T::*func)(Params...),
1418 template <
class Ret,
class... Params,
class... Args,
class T>
1420 GMainContext* context,
1422 Ret (T::*func)(Params...),
1424 static_assert(
sizeof...(Args) < 4,
1425 "No greater than three bound arguments can be passed to "
1426 "TaskManager::make_task_when() taking a member function.");
1430 std::unique_ptr<const Cgu::Callback::Callback>(),
1436 std::forward<Args>(args)...);
1495 template <
class Ret,
class... Params,
class... Args,
class T>
1497 Ret (T::*func)(Params...)
const,
1632 template <
class Ret,
class... Params,
class... Args,
class T>
1635 std::unique_ptr<const Cgu::Callback::Callback> fail,
1638 GMainContext* context,
1640 Ret (T::*func)(Params...)
const,
1731 template <
class Ret,
class... Params,
class... Args,
class T>
1733 GMainContext* context,
1735 Ret (T::*func)(Params...)
const,
1737 static_assert(
sizeof...(Args) < 4,
1738 "No greater than three bound arguments can be passed to "
1739 "TaskManager::make_task_when() taking a member function.");
1743 std::unique_ptr<const Cgu::Callback::Callback>(),
1749 std::forward<Args>(args)...);
1805 template <
class Ret,
class... Params,
class... Args>
1938 template <
class Ret,
class... Params,
class... Args>
1941 std::unique_ptr<const Cgu::Callback::Callback> fail,
1944 GMainContext* context,
1945 Ret (*func)(Params...),
2033 template <
class Ret,
class... Params,
class... Args>
2035 GMainContext* context,
2036 Ret (*func)(Params...),
2038 static_assert(
sizeof...(Args) < 5,
2039 "No greater than four bound arguments can be passed to "
2040 "TaskManager::make_task_when() taking a function.");
2044 std::unique_ptr<const Cgu::Callback::Callback>(),
2049 std::forward<Args>(args)...);
2120 template <
class Ret,
class Func>
2126 #ifndef DOXYGEN_PARSING
2127 template <
class Func>
2146 typedef typename std::remove_const<typename std::remove_reference<Func>::type>::type FType;
2151 typedef decltype(f()) Ret;
2155 CbPtr exec_cb(
new TaskManagerHelper2::FunctorResultExec<Ret, FType>(std::forward<Func>(f), ret));
2156 CbPtr do_fail_cb(
Callback::make_ref(&TaskManagerHelper2::FunctorResultWrapper<Ret, FType>::do_fail,
2158 add_task(std::move(exec_cb), std::move(do_fail_cb));
2292 template <
class Ret,
class Func>
2295 std::unique_ptr<const Cgu::Callback::Callback> fail,
2298 GMainContext* context,
2439 template <
class When,
class Fail,
class Func,
2440 class =
typename std::enable_if<!std::is_convertible<When, std::unique_ptr<
const Callback::CallbackArg<
const typename std::result_of<Func()>::type&>>>::value
2441 && !std::is_convertible<Fail, std::unique_ptr<const Callback::Callback>>::value>::type>
2447 GMainContext* context,
2453 typedef decltype(func()) Ret;
2454 std::unique_ptr<const Callback::CallbackArg<const Ret&>> when_ptr(
2455 Callback::lambda<const Ret&>(std::forward<When>(when))
2457 std::unique_ptr<const Callback::Callback> fail_ptr(
2458 Callback::lambda<>(std::forward<Fail>(fail))
2462 std::move(fail_ptr),
2466 std::forward<Func>(func));
2553 template <
class Ret,
class Func>
2555 GMainContext* context,
2559 std::unique_ptr<const Cgu::Callback::Callback>(),
2563 std::forward<Func>(f));
2661 template <
class When,
class Func,
2662 class =
typename std::enable_if<!std::is_convertible<When, std::unique_ptr<
const Callback::CallbackArg<
const typename std::result_of<Func()>::type&>>>::value>::type>
2664 GMainContext* context,
2670 typedef decltype(func()) Ret;
2671 std::unique_ptr<const Callback::CallbackArg<const Ret&>> when_ptr(
2672 Callback::lambda<const Ret&>(std::forward<When>(when))
2676 std::unique_ptr<const Cgu::Callback::Callback>(),
2680 std::forward<Func>(func));
2768 template <
class Ret,
class Func>
2770 GMainContext* context,
2774 std::unique_ptr<const Cgu::Callback::Callback>(),
2778 std::forward<Func>(f));
2878 template <
class Func,
class When,
2879 class =
typename std::enable_if<!std::is_convertible<When, std::unique_ptr<
const Callback::CallbackArg<
const typename std::result_of<Func()>::type&>>>::value>::type>
2881 GMainContext* context,
2887 typedef decltype(func()) Ret;
2888 std::unique_ptr<const Callback::CallbackArg<const Ret&>> when_ptr(
2889 Callback::lambda<const Ret&>(std::forward<When>(when))
2893 std::unique_ptr<const Cgu::Callback::Callback>(),
2897 std::forward<Func>(func));
2962 template <
class Func>
2975 typedef decltype(f()) Ret;
2977 std::packaged_task<Ret()> task{std::forward<Func>(f)};
2978 std::future<Ret> ret{task.get_future()};
3109 template <
class When,
class Func>
3113 GMainContext* context,
3205 template <
class When,
class Func>
3207 GMainContext* context,
3213 std::forward<Func>(func));
3308 template <
class When,
class Func>
3310 GMainContext* context,
3316 std::forward<Func>(func));
3392 unsigned int idle = 10000,
bool blocking =
true,
3534 #include <c++-gtk-utils/task_manager.tpp>
StopMode
Definition: task_manager.h:504
void make_task_when(std::unique_ptr< const Cgu::Callback::CallbackArg< const Ret & >> when, GMainContext *context, const T &t, Ret(T::*func)(Params...) const, Args &&... args)
Definition: task_manager.h:1732
void make_task_when_full(std::unique_ptr< const Cgu::Callback::CallbackArg< const Ret & >> when, Cgu::Releaser *when_releaser, std::unique_ptr< const Cgu::Callback::Callback > fail, Cgu::Releaser *fail_releaser, gint priority, GMainContext *context, Func &&func)
CallbackArg< FreeArgs... > * make_ref(T &t, void(T::*func)(FreeArgs...))
Definition: callback.h:1352
The callback interface class.
Definition: callback.h:650
Cgu::SharedLockPtr< Cgu::AsyncResult< Ret > > make_task_result(const T &t, Ret(T::*func)(Params...) const, Args &&... args)
Definition: application.h:44
void make_task_when_full(std::unique_ptr< const Cgu::Callback::CallbackArg< const Ret & >> when, Cgu::Releaser *when_releaser, std::unique_ptr< const Cgu::Callback::Callback > fail, Cgu::Releaser *fail_releaser, gint priority, GMainContext *context, Ret(*func)(Params...), Args &&... args)
virtual const char * what() const
Definition: task_manager.h:64
void make_task_compose(Func &&f, GMainContext *context, std::unique_ptr< const Cgu::Callback::CallbackArg< const Ret & >> when)
Definition: task_manager.h:2769
void add_task(Task &&task, Fail &&fail)
Definition: task_manager.h:1098
Definition: task_manager.h:63
void make_task_when(std::unique_ptr< const Cgu::Callback::CallbackArg< const Ret & >> when, GMainContext *context, Ret(*func)(Params...), Args &&... args)
Definition: task_manager.h:2034
void make_task_compose(Func &&func, GMainContext *context, When &&when)
Definition: task_manager.h:2880
void set_max_threads(unsigned int max)
void make_task_packaged_when(When &&when, Cgu::Releaser *when_releaser, gint priority, GMainContext *context, Func &&func)
void make_task_when_full(std::unique_ptr< const Cgu::Callback::CallbackArg< const Ret & >> when, Cgu::Releaser *when_releaser, std::unique_ptr< const Cgu::Callback::Callback > fail, Cgu::Releaser *fail_releaser, gint priority, GMainContext *context, T &t, Ret(T::*func)(Params...), Args &&... args)
void make_task_packaged_compose(Func &&func, GMainContext *context, When &&when)
Definition: task_manager.h:3309
TaskManager(unsigned int max=8, unsigned int min=0, unsigned int idle=10000, bool blocking=true, StopMode mode=TaskManager::wait_for_all)
TaskManager & operator=(const TaskManager &)=delete
unsigned int get_idle_time() const
auto exec(const std::string &preamble, const std::string &file, Translator &&translator) -> typename std::result_of< Translator(SCM)>::type
Definition: extension.h:1659
This file provides classes for type erasure.
bool get_blocking() const
void add_task(std::unique_ptr< const Callback::Callback > task, std::unique_ptr< const Callback::Callback > fail)
A scoped locking class for exception safe Mutex locking which tracks the status of its mutex.
Definition: mutex.h:331
A thread-safe asynchronous result class.
Definition: async_result.h:165
unsigned int get_min_threads() const
Cgu::SharedLockPtr< Cgu::AsyncResult< Ret > > make_task_result(T &t, Ret(T::*func)(Params...), Args &&... args)
This file provides thread-safe asynchronous queue classes.
unsigned int get_used_threads() const
unsigned int get_tasks() const
static unsigned int get_max_tasks()
Definition: task_manager.h:693
CallbackArg Callback
Definition: callback.h:567
IncHandle(const TaskManager::IncHandle &)=delete
void make_task_packaged_when(When &&when, GMainContext *context, Func &&func)
Definition: task_manager.h:3206
TaskManager(const TaskManager &)=delete
auto make_task_packaged(Func &&f) -> std::future< decltype(f())>
Definition: task_manager.h:2963
void set_idle_time(unsigned int idle)
IncHandle(TaskManager &tm_)
Definition: task_manager.h:3516
void make_task_when_full(std::unique_ptr< const Cgu::Callback::CallbackArg< const Ret & >> when, Cgu::Releaser *when_releaser, std::unique_ptr< const Cgu::Callback::Callback > fail, Cgu::Releaser *fail_releaser, gint priority, GMainContext *context, const T &t, Ret(T::*func)(Params...) const, Args &&... args)
This file provides a thread-safe asynchronous result class.
@ wait_for_all
Definition: task_manager.h:504
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
void add_task(Task &&task)
Definition: task_manager.h:1015
void set_stop_mode(StopMode mode)
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
This is a smart pointer for managing the lifetime of objects allocated on freestore,...
Definition: shared_ptr.h:644
void change_max_threads(int delta)
void make_task_when(When &&when, GMainContext *context, Func &&func)
Definition: task_manager.h:2663
void make_task_when_full(When &&when, Cgu::Releaser *when_releaser, Fail &&fail, Cgu::Releaser *fail_releaser, gint priority, GMainContext *context, Func &&func)
Definition: task_manager.h:2442
A scoped handle for exception safe incrementing of the maximum number of threads that a TaskManager o...
Definition: task_manager.h:3470
A class used for tracking EmitterArg and SafeEmitterArg connections.
Definition: emitter.h:352
void add_task(const Callback::Callback *task)
Definition: task_manager.h:887
StopMode get_stop_mode() const
~IncHandle()
Definition: task_manager.h:3527
void make_task_when(std::unique_ptr< const Cgu::Callback::CallbackArg< const Ret & >> when, GMainContext *context, Func &&f)
Definition: task_manager.h:2554
This file provides a thread-safe signal/slot mechanism, with automatic disconnection.
void make_task_when(std::unique_ptr< const Cgu::Callback::CallbackArg< const Ret & >> when, GMainContext *context, T &t, Ret(T::*func)(Params...), Args &&... args)
Definition: task_manager.h:1419
TaskManager::IncHandle & operator=(const TaskManager::IncHandle &)=delete
Cgu::SharedLockPtr< Cgu::AsyncResult< Ret > > make_task_result(Func &&f)
A thread-pool class for managing tasks in multi-threaded programs.
Definition: task_manager.h:502
void set_blocking(bool blocking)
Cgu::SharedLockPtr< Cgu::AsyncResult< Ret > > make_task_result(Ret(*func)(Params...), Args &&... args)
@ wait_for_running
Definition: task_manager.h:504
unsigned int get_max_threads() const