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"
14 /* Process creation/destruction callbacks */
15 typedef void (*void_pfn_smxprocess_t)(smx_actor_t);
21 class XBT_PUBLIC ContextFactory {
23 explicit ContextFactory() {}
24 virtual ~ContextFactory();
25 virtual Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
27 /** Turn the current thread into a simulation context */
28 virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
29 /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
30 virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
32 virtual void run_all() = 0;
35 template <class T, class... Args> T* new_context(Args&&... args)
37 T* context = new T(std::forward<Args>(args)...);
38 context->declare_context(sizeof(T));
43 class XBT_PUBLIC Context {
44 friend ContextFactory;
47 std::function<void()> code_;
48 void_pfn_smxprocess_t cleanup_func_ = nullptr;
49 smx_actor_t actor_ = nullptr;
50 void declare_context(std::size_t size);
53 bool iwannadie = false;
55 Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor);
56 Context(const Context&) = delete;
57 Context& operator=(const Context&) = delete;
60 void operator()() { code_(); }
61 bool has_code() const { return static_cast<bool>(code_); }
62 smx_actor_t get_actor() { return this->actor_; }
63 void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
67 virtual void suspend() = 0;
69 // Retrieving the self() context
70 /** @brief Retrives the current context of this thread */
71 static Context* self();
72 /** @brief Sets the current context of this thread */
73 static void set_current(Context* self);
76 /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
78 * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
79 * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
82 StopRequest() = default;
83 explicit StopRequest(std::string msg) : msg_(std::string("Actor killed (") + msg + std::string(").")) {}
84 const char* what() const noexcept { return msg_.c_str(); }
87 std::string msg_ = std::string("Actor killed.");
91 class XBT_PUBLIC AttachContext : public Context {
93 AttachContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor)
94 : Context(std::move(code), cleanup_func, actor)
98 ~AttachContext() override;
100 /** Called by the context when it is ready to give control
103 virtual void attach_start() = 0;
105 /** Called by the context when it has finished its job */
106 virtual void attach_stop() = 0;
109 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
110 typedef ContextFactory* (*ContextFactoryInitializer)();
111 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
113 XBT_PRIVATE ContextFactory* thread_factory();
114 XBT_PRIVATE ContextFactory* sysv_factory();
115 XBT_PRIVATE ContextFactory* raw_factory();
116 XBT_PRIVATE ContextFactory* boost_factory();
120 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
122 XBT_PRIVATE void SIMIX_context_mod_init();
123 XBT_PRIVATE void SIMIX_context_mod_exit();
125 XBT_PUBLIC smx_context_t SIMIX_context_new(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
126 smx_actor_t simix_process);
129 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
132 /** @brief Executes all the processes to run (in parallel if possible). */
133 XBT_PRIVATE void SIMIX_context_runall();
135 XBT_PUBLIC int SIMIX_process_get_maxpid();
137 XBT_PRIVATE void SIMIX_post_create_environment();
139 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(std::string name);