Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
473af7a0c550306b14b3c2bea42b336853c0d320
[simgrid.git] / src / kernel / context / Context.hpp
1 /* Copyright (c) 2007-2022. 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 <xbt/parmap.h>
11
12 #include "src/kernel/activity/ActivityImpl.hpp"
13
14 #include <csignal>
15 #include <functional>
16
17 namespace simgrid {
18 namespace kernel {
19 namespace context {
20 extern unsigned stack_size;
21 extern unsigned guard_size;
22
23 class XBT_PUBLIC ContextFactory {
24 public:
25   explicit ContextFactory()             = default;
26   ContextFactory(const ContextFactory&) = delete;
27   ContextFactory& operator=(const ContextFactory&) = delete;
28   virtual ~ContextFactory();
29   virtual Context* create_context(std::function<void()>&& code, actor::ActorImpl* actor) = 0;
30
31   /** Turn the current thread into a simulation context */
32   virtual Context* attach(actor::ActorImpl* actor);
33   /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
34   virtual Context* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor);
35
36   virtual void run_all() = 0;
37
38 protected:
39   template <class T, class... Args> T* new_context(Args&&... args)
40   {
41     auto* context = new T(std::forward<Args>(args)...);
42     context->declare_context(sizeof(T));
43     return context;
44   }
45 };
46
47 class XBT_PUBLIC Context {
48   friend ContextFactory;
49
50   static thread_local Context* current_context_;
51
52   std::function<void()> code_;
53   actor::ActorImpl* actor_ = nullptr;
54   bool iwannadie_          = false; // True if we need to do some cleanups in actor mode.
55   bool to_be_freed_        = false; // True if cleanups in actor mode done, but cleanups in kernel mode pending
56   bool is_maestro_;
57   void declare_context(std::size_t size);
58
59 public:
60 #ifndef WIN32
61   static int install_sigsegv_stack(stack_t* old_stack, bool enable);
62 #endif
63
64   Context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
65   Context(const Context&) = delete;
66   Context& operator=(const Context&) = delete;
67   virtual ~Context();
68
69   bool wannadie() const { return iwannadie_; }
70   void set_wannadie(bool value = true);
71   bool to_be_freed() const { return to_be_freed_; }
72   void set_to_be_freed() { to_be_freed_ = true; }
73   bool is_maestro() const { return is_maestro_; }
74   void operator()() const { code_(); }
75   bool has_code() const { return static_cast<bool>(code_); }
76   actor::ActorImpl* get_actor() const { return this->actor_; }
77
78   // Scheduling methods
79   virtual void stop();
80   virtual void suspend() = 0;
81
82   // Retrieving the self() context
83   /** @brief Retrieves the current context of this thread */
84   static Context* self();
85   /** @brief Sets the current context of this thread */
86   static void set_current(Context* self);
87 };
88
89 class XBT_PUBLIC AttachContext : public Context {
90 public:
91   AttachContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
92       : Context(std::move(code), actor, maestro)
93   {
94   }
95   AttachContext(const AttachContext&) = delete;
96   AttachContext& operator=(const AttachContext&) = delete;
97   ~AttachContext() override;
98
99   /** Called by the context when it is ready to give control to the maestro */
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
107 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
108 using ContextFactoryInitializer = ContextFactory* (*)();
109 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
110
111 XBT_PRIVATE ContextFactory* thread_factory();
112 XBT_PRIVATE ContextFactory* sysv_factory();
113 XBT_PRIVATE ContextFactory* raw_factory();
114 XBT_PRIVATE ContextFactory* boost_factory();
115
116 XBT_PUBLIC bool is_parallel();
117 XBT_PUBLIC int get_nthreads();
118 XBT_PUBLIC void set_nthreads(int nb_threads);
119 XBT_PUBLIC void set_parallel_mode(e_xbt_parmap_mode_t mode);
120 XBT_PUBLIC e_xbt_parmap_mode_t get_parallel_mode();
121 } // namespace context
122 } // namespace kernel
123 } // namespace simgrid
124
125 #endif