Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unneeded extern "C".
[simgrid.git] / src / kernel / context / Context.hpp
1 /* Copyright (c) 2007-2018. 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 "instr/instr_interface.h"
10 #include "src/internal_config.h"
11 #include "src/simix/smx_network_private.hpp"
12
13 #include <csignal>
14
15 namespace simgrid {
16 namespace kernel {
17 namespace context {
18
19 class XBT_PUBLIC ContextFactory {
20 private:
21   std::string name_;
22
23 public:
24   explicit ContextFactory(std::string name) : name_(std::move(name)) {}
25   virtual ~ContextFactory();
26   virtual Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
27
28   // Optional methods for attaching main() as a context:
29
30   /** Creates a context from the current context of execution
31    *
32    *  This will not work on all implementation of `ContextFactory`.
33    */
34   virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
35   virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
36
37   virtual void run_all() = 0;
38   virtual Context* self();
39   std::string const& name() const { return name_; }
40 private:
41   void declare_context(void* T, std::size_t size);
42
43 protected:
44   template <class T, class... Args> T* new_context(Args&&... args)
45   {
46     T* context = new T(std::forward<Args>(args)...);
47     this->declare_context(context, sizeof(T));
48     return context;
49   }
50 };
51
52 class XBT_PUBLIC Context {
53 private:
54   std::function<void()> code_;
55   void_pfn_smxprocess_t cleanup_func_ = nullptr;
56   smx_actor_t process_                = nullptr;
57
58 public:
59   class StopRequest {
60     /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
61      *
62      * Nope, Sonar, this should not inherit of std::exception.
63      * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
64      */
65   };
66   bool iwannadie;
67
68   Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
69   void operator()() { code_(); }
70   bool has_code() const { return static_cast<bool>(code_); }
71   smx_actor_t process() { return this->process_; }
72   void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
73
74   // Virtual methods
75   virtual ~Context();
76   virtual void stop();
77   virtual void suspend() = 0;
78 };
79
80 class XBT_PUBLIC AttachContext : public Context {
81 public:
82   AttachContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
83       : Context(std::move(code), cleanup_func, process)
84   {
85   }
86
87   ~AttachContext() override;
88
89   /** Called by the context when it is ready to give control
90    *  to the maestro.
91    */
92   virtual void attach_start() = 0;
93
94   /** Called by the context when it has finished its job */
95   virtual void attach_stop() = 0;
96 };
97
98 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
99 typedef ContextFactory* (*ContextFactoryInitializer)();
100 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
101
102 XBT_PRIVATE ContextFactory* thread_factory();
103 XBT_PRIVATE ContextFactory* sysv_factory();
104 XBT_PRIVATE ContextFactory* raw_factory();
105 XBT_PRIVATE ContextFactory* boost_factory();
106
107 }}}
108
109 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
110
111 XBT_PRIVATE void SIMIX_context_mod_init();
112 XBT_PRIVATE void SIMIX_context_mod_exit();
113
114 XBT_PUBLIC smx_context_t SIMIX_context_new(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
115                                            smx_actor_t simix_process);
116
117 #ifndef WIN32
118 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
119 #endif
120
121 /* We are using the bottom of the stack to save some information, like the
122  * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
123  * size for the stack. Round its value to a multiple of 16 (asan wants the stacks to be aligned this way). */
124 #if HAVE_VALGRIND_H
125 #define smx_context_usable_stack_size                                                                                  \
126   ((smx_context_stack_size - sizeof(unsigned int)) & ~0xf) /* for valgrind_stack_id */
127 #else
128 #define smx_context_usable_stack_size (smx_context_stack_size & ~0xf)
129 #endif
130
131 /** @brief Executes all the processes to run (in parallel if possible). */
132 XBT_PRIVATE void SIMIX_context_runall();
133 /** @brief returns the current running context */
134 XBT_PUBLIC smx_context_t SIMIX_context_self(); // public because it's used in simgrid-java
135
136 XBT_PRIVATE void *SIMIX_context_stack_new();
137 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
138
139 XBT_PUBLIC void SIMIX_context_set_current(smx_context_t context);
140 XBT_PRIVATE smx_context_t SIMIX_context_get_current();
141
142 XBT_PUBLIC int SIMIX_process_get_maxpid();
143
144 XBT_PRIVATE void SIMIX_post_create_environment();
145
146 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name);
147
148 #endif