Logo AND Algorithmique Numérique Distribuée

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