Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[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     return context;
39   }
40 };
41
42 class XBT_PUBLIC Context {
43   friend ContextFactory;
44
45   static int parallel_contexts;
46   static thread_local Context* current_context_;
47
48   std::function<void()> code_;
49   actor::ActorImpl* actor_ = nullptr;
50   bool is_maestro_;
51
52 public:
53   static e_xbt_parmap_mode_t parallel_mode;
54   static unsigned stack_size;
55   static unsigned guard_size;
56
57   static int install_sigsegv_stack(bool enable);
58
59   Context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
60   Context(const Context&) = delete;
61   Context& operator=(const Context&) = delete;
62   virtual ~Context();
63
64   bool is_maestro() const { return is_maestro_; }
65   void operator()() const { code_(); }
66   bool has_code() const { return static_cast<bool>(code_); }
67   actor::ActorImpl* get_actor() const { return this->actor_; }
68
69   /** @brief Returns whether some parallel threads are used for the user contexts. */
70   static bool is_parallel() { return parallel_contexts > 1; }
71   /** @brief Returns the number of parallel threads used for the user contexts (1 means no parallelism). */
72   static int get_nthreads() { return parallel_contexts; }
73   /**
74    * @brief Sets the number of parallel threads to use  for the user contexts.
75    *
76    * This function should be called before initializing SIMIX.
77    * A value of 1 means no parallelism (1 thread only).
78    * If the value is greater than 1, the thread support must be enabled.
79    * If the value is less than 1, the optimal number of threads is chosen automatically.
80    *
81    * @param nb_threads the number of threads to use
82    */
83   static void set_nthreads(int nb_threads);
84
85   // Scheduling methods
86   virtual void stop();
87   virtual void suspend() = 0;
88
89   // Retrieving the self() context
90   /** @brief Retrieves the current context of this thread */
91   static Context* self();
92   /** @brief Sets the current context of this thread */
93   static void set_current(Context* self);
94 };
95
96 class XBT_PUBLIC AttachContext : public Context {
97 public:
98   AttachContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
99       : Context(std::move(code), actor, maestro)
100   {
101   }
102   AttachContext(const AttachContext&) = delete;
103   AttachContext& operator=(const AttachContext&) = delete;
104   ~AttachContext() override;
105
106   /** Called by the context when it is ready to give control to the maestro */
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 XBT_PRIVATE ContextFactory* thread_factory();
114 XBT_PRIVATE ContextFactory* sysv_factory();
115 XBT_PRIVATE ContextFactory* raw_factory();
116 XBT_PRIVATE ContextFactory* boost_factory();
117
118 } // namespace simgrid::kernel::context
119
120 #endif