LeechCraft  0.6.70-9312-g4cc613a2df
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
workerthreadbase.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #pragma once
31 
32 #include <functional>
33 #include <atomic>
34 #include <QThread>
35 #include <QMutex>
36 #include <QMutexLocker>
37 #include <QFutureInterface>
38 #include <QFuture>
39 #include <QList>
40 #include <util/sll/oldcppkludges.h>
41 #include "futures.h"
42 #include "threadsconfig.h"
43 
44 namespace LeechCraft
45 {
46 namespace Util
47 {
48  class UTIL_THREADS_API WorkerThreadBase : public QThread
49  {
50  Q_OBJECT
51 
52  std::atomic_bool IsPaused_ { false };
53 
54  QMutex FunctionsMutex_;
55  QList<std::function<void ()>> Functions_;
56  public:
57  using QThread::QThread;
58 
59  void SetPaused (bool);
60 
61  template<typename F>
63  {
65  iface.reportStarted ();
66 
67  auto reporting = [func, iface] () mutable
68  {
69  ReportFutureResult (iface, func);
70  };
71 
72  {
73  QMutexLocker locker { &FunctionsMutex_ };
74  Functions_ << reporting;
75  }
76 
77  emit rotateFuncs ();
78 
79  return iface.future ();
80  }
81 
82  template<typename F, typename... Args>
83  QFuture<std::result_of_t<F (Args...)>> ScheduleImpl (F f, Args&&... args)
84  {
85  return ScheduleImpl ([f, args...] () mutable { return Invoke (f, args...); });
86  }
87 
88  virtual size_t GetQueueSize ();
89  protected:
90  void run () override final;
91 
92  virtual void Initialize () = 0;
93  virtual void Cleanup () = 0;
94  private:
95  void RotateFuncs ();
96  signals:
97  void rotateFuncs ();
98  };
99 
100  namespace detail
101  {
102  template<typename WorkerType>
104  {
105  virtual std::unique_ptr<WorkerType> Initialize () = 0;
106  };
107 
108  template<typename WorkerType, typename... Args>
109  struct Initializer : InitializerBase<WorkerType>
110  {
111  std::tuple<Args...> Args_;
112 
113  Initializer (std::tuple<Args...>&& tuple)
114  : Args_ { std::move (tuple) }
115  {
116  }
117 
118  std::unique_ptr<WorkerType> Initialize () override
119  {
120  return CPP17::Apply ([] (auto&&... args) { return std::make_unique<WorkerType> (std::forward<Args> (args)...); }, Args_);
121  }
122  };
123 
124  template<typename WorkerType>
125  struct Initializer<WorkerType> : InitializerBase<WorkerType>
126  {
127  std::unique_ptr<WorkerType> Initialize () override
128  {
129  return std::make_unique<WorkerType> ();
130  }
131  };
132  }
133 
134  template<typename WorkerType>
136  {
137  std::atomic_bool IsAutoQuit_ { false };
138  protected:
139  using W = WorkerType;
140 
141  std::unique_ptr<WorkerType> Worker_;
142 
143  std::unique_ptr<detail::InitializerBase<WorkerType>> Initializer_;
144  public:
145  WorkerThread (QObject *parent = nullptr)
147  , Initializer_ { std::make_unique<detail::Initializer<WorkerType>> () }
148  {
149  }
150 
151  template<typename... Args>
152  WorkerThread (QObject *parent, const Args&... args)
153  : WorkerThreadBase { parent }
154  , Initializer_ { std::make_unique<detail::Initializer<WorkerType, std::decay_t<Args>...>> (std::tuple<std::decay_t<Args>...> { args... }) }
155  {
156  }
157 
158  template<
159  typename Head,
160  typename... Rest,
161  typename = std::enable_if_t<
162  !std::is_base_of<QObject, std::remove_pointer_t<std::decay_t<Head>>>::value
163  >
164  >
165  WorkerThread (const Head& head, const Rest&... rest)
166  : WorkerThread { static_cast<QObject*> (nullptr), head, rest... }
167  {
168  }
169 
170  ~WorkerThread ()
171  {
172  if (!IsAutoQuit_)
173  return;
174 
175  quit ();
176  wait (2000);
177 
178  if (isRunning ())
179  qWarning () << Q_FUNC_INFO
180  << "thread is still running";
181  }
182 
184 
185  void SetAutoQuit (bool autoQuit)
186  {
187  IsAutoQuit_ = autoQuit;
188  }
189 
190  template<typename F, typename... Args>
191  QFuture<std::result_of_t<F (WorkerType*, Args...)>> ScheduleImpl (F f, Args&&... args)
192  {
193  const auto fWrapped = [f, this] (auto... args) mutable { return Invoke (f, Worker_.get (), args...); };
194  return WorkerThreadBase::ScheduleImpl (fWrapped, std::forward<Args> (args)...);
195  }
196  protected:
197  void Initialize () override
198  {
199  Worker_ = Initializer_->Initialize ();
200 
201  Initializer_.reset ();
202  }
203 
204  void Cleanup () override
205  {
206  Worker_.reset ();
207  }
208  };
209 }
210 }
#define UTIL_THREADS_API
Definition: threadsconfig.h:37
WorkerThread(QObject *parent=nullptr)
std::enable_if_t<!std::is_same< R, void >::value > ReportFutureResult(QFutureInterface< R > &iface, F &&f, Args &&...args)
Definition: futures.h:49
Initializer(std::tuple< Args...> &&tuple)
auto Apply(F &&f, Tuple &&tuple)
Definition: oldcppkludges.h:76
WorkerThread(QObject *parent, const Args &...args)
std::unique_ptr< WorkerType > Initialize() override
std::unique_ptr< WorkerType > Worker_
QFuture< std::result_of_t< F(WorkerType *, Args...)> > ScheduleImpl(F f, Args &&...args)
QFuture< std::result_of_t< F()> > ScheduleImpl(F func)
QFuture< std::result_of_t< F(Args...)> > ScheduleImpl(F f, Args &&...args)
std::unique_ptr< detail::InitializerBase< WorkerType > > Initializer_
auto Invoke(F &&f, Args &&...args) -> decltype(std::forward< F >(f)(std::forward< Args >(args)...))
Definition: oldcppkludges.h:40
std::unique_ptr< WorkerType > Initialize() override