Logo AND Algorithmique Numérique Distribuée

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