Logo AND Algorithmique Numérique Distribuée

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