Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move a static field of SwappedContext into SwappedContextFactory
[simgrid.git] / src / kernel / context / ContextBoost.hpp
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 #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 #include <xbt/xbt_os_thread.h>
24
25 #include "src/internal_config.h"
26 #include "src/kernel/context/Context.hpp"
27 #include "src/kernel/context/ContextSwapped.hpp"
28
29 namespace simgrid {
30 namespace kernel {
31 namespace context {
32
33 /** @brief Userspace context switching implementation based on Boost.Context */
34 class BoostContext : public SwappedContext {
35 public:
36   BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
37                SwappedContextFactory* factory);
38   ~BoostContext() override;
39   void stop() override;
40
41   void swap_into(SwappedContext* to) override;
42
43 private:
44 #if BOOST_VERSION < 105600
45   boost::context::fcontext_t* fc_ = nullptr;
46   typedef intptr_t arg_type;
47 #elif BOOST_VERSION < 106100
48   boost::context::fcontext_t fc_;
49   typedef intptr_t arg_type;
50 #else
51   boost::context::detail::fcontext_t fc_;
52   typedef boost::context::detail::transfer_t arg_type;
53 #endif
54 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
55   const void* asan_stack_ = nullptr;
56   size_t asan_stack_size_ = 0;
57   BoostContext* asan_ctx_ = nullptr;
58   bool asan_stop_         = false;
59 #endif
60
61   static void wrapper(arg_type arg);
62 };
63
64 class ParallelBoostContext : public BoostContext {
65 public:
66   ParallelBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
67                        SwappedContextFactory* factory)
68       : BoostContext(std::move(code), cleanup_func, process, factory)
69   {
70   }
71   void suspend() override;
72   void resume() override;
73 };
74
75 class BoostContextFactory : public SwappedContextFactory {
76 public:
77   BoostContextFactory();
78   ~BoostContextFactory() override;
79   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
80 };
81 }}} // namespace
82
83 #endif