Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a race condition
[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 "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 private:
24   std::string name_;
25
26 public:
27   explicit ContextFactory(std::string name) : name_(std::move(name)) {}
28   virtual ~ContextFactory();
29   virtual Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
30
31   /** Turn the current thread into a simulation context */
32   virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
33   /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
34   virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
35
36   virtual void run_all() = 0;
37   std::string const& name() const { return name_; }
38
39 protected:
40   template <class T, class... Args> T* new_context(Args&&... args)
41   {
42     T* context = new T(std::forward<Args>(args)...);
43     context->declare_context(sizeof(T));
44     return context;
45   }
46 };
47
48 class XBT_PUBLIC Context {
49   friend ContextFactory;
50
51 private:
52   std::function<void()> code_;
53   void_pfn_smxprocess_t cleanup_func_ = nullptr;
54   smx_actor_t actor_                  = nullptr;
55   void declare_context(std::size_t size);
56
57 public:
58   bool iwannadie = false;
59
60   Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
61   Context(const Context&) = delete;
62   Context& operator=(const Context&) = delete;
63   virtual ~Context();
64
65   void operator()() { code_(); }
66   bool has_code() const { return static_cast<bool>(code_); }
67   smx_actor_t process() { return this->actor_; }
68   void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
69
70   // Scheduling methods
71   virtual void stop();
72   virtual void suspend() = 0;
73
74   // Retrieving the self() context
75   /** @brief Retrives the current context of this thread */
76   static Context* self();
77   /** @brief Sets the current context of this thread */
78   static void set_current(Context* self);
79
80   class StopRequest {
81     /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
82      *
83      * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
84      * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
85      */
86   public:
87     StopRequest() = default;
88     explicit StopRequest(std::string msg) : msg_(msg) {}
89
90   private:
91     std::string msg_;
92   };
93 };
94
95 class XBT_PUBLIC AttachContext : public Context {
96 public:
97   AttachContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor)
98       : Context(std::move(code), cleanup_func, actor)
99   {
100   }
101
102   ~AttachContext() override;
103
104   /** Called by the context when it is ready to give control
105    *  to the maestro.
106    */
107   virtual void attach_start() = 0;
108
109   /** Called by the context when it has finished its job */
110   virtual void attach_stop() = 0;
111 };
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 /* We are using the bottom of the stack to save some information, like the
137  * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
138  * size for the stack. Round its value to a multiple of 16 (asan wants the stacks to be aligned this way). */
139 #if HAVE_VALGRIND_H
140 #define smx_context_usable_stack_size                                                                                  \
141   ((smx_context_stack_size - sizeof(unsigned int)) & ~0xf) /* for valgrind_stack_id */
142 #else
143 #define smx_context_usable_stack_size (smx_context_stack_size & ~0xf)
144 #endif
145
146 /** @brief Executes all the processes to run (in parallel if possible). */
147 XBT_PRIVATE void SIMIX_context_runall();
148
149 XBT_PUBLIC int SIMIX_process_get_maxpid();
150
151 XBT_PRIVATE void SIMIX_post_create_environment();
152
153 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(std::string name);
154
155 #endif