Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
314b2a95f8f702f490422d16f13d67198e89dd8a
[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   static BoostContext* maestro_context_;
41
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   static void wrapper(arg_type arg);
53   static void swap(BoostContext* from, BoostContext* to);
54
55 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
56   const void* asan_stack_ = nullptr;
57   size_t asan_stack_size_ = 0;
58   bool asan_stop_         = false;
59 #endif
60
61   void* stack_ = nullptr;
62 public:
63   BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
64   ~BoostContext() override;
65   void stop() override;
66   virtual void resume() = 0;
67
68   friend BoostContextFactory;
69 };
70
71 class SerialBoostContext : public BoostContext {
72 public:
73   SerialBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
74       : BoostContext(std::move(code), cleanup_func, process)
75   {
76   }
77   void suspend() override;
78   void resume() override;
79 };
80
81 #if HAVE_THREAD_CONTEXTS
82 class ParallelBoostContext : public BoostContext {
83 public:
84   ParallelBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
85       : BoostContext(std::move(code), cleanup_func, process)
86   {
87   }
88   void suspend() override;
89   void resume() override;
90 };
91 #endif
92
93 class BoostContextFactory : public ContextFactory {
94 public:
95   BoostContextFactory();
96   ~BoostContextFactory() override;
97   Context* create_context(std::function<void()> code, void_pfn_smxprocess_t, smx_actor_t process) override;
98   void run_all() override;
99
100 private:
101   bool parallel_;
102 };
103
104 }}} // namespace
105
106 #endif