Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Inline Context::self().
[simgrid.git] / src / kernel / context / Context.hpp
1 /* Copyright (c) 2007-2019. 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() {}
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     T* 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_;
47
48   std::function<void()> code_;
49   actor::ActorImpl* actor_ = nullptr;
50   void declare_context(std::size_t size);
51
52 public:
53   bool iwannadie = false;
54
55   Context(std::function<void()>&& code, actor::ActorImpl* actor);
56   Context(const Context&) = delete;
57   Context& operator=(const Context&) = delete;
58   virtual ~Context();
59
60   void operator()() { code_(); }
61   bool has_code() const { return static_cast<bool>(code_); }
62   actor::ActorImpl* get_actor() { return this->actor_; }
63
64   // Scheduling methods
65   virtual void stop();
66   virtual void suspend() = 0;
67
68   // Retrieving the self() context
69   /** @brief Retrives the current context of this thread */
70   static Context* self() { return current_; }
71   /** @brief Sets the current context of this thread */
72   static void set_current(Context* self) { current_ = 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 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
106
107 XBT_PRIVATE void SIMIX_context_mod_init();
108 XBT_PRIVATE void SIMIX_context_mod_exit();
109
110 #ifndef WIN32
111 XBT_PUBLIC_DATA unsigned char sigsegv_stack[SIGSTKSZ];
112 #endif
113
114 /** @brief Executes all the processes to run (in parallel if possible). */
115 XBT_PRIVATE void SIMIX_context_runall();
116
117 XBT_PUBLIC int SIMIX_process_get_maxpid();
118
119 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const std::string& name);
120
121 #endif