Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Replace some C-style arrays.
[simgrid.git] / src / kernel / context / Context.hpp
1 /* Copyright (c) 2007-2020. 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 "src/kernel/activity/ActivityImpl.hpp"
11
12 #include <array>
13 #include <csignal>
14 #include <functional>
15
16 namespace simgrid {
17 namespace kernel {
18 namespace context {
19
20 class XBT_PUBLIC ContextFactory {
21 public:
22   explicit ContextFactory()             = default;
23   ContextFactory(const ContextFactory&) = delete;
24   ContextFactory& operator=(const ContextFactory&) = delete;
25   virtual ~ContextFactory();
26   virtual Context* create_context(std::function<void()>&& code, actor::ActorImpl* actor) = 0;
27
28   /** Turn the current thread into a simulation context */
29   virtual Context* attach(actor::ActorImpl* actor);
30   /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
31   virtual Context* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor);
32
33   virtual void run_all() = 0;
34
35 protected:
36   template <class T, class... Args> T* new_context(Args&&... args)
37   {
38     auto* context = new T(std::forward<Args>(args)...);
39     context->declare_context(sizeof(T));
40     return context;
41   }
42 };
43
44 class XBT_PUBLIC Context {
45   friend ContextFactory;
46
47   std::function<void()> code_;
48   actor::ActorImpl* actor_ = nullptr;
49   bool iwannadie_          = false;
50   void declare_context(std::size_t size);
51
52 public:
53   Context(std::function<void()>&& code, actor::ActorImpl* actor);
54   Context(const Context&) = delete;
55   Context& operator=(const Context&) = delete;
56   virtual ~Context();
57
58   bool wannadie() const { return iwannadie_; }
59   void set_wannadie(bool value = true) { iwannadie_ = value; }
60   void operator()() const { code_(); }
61   bool has_code() const { return static_cast<bool>(code_); }
62   actor::ActorImpl* get_actor() const { return this->actor_; }
63
64   // Scheduling methods
65   virtual void stop();
66   virtual void suspend() = 0;
67
68   // Retrieving the self() context
69   /** @brief Retrieves the current context of this thread */
70   static Context* self();
71   /** @brief Sets the current context of this thread */
72   static void set_current(Context* self);
73 };
74
75 class XBT_PUBLIC AttachContext : public Context {
76 public:
77   AttachContext(std::function<void()>&& code, actor::ActorImpl* actor) : Context(std::move(code), actor) {}
78   AttachContext(const AttachContext&) = delete;
79   AttachContext& operator=(const AttachContext&) = delete;
80   ~AttachContext() override;
81
82   /** Called by the context when it is ready to give control
83    *  to the maestro.
84    */
85   virtual void attach_start() = 0;
86
87   /** Called by the context when it has finished its job */
88   virtual void attach_stop() = 0;
89 };
90
91
92 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
93 typedef ContextFactory* (*ContextFactoryInitializer)();
94 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
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 } // namespace context
102 } // namespace kernel
103 } // namespace simgrid
104
105 XBT_PRIVATE void SIMIX_context_mod_init();
106 XBT_PRIVATE void SIMIX_context_mod_exit();
107
108 #ifndef WIN32
109 XBT_PUBLIC_DATA std::array<unsigned char, SIGSTKSZ> sigsegv_stack;
110 #endif
111 #endif