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 pathscheduler.cpp
More file actions
351 lines (300 loc) · 11.2 KB
/
Copy pathscheduler.cpp
File metadata and controls
351 lines (300 loc) · 11.2 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \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 <thread>
#include <atomic>
#include <chrono>
#include <iostream>
#include "cppa/on.hpp"
#include "cppa/self.hpp"
#include "cppa/receive.hpp"
#include "cppa/anything.hpp"
#include "cppa/to_string.hpp"
#include "cppa/scheduler.hpp"
#include "cppa/local_actor.hpp"
#include "cppa/thread_mapped_actor.hpp"
#include "cppa/detail/actor_count.hpp"
#include "cppa/detail/singleton_manager.hpp"
#include "cppa/detail/thread_pool_scheduler.hpp"
using std::move;
namespace cppa { namespace {
typedef std::uint32_t ui32;
struct exit_observer : cppa::attachable {
~exit_observer() {
cppa::detail::dec_actor_count();
}
void actor_exited(std::uint32_t) {
}
bool matches(const token&) {
return false;
}
};
inline decltype(std::chrono::high_resolution_clock::now()) now() {
return std::chrono::high_resolution_clock::now();
}
void async_send_impl(channel* to, actor* from, any_tuple&& msg) {
to->enqueue(from, move(msg));
}
void sync_reply_impl(actor* to, actor* from, message_id_t id, any_tuple&& msg) {
to->sync_enqueue(from, id, move(msg));
}
typedef void (*async_send_fun)(channel*, actor*, any_tuple&&);
typedef void (*sync_reply_fun)(actor*, actor*, message_id_t, any_tuple&&);
class delayed_msg {
public:
delayed_msg(async_send_fun arg0,
const channel_ptr& arg1,
const actor_ptr& arg2,
message_id_t,
any_tuple&& arg4)
: fun(arg0), ptr_a(arg1), from(arg2), msg(move(arg4)) { }
delayed_msg(sync_reply_fun arg0,
const actor_ptr& arg1,
const actor_ptr& arg2,
message_id_t arg3,
any_tuple&& arg4)
: fun(arg0), ptr_b(arg1), from(arg2), id(arg3), msg(move(arg4)) { }
delayed_msg(delayed_msg&&) = default;
delayed_msg(const delayed_msg&) = default;
delayed_msg& operator=(delayed_msg&&) = default;
delayed_msg& operator=(const delayed_msg&) = default;
inline void eval() {
if (fun.is_left()) (fun.left())(ptr_a.get(), from.get(), move(msg));
else (fun.right())(ptr_b.get(), from.get(), id, move(msg));
}
private:
either<async_send_fun, sync_reply_fun> fun;
channel_ptr ptr_a;
actor_ptr ptr_b;
actor_ptr from;
message_id_t id;
any_tuple msg;
};
} // namespace <anonymous>
class scheduler_helper {
public:
typedef intrusive_ptr<thread_mapped_actor> ptr_type;
void start() {
ptr_type mtimer{detail::memory::create<thread_mapped_actor>()};
ptr_type mprinter{detail::memory::create<thread_mapped_actor>()};
// launch threads
m_timer_thread = std::thread(&scheduler_helper::timer_loop, mtimer);
m_printer_thread = std::thread(&scheduler_helper::printer_loop, mprinter);
// set member variables
m_timer = mtimer;
m_printer = mprinter;
}
void stop() {
auto msg = make_any_tuple(atom("DIE"));
m_timer->enqueue(nullptr, msg);
m_printer->enqueue(nullptr, msg);
m_timer_thread.join();
m_printer_thread.join();
}
actor_ptr m_timer;
std::thread m_timer_thread;
actor_ptr m_printer;
std::thread m_printer_thread;
private:
static void timer_loop(ptr_type m_self);
static void printer_loop(ptr_type m_self);
};
template<class Map, typename Fun, class T>
void insert_dmsg(Map& storage,
const util::duration& d,
Fun fun_ptr,
const T& to,
const actor_ptr& sender,
message_id_t id,
any_tuple&& tup ) {
auto tout = now();
tout += d;
delayed_msg dmsg{fun_ptr, to, sender, id, move(tup)};
storage.insert(std::make_pair(std::move(tout), std::move(dmsg)));
}
void scheduler_helper::timer_loop(scheduler_helper::ptr_type m_self) {
typedef detail::abstract_actor<local_actor> impl_type;
typedef std::unique_ptr<detail::recursive_queue_node,detail::disposer> queue_node_ptr;
// setup & local variables
self.set(m_self.get());
auto& queue = m_self->mailbox();
std::multimap<decltype(now()), delayed_msg> messages;
queue_node_ptr msg_ptr;
//decltype(queue.pop()) msg_ptr = nullptr;
decltype(now()) tout;
bool done = false;
// message handling rules
auto mfun = (
on(atom("SEND"), arg_match) >> [&](const util::duration& d,
const channel_ptr& ptr,
any_tuple& tup) {
insert_dmsg(messages, d, async_send_impl,
ptr, msg_ptr->sender, message_id_t(), move(tup));
},
on(atom("REPLY"), arg_match) >> [&](const util::duration& d,
const actor_ptr& ptr,
message_id_t id,
any_tuple& tup) {
insert_dmsg(messages, d, sync_reply_impl,
ptr, msg_ptr->sender, id, move(tup));
},
on<atom("DIE")>() >> [&]() {
done = true;
},
others() >> [&]() {
# ifdef CPPA_DEBUG
std::cerr << "scheduler_helper::timer_loop: UNKNOWN MESSAGE: "
<< to_string(msg_ptr->msg)
<< std::endl;
# endif
}
);
// loop
while (!done) {
while (!msg_ptr) {
if (messages.empty()) {
msg_ptr.reset(queue.pop());
}
else {
tout = now();
// handle timeouts (send messages)
auto it = messages.begin();
while (it != messages.end() && (it->first) <= tout) {
it->second.eval();
messages.erase(it);
it = messages.begin();
}
// wait for next message or next timeout
if (it != messages.end()) {
msg_ptr.reset(queue.try_pop(it->first));
}
}
}
mfun(msg_ptr->msg);
msg_ptr.reset();
}
}
void scheduler_helper::printer_loop(ptr_type m_self) {
self.set(m_self.get());
std::map<actor_ptr,std::string> out;
auto flush_output = [&out](const actor_ptr& s) {
auto i = out.find(s);
if (i != out.end()) {
auto& line = i->second;
if (!line.empty()) {
std::cout << line << std::flush;
line.clear();
}
}
};
auto flush_if_needed = [](std::string& str) {
if (str.back() == '\n') {
std::cout << str << std::flush;
str.clear();
}
};
bool running = true;
receive_while (gref(running)) (
on(atom("add"), arg_match) >> [&](std::string& str) {
auto s = self->last_sender();
if (!str.empty() && s != nullptr) {
auto i = out.find(s);
if (i == out.end()) {
i = out.insert(make_pair(s, move(str))).first;
// monitor actor to flush its output on exit
self->monitor(s);
flush_if_needed(i->second);
}
else {
auto& ref = i->second;
ref += move(str);
flush_if_needed(ref);
}
}
},
on(atom("flush")) >> [&] {
flush_output(self->last_sender());
},
on(atom("DOWN"), any_vals) >> [&] {
auto s = self->last_sender();
flush_output(s);
out.erase(s);
},
on(atom("DIE")) >> [&] {
running = false;
},
others() >> [] {
//cout << "*** unexpected: "
// << to_string(self->last_dequeued())
// << endl;
}
);
}
scheduler::scheduler() : m_helper(nullptr) { }
void scheduler::initialize() {
m_helper = new scheduler_helper;
m_helper->start();
}
void scheduler::destroy() {
m_helper->stop();
delete this;
}
scheduler::~scheduler() {
delete m_helper;
}
channel* scheduler::delayed_send_helper() {
return m_helper->m_timer.get();
}
void scheduler::register_converted_context(actor* what) {
if (what) {
detail::inc_actor_count();
what->attach(new exit_observer);
}
}
attachable* scheduler::register_hidden_context() {
detail::inc_actor_count();
return new exit_observer;
}
void set_scheduler(scheduler* sched) {
if (detail::singleton_manager::set_scheduler(sched) == false) {
throw std::runtime_error("scheduler already set");
}
}
void set_default_scheduler(size_t num_threads) {
set_scheduler(new detail::thread_pool_scheduler(num_threads));
}
scheduler* get_scheduler() {
return detail::singleton_manager::get_scheduler();
}
scheduler* scheduler::create_singleton() {
return new detail::thread_pool_scheduler;
}
const actor_ptr& scheduler::printer() const {
return m_helper->m_printer;
}
} // namespace cppa
You can’t perform that action at this time.
