Logo AND Algorithmique Numérique Distribuée

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