Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Encapsulate main function, argc and argv in a closure
[simgrid.git] / src / simix / BoostContext.hpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /** @file BoostContext.hpp
8     Userspace context switching implementation based on Boost.Context */
9
10 #ifndef SIMGRID_SIMIX_BOOST_CONTEXT_HPP
11 #define SIMGRID_SIMIX_BOOST_CONTEXT_HPP
12
13 #include <vector>
14
15 #include <xbt/parmap.h>
16
17 #include <simgrid/simix.hpp>
18
19
20 namespace simgrid {
21 namespace simix {
22
23 class BoostContext;
24 class BoostSerialContext;
25 class BoostParallelContext;
26 class BoostContextFactory;
27
28 class BoostContext : public Context {
29 protected: // static
30   static bool parallel_;
31   static xbt_parmap_t parmap_;
32   static std::vector<BoostContext*> workers_context_;
33   static unsigned long threads_working_;
34   static xbt_os_thread_key_t worker_id_key_;
35   static unsigned long process_index_;
36   static BoostContext* maestro_context_;
37 protected:
38 #if HAVE_BOOST_CONTEXT == 1
39   boost::context::fcontext_t* fc_ = nullptr;
40 #else
41   boost::context::fcontext_t fc_;
42 #endif
43   void* stack_ = nullptr;
44 public:
45   friend BoostContextFactory;
46   BoostContext(std::function<void()> code,
47           void_pfn_smxprocess_t cleanup_func,
48           smx_process_t process);
49   ~BoostContext();
50   void resume();
51 private:
52   static void wrapper(int first, ...);
53 };
54
55 class BoostContextFactory : public ContextFactory {
56 public:
57   friend BoostContext;
58   friend BoostSerialContext;
59   friend BoostParallelContext;
60
61   BoostContextFactory();
62   virtual ~BoostContextFactory();
63   virtual Context* create_context(std::function<void()> code,
64     void_pfn_smxprocess_t, smx_process_t process) override;
65   void run_all() override;
66 };
67
68 }
69 }
70
71 #endif