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 "src/internal_config.h"
10 #include "src/simix/smx_network_private.hpp"
18 class XBT_PUBLIC ContextFactory {
23 explicit ContextFactory(std::string name) : name_(std::move(name)) {}
24 virtual ~ContextFactory();
25 virtual Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) = 0;
27 // Optional methods for attaching main() as a context:
29 /** Creates a context from the current context of execution
31 * This will not work on all implementation of `ContextFactory`.
33 virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
34 virtual Context* create_maestro(std::function<void()> code, smx_actor_t process);
36 virtual void run_all() = 0;
37 virtual Context* self();
38 std::string const& name() const { return name_; }
40 void declare_context(void* T, std::size_t size);
43 template <class T, class... Args> T* new_context(Args&&... args)
45 T* context = new T(std::forward<Args>(args)...);
46 this->declare_context(context, sizeof(T));
51 class XBT_PUBLIC Context {
53 std::function<void()> code_;
54 void_pfn_smxprocess_t cleanup_func_ = nullptr;
55 smx_actor_t process_ = nullptr;
59 /** @brief Exception launched to kill a process, in order to properly unwind its stack and release RAII stuff
61 * Nope, Sonar, this should not inherit of std::exception.
62 * Otherwise, users may accidentally catch it with a try {} catch (std::exception)
67 Context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
68 void operator()() { code_(); }
69 bool has_code() const { return static_cast<bool>(code_); }
70 smx_actor_t process() { return this->process_; }
71 void set_cleanup(void_pfn_smxprocess_t cleanup) { cleanup_func_ = cleanup; }
76 virtual void suspend() = 0;
79 class XBT_PUBLIC AttachContext : public Context {
81 AttachContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
82 : Context(std::move(code), cleanup_func, process)
86 ~AttachContext() override;
88 /** Called by the context when it is ready to give control
91 virtual void attach_start() = 0;
93 /** Called by the context when it has finished its job */
94 virtual void attach_stop() = 0;
97 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
98 typedef ContextFactory* (*ContextFactoryInitializer)();
99 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
101 XBT_PRIVATE ContextFactory* thread_factory();
102 XBT_PRIVATE ContextFactory* sysv_factory();
103 XBT_PRIVATE ContextFactory* raw_factory();
104 XBT_PRIVATE ContextFactory* boost_factory();
108 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
110 XBT_PRIVATE void SIMIX_context_mod_init();
111 XBT_PRIVATE void SIMIX_context_mod_exit();
113 XBT_PUBLIC smx_context_t SIMIX_context_new(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
114 smx_actor_t simix_process);
117 XBT_PUBLIC_DATA char sigsegv_stack[SIGSTKSZ];
120 /* We are using the bottom of the stack to save some information, like the
121 * valgrind_stack_id. Define smx_context_usable_stack_size to give the remaining
122 * size for the stack. Round its value to a multiple of 16 (asan wants the stacks to be aligned this way). */
124 #define smx_context_usable_stack_size \
125 ((smx_context_stack_size - sizeof(unsigned int)) & ~0xf) /* for valgrind_stack_id */
127 #define smx_context_usable_stack_size (smx_context_stack_size & ~0xf)
130 /** @brief Executes all the processes to run (in parallel if possible). */
131 XBT_PRIVATE void SIMIX_context_runall();
132 /** @brief returns the current running context */
133 XBT_PUBLIC smx_context_t SIMIX_context_self(); // public because it's used in simgrid-java
135 XBT_PRIVATE void *SIMIX_context_stack_new();
136 XBT_PRIVATE void SIMIX_context_stack_delete(void *stack);
138 XBT_PUBLIC void SIMIX_context_set_current(smx_context_t context);
139 XBT_PRIVATE smx_context_t SIMIX_context_get_current();
141 XBT_PUBLIC int SIMIX_process_get_maxpid();
143 XBT_PRIVATE void SIMIX_post_create_environment();
145 XBT_PRIVATE simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const char *name);