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 pathcs_thread.cpp
More file actions
309 lines (260 loc) · 10.5 KB
/
Copy pathcs_thread.cpp
File metadata and controls
309 lines (260 loc) · 10.5 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
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \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 2.1 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 <cstdint>
#include <stdexcept>
#include "cppa/detail/cs_thread.hpp"
namespace {
typedef void* vptr;
typedef void (*cst_fun)(vptr);
} // namespace <anonmyous>
#ifdef CPPA_DISABLE_CONTEXT_SWITCHING
namespace cppa { namespace detail {
cs_thread::cs_thread() : m_impl(nullptr) { }
cs_thread::cs_thread(cst_fun, vptr) : m_impl(nullptr) { }
cs_thread::~cs_thread() { }
void cs_thread::swap(cs_thread&, cs_thread&) {
throw std::logic_error("libcppa was compiled using "
"CPPA_DISABLE_CONTEXT_SWITCHING");
}
const bool cs_thread::is_disabled_feature = true;
} } // namespace cppa::detail
#else // ifdef CPPA_DISABLE_CONTEXT_SWITCHING
// optional valgrind include
#ifdef CPPA_ANNOTATE_VALGRIND
# include <valgrind/valgrind.h>
#endif
// boost includes
#include <boost/version.hpp>
#include <boost/context/all.hpp>
#if BOOST_VERSION >= 105300
# include <boost/coroutine/all.hpp>
#endif
namespace cppa { namespace detail {
void cst_trampoline(intptr_t iptr);
namespace {
#if CPPA_ANNOTATE_VALGRIND
typedef int vg_member;
inline void vg_register(vg_member& stack_id, vptr ptr1, vptr ptr2) {
stack_id = VALGRIND_STACK_REGISTER(ptr1, ptr2);
}
inline void vg_deregister(vg_member stack_id) {
VALGRIND_STACK_DEREGISTER(stack_id);
}
#else
struct vg_member { };
inline void vg_register(vg_member&, vptr, vptr) {/*NOP*/}
inline void vg_deregister(const vg_member&) { }
#endif
/* Interface dependent on Boost version:
*
* === namespace aliases ===
*
* ctxn:
* namespace of context library; alias for boost::context or boost::ctx
*
* === types ===
*
* context:
* execution context; either fcontext_t or fcontext_t*
*
* converted_context:
* additional member for converted_cs_thread;
* needed if context is defined as fcontext_t*
*
* ctx_stack_info:
* result of new_stack(), needed to delete stack properly in some versions
*
* stack_allocator:
* a stack allocator for cs_thread instances
*
* === functions ===
*
* void init_converted_context(converted_context&, context&)
*
* void ctx_switch(context&, context&, cst_impl*):
* implements the context switching from one cs_thread to another
*
* ctx_stack_info new_stack(context&, stack_allocator&, vg_member&):
* allocates a stack, prepares execution of context
* and (optionally) registers the new stack to valgrind
*
* void del_stack(stack_allocator&, ctx_stack_info, vg_member&):
* destroys the stack and (optionally) deregisters it from valgrind
*/
#if BOOST_VERSION == 105100
// === namespace aliases ===
namespace ctxn = boost::ctx;
// === types ===
typedef ctxn::fcontext_t context;
struct converted_context { };
typedef int ctx_stack_info;
typedef ctxn::stack_allocator stack_allocator;
// === functions ===
inline void init_converted_context(converted_context&, context&) {/*NOP*/}
inline void ctx_switch(context& from, context& to, cst_impl* ptr) {
ctxn::jump_fcontext(&from, &to, (intptr_t) ptr);
}
ctx_stack_info new_stack(context& ctx,
stack_allocator& alloc,
vg_member& vgm) {
size_t mss = ctxn::minimum_stacksize();
ctx.fc_stack.base = alloc.allocate(mss);
ctx.fc_stack.limit = reinterpret_cast<vptr>(
reinterpret_cast<intptr_t>(ctx.fc_stack.base) - mss);
ctxn::make_fcontext(&ctx, cst_trampoline);
vg_register(vgm,
ctx.fc_stack.base,
reinterpret_cast<vptr>(
reinterpret_cast<intptr_t>(ctx.fc_stack.base) - mss));
return 0; // dummy value
}
inline void del_stack(stack_allocator&, ctx_stack_info, vg_member& vgm) {
vg_deregister(vgm);
}
#elif BOOST_VERSION < 105400
// === namespace aliases ===
namespace ctxn = boost::context;
// === types ===
typedef ctxn::fcontext_t* context;
typedef ctxn::fcontext_t converted_context;
typedef int ctx_stack_info;
# if BOOST_VERSION < 105300
typedef ctxn::guarded_stack_allocator stack_allocator;
# else
typedef boost::coroutines::stack_allocator stack_allocator;
# endif
// === functions ===
inline void init_converted_context(converted_context& cctx, context& ctx) {
ctx = &cctx;
}
inline void ctx_switch(context& from, context& to, cst_impl* ptr) {
ctxn::jump_fcontext(from, to, (intptr_t) ptr);
}
ctx_stack_info new_stack(context& ctx,
stack_allocator& alloc,
vg_member& vgm) {
size_t mss = stack_allocator::minimum_stacksize();
ctx = ctxn::make_fcontext(alloc.allocate(mss), mss, cst_trampoline);
vg_register(vgm,
ctx->fc_stack.sp,
reinterpret_cast<vptr>(
reinterpret_cast<intptr_t>(ctx->fc_stack.sp) - mss));
return 0; // dummy value
}
inline void del_stack(stack_allocator&, ctx_stack_info, vg_member& vgm) {
vg_deregister(vgm);
}
#else // BOOST_VERSION >= 105400
// === namespace aliases ===
namespace ctxn = boost::context;
// === types ===
typedef ctxn::fcontext_t* context;
typedef ctxn::fcontext_t converted_context;
typedef boost::coroutines::stack_context ctx_stack_info;
typedef boost::coroutines::stack_allocator stack_allocator;
// === functions ===
inline void init_converted_context(converted_context& cctx, context& ctx) {
ctx = &cctx;
}
inline void ctx_switch(context& from, context& to, cst_impl* ptr) {
ctxn::jump_fcontext(from, to, (intptr_t) ptr);
}
ctx_stack_info new_stack(context& ctx,
stack_allocator& alloc,
vg_member& vgm) {
size_t mss = stack_allocator::minimum_stacksize();
ctx_stack_info sinf;
alloc.allocate(sinf, mss);
ctx = ctxn::make_fcontext(sinf.sp, sinf.size, cst_trampoline);
vg_register(vgm,
ctx->fc_stack.sp,
reinterpret_cast<vptr>(
reinterpret_cast<intptr_t>(ctx->fc_stack.sp) - mss));
return sinf;
}
inline void del_stack(stack_allocator& alloc,
ctx_stack_info sctx,
vg_member& vgm) {
vg_deregister(vgm);
alloc.deallocate(sctx);
}
#endif
} // namespace <anonymous>
// base class for cs_thread pimpls
struct cst_impl {
cst_impl() : m_ctx() { }
virtual ~cst_impl() { }
virtual void run() = 0;
inline void swap(cst_impl* to) {
ctx_switch(m_ctx, to->m_ctx, to);
}
context m_ctx;
};
// a cs_thread representing a thread ('converts' the thread to a cs_thread)
struct converted_cs_thread : cst_impl {
converted_cs_thread() {
init_converted_context(m_converted, m_ctx);
}
void run() override {
throw std::logic_error("converted_cs_thread::run called");
}
converted_context m_converted;
};
// a cs_thread executing a function
struct fun_cs_thread : cst_impl {
fun_cs_thread(cst_fun fun, vptr arg) : m_fun(fun), m_arg(arg) {
m_stack_info = new_stack(m_ctx, m_alloc, m_vgm);
}
~fun_cs_thread() {
del_stack(m_alloc, m_stack_info, m_vgm);
}
void run() override {
m_fun(m_arg);
}
cst_fun m_fun; // thread function
vptr m_arg; // argument for thread function invocation
stack_allocator m_alloc; // allocates memory for our stack
vg_member m_vgm; // valgrind meta informations (optionally)
ctx_stack_info m_stack_info; // needed to delete stack in destructor
};
void cst_trampoline(intptr_t iptr) {
auto ptr = (cst_impl*) iptr;
ptr->run();
}
cs_thread::cs_thread() : m_impl(new converted_cs_thread) { }
cs_thread::cs_thread(cst_fun f, vptr x) : m_impl(new fun_cs_thread(f, x)) { }
void cs_thread::swap(cs_thread& from, cs_thread& to) {
from.m_impl->swap(to.m_impl);
}
cs_thread::~cs_thread() {
delete m_impl;
}
const bool cs_thread::is_disabled_feature = false;
} } // namespace cppa::detail
#endif // CPPA_DISABLE_CONTEXT_SWITCHING
You can’t perform that action at this time.
