Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove Java bindings. They are not updated since maybe 10 years
[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 extern unsigned stack_size;
19 extern unsigned guard_size;
20
21 class XBT_PUBLIC ContextFactory {
22 public:
23   explicit ContextFactory()             = default;
24   ContextFactory(const ContextFactory&) = delete;
25   ContextFactory& operator=(const ContextFactory&) = delete;
26   virtual ~ContextFactory();
27   virtual Context* create_context(std::function<void()>&& code, actor::ActorImpl* actor) = 0;
28
29   /** Turn the current thread into a simulation context */
30   virtual Context* attach(actor::ActorImpl* actor);
31   /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
32   virtual Context* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor);
33
34   virtual void run_all(std::vector<actor::ActorImpl*> const& actors_list) = 0;
35
36 protected:
37   template <class T, class... Args> T* new_context(Args&&... args)
38   {
39     auto* context = new T(std::forward<Args>(args)...);
40     context->declare_context(sizeof(T));
41     return context;
42   }
43 };
44
45 class XBT_PUBLIC Context {
46   friend ContextFactory;
47
48   static thread_local Context* current_context_;
49
50   std::function<void()> code_;
51   actor::ActorImpl* actor_ = nullptr;
52   bool is_maestro_;
53   void declare_context(std::size_t size);
54
55 public:
56   static int install_sigsegv_stack(stack_t* old_stack, bool enable);
57
58   Context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
59   Context(const Context&) = delete;
60   Context& operator=(const Context&) = delete;
61   virtual ~Context();
62
63   bool is_maestro() const { return is_maestro_; }
64   void operator()() const { code_(); }
65   bool has_code() const { return static_cast<bool>(code_); }
66   actor::ActorImpl* get_actor() const { return this->actor_; }
67
68   // Scheduling methods
69   virtual void stop();
70   virtual void suspend() = 0;
71
72   // Retrieving the self() context
73   /** @brief Retrieves the current context of this thread */
74   static Context* self();
75   /** @brief Sets the current context of this thread */
76   static void set_current(Context* self);
77 };
78
79 class XBT_PUBLIC AttachContext : public Context {
80 public:
81   AttachContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
82       : Context(std::move(code), actor, maestro)
83   {
84   }
85   AttachContext(const AttachContext&) = delete;
86   AttachContext& operator=(const AttachContext&) = delete;
87   ~AttachContext() override;
88
89   /** Called by the context when it is ready to give control to the maestro */
90   virtual void attach_start() = 0;
91
92   /** Called by the context when it has finished its job */
93   virtual void attach_stop() = 0;
94 };
95
96 XBT_PRIVATE ContextFactory* thread_factory();
97 XBT_PRIVATE ContextFactory* sysv_factory();
98 XBT_PRIVATE ContextFactory* raw_factory();
99 XBT_PRIVATE ContextFactory* boost_factory();
100
101 XBT_PUBLIC bool is_parallel();
102 XBT_PUBLIC int get_nthreads();
103 XBT_PUBLIC void set_nthreads(int nb_threads);
104 XBT_PUBLIC void set_parallel_mode(e_xbt_parmap_mode_t mode);
105 XBT_PUBLIC e_xbt_parmap_mode_t get_parallel_mode();
106 } // namespace simgrid::kernel::context
107
108 #endif