Logo AND Algorithmique Numérique Distribuée

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