Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
document a class
[simgrid.git] / src / kernel / context / Context.hpp
1 /* Copyright (c) 2007-2017. 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 <functional>
10 #include <memory>
11 #include <unordered_map>
12 #include <vector>
13
14 #include <xbt/functional.hpp>
15
16 #include "simgrid/simix.h"
17 #include "src/instr/instr_private.h"
18 #include "src/internal_config.h"
19 #include "src/simix/popping_private.h"
20 #include "src/simix/smx_host_private.h"
21 #include "src/simix/smx_io_private.h"
22 #include "src/simix/smx_network_private.h"
23 #include "src/simix/smx_synchro_private.hpp"
24 #include "surf/surf.h"
25 #include "xbt/base.h"
26 #include "xbt/config.h"
27 #include "xbt/function_types.h"
28 #include "xbt/mallocator.h"
29 #include "xbt/xbt_os_time.h"
30
31 #include "src/simix/ActorImpl.hpp"
32 #include <csignal>
33
34 #include <simgrid/simix.hpp>
35
36 namespace simgrid {
37 namespace kernel {
38 namespace context {
39
40   XBT_PUBLIC_CLASS ContextFactory {
41   private:
42     std::string name_;
43   public:
44
45     explicit ContextFactory(std::string name) : name_(std::move(name)) {}
46     virtual ~ContextFactory();
47     virtual Context* create_context(std::function<void()> code,
48       void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
49
50     // Optional methods for attaching main() as a context:
51
52     /** Creates a context from the current context of execution
53      *
54      *  This will not work on all implementation of `ContextFactory`.
55      */
56     virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
57     virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
58
59     virtual void run_all() = 0;
60     virtual Context* self();
61     std::string const& name() const
62     {
63       return name_;
64     }
65   private:
66     void declare_context(void* T, std::size_t size);
67   protected:
68     template<class T, class... Args>
69     T* new_context(Args&&... args)
70     {
71       T* context = new T(std::forward<Args>(args)...);
72       this->declare_context(context, sizeof(T));
73       return context;
74     }
75   };
76
77   XBT_PUBLIC_CLASS Context {
78   private:
79     std::function<void()> code_;
80     void_pfn_smxprocess_t cleanup_func_ = nullptr;
81     smx_actor_t process_ = nullptr;
82   public:
83     class StopRequest {
84       /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
85        *
86        * Nope, Sonar, this should not inherit of std::exception.
87        * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
88        */
89     };
90     bool iwannadie;
91
92     Context(std::function<void()> code,
93             void_pfn_smxprocess_t cleanup_func,
94             smx_actor_t process);
95     void operator()()
96     {
97       code_();
98     }
99     bool has_code() const
100     {
101       return static_cast<bool>(code_);
102     }
103     smx_actor_t process()
104     {
105       return this->process_;
106     }
107     void set_cleanup(void_pfn_smxprocess_t cleanup)
108     {
109       cleanup_func_ = cleanup;
110     }
111
112     // Virtual methods
113     virtual ~Context();
114     virtual void stop();
115     virtual void suspend() = 0;
116   };
117
118   XBT_PUBLIC_CLASS AttachContext : public Context {
119   public:
120
121     AttachContext(std::function<void()> code,
122             void_pfn_smxprocess_t cleanup_func,
123             smx_actor_t process)
124       : Context(std::move(code), cleanup_func, process)
125     {}
126
127     ~AttachContext() override;
128
129     /** Called by the context when it is ready to give control
130      *  to the maestro.
131      */
132     virtual void attach_start() = 0;
133
134     /** Called by the context when it has finished its job */
135     virtual void attach_stop() = 0;
136   };
137
138 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
139 typedef ContextFactory* (*ContextFactoryInitializer)();
140 XBT_PUBLIC_DATA(ContextFactoryInitializer) factory_initializer;
141
142 XBT_PRIVATE ContextFactory* thread_factory();
143 XBT_PRIVATE ContextFactory* sysv_factory();
144 XBT_PRIVATE ContextFactory* raw_factory();
145 XBT_PRIVATE ContextFactory* boost_factory();
146
147 }}}
148
149 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
150
151 SG_BEGIN_DECL()
152
153
154 XBT_PRIVATE void SIMIX_context_mod_init();
155 XBT_PRIVATE void SIMIX_context_mod_exit();
156
157 XBT_PUBLIC(smx_context_t)
158 SIMIX_context_new(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t simix_process);
159
160 #ifndef WIN32
161 XBT_PUBLIC_DATA(char sigsegv_stack[SIGSTKSZ]);
162 #endif
163
164 /* We are using the bottom of the stack to save some information, like the
165  * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
166  * size for the stack. Round its value to a multiple of 16 (asan wants the stacks to be aligned this way). */
167 #if HAVE_VALGRIND_H
168 #define smx_context_usable_stack_size                                                                                  \
169   ((smx_context_stack_size - sizeof(unsigned int)) & ~0xf) /* for valgrind_stack_id */
170 #else
171 #define smx_context_usable_stack_size (smx_context_stack_size & ~0xf)
172 #endif
173
174 /** @brief Executes all the processes to run (in parallel if possible). */
175 XBT_PRIVATE void SIMIX_context_runall();
176 /** @brief returns the current running context */
177 XBT_PUBLIC(smx_context_t) SIMIX_context_self(); // public because it's used in simgrid-java
178
179 XBT_PRIVATE void *SIMIX_context_stack_new();
180 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
181
182 XBT_PUBLIC(void) SIMIX_context_set_current(smx_context_t context);
183 XBT_PRIVATE smx_context_t SIMIX_context_get_current();
184
185 XBT_PUBLIC(int) SIMIX_process_get_maxpid();
186
187 XBT_PRIVATE void SIMIX_post_create_environment();
188
189 SG_END_DECL()
190
191 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name);
192
193 #endif