-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.h
214 lines (192 loc) · 4.53 KB
/
worker.h
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
//
// Copyright 2018 Yoshinori Suzuki<wave.suzuki.z@gmail.com>
//
#pragma once
#include "atomic_queue.h"
#include <atomic>
#include <thread>
#include <vector>
// ワーカースレッド呼び出し
class WorkerThread
{
private:
//
using work_size_t = uint16_t;
using atomic_work_size_t = std::atomic<work_size_t>;
using count_t = std::atomic_int;
// ワーカー
class Worker
{
public:
virtual ~Worker() = default;
virtual void run() = 0;
virtual count_t* getCounter() { return nullptr; }
};
class CWorker : public Worker
{
count_t& count;
public:
CWorker(count_t& c) : count(c) {}
count_t* getCounter() override { return &count; }
};
//
std::atomic_bool enabled{true};
std::atomic_int launch{0};
std::atomic_int exec_count{0};
std::vector<uint8_t> buffer;
atomic_work_size_t buffer_index{0};
Queue<Worker> queue;
std::vector<std::thread> thread_list;
//
void add_atomic(std::atomic_int& i, int a)
{
int c, n;
do
{
c = i;
n = c + a;
} while (i.compare_exchange_weak(c, n) == false);
}
// スレッド本体
void execute()
{
add_atomic(launch, 1);
do
{
add_atomic(exec_count, 1);
auto* p = queue.pop();
if (p)
{
auto* c = p->getCounter();
if (c == nullptr || *c <= 0)
p->run();
else
add(p);
}
else
{
std::this_thread::sleep_for(std::chrono::microseconds(0));
}
add_atomic(exec_count, -1);
} while (enabled);
}
// ワーカー用メモリ確保
std::pair<void*, work_size_t> alloc(size_t sz)
{
work_size_t next, cidx;
do
{
cidx = buffer_index;
next = cidx + sz;
if (next > buffer.size())
{
// no memory
return std::make_pair(nullptr, 0);
}
} while (buffer_index.compare_exchange_weak(cidx, next) == false);
auto buff = reinterpret_cast<void*>(&buffer[cidx]);
return std::make_pair(buff, next);
}
// キューに積む
void add(Worker* w)
{
bool loop = true;
do
{
loop = !queue.push(w);
} while (loop);
}
// ワーカー生成
template <typename F>
bool registerWorker(size_t sz, F func)
{
auto r = alloc(sz);
if (r.first)
{
add(func(r.first));
return true;
}
return false;
}
public:
WorkerThread(size_t bsz = 4096) : queue(bsz / 32)
{
auto max_th = std::max(1U, std::thread::hardware_concurrency() - 1);
thread_list.resize(max_th);
for (int id = 0; id < max_th; id++)
{
auto& th = thread_list[id];
th = std::thread([&] { execute(); });
}
buffer.resize(bsz);
while (launch < max_th)
std::this_thread::sleep_for(std::chrono::microseconds(1));
}
virtual ~WorkerThread()
{
enabled = false;
for (auto& th : thread_list)
{
th.join();
}
}
// ワーカーをもう一度使えるようにする(checkCompleteで確認した後に呼ぶ)
void clear() { buffer_index = 0; }
// 全てのワーカーが終了しているか
bool checkComplete() const { return exec_count == 0 && queue.empty(); }
//
bool remain() const { return !queue.empty(); }
int remain2() const { return exec_count.load(); }
// 登録実行(引数付き)
template <typename F, typename A>
bool push(F f, A a)
{
class m : public Worker
{
F func;
A arg;
public:
m(F f, A a) : func(f), arg(a) {}
void run() override { func(arg); }
};
return registerWorker(sizeof(m), [&](auto* p) { return new (p) m{f, a}; });
}
template <typename F, typename A>
bool push(count_t& c, F f, A a)
{
class m : public CWorker
{
F func;
A arg;
public:
m(count_t& c, F f, A a) : CWorker(c), func(f), arg(a) {}
void run() override { func(arg); }
};
return registerWorker(sizeof(m), [&](auto* p) { return new (p) m{c, f, a}; });
}
// 登録実行(引数無し)
template <typename F>
bool push(F f)
{
class m : public Worker
{
F func;
public:
m(F f) : func(f) {}
void run() override { func(); }
};
return registerWorker(sizeof(m), [&](auto* p) { return new (p) m{f}; });
}
template <typename F>
bool push(count_t& c, F f)
{
class m : public CWorker
{
F func;
public:
m(count_t& c, F f) : CWorker(c), func(f) {}
void run() override { func(); }
};
return registerWorker(sizeof(m), [&](auto* p) { return new (p) m{c, f}; });
}
};