Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar
[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   virtual ~ContextFactory();
23   virtual Context* create_context(std::function<void()> code, smx_actor_t actor) = 0;
24
25   /** Turn the current thread into a simulation context */
26   virtual Context* attach(smx_actor_t actor);
27   /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
28   virtual Context* create_maestro(std::function<void()> code, smx_actor_t actor);
29
30   virtual void run_all() = 0;
31
32 protected:
33   template <class T, class... Args> T* new_context(Args&&... args)
34   {
35     T* context = new T(std::forward<Args>(args)...);
36     context->declare_context(sizeof(T));
37     return context;
38   }
39 };
40
41 class XBT_PUBLIC Context {
42   friend ContextFactory;
43
44   std::function<void()> code_;
45   smx_actor_t actor_                  = nullptr;
46   void declare_context(std::size_t size);
47
48 public:
49   bool iwannadie = false;
50
51   Context(std::function<void()> code, smx_actor_t actor);
52   Context(const Context&) = delete;
53   Context& operator=(const Context&) = delete;
54   virtual ~Context();
55
56   void operator()() { code_(); }
57   bool has_code() const { return static_cast<bool>(code_); }
58   smx_actor_t get_actor() { return this->actor_; }
59
60   // Scheduling methods
61   virtual void stop();
62   virtual void suspend() = 0;
63
64   // Retrieving the self() context
65   /** @brief Retrives the current context of this thread */
66   static Context* self();
67   /** @brief Sets the current context of this thread */
68   static void set_current(Context* self);
69 };
70
71 class XBT_PUBLIC AttachContext : public Context {
72 public:
73   AttachContext(std::function<void()> code, smx_actor_t actor) : Context(std::move(code), actor) {}
74
75   ~AttachContext() override;
76
77   /** Called by the context when it is ready to give control
78    *  to the maestro.
79    */
80   virtual void attach_start() = 0;
81
82   /** Called by the context when it has finished its job */
83   virtual void attach_stop() = 0;
84 };
85
86 class XBT_PUBLIC StopRequest {
87   /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
88    *
89    * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
90    * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
91    */
92 public:
93   StopRequest() = default;
94   explicit StopRequest(const std::string& msg) : msg_(std::string("Actor killed (") + msg + std::string(").")) {}
95   ~StopRequest();
96   const char* what() const noexcept { return msg_.c_str(); }
97
98   static void do_throw();
99   static bool try_n_catch(std::function<void(void)> try_block);
100
101 private:
102   std::string msg_ = std::string("Actor killed.");
103 };
104
105 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
106 typedef ContextFactory* (*ContextFactoryInitializer)();
107 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
108
109 XBT_PRIVATE ContextFactory* thread_factory();
110 XBT_PRIVATE ContextFactory* sysv_factory();
111 XBT_PRIVATE ContextFactory* raw_factory();
112 XBT_PRIVATE ContextFactory* boost_factory();
113
114 }}}
115
116 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
117
118 XBT_PRIVATE void SIMIX_context_mod_init();
119 XBT_PRIVATE void SIMIX_context_mod_exit();
120
121 #ifndef WIN32
122 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
123 #endif
124
125 /** @brief Executes all the processes to run (in parallel if possible). */
126 XBT_PRIVATE void SIMIX_context_runall();
127
128 XBT_PUBLIC int SIMIX_process_get_maxpid();
129
130 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const std::string& name);
131
132 #endif