Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in UCtx: hide one internal function and inline another
[simgrid.git] / src / kernel / context / Context.hpp
1 /* Copyright (c) 2007-2019. 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 public:
24   explicit ContextFactory() {}
25   virtual ~ContextFactory();
26   virtual Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
27
28   /** Turn the current thread into a simulation context */
29   virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
30   /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
31   virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
32
33   virtual void run_all() = 0;
34
35 protected:
36   template <class T, class... Args> T* new_context(Args&&... args)
37   {
38     T* context = new T(std::forward<Args>(args)...);
39     context->declare_context(sizeof(T));
40     return context;
41   }
42 };
43
44 class XBT_PUBLIC Context {
45   friend ContextFactory;
46
47 private:
48   std::function<void()> code_;
49   void_pfn_smxprocess_t cleanup_func_ = nullptr;
50   smx_actor_t actor_                  = nullptr;
51   void declare_context(std::size_t size);
52
53 public:
54   bool iwannadie = false;
55
56   Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor);
57   Context(const Context&) = delete;
58   Context& operator=(const Context&) = delete;
59   virtual ~Context();
60
61   void operator()() { code_(); }
62   bool has_code() const { return static_cast<bool>(code_); }
63   smx_actor_t get_actor() { return this->actor_; }
64   void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
65
66   // Scheduling methods
67   virtual void stop();
68   virtual void suspend() = 0;
69
70   // Retrieving the self() context
71   /** @brief Retrives the current context of this thread */
72   static Context* self();
73   /** @brief Sets the current context of this thread */
74   static void set_current(Context* self);
75
76   class StopRequest {
77     /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
78      *
79      * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
80      * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
81      */
82   public:
83     StopRequest() = default;
84     explicit StopRequest(std::string msg) : msg_(std::string("Actor killed (") + msg + std::string(").")) {}
85     const char* what() const noexcept { return msg_.c_str(); }
86
87   private:
88     std::string msg_ = std::string("Actor killed.");
89   };
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
146 XBT_PUBLIC int SIMIX_process_get_maxpid();
147
148 XBT_PRIVATE void SIMIX_post_create_environment();
149
150 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(std::string name);
151
152 #endif