Logo AND Algorithmique Numérique Distribuée

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