-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacontrol.hpp
224 lines (208 loc) · 5.27 KB
/
acontrol.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#ifndef ACONTROL_HPP
#define ACONTROL_HPP
#include <thread>
#include <tuple>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <deque>
#include <vector>
#include <algorithm>
#include <atomic>
namespace act
{
class abstract_task
{
protected:
mutable std::mutex m_mutex;
mutable std::condition_variable m_cond_var;
mutable bool m_complete;
public:
abstract_task(): m_complete(false) {}
virtual ~abstract_task() {}
virtual void invoke() = 0;
virtual void wait() const
{
std::unique_lock<std::mutex> lock(m_mutex);
while (!m_complete)
{
m_cond_var.wait(lock);
}
}
};
class control
{
std::deque<std::shared_ptr<abstract_task>> m_tasks;
std::vector<std::thread> m_pool;
std::mutex m_mutex;
std::condition_variable m_cond_var;
std::condition_variable m_empty_cond;
std::atomic<bool> m_run;
std::vector<bool> m_active;
public:
struct sync
{
/**/
};
control(std::size_t pool_size = 2)
{
m_run.store(true, std::memory_order_relaxed);
auto func = [this](int n)
{
while (m_run.load(std::memory_order_relaxed))
{
std::unique_lock<std::mutex> lock(m_mutex);
m_active[n] = true;
if (m_tasks.empty())
{
m_empty_cond.notify_all();
m_active[n] = false;
m_cond_var.wait(lock);
}
else
{
std::shared_ptr<abstract_task> t = m_tasks.front();
m_tasks.pop_front();
lock.unlock();
t->invoke();
lock.lock();
m_active[n] = false;
}
}
};
pool_size = pool_size > 0 ? pool_size : 1;
m_active.resize(pool_size, false);
for(std::size_t i = 0; i < pool_size; ++i)
{
m_pool.emplace_back(func, i);
}
}
virtual ~control()
{
m_run.store(false, std::memory_order_relaxed);
std::unique_lock<std::mutex> lock(m_mutex);
m_cond_var.notify_all();
lock.unlock();
for(auto i = m_pool.begin(); i != m_pool.end(); ++i)
{
(*i).join();
}
}
control & schedule(const std::shared_ptr<abstract_task> & t)
{
std::unique_lock<std::mutex> lock(m_mutex);
if (t)
{
m_tasks.push_back(t);
m_cond_var.notify_one();
}
return *this;
}
control & synchronize()
{
std::unique_lock<std::mutex> lock(m_mutex);
bool x = false;
while (!m_tasks.empty() && (std::for_each(m_active.begin(), m_active.end(), [&x](bool e){if (e) x = true;}), x))
{
m_empty_cond.wait(lock);
x = false;
}
return *this;
}
control & operator << (const std::shared_ptr<abstract_task> & t)
{
return schedule(t);
}
control & operator<< (const sync & )
{
return synchronize();
}
};
template<int ...>
struct seq { };
template<int N, int ...S>
struct gens : gens<N-1, N-1, S...> { };
template<int ...S>
struct gens<0, S...> {
typedef seq<S...> type;
};
template <typename T>
class task
: public task<decltype(&T::operator())>
{
};
template <typename ClassType, typename ReturnType, typename ... Args>
class task<ReturnType(ClassType::*)(Args...) const>: public abstract_task
{
protected:
const ClassType &m_func;
std::tuple<Args...> m_vars;
ReturnType m_return;
public:
task(const ClassType &v, Args... args): m_func(v), m_vars(args ...) {}
virtual ~task() {}
virtual void invoke()
{
ReturnType r = caller(typename gens<sizeof...(Args)>::type());
std::unique_lock<std::mutex> lock(m_mutex);
m_return = r;
m_complete = true;
m_cond_var.notify_all();
}
ReturnType get() const
{
wait();
return m_return;
}
private:
template<int ...S>
ReturnType caller(seq<S...>) const
{
return m_func(std::get<S>(m_vars) ...);
}
};
template <typename ClassType, typename ... Args>
class task<void(ClassType::*)(Args...) const>: public abstract_task
{
protected:
const ClassType &m_func;
std::tuple<Args...> m_vars;
public:
task(const ClassType &v, Args... args): m_func(v), m_vars(args ...) {}
virtual ~task() {}
virtual void invoke()
{
caller(typename gens<sizeof...(Args)>::type());
std::unique_lock<std::mutex> lock(m_mutex);
m_complete = true;
m_cond_var.notify_all();
}
private:
template<int ...S>
void caller(seq<S...>) {
m_func()(std::get<S>(m_vars) ...);
}
};
template <typename ClassType>
class task<void(ClassType::*)(void) const>: public abstract_task
{
protected:
const ClassType &m_func;
public:
task(const ClassType &v): m_func(v) {}
virtual ~task() {}
virtual void invoke()
{
m_func();
std::unique_lock<std::mutex> lock(m_mutex);
m_complete = true;
m_cond_var.notify_all();
}
};
template <typename T, typename ... Args>
std::shared_ptr<task<decltype(&T::operator())>> make_task(T func, Args ... args )
{
return std::shared_ptr<task<decltype(&T::operator())>>(new task<decltype(&T::operator())>(func, args ...));
}
}
#endif // ACONTROL_HPP