Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d5eca56cd5ffbc9d7879775500bb753bec32506b
[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 #include <functional>
16 #include <vector>
17
18 #include <xbt/parmap.hpp>
19
20 #include <simgrid/simix.hpp>
21
22
23 namespace simgrid {
24 namespace kernel {
25 namespace context {
26
27 class BoostContext;
28 class BoostSerialContext;
29 class BoostParallelContext;
30 class BoostContextFactory;
31
32 /** @brief Userspace context switching implementation based on Boost.Context */
33 class BoostContext : public Context {
34 protected: // static
35   static bool parallel_;
36   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
37   static std::vector<BoostContext*> workers_context_;
38   static uintptr_t threads_working_;
39   static xbt_os_thread_key_t worker_id_key_;
40   static unsigned long process_index_;
41   static BoostContext* maestro_context_;
42
43 #if BOOST_VERSION < 105600
44   boost::context::fcontext_t* fc_ = nullptr;
45   typedef intptr_t ctx_arg_type;
46 #elif BOOST_VERSION < 106100
47   boost::context::fcontext_t fc_;
48   typedef intptr_t ctx_arg_type;
49 #else
50   boost::context::detail::fcontext_t fc_;
51   typedef boost::context::detail::transfer_t ctx_arg_type;
52 #endif
53   static void smx_ctx_boost_wrapper(ctx_arg_type);
54   static void smx_ctx_boost_jump_fcontext(BoostContext*, BoostContext*);
55
56   void* stack_ = nullptr;
57 public:
58   friend BoostContextFactory;
59   BoostContext(std::function<void()> code,
60           void_pfn_smxprocess_t cleanup_func,
61           smx_actor_t process);
62   ~BoostContext() override;
63   void stop() override;
64   virtual void resume();
65 private:
66   static void wrapper(int first, ...);
67 };
68
69 class BoostContextFactory : public ContextFactory {
70 public:
71   friend BoostContext;
72   friend BoostSerialContext;
73   friend BoostParallelContext;
74
75   BoostContextFactory();
76   ~BoostContextFactory() override;
77   Context* create_context(std::function<void()> code,
78     void_pfn_smxprocess_t, smx_actor_t process) override;
79   void run_all() override;
80 };
81
82 }}} // namespace
83
84 #endif