Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d78cc2336d06074aa4c16cdb0b0ae70cb29cccf7
[simgrid.git] / src / kernel / context / ContextBoost.hpp
1 /* Copyright (c) 2015-2019. 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 #ifndef SIMGRID_SIMIX_BOOST_CONTEXT_HPP
7 #define SIMGRID_SIMIX_BOOST_CONTEXT_HPP
8
9 #include <boost/version.hpp>
10 #if BOOST_VERSION < 106100
11 #include <boost/context/fcontext.hpp>
12 #else
13 #include <boost/context/detail/fcontext.hpp>
14 #endif
15
16 #include <atomic>
17 #include <cstdint>
18 #include <functional>
19 #include <vector>
20
21 #include <simgrid/simix.hpp>
22 #include <xbt/parmap.hpp>
23
24 #include "src/internal_config.h"
25 #include "src/kernel/context/Context.hpp"
26 #include "src/kernel/context/ContextSwapped.hpp"
27
28 namespace simgrid {
29 namespace kernel {
30 namespace context {
31
32 /** @brief Userspace context switching implementation based on Boost.Context */
33 class BoostContext : public SwappedContext {
34 public:
35   BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor,
36                SwappedContextFactory* factory);
37   ~BoostContext() override;
38
39   void swap_into(SwappedContext* to) override;
40
41 private:
42 #if BOOST_VERSION < 105600
43   boost::context::fcontext_t* fc_ = nullptr;
44   typedef intptr_t arg_type;
45 #elif BOOST_VERSION < 106100
46   boost::context::fcontext_t fc_;
47   typedef intptr_t arg_type;
48 #else
49   boost::context::detail::fcontext_t fc_;
50   typedef boost::context::detail::transfer_t arg_type;
51 #endif
52 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
53   const void* asan_stack_ = nullptr;
54   size_t asan_stack_size_ = 0;
55   BoostContext* asan_ctx_ = nullptr;
56   bool asan_stop_         = false;
57 #endif
58
59   static void wrapper(arg_type arg);
60 };
61
62 class BoostContextFactory : public SwappedContextFactory {
63 public:
64   BoostContextFactory() : SwappedContextFactory("BoostContextFactory") {}
65
66   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
67 };
68 }}} // namespace
69
70 #endif