Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ContextBoost: move static fields where they belong to.
[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/context/all.hpp>
10
11 #include <cstdint>
12 #include <functional>
13 #include <vector>
14
15 #include <simgrid/simix.hpp>
16 #include <xbt/parmap.hpp>
17 #include <xbt/xbt_os_thread.h>
18
19 #include "Context.hpp"
20 #include "src/internal_config.h"
21 #include "src/simix/smx_private.hpp"
22
23 namespace simgrid {
24 namespace kernel {
25 namespace context {
26
27 class BoostContext;
28 class SerialBoostContext;
29 class ParallelBoostContext;
30 class BoostContextFactory;
31
32 /** @brief Userspace context switching implementation based on Boost.Context */
33 class BoostContext : public Context {
34 public:
35   BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
36   ~BoostContext() override;
37   void stop() override;
38   virtual void resume() = 0;
39
40   static void swap(BoostContext* from, BoostContext* to);
41   static BoostContext* getMaestro() { return maestro_context_; }
42   static void setMaestro(BoostContext* maestro) { maestro_context_ = maestro; }
43
44 private:
45   static BoostContext* maestro_context_;
46   void* stack_ = nullptr;
47
48 #if BOOST_VERSION < 105600
49   boost::context::fcontext_t* fc_ = nullptr;
50   typedef intptr_t arg_type;
51 #elif BOOST_VERSION < 106100
52   boost::context::fcontext_t fc_;
53   typedef intptr_t arg_type;
54 #else
55   boost::context::detail::fcontext_t fc_;
56   typedef boost::context::detail::transfer_t arg_type;
57 #endif
58 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
59   const void* asan_stack_ = nullptr;
60   size_t asan_stack_size_ = 0;
61   bool asan_stop_         = false;
62 #endif
63
64   static void wrapper(arg_type arg);
65 };
66
67 class SerialBoostContext : public BoostContext {
68   friend BoostContextFactory;
69
70 public:
71   SerialBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
72       : BoostContext(std::move(code), cleanup_func, process)
73   {
74   }
75   void suspend() override;
76   void resume() override;
77
78 private:
79   static unsigned long process_index_;
80 };
81
82 #if HAVE_THREAD_CONTEXTS
83 class ParallelBoostContext : public BoostContext {
84   friend BoostContextFactory;
85
86 public:
87   ParallelBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
88       : BoostContext(std::move(code), cleanup_func, process)
89   {
90   }
91   void suspend() override;
92   void resume() override;
93
94 private:
95   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
96   static std::vector<BoostContext*> workers_context_;
97   static uintptr_t threads_working_;
98   static xbt_os_thread_key_t worker_id_key_;
99 };
100 #endif
101
102 class BoostContextFactory : public ContextFactory {
103 public:
104   BoostContextFactory();
105   ~BoostContextFactory() override;
106   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t, smx_actor_t process) override;
107   void run_all() override;
108
109 private:
110   bool parallel_;
111 };
112
113 }}} // namespace
114
115 #endif