Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics: fix "Malformed whitespace in C++" spotted by codefactor.io.
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "ContextBoost.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "src/simix/smx_private.hpp"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
11
12 namespace simgrid {
13 namespace kernel {
14 namespace context {
15
16 // BoostContextFactory
17 BoostContext* BoostContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor)
18 {
19   return this->new_context<BoostContext>(std::move(code), actor, this);
20 }
21
22 // BoostContext
23
24 BoostContext::BoostContext(std::function<void()>&& code, actor::ActorImpl* actor, SwappedContextFactory* factory)
25     : SwappedContext(std::move(code), actor, factory)
26 {
27   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
28   if (has_code()) {
29 #if BOOST_VERSION < 106100
30     this->fc_ = boost::context::make_fcontext(get_stack_bottom(), smx_context_stack_size, BoostContext::wrapper);
31 #else
32     this->fc_ =
33         boost::context::detail::make_fcontext(get_stack_bottom(), smx_context_stack_size, BoostContext::wrapper);
34 #endif
35   }
36 }
37
38 void BoostContext::wrapper(BoostContext::arg_type arg)
39 {
40 #if BOOST_VERSION < 106100
41   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
42 #else
43   BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
44   context->verify_previous_context(static_cast<BoostContext**>(arg.data)[0]);
45   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
46 #endif
47   smx_ctx_wrapper(context);
48 }
49
50 void BoostContext::swap_into_for_real(SwappedContext* to_)
51 {
52   BoostContext* to = static_cast<BoostContext*>(to_);
53 #if BOOST_VERSION < 106100
54   boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
55 #else
56   BoostContext* ctx[2] = {this, to};
57   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
58   this->verify_previous_context(static_cast<BoostContext**>(arg.data)[0]);
59   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
60 #endif
61 }
62
63 XBT_PRIVATE ContextFactory* boost_factory()
64 {
65   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
66   return new BoostContextFactory();
67 }
68 } // namespace context
69 } // namespace kernel
70 } // namespace simgrid