Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
37a348195fffd03b0e27110d1051ade5a2cc2311
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-2018. 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 "context_private.hpp"
8 #include "simgrid/Exception.hpp"
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 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
19                                                   smx_actor_t process)
20 {
21   if (parallel_)
22     return this->new_context<ParallelBoostContext>(std::move(code), cleanup_func, process, this);
23   return this->new_context<BoostContext>(std::move(code), cleanup_func, process, this);
24 }
25
26 // BoostContext
27
28 BoostContext::BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
29                            SwappedContextFactory* factory)
30     : SwappedContext(std::move(code), cleanup_func, process, factory)
31 {
32
33   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
34   if (has_code()) {
35     /* We need to pass the bottom of the stack to make_fcontext,
36        depending on the stack direction it may be the lower or higher address: */
37 #if PTH_STACKGROWTH == -1
38     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
39 #else
40     void* stack = this->stack_;
41 #endif
42     ASAN_ONLY(this->asan_stack_ = stack);
43 #if BOOST_VERSION < 106100
44     this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
45 #else
46     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, BoostContext::wrapper);
47 #endif
48   } else {
49 #if BOOST_VERSION < 105600
50     this->fc_ = new boost::context::fcontext_t();
51 #endif
52     if (get_maestro() == nullptr)
53       set_maestro(this);
54   }
55 }
56
57 BoostContext::~BoostContext()
58 {
59 #if BOOST_VERSION < 105600
60   if (not this->stack_)
61     delete this->fc_;
62 #endif
63   if (this == get_maestro())
64     set_maestro(nullptr);
65 }
66
67 void BoostContext::wrapper(BoostContext::arg_type arg)
68 {
69 #if BOOST_VERSION < 106100
70   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
71 #else
72   BoostContext* context = static_cast<BoostContext**>(arg.data)[1];
73   ASAN_ONLY(xbt_assert(context->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
74   ASAN_FINISH_SWITCH(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_);
75   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
76 #endif
77   try {
78     (*context)();
79   } catch (StopRequest const&) {
80     XBT_DEBUG("Caught a StopRequest");
81   } catch (simgrid::Exception const& e) {
82     XBT_INFO("Actor killed by an uncatched exception %s", simgrid::xbt::demangle(typeid(e).name()).get());
83     throw;
84   }
85   context->Context::stop();
86   ASAN_ONLY(context->asan_stop_ = true);
87   context->suspend();
88 }
89
90 void BoostContext::swap_into(SwappedContext* to_)
91 {
92   BoostContext* to = static_cast<BoostContext*>(to_);
93 #if BOOST_VERSION < 105600
94   boost::context::jump_fcontext(this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
95 #elif BOOST_VERSION < 106100
96   boost::context::jump_fcontext(&this->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
97 #else
98   BoostContext* ctx[2] = {this, to};
99   ASAN_ONLY(void* fake_stack = nullptr);
100   ASAN_ONLY(to->asan_ctx_ = from);
101   ASAN_START_SWITCH(from->asan_stop_ ? nullptr : &fake_stack, to->asan_stack_, to->asan_stack_size_);
102   boost::context::detail::transfer_t arg = boost::context::detail::jump_fcontext(to->fc_, ctx);
103   ASAN_ONLY(xbt_assert(from->asan_ctx_ == static_cast<BoostContext**>(arg.data)[0]));
104   ASAN_FINISH_SWITCH(fake_stack, &from->asan_ctx_->asan_stack_, &from->asan_ctx_->asan_stack_size_);
105   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
106 #endif
107 }
108
109 // ParallelBoostContext
110 void ParallelBoostContext::suspend()
111 {
112   boost::optional<smx_actor_t> next_work = parmap_->next();
113   SwappedContext* next_context;
114   if (next_work) {
115     XBT_DEBUG("Run next process");
116     next_context = static_cast<ParallelBoostContext*>(next_work.get()->context_);
117   } else {
118     XBT_DEBUG("No more processes to run");
119     next_context = workers_context_[worker_id_];
120   }
121
122   Context::set_current(next_context);
123   this->swap_into(next_context);
124 }
125
126 void ParallelBoostContext::resume()
127 {
128   worker_id_ = threads_working_.fetch_add(1, std::memory_order_relaxed);
129
130   SwappedContext* worker_context = static_cast<SwappedContext*>(self());
131   workers_context_[worker_id_]   = worker_context;
132
133   Context::set_current(this);
134   worker_context->swap_into(this);
135 }
136
137
138 XBT_PRIVATE ContextFactory* boost_factory()
139 {
140   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
141   return new BoostContextFactory();
142 }
143 }}} // namespace