Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1bbcd8b0a7db9ae2b8cb4008fc13b0cacc381539
[simgrid.git] / src / kernel / context / Context.hpp
1 /* Copyright (c) 2007-2016. 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 "src/internal_config.h"
17 #include "simgrid/simix.h"
18 #include "surf/surf.h"
19 #include "xbt/base.h"
20 #include "xbt/swag.h"
21 #include "xbt/dict.h"
22 #include "xbt/mallocator.h"
23 #include "xbt/config.h"
24 #include "xbt/xbt_os_time.h"
25 #include "xbt/function_types.h"
26 #include "src/xbt/ex_interface.h"
27 #include "src/instr/instr_private.h"
28 #include "src/simix/smx_host_private.h"
29 #include "src/simix/smx_io_private.h"
30 #include "src/simix/smx_network_private.h"
31 #include "src/simix/popping_private.h"
32 #include "src/simix/smx_synchro_private.h"
33
34 #include <signal.h>
35 #include "src/simix/ActorImpl.hpp"
36
37 #include <simgrid/simix.hpp>
38
39 namespace simgrid {
40 namespace kernel {
41 namespace context {
42
43   XBT_PUBLIC_CLASS ContextFactory {
44   private:
45     std::string name_;
46   public:
47
48     explicit ContextFactory(std::string name) : name_(std::move(name)) {}
49     virtual ~ContextFactory();
50     virtual Context* create_context(std::function<void()> code,
51       void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
52
53     // Optional methods for attaching main() as a context:
54
55     /** Creates a context from the current context of execution
56      *
57      *  This will not work on all implementation of `ContextFactory`.
58      */
59     virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
60     virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
61
62     virtual void run_all() = 0;
63     virtual Context* self();
64     std::string const& name() const
65     {
66       return name_;
67     }
68   private:
69     void declare_context(void* T, std::size_t size);
70   protected:
71     template<class T, class... Args>
72     T* new_context(Args&&... args)
73     {
74       T* context = new T(std::forward<Args>(args)...);
75       this->declare_context(context, sizeof(T));
76       return context;
77     }
78   };
79
80   XBT_PUBLIC_CLASS Context {
81   private:
82     std::function<void()> code_;
83     void_pfn_smxprocess_t cleanup_func_ = nullptr;
84     smx_actor_t process_ = nullptr;
85   public:
86     bool iwannadie;
87   public:
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_PRIVATE smx_context_t SIMIX_context_new(
154   std::function<void()> code,
155   void_pfn_smxprocess_t cleanup_func,
156   smx_actor_t simix_process);
157
158 #ifndef WIN32
159 XBT_PUBLIC_DATA(char sigsegv_stack[SIGSTKSZ]);
160 #endif
161
162 /* We are using the bottom of the stack to save some information, like the
163  * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
164  * size for the stack. */
165 #if HAVE_VALGRIND_H
166 # define smx_context_usable_stack_size                                  \
167   (smx_context_stack_size - sizeof(unsigned int)) /* for valgrind_stack_id */
168 #else
169 # define smx_context_usable_stack_size smx_context_stack_size
170 #endif
171
172 /** @brief Executes all the processes to run (in parallel if possible). */
173 XBT_PRIVATE void SIMIX_context_runall();
174 /** @brief returns the current running context */
175 XBT_PUBLIC(smx_context_t) SIMIX_context_self(); // public because it's used in simgrid-java
176
177 XBT_PRIVATE void *SIMIX_context_stack_new();
178 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
179
180 XBT_PRIVATE void SIMIX_context_set_current(smx_context_t context);
181 XBT_PRIVATE smx_context_t SIMIX_context_get_current();
182
183 XBT_PUBLIC(int) SIMIX_process_get_maxpid();
184
185 XBT_PRIVATE void SIMIX_post_create_environment();
186
187 SG_END_DECL()
188
189 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name);
190
191 #endif