Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check if ASan is enabled from CMake, and define variables.
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-2017. 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 <cstdint>
7
8 #include <functional>
9 #include <utility>
10 #include <vector>
11
12 #include <boost/context/all.hpp>
13
14 #include <xbt/log.h>
15 #include <xbt/xbt_os_thread.h>
16
17 #include "src/simix/smx_private.h"
18 #include "src/internal_config.h"
19 #include "src/kernel/context/ContextBoost.hpp"
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
22
23 namespace simgrid {
24 namespace kernel {
25 namespace context {
26
27 class BoostSerialContext : public BoostContext {
28 public:
29   BoostSerialContext(std::function<void()> code,
30       void_pfn_smxprocess_t cleanup_func,
31       smx_actor_t process)
32     : BoostContext(std::move(code), cleanup_func, process) {}
33   void suspend() override;
34 };
35
36 #if HAVE_THREAD_CONTEXTS
37 class BoostParallelContext : public BoostContext {
38 public:
39   BoostParallelContext(std::function<void()> code,
40       void_pfn_smxprocess_t cleanup_func,
41       smx_actor_t process)
42     : BoostContext(std::move(code), cleanup_func, process) {}
43   void suspend() override;
44   void resume() override;
45 };
46 #endif
47
48 // BoostContextFactory
49
50 bool BoostContext::parallel_                             = false;
51 simgrid::xbt::Parmap<smx_actor_t>* BoostContext::parmap_ = nullptr;
52 uintptr_t BoostContext::threads_working_                 = 0;
53 xbt_os_thread_key_t BoostContext::worker_id_key_;
54 unsigned long BoostContext::process_index_   = 0;
55 BoostContext* BoostContext::maestro_context_ = nullptr;
56 std::vector<BoostContext*> BoostContext::workers_context_;
57
58 BoostContextFactory::BoostContextFactory()
59   : ContextFactory("BoostContextFactory")
60 {
61   BoostContext::parallel_ = SIMIX_context_is_parallel();
62   if (BoostContext::parallel_) {
63 #if HAVE_THREAD_CONTEXTS
64     BoostContext::parmap_ = nullptr;
65     BoostContext::workers_context_.clear();
66     BoostContext::workers_context_.resize(SIMIX_context_get_nthreads(), nullptr);
67     BoostContext::maestro_context_ = nullptr;
68     xbt_os_thread_key_create(&BoostContext::worker_id_key_);
69 #else
70     xbt_die("No thread support for parallel context execution");
71 #endif
72   }
73 }
74
75 BoostContextFactory::~BoostContextFactory()
76 {
77 #if HAVE_THREAD_CONTEXTS
78   delete BoostContext::parmap_;
79   BoostContext::parmap_ = nullptr;
80   BoostContext::workers_context_.clear();
81 #endif
82 }
83
84 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
85                                                   smx_actor_t process)
86 {
87   BoostContext* context = nullptr;
88   if (BoostContext::parallel_)
89 #if HAVE_THREAD_CONTEXTS
90     context = this->new_context<BoostParallelContext>(std::move(code), cleanup_func, process);
91 #else
92     xbt_die("No support for parallel execution");
93 #endif
94   else
95     context = this->new_context<BoostSerialContext>(std::move(code), cleanup_func, process);
96   return context;
97 }
98
99 void BoostContextFactory::run_all()
100 {
101 #if HAVE_THREAD_CONTEXTS
102   if (BoostContext::parallel_) {
103     BoostContext::threads_working_ = 0;
104     if (not BoostContext::parmap_)
105       BoostContext::parmap_ =
106           new simgrid::xbt::Parmap<smx_actor_t>(SIMIX_context_get_nthreads(), SIMIX_context_get_parallel_mode());
107     BoostContext::parmap_->apply(
108         [](smx_actor_t process) {
109           BoostContext* context = static_cast<BoostContext*>(process->context);
110           return context->resume();
111         },
112         simix_global->process_to_run);
113   } else
114 #endif
115   {
116     if (simix_global->process_to_run.empty())
117       return;
118     smx_actor_t first_process    = simix_global->process_to_run.front();
119     BoostContext::process_index_ = 1;
120     /* execute the first process */
121     static_cast<BoostContext*>(first_process->context)->resume();
122   }
123 }
124
125
126 // BoostContext
127
128 void BoostContext::smx_ctx_boost_wrapper(BoostContext::ctx_arg_type arg)
129 {
130 #if BOOST_VERSION < 106100
131   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
132 #else
133   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
134   BoostContext* context                         = static_cast<BoostContext**>(arg.data)[1];
135 #endif
136   try {
137     (*context)();
138     context->Context::stop();
139   } catch (StopRequest const&) {
140     XBT_DEBUG("Caught a StopRequest");
141   }
142   context->suspend();
143 }
144
145 inline void BoostContext::smx_ctx_boost_jump_fcontext(BoostContext* from, BoostContext* to)
146 {
147 #if BOOST_VERSION < 105600
148   boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
149 #elif BOOST_VERSION < 106100
150   boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
151 #else
152   BoostContext* ctx[2]                          = {from, to};
153   boost::context::detail::transfer_t arg        = boost::context::detail::jump_fcontext(to->fc_, ctx);
154   static_cast<BoostContext**>(arg.data)[0]->fc_ = arg.fctx;
155 #endif
156 }
157
158 BoostContext::BoostContext(std::function<void()> code,
159     void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
160   : Context(std::move(code), cleanup_func, process)
161 {
162
163   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
164   if (has_code()) {
165     this->stack_ = SIMIX_context_stack_new();
166 // We need to pass the bottom of the stack to make_fcontext, depending on the stack direction it may be the lower
167 // or higher address:
168 #if PTH_STACKGROWTH == -1
169     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size;
170 #else
171     void* stack = this->stack_;
172 #endif
173 #if BOOST_VERSION < 106100
174     this->fc_ = boost::context::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
175 #else
176     this->fc_ = boost::context::detail::make_fcontext(stack, smx_context_usable_stack_size, smx_ctx_boost_wrapper);
177 #endif
178   } else {
179 #if BOOST_VERSION < 105600
180     this->fc_ = new boost::context::fcontext_t();
181 #endif
182     if (BoostContext::maestro_context_ == nullptr)
183       BoostContext::maestro_context_ = this;
184   }
185 }
186
187 BoostContext::~BoostContext()
188 {
189 #if BOOST_VERSION < 105600
190   if (not this->stack_)
191     delete this->fc_;
192 #endif
193   if (this == maestro_context_)
194     maestro_context_ = nullptr;
195   SIMIX_context_stack_delete(this->stack_);
196 }
197
198 // BoostSerialContext
199
200 void BoostContext::stop()
201 {
202   Context::stop();
203   throw StopRequest();
204 }
205
206 void BoostContext::resume()
207 {
208   SIMIX_context_set_current(this);
209   smx_ctx_boost_jump_fcontext(maestro_context_, this);
210 }
211
212 void BoostSerialContext::suspend()
213 {
214   /* determine the next context */
215   BoostSerialContext* next_context;
216   unsigned long int i = process_index_;
217   process_index_++;
218
219   if (i < simix_global->process_to_run.size()) {
220     /* execute the next process */
221     XBT_DEBUG("Run next process");
222     next_context = static_cast<BoostSerialContext*>(simix_global->process_to_run[i]->context);
223   } else {
224     /* all processes were run, return to maestro */
225     XBT_DEBUG("No more process to run");
226     next_context = static_cast<BoostSerialContext*>(maestro_context_);
227   }
228   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
229   smx_ctx_boost_jump_fcontext(this, next_context);
230 }
231
232 // BoostParallelContext
233
234 #if HAVE_THREAD_CONTEXTS
235
236 void BoostParallelContext::suspend()
237 {
238   boost::optional<smx_actor_t> next_work = parmap_->next();
239   BoostParallelContext* next_context;
240   if (next_work) {
241     XBT_DEBUG("Run next process");
242     next_context = static_cast<BoostParallelContext*>(next_work.get()->context);
243   } else {
244     XBT_DEBUG("No more processes to run");
245     uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
246     next_context = static_cast<BoostParallelContext*>(workers_context_[worker_id]);
247   }
248
249   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
250   smx_ctx_boost_jump_fcontext(this, next_context);
251 }
252
253 void BoostParallelContext::resume()
254 {
255   uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
256   xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
257
258   BoostParallelContext* worker_context = static_cast<BoostParallelContext*>(SIMIX_context_self());
259   workers_context_[worker_id] = worker_context;
260
261   SIMIX_context_set_current(this);
262   smx_ctx_boost_jump_fcontext(worker_context, this);
263 }
264
265 #endif
266
267 XBT_PRIVATE ContextFactory* boost_factory()
268 {
269   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
270   return new BoostContextFactory();
271 }
272
273 }}} // namespace