Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move StopRequest out of Context.
[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 /* 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
77 class XBT_PUBLIC AttachContext : public Context {
78 public:
79   AttachContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor)
80       : Context(std::move(code), cleanup_func, actor)
81   {
82   }
83
84   ~AttachContext() override;
85
86   /** Called by the context when it is ready to give control
87    *  to the maestro.
88    */
89   virtual void attach_start() = 0;
90
91   /** Called by the context when it has finished its job */
92   virtual void attach_stop() = 0;
93 };
94
95 class XBT_PUBLIC StopRequest {
96   /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
97    *
98    * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
99    * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
100    */
101 public:
102   StopRequest() = default;
103   explicit StopRequest(std::string msg) : msg_(std::string("Actor killed (") + msg + std::string(").")) {}
104   ~StopRequest();
105   const char* what() const noexcept { return msg_.c_str(); }
106
107 private:
108   std::string msg_ = std::string("Actor killed.");
109 };
110 XBT_PUBLIC void throw_stoprequest();
111 XBT_PUBLIC bool try_n_catch_stoprequest(std::function<void(void)> try_block);
112
113 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
114 typedef ContextFactory* (*ContextFactoryInitializer)();
115 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
116
117 XBT_PRIVATE ContextFactory* thread_factory();
118 XBT_PRIVATE ContextFactory* sysv_factory();
119 XBT_PRIVATE ContextFactory* raw_factory();
120 XBT_PRIVATE ContextFactory* boost_factory();
121
122 }}}
123
124 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
125
126 XBT_PRIVATE void SIMIX_context_mod_init();
127 XBT_PRIVATE void SIMIX_context_mod_exit();
128
129 XBT_PUBLIC smx_context_t SIMIX_context_new(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
130                                            smx_actor_t simix_process);
131
132 #ifndef WIN32
133 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
134 #endif
135
136 /** @brief Executes all the processes to run (in parallel if possible). */
137 XBT_PRIVATE void SIMIX_context_runall();
138
139 XBT_PUBLIC int SIMIX_process_get_maxpid();
140
141 XBT_PRIVATE void SIMIX_post_create_environment();
142
143 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(std::string name);
144
145 #endif