Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Forgotten guard.
[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 <csignal>
13 #include <functional>
14
15 namespace simgrid {
16 namespace kernel {
17 namespace 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() = 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 thread_local Context* current_context_;
47
48   std::function<void()> code_;
49   actor::ActorImpl* actor_ = nullptr;
50   bool iwannadie_          = false;
51   void declare_context(std::size_t size);
52
53 public:
54 #ifndef WIN32
55   static int install_sigsegv_stack(stack_t* old_stack, bool enable);
56 #endif
57
58   Context(std::function<void()>&& code, actor::ActorImpl* actor);
59   Context(const Context&) = delete;
60   Context& operator=(const Context&) = delete;
61   virtual ~Context();
62
63   bool wannadie() const { return iwannadie_; }
64   void set_wannadie(bool value = true) { iwannadie_ = value; }
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   // Scheduling methods
70   virtual void stop();
71   virtual void suspend() = 0;
72
73   // Retrieving the self() context
74   /** @brief Retrieves the current context of this thread */
75   static Context* self();
76   /** @brief Sets the current context of this thread */
77   static void set_current(Context* self);
78 };
79
80 class XBT_PUBLIC AttachContext : public Context {
81 public:
82   AttachContext(std::function<void()>&& code, actor::ActorImpl* actor) : Context(std::move(code), actor) {}
83   AttachContext(const AttachContext&) = delete;
84   AttachContext& operator=(const AttachContext&) = delete;
85   ~AttachContext() override;
86
87   /** Called by the context when it is ready to give control
88    *  to the maestro.
89    */
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
97 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
98 typedef ContextFactory* (*ContextFactoryInitializer)();
99 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
100
101 XBT_PRIVATE ContextFactory* thread_factory();
102 XBT_PRIVATE ContextFactory* sysv_factory();
103 XBT_PRIVATE ContextFactory* raw_factory();
104 XBT_PRIVATE ContextFactory* boost_factory();
105
106 } // namespace context
107 } // namespace kernel
108 } // namespace simgrid
109
110 XBT_PRIVATE void SIMIX_context_mod_init();
111 XBT_PRIVATE void SIMIX_context_mod_exit();
112 #endif