Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-2021. 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/internal_config.h"
9 #include "src/simix/smx_private.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
12
13 namespace simgrid {
14 namespace kernel {
15 namespace context {
16
17 // BoostContextFactory
18 BoostContext* BoostContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor)
19 {
20   return this->new_context<BoostContext>(std::move(code), actor, this);
21 }
22
23 // BoostContext
24
25 BoostContext::BoostContext(std::function<void()>&& code, actor::ActorImpl* actor, SwappedContextFactory* factory)
26     : SwappedContext(std::move(code), actor, factory)
27 {
28   XBT_VERB("Creating a context of stack %uMb", actor->get_stacksize() / 1024 / 1024);
29   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
30   if (has_code()) {
31 #if BOOST_VERSION < 106100
32     this->fc_ = boost::context::make_fcontext(get_stack_bottom(), actor->get_stacksize(), BoostContext::wrapper);
33 #else
34     this->fc_ =
35         boost::context::detail::make_fcontext(get_stack_bottom(), actor->get_stacksize(), BoostContext::wrapper);
36 #endif
37   }
38 }
39
40 void BoostContext::wrapper(BoostContext::arg_type arg)
41 {
42 #if BOOST_VERSION < 106100
43   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
44 #else
45   BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
46   context->verify_previous_context(static_cast<BoostContext**>(arg.data)[0]);
47   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
48 #endif
49   smx_ctx_wrapper(context);
50 }
51
52 void BoostContext::swap_into_for_real(SwappedContext* to_)
53 {
54   auto* to = static_cast<BoostContext*>(to_);
55 #if BOOST_VERSION < 106100
56   boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
57 #else
58   BoostContext* ctx[2] = {this, to};
59   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
60   this->verify_previous_context(static_cast<BoostContext**>(arg.data)[0]);
61   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
62 #endif
63 }
64
65 XBT_PRIVATE ContextFactory* boost_factory()
66 {
67   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
68   return new BoostContextFactory();
69 }
70 } // namespace context
71 } // namespace kernel
72 } // namespace simgrid