Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make factory_initializer a static member of ContextFactory.
[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(std::vector<actor::ActorImpl*> const& actors_list) = 0;
37
38   /* This allows Java to hijack the context factory (Java induces factories of factory :) */
39   static std::function<ContextFactory*(void)> initializer;
40
41 protected:
42   template <class T, class... Args> T* new_context(Args&&... args)
43   {
44     auto* context = new T(std::forward<Args>(args)...);
45     context->declare_context(sizeof(T));
46     return context;
47   }
48 };
49
50 class XBT_PUBLIC Context {
51   friend ContextFactory;
52
53   static thread_local Context* current_context_;
54
55   std::function<void()> code_;
56   actor::ActorImpl* actor_ = nullptr;
57   bool is_maestro_;
58   void declare_context(std::size_t size);
59
60 public:
61 #ifndef WIN32
62   static int install_sigsegv_stack(stack_t* old_stack, bool enable);
63 #endif
64
65   Context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
66   Context(const Context&) = delete;
67   Context& operator=(const Context&) = delete;
68   virtual ~Context();
69
70   bool is_maestro() const { return is_maestro_; }
71   void operator()() const { code_(); }
72   bool has_code() const { return static_cast<bool>(code_); }
73   actor::ActorImpl* get_actor() const { return this->actor_; }
74
75   // Scheduling methods
76   virtual void stop();
77   virtual void suspend() = 0;
78
79   // Retrieving the self() context
80   /** @brief Retrieves the current context of this thread */
81   static Context* self();
82   /** @brief Sets the current context of this thread */
83   static void set_current(Context* self);
84 };
85
86 class XBT_PUBLIC AttachContext : public Context {
87 public:
88   AttachContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
89       : Context(std::move(code), actor, maestro)
90   {
91   }
92   AttachContext(const AttachContext&) = delete;
93   AttachContext& operator=(const AttachContext&) = delete;
94   ~AttachContext() override;
95
96   /** Called by the context when it is ready to give control to the maestro */
97   virtual void attach_start() = 0;
98
99   /** Called by the context when it has finished its job */
100   virtual void attach_stop() = 0;
101 };
102
103 XBT_PRIVATE ContextFactory* thread_factory();
104 XBT_PRIVATE ContextFactory* sysv_factory();
105 XBT_PRIVATE ContextFactory* raw_factory();
106 XBT_PRIVATE ContextFactory* boost_factory();
107
108 XBT_PUBLIC bool is_parallel();
109 XBT_PUBLIC int get_nthreads();
110 XBT_PUBLIC void set_nthreads(int nb_threads);
111 XBT_PUBLIC void set_parallel_mode(e_xbt_parmap_mode_t mode);
112 XBT_PUBLIC e_xbt_parmap_mode_t get_parallel_mode();
113 } // namespace context
114 } // namespace kernel
115 } // namespace simgrid
116
117 #endif