Logo AND Algorithmique Numérique Distribuée

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