1 /* Copyright (c) 2007-2018. 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 {
27 explicit ContextFactory(std::string name) : name_(std::move(name)) {}
28 virtual ~ContextFactory();
29 virtual Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
31 /** Turn the current thread into a simulation context */
32 virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
33 /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
34 virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
36 virtual void run_all() = 0;
37 /** @brief Returns the current context of this thread. */
38 virtual Context* self();
39 std::string const& name() const { return name_; }
42 template <class T, class... Args> T* new_context(Args&&... args)
44 T* context = new T(std::forward<Args>(args)...);
45 context->declare_context(sizeof(T));
50 class XBT_PUBLIC Context {
51 friend ContextFactory;
54 std::function<void()> code_;
55 void_pfn_smxprocess_t cleanup_func_ = nullptr;
56 smx_actor_t actor_ = nullptr;
57 void declare_context(std::size_t size);
61 /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
63 * Nope, Sonar, this should not inherit of std::exception nor of simgrid::Exception.
64 * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
67 StopRequest() = default;
68 explicit StopRequest(std::string msg) : msg_(msg) {}
73 bool iwannadie = false;
75 Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
76 Context(const Context&) = delete;
77 Context& operator=(const Context&) = delete;
79 void operator()() { code_(); }
80 bool has_code() const { return static_cast<bool>(code_); }
81 smx_actor_t process() { return this->actor_; }
82 void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
87 virtual void suspend() = 0;
90 class XBT_PUBLIC AttachContext : public Context {
92 AttachContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t actor)
93 : Context(std::move(code), cleanup_func, actor)
97 ~AttachContext() override;
99 /** Called by the context when it is ready to give control
102 virtual void attach_start() = 0;
104 /** Called by the context when it has finished its job */
105 virtual void attach_stop() = 0;
108 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
109 typedef ContextFactory* (*ContextFactoryInitializer)();
110 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
112 XBT_PRIVATE ContextFactory* thread_factory();
113 XBT_PRIVATE ContextFactory* sysv_factory();
114 XBT_PRIVATE ContextFactory* raw_factory();
115 XBT_PRIVATE ContextFactory* boost_factory();
119 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
121 XBT_PRIVATE void SIMIX_context_mod_init();
122 XBT_PRIVATE void SIMIX_context_mod_exit();
124 XBT_PUBLIC smx_context_t SIMIX_context_new(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
125 smx_actor_t simix_process);
128 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
131 /* We are using the bottom of the stack to save some information, like the
132 * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
133 * size for the stack. Round its value to a multiple of 16 (asan wants the stacks to be aligned this way). */
135 #define smx_context_usable_stack_size \
136 ((smx_context_stack_size - sizeof(unsigned int)) & ~0xf) /* for valgrind_stack_id */
138 #define smx_context_usable_stack_size (smx_context_stack_size & ~0xf)
141 /** @brief Executes all the processes to run (in parallel if possible). */
142 XBT_PRIVATE void SIMIX_context_runall();
143 /** @brief returns the current running context */
144 XBT_PUBLIC smx_context_t SIMIX_context_self(); // public because it's used in simgrid-java
146 XBT_PRIVATE void *SIMIX_context_stack_new();
147 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
149 XBT_PUBLIC void SIMIX_context_set_current(smx_context_t context);
151 XBT_PUBLIC int SIMIX_process_get_maxpid();
153 XBT_PRIVATE void SIMIX_post_create_environment();
155 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(std::string name);