Logo AND Algorithmique Numérique Distribuée

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