Skip to content
Navigation Menu
{{ message }}
forked from actor-framework/actor-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_pool_scheduler.cpp
More file actions
304 lines (264 loc) · 10.8 KB
/
Copy paththread_pool_scheduler.cpp
File metadata and controls
304 lines (264 loc) · 10.8 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011-2013 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#include <mutex>
#include <thread>
#include <cstdint>
#include <cstddef>
#include <iostream>
#include "cppa/event_based_actor.hpp"
#include "cppa/thread_mapped_actor.hpp"
#include "cppa/detail/actor_count.hpp"
#include "cppa/context_switching_actor.hpp"
#include "cppa/detail/thread_pool_scheduler.hpp"
using std::cout;
using std::endl;
namespace cppa { namespace detail {
namespace {
typedef std::unique_lock<std::mutex> guard_type;
typedef intrusive::single_reader_queue<thread_pool_scheduler::worker> worker_queue;
} // namespace <anonmyous>
struct thread_pool_scheduler::worker {
typedef scheduled_actor* job_ptr;
job_queue* m_job_queue;
job_ptr m_dummy;
std::thread m_thread;
worker(job_queue* jq, job_ptr dummy) : m_job_queue(jq), m_dummy(dummy) { }
void start() {
m_thread = std::thread(&thread_pool_scheduler::worker_loop, this);
}
worker(const worker&) = delete;
worker& operator=(const worker&) = delete;
job_ptr aggressive_polling() {
job_ptr result = nullptr;
for (int i = 0; i < 100; ++i) {
result = m_job_queue->try_pop();
if (result) {
return result;
}
std::this_thread::yield();
}
return result;
}
job_ptr less_aggressive_polling() {
job_ptr result = nullptr;
for (int i = 0; i < 550; ++i) {
result = m_job_queue->try_pop();
if (result) {
return result;
}
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::this_thread::sleep_for(std::chrono::microseconds(50));
}
return result;
}
job_ptr relaxed_polling() {
job_ptr result = nullptr;
for (;;) {
result = m_job_queue->try_pop();
if (result) {
return result;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void operator()() {
util::fiber fself;
job_ptr job = nullptr;
actor_ptr next_job;
for (;;) {
job = aggressive_polling();
if (job == nullptr) {
job = less_aggressive_polling();
if (job == nullptr) {
job = relaxed_polling();
}
}
if (job == m_dummy) {
// dummy of doom received ...
m_job_queue->push_back(job); // kill the next guy
return; // and say goodbye
}
else {
do {
next_job.reset();
if (job->resume(&fself, next_job) == resume_result::actor_done) {
bool hidden = job->is_hidden();
job->deref();
//std::atomic_thread_fence(std::memory_order_seq_cst);
if (!hidden) dec_actor_count();
}
if (next_job) {
job = static_cast<job_ptr>(next_job.get());
//get_scheduler()->printer()->enqueue(job, make_any_tuple("fast-forwarded execution (chained actor)\n"));
}
else job = nullptr;
}
while (job); // loops until next_job was nullptr
}
}
}
};
void thread_pool_scheduler::worker_loop(thread_pool_scheduler::worker* w) {
(*w)();
}
thread_pool_scheduler::thread_pool_scheduler() {
m_num_threads = std::max<size_t>(std::thread::hardware_concurrency() * 2, 4);
}
thread_pool_scheduler::thread_pool_scheduler(size_t num_worker_threads) {
m_num_threads = num_worker_threads;
}
void thread_pool_scheduler::supervisor_loop(job_queue* jqueue,
scheduled_actor* dummy,
size_t num_threads) {
std::vector<std::unique_ptr<thread_pool_scheduler::worker> > workers;
for (size_t i = 0; i < num_threads; ++i) {
workers.emplace_back(new worker(jqueue, dummy));
workers.back()->start();
}
// wait for workers
for (auto& w : workers) {
w->m_thread.join();
}
}
void thread_pool_scheduler::initialize() {
m_supervisor = std::thread(&thread_pool_scheduler::supervisor_loop,
&m_queue, &m_dummy, m_num_threads);
super::initialize();
}
void thread_pool_scheduler::destroy() {
m_queue.push_back(&m_dummy);
m_supervisor.join();
// make sure job queue is empty, because destructor of m_queue would
// otherwise delete elements it shouldn't
auto ptr = m_queue.try_pop();
while (ptr != nullptr) {
if (ptr != &m_dummy) {
bool hidden = ptr->is_hidden();
ptr->deref();
std::atomic_thread_fence(std::memory_order_seq_cst);
if (!hidden) dec_actor_count();
}
ptr = m_queue.try_pop();
}
super::destroy();
}
void thread_pool_scheduler::enqueue(scheduled_actor* what) {
m_queue.push_back(what);
}
actor_ptr thread_pool_scheduler::spawn_as_thread(void_function fun,
init_callback cb,
bool hidden) {
if (!hidden) inc_actor_count();
thread_mapped_actor_ptr ptr{detail::memory::create<thread_mapped_actor>(std::move(fun))};
ptr->init();
ptr->initialized(true);
cb(ptr.get());
std::thread([hidden, ptr]() {
scoped_self_setter sss{ptr.get()};
try {
ptr->run();
ptr->on_exit();
}
catch (...) { }
std::atomic_thread_fence(std::memory_order_seq_cst);
if (!hidden) dec_actor_count();
}).detach();
return ptr;
}
actor_ptr thread_pool_scheduler::spawn_impl(scheduled_actor_ptr what) {
if (what->has_behavior()) {
if (!what->is_hidden()) { inc_actor_count(); }
what->ref();
// event-based actors are not pushed to the job queue on startup
if (what->impl_type() == context_switching_impl) {
m_queue.push_back(what.get());
}
}
else {
what->on_exit();
}
return std::move(what);
}
actor_ptr thread_pool_scheduler::spawn(scheduled_actor* raw,
scheduling_hint hint) {
scheduled_actor_ptr ptr{raw};
ptr->attach_to_scheduler(this, hint == scheduled_and_hidden);
return spawn_impl(std::move(ptr));
}
actor_ptr thread_pool_scheduler::spawn(scheduled_actor* raw,
init_callback cb,
scheduling_hint hint) {
scheduled_actor_ptr ptr{raw};
ptr->attach_to_scheduler(this, hint == scheduled_and_hidden);
cb(ptr.get());
return spawn_impl(std::move(ptr));
}
#ifndef CPPA_DISABLE_CONTEXT_SWITCHING
actor_ptr thread_pool_scheduler::spawn(void_function fun, scheduling_hint hint) {
if (hint == scheduled || hint == scheduled_and_hidden) {
scheduled_actor_ptr ptr{detail::memory::create<context_switching_actor>(std::move(fun))};
ptr->attach_to_scheduler(this, hint == scheduled_and_hidden);
return spawn_impl(std::move(ptr));
}
else {
return spawn_as_thread(std::move(fun),
[](local_actor*) { },
hint == detached_and_hidden);
}
}
actor_ptr thread_pool_scheduler::spawn(void_function fun,
init_callback init_cb,
scheduling_hint hint) {
if (hint == scheduled || hint == scheduled_and_hidden) {
scheduled_actor_ptr ptr{detail::memory::create<context_switching_actor>(std::move(fun))};
ptr->attach_to_scheduler(this, hint == scheduled_and_hidden);
init_cb(ptr.get());
return spawn_impl(std::move(ptr));
}
else {
return spawn_as_thread(std::move(fun),
std::move(init_cb),
hint == detached_and_hidden);
}
}
#else // CPPA_DISABLE_CONTEXT_SWITCHING
actor_ptr thread_pool_scheduler::spawn(void_function what, scheduling_hint sh) {
return spawn_as_thread(std::move(what),
[](local_actor*) { },
sh == detached_and_hidden);
}
actor_ptr thread_pool_scheduler::spawn(void_function what,
init_callback init_cb,
scheduling_hint sh) {
return spawn_as_thread(std::move(what),
std::move(init_cb),
sh == detached_and_hidden);
}
#endif
} } // namespace cppa::detail
You can’t perform that action at this time.
