Logo AND Algorithmique Numérique Distribuée

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