Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce the scope of some #include, and cut useless ones
[simgrid.git] / src / kernel / context / ContextBoost.hpp
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 #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 "Context.hpp"
26 #include "src/internal_config.h"
27 #include "src/simix/smx_private.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 Context {
35 public:
36   BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
37   ~BoostContext() override;
38   void stop() override;
39   virtual void resume() = 0;
40
41   static void swap(BoostContext* from, BoostContext* to);
42   static BoostContext* getMaestro() { return maestro_context_; }
43   static void setMaestro(BoostContext* maestro) { maestro_context_ = maestro; }
44
45 private:
46   static BoostContext* maestro_context_;
47   void* stack_ = nullptr;
48
49 #if BOOST_VERSION < 105600
50   boost::context::fcontext_t* fc_ = nullptr;
51   typedef intptr_t arg_type;
52 #elif BOOST_VERSION < 106100
53   boost::context::fcontext_t fc_;
54   typedef intptr_t arg_type;
55 #else
56   boost::context::detail::fcontext_t fc_;
57   typedef boost::context::detail::transfer_t arg_type;
58 #endif
59 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
60   const void* asan_stack_ = nullptr;
61   size_t asan_stack_size_ = 0;
62   bool asan_stop_         = false;
63 #endif
64
65   static void wrapper(arg_type arg);
66 };
67
68 class SerialBoostContext : public BoostContext {
69 public:
70   SerialBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
71       : BoostContext(std::move(code), cleanup_func, process)
72   {
73   }
74   void suspend() override;
75   void resume() override;
76
77   static void run_all();
78
79 private:
80   static unsigned long process_index_;
81 };
82
83 #if HAVE_THREAD_CONTEXTS
84 class ParallelBoostContext : public BoostContext {
85 public:
86   ParallelBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
87       : BoostContext(std::move(code), cleanup_func, process)
88   {
89   }
90   void suspend() override;
91   void resume() override;
92
93   static void initialize();
94   static void finalize();
95   static void run_all();
96
97 private:
98   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
99   static std::vector<ParallelBoostContext*> workers_context_;
100   static std::atomic<uintptr_t> threads_working_;
101   static xbt_os_thread_key_t worker_id_key_;
102 };
103 #endif
104
105 class BoostContextFactory : public ContextFactory {
106 public:
107   BoostContextFactory();
108   ~BoostContextFactory() override;
109   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
110   void run_all() override;
111
112 private:
113   bool parallel_;
114 };
115 }}} // namespace
116
117 #endif