Logo AND Algorithmique Numérique Distribuée

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