Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
196139975dcadf29cd620bea7d7a0fa4d78b1c0d
[simgrid.git] / src / kernel / context / Context.hpp
1 /* Copyright (c) 2007-2023. 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::kernel::context {
18
19 class XBT_PUBLIC ContextFactory {
20 public:
21   explicit ContextFactory()             = default;
22   ContextFactory(const ContextFactory&) = delete;
23   ContextFactory& operator=(const ContextFactory&) = delete;
24   virtual ~ContextFactory();
25   virtual Context* create_context(std::function<void()>&& code, actor::ActorImpl* actor) = 0;
26
27   /** Turn the current thread into a simulation context */
28   virtual Context* attach(actor::ActorImpl* actor);
29   /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
30   virtual Context* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor);
31
32   virtual void run_all(std::vector<actor::ActorImpl*> const& actors_list) = 0;
33
34 protected:
35   template <class T, class... Args> T* new_context(Args&&... args)
36   {
37     auto* context = new T(std::forward<Args>(args)...);
38     context->declare_context(sizeof(T));
39     return context;
40   }
41 };
42
43 class XBT_PUBLIC Context {
44   friend ContextFactory;
45
46   static int parallel_contexts;
47   static thread_local Context* current_context_;
48
49   std::function<void()> code_;
50   actor::ActorImpl* actor_ = nullptr;
51   bool is_maestro_;
52   void declare_context(std::size_t size);
53
54 public:
55   static e_xbt_parmap_mode_t parallel_mode;
56   static unsigned stack_size;
57   static unsigned guard_size;
58
59   static int install_sigsegv_stack(bool enable);
60
61   Context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
62   Context(const Context&) = delete;
63   Context& operator=(const Context&) = delete;
64   virtual ~Context();
65
66   bool is_maestro() const { return is_maestro_; }
67   void operator()() const { code_(); }
68   bool has_code() const { return static_cast<bool>(code_); }
69   actor::ActorImpl* get_actor() const { return this->actor_; }
70
71   /** @brief Returns whether some parallel threads are used for the user contexts. */
72   static bool is_parallel() { return parallel_contexts > 1; }
73   /** @brief Returns the number of parallel threads used for the user contexts (1 means no parallelism). */
74   static int get_nthreads() { return parallel_contexts; }
75   /**
76    * @brief Sets the number of parallel threads to use  for the user contexts.
77    *
78    * This function should be called before initializing SIMIX.
79    * A value of 1 means no parallelism (1 thread only).
80    * If the value is greater than 1, the thread support must be enabled.
81    * If the value is less than 1, the optimal number of threads is chosen automatically.
82    *
83    * @param nb_threads the number of threads to use
84    */
85   static void set_nthreads(int nb_threads);
86
87   // Scheduling methods
88   virtual void stop();
89   virtual void suspend() = 0;
90
91   // Retrieving the self() context
92   /** @brief Retrieves the current context of this thread */
93   static Context* self();
94   /** @brief Sets the current context of this thread */
95   static void set_current(Context* self);
96 };
97
98 class XBT_PUBLIC AttachContext : public Context {
99 public:
100   AttachContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
101       : Context(std::move(code), actor, maestro)
102   {
103   }
104   AttachContext(const AttachContext&) = delete;
105   AttachContext& operator=(const AttachContext&) = delete;
106   ~AttachContext() override;
107
108   /** Called by the context when it is ready to give control to the maestro */
109   virtual void attach_start() = 0;
110
111   /** Called by the context when it has finished its job */
112   virtual void attach_stop() = 0;
113 };
114
115 XBT_PRIVATE ContextFactory* thread_factory();
116 XBT_PRIVATE ContextFactory* sysv_factory();
117 XBT_PRIVATE ContextFactory* raw_factory();
118 XBT_PRIVATE ContextFactory* boost_factory();
119
120 } // namespace simgrid::kernel::context
121
122 #endif