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"
19 class XBT_PUBLIC ContextFactory {
21 explicit ContextFactory() {}
22 virtual ~ContextFactory();
23 virtual Context* create_context(std::function<void()> code, smx_actor_t actor) = 0;
25 /** Turn the current thread into a simulation context */
26 virtual Context* attach(smx_actor_t actor);
27 /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
28 virtual Context* create_maestro(std::function<void()> code, smx_actor_t actor);
30 virtual void run_all() = 0;
33 template <class T, class... Args> T* new_context(Args&&... args)
35 T* context = new T(std::forward<Args>(args)...);
36 context->declare_context(sizeof(T));
41 class XBT_PUBLIC Context {
42 friend ContextFactory;
45 std::function<void()> code_;
46 smx_actor_t actor_ = nullptr;
47 void declare_context(std::size_t size);
50 bool iwannadie = false;
52 Context(std::function<void()> code, smx_actor_t actor);
53 Context(const Context&) = delete;
54 Context& operator=(const Context&) = delete;
57 void operator()() { code_(); }
58 bool has_code() const { return static_cast<bool>(code_); }
59 smx_actor_t get_actor() { return this->actor_; }
63 virtual void suspend() = 0;
65 // Retrieving the self() context
66 /** @brief Retrives the current context of this thread */
67 static Context* self();
68 /** @brief Sets the current context of this thread */
69 static void set_current(Context* self);
72 class XBT_PUBLIC AttachContext : public Context {
74 AttachContext(std::function<void()> code, smx_actor_t actor) : Context(std::move(code), actor) {}
76 ~AttachContext() override;
78 /** Called by the context when it is ready to give control
81 virtual void attach_start() = 0;
83 /** Called by the context when it has finished its job */
84 virtual void attach_stop() = 0;
87 class XBT_PUBLIC StopRequest {
88 /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
90 * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
91 * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
94 StopRequest() = default;
95 explicit StopRequest(const std::string& msg) : msg_(std::string("Actor killed (") + msg + std::string(").")) {}
97 const char* what() const noexcept { return msg_.c_str(); }
99 static void do_throw();
100 static bool try_n_catch(std::function<void(void)> try_block);
103 std::string msg_ = std::string("Actor killed.");
106 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
107 typedef ContextFactory* (*ContextFactoryInitializer)();
108 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
110 XBT_PRIVATE ContextFactory* thread_factory();
111 XBT_PRIVATE ContextFactory* sysv_factory();
112 XBT_PRIVATE ContextFactory* raw_factory();
113 XBT_PRIVATE ContextFactory* boost_factory();
117 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
119 XBT_PRIVATE void SIMIX_context_mod_init();
120 XBT_PRIVATE void SIMIX_context_mod_exit();
123 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
126 /** @brief Executes all the processes to run (in parallel if possible). */
127 XBT_PRIVATE void SIMIX_context_runall();
129 XBT_PUBLIC int SIMIX_process_get_maxpid();
131 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const std::string& name);