Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / kernel / context / Context.hpp
1 /* Copyright (c) 2007-2019. 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_KERNEL_CONTEXT_CONTEXT_HPP
7 #define SIMGRID_KERNEL_CONTEXT_CONTEXT_HPP
8
9 #include "simgrid/forward.h"
10 #include "src/kernel/activity/ActivityImpl.hpp"
11
12 #include <csignal>
13
14 /* Process creation/destruction callbacks */
15 typedef void (*void_pfn_smxprocess_t)(smx_actor_t);
16
17 namespace simgrid {
18 namespace kernel {
19 namespace context {
20
21 class XBT_PUBLIC ContextFactory {
22 public:
23   explicit ContextFactory() {}
24   virtual ~ContextFactory();
25   virtual Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
26
27   /** Turn the current thread into a simulation context */
28   virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
29   /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
30   virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
31
32   virtual void run_all() = 0;
33
34 protected:
35   template <class T, class... Args> T* new_context(Args&&... args)
36   {
37     T* context = new T(std::forward<Args>(args)...);
38     context->declare_context(sizeof(T));
39     return context;
40   }
41 };
42
43 class XBT_PUBLIC Context {
44   friend ContextFactory;
45
46 private:
47   std::function<void()> code_;
48   void_pfn_smxprocess_t cleanup_func_ = nullptr;
49   smx_actor_t actor_                  = nullptr;
50   void declare_context(std::size_t size);
51
52 public:
53   bool iwannadie = false;
54
55   Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor);
56   Context(const Context&) = delete;
57   Context& operator=(const Context&) = delete;
58   virtual ~Context();
59
60   void operator()() { code_(); }
61   bool has_code() const { return static_cast<bool>(code_); }
62   smx_actor_t get_actor() { return this->actor_; }
63   void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
64
65   // Scheduling methods
66   virtual void stop();
67   virtual void suspend() = 0;
68
69   // Retrieving the self() context
70   /** @brief Retrives the current context of this thread */
71   static Context* self();
72   /** @brief Sets the current context of this thread */
73   static void set_current(Context* self);
74
75   class StopRequest {
76     /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
77      *
78      * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
79      * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
80      */
81   public:
82     StopRequest() = default;
83     explicit StopRequest(std::string msg) : msg_(std::string("Actor killed (") + msg + std::string(").")) {}
84     const char* what() const noexcept { return msg_.c_str(); }
85
86   private:
87     std::string msg_ = std::string("Actor killed.");
88   };
89 };
90
91 class XBT_PUBLIC AttachContext : public Context {
92 public:
93   AttachContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor)
94       : Context(std::move(code), cleanup_func, actor)
95   {
96   }
97
98   ~AttachContext() override;
99
100   /** Called by the context when it is ready to give control
101    *  to the maestro.
102    */
103   virtual void attach_start() = 0;
104
105   /** Called by the context when it has finished its job */
106   virtual void attach_stop() = 0;
107 };
108
109 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
110 typedef ContextFactory* (*ContextFactoryInitializer)();
111 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
112
113 XBT_PRIVATE ContextFactory* thread_factory();
114 XBT_PRIVATE ContextFactory* sysv_factory();
115 XBT_PRIVATE ContextFactory* raw_factory();
116 XBT_PRIVATE ContextFactory* boost_factory();
117
118 }}}
119
120 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
121
122 XBT_PRIVATE void SIMIX_context_mod_init();
123 XBT_PRIVATE void SIMIX_context_mod_exit();
124
125 XBT_PUBLIC smx_context_t SIMIX_context_new(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
126                                            smx_actor_t simix_process);
127
128 #ifndef WIN32
129 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
130 #endif
131
132 /** @brief Executes all the processes to run (in parallel if possible). */
133 XBT_PRIVATE void SIMIX_context_runall();
134
135 XBT_PUBLIC int SIMIX_process_get_maxpid();
136
137 XBT_PRIVATE void SIMIX_post_create_environment();
138
139 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(std::string name);
140
141 #endif