Logo AND Algorithmique Numérique Distribuée

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