Logo AND Algorithmique Numérique Distribuée

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