Logo AND Algorithmique Numérique Distribuée

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