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/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 XBT_PUBLIC void throw_stoprequest();
111 XBT_PUBLIC bool try_n_catch_stoprequest(std::function<void(void)> try_block, std::function<void(void)> catch_block);
113 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
114 typedef ContextFactory* (*ContextFactoryInitializer)();
115 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
117 XBT_PRIVATE ContextFactory* thread_factory();
118 XBT_PRIVATE ContextFactory* sysv_factory();
119 XBT_PRIVATE ContextFactory* raw_factory();
120 XBT_PRIVATE ContextFactory* boost_factory();
124 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
126 XBT_PRIVATE void SIMIX_context_mod_init();
127 XBT_PRIVATE void SIMIX_context_mod_exit();
129 XBT_PUBLIC smx_context_t SIMIX_context_new(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
130 smx_actor_t simix_process);
133 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
136 /** @brief Executes all the processes to run (in parallel if possible). */
137 XBT_PRIVATE void SIMIX_context_runall();
139 XBT_PUBLIC int SIMIX_process_get_maxpid();
141 XBT_PRIVATE void SIMIX_post_create_environment();
143 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(std::string name);