1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_KERNEL_CONTEXT_CONTEXT_HPP
7 #define SIMGRID_KERNEL_CONTEXT_CONTEXT_HPP
9 #include "simgrid/forward.h"
10 #include "src/internal_config.h"
11 #include "src/kernel/activity/ActivityImpl.hpp"
15 /* Process creation/destruction callbacks */
16 typedef void (*void_pfn_smxprocess_t)(smx_actor_t);
22 class XBT_PUBLIC ContextFactory {
24 explicit ContextFactory() {}
25 virtual ~ContextFactory();
26 virtual Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
28 /** Turn the current thread into a simulation context */
29 virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
30 /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
31 virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
33 virtual void run_all() = 0;
36 template <class T, class... Args> T* new_context(Args&&... args)
38 T* context = new T(std::forward<Args>(args)...);
39 context->declare_context(sizeof(T));
44 class XBT_PUBLIC Context {
45 friend ContextFactory;
48 std::function<void()> code_;
49 void_pfn_smxprocess_t cleanup_func_ = nullptr;
50 smx_actor_t actor_ = nullptr;
51 void declare_context(std::size_t size);
54 bool iwannadie = false;
56 Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor);
57 Context(const Context&) = delete;
58 Context& operator=(const Context&) = delete;
61 void operator()() { code_(); }
62 bool has_code() const { return static_cast<bool>(code_); }
63 smx_actor_t get_actor() { return this->actor_; }
64 void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
68 virtual void suspend() = 0;
70 // Retrieving the self() context
71 /** @brief Retrives the current context of this thread */
72 static Context* self();
73 /** @brief Sets the current context of this thread */
74 static void set_current(Context* self);
77 /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
79 * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
80 * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
83 StopRequest() = default;
84 explicit StopRequest(std::string msg) : msg_(std::string("Actor killed (") + msg + std::string(").")) {}
85 const char* what() const noexcept { return msg_.c_str(); }
88 std::string msg_ = std::string("Actor killed.");
92 class XBT_PUBLIC AttachContext : public Context {
94 AttachContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor)
95 : Context(std::move(code), cleanup_func, actor)
99 ~AttachContext() override;
101 /** Called by the context when it is ready to give control
104 virtual void attach_start() = 0;
106 /** Called by the context when it has finished its job */
107 virtual void attach_stop() = 0;
110 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
111 typedef ContextFactory* (*ContextFactoryInitializer)();
112 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
114 XBT_PRIVATE ContextFactory* thread_factory();
115 XBT_PRIVATE ContextFactory* sysv_factory();
116 XBT_PRIVATE ContextFactory* raw_factory();
117 XBT_PRIVATE ContextFactory* boost_factory();
121 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
123 XBT_PRIVATE void SIMIX_context_mod_init();
124 XBT_PRIVATE void SIMIX_context_mod_exit();
126 XBT_PUBLIC smx_context_t SIMIX_context_new(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
127 smx_actor_t simix_process);
130 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
133 /* We are using the bottom of the stack to save some information, like the
134 * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
135 * size for the stack. Round its value to a multiple of 16 (asan wants the stacks to be aligned this way). */
137 #define smx_context_usable_stack_size \
138 ((smx_context_stack_size - sizeof(unsigned int)) & ~0xf) /* for valgrind_stack_id */
140 #define smx_context_usable_stack_size (smx_context_stack_size & ~0xf)
143 /** @brief Executes all the processes to run (in parallel if possible). */
144 XBT_PRIVATE void SIMIX_context_runall();
146 XBT_PUBLIC int SIMIX_process_get_maxpid();
148 XBT_PRIVATE void SIMIX_post_create_environment();
150 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(std::string name);