Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename StopRequest into ForcefulKillException to contribute to #325
[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/kernel/activity/ActivityImpl.hpp"
11
12 #include <csignal>
13 #include <functional>
14
15 namespace simgrid {
16 namespace kernel {
17 namespace context {
18
19 class XBT_PUBLIC ContextFactory {
20 public:
21   explicit ContextFactory() {}
22   ContextFactory(const ContextFactory&) = delete;
23   ContextFactory& operator=(const ContextFactory&) = delete;
24   virtual ~ContextFactory();
25   virtual Context* create_context(std::function<void()> code, smx_actor_t actor) = 0;
26
27   /** Turn the current thread into a simulation context */
28   virtual Context* attach(smx_actor_t actor);
29   /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
30   virtual Context* create_maestro(std::function<void()> code, smx_actor_t actor);
31
32   virtual void run_all() = 0;
33
34 protected:
35   template <class T, class... Args> T* new_context(Args&&... args)
36   {
37     T* context = new T(std::forward<Args>(args)...);
38     context->declare_context(sizeof(T));
39     return context;
40   }
41 };
42
43 class XBT_PUBLIC Context {
44   friend ContextFactory;
45
46   std::function<void()> code_;
47   smx_actor_t actor_                  = nullptr;
48   void declare_context(std::size_t size);
49
50 public:
51   bool iwannadie = false;
52
53   Context(std::function<void()> code, smx_actor_t actor);
54   Context(const Context&) = delete;
55   Context& operator=(const Context&) = delete;
56   virtual ~Context();
57
58   void operator()() { code_(); }
59   bool has_code() const { return static_cast<bool>(code_); }
60   smx_actor_t get_actor() { return this->actor_; }
61
62   // Scheduling methods
63   virtual void stop();
64   virtual void suspend() = 0;
65
66   // Retrieving the self() context
67   /** @brief Retrives the current context of this thread */
68   static Context* self();
69   /** @brief Sets the current context of this thread */
70   static void set_current(Context* self);
71 };
72
73 class XBT_PUBLIC AttachContext : public Context {
74 public:
75   AttachContext(std::function<void()> code, smx_actor_t actor) : Context(std::move(code), actor) {}
76   AttachContext(const AttachContext&) = delete;
77   AttachContext& operator=(const AttachContext&) = delete;
78   ~AttachContext() override;
79
80   /** Called by the context when it is ready to give control
81    *  to the maestro.
82    */
83   virtual void attach_start() = 0;
84
85   /** Called by the context when it has finished its job */
86   virtual void attach_stop() = 0;
87 };
88
89 class XBT_PUBLIC ForcefulKillException {
90   /** @brief Exception launched to kill an actor; DO NOT BLOCK IT!
91    *
92    * This exception is thrown whenever the actor's host is turned off. The actor stack is properly unwinded to release
93    * all objects allocated on the stack (RAII powa).
94    *
95    * You may want to catch this exception to perform some extra cleanups in your simulation, but YOUR ACTORS MUST NEVER
96    * SURVIVE a ForcefulKillException, or your simulation will segfault.
97    *
98    * @verbatim
99    * void* payload = malloc(512);
100    *
101    * try {
102    *   simgrid::s4u::this_actor::execute(100000);
103    * } catch (simgrid::kernel::context::ForcefulKillException& e) { // oops, my host just turned off
104    *   free(malloc);
105    *   throw; // I shall never survive on an host that was switched off
106    * }
107    * @endverbatim
108    *
109    * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
110    * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
111    */
112 public:
113   ForcefulKillException() = default;
114   explicit ForcefulKillException(const std::string& msg) : msg_(std::string("Actor killed (") + msg + std::string(")."))
115   {
116   }
117   ~ForcefulKillException();
118   const char* what() const noexcept { return msg_.c_str(); }
119
120   static void do_throw();
121   static bool try_n_catch(std::function<void(void)> try_block);
122
123 private:
124   std::string msg_ = std::string("Actor killed.");
125 };
126
127 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
128 typedef ContextFactory* (*ContextFactoryInitializer)();
129 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
130
131 XBT_PRIVATE ContextFactory* thread_factory();
132 XBT_PRIVATE ContextFactory* sysv_factory();
133 XBT_PRIVATE ContextFactory* raw_factory();
134 XBT_PRIVATE ContextFactory* boost_factory();
135
136 }}}
137
138 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
139
140 XBT_PRIVATE void SIMIX_context_mod_init();
141 XBT_PRIVATE void SIMIX_context_mod_exit();
142
143 #ifndef WIN32
144 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
145 #endif
146
147 /** @brief Executes all the processes to run (in parallel if possible). */
148 XBT_PRIVATE void SIMIX_context_runall();
149
150 XBT_PUBLIC int SIMIX_process_get_maxpid();
151
152 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const std::string& name);
153
154 #endif