Logo AND Algorithmique Numérique Distribuée

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