Logo AND Algorithmique Numérique Distribuée

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