1 /* Copyright (c) 2007-2023. 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 <xbt/parmap.h>
12 #include "src/kernel/activity/ActivityImpl.hpp"
17 namespace simgrid::kernel::context {
18 extern unsigned stack_size;
19 extern unsigned guard_size;
21 class XBT_PUBLIC ContextFactory {
23 explicit ContextFactory() = default;
24 ContextFactory(const ContextFactory&) = delete;
25 ContextFactory& operator=(const ContextFactory&) = delete;
26 virtual ~ContextFactory();
27 virtual Context* create_context(std::function<void()>&& code, actor::ActorImpl* actor) = 0;
29 /** Turn the current thread into a simulation context */
30 virtual Context* attach(actor::ActorImpl* actor);
31 /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
32 virtual Context* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor);
34 virtual void run_all(std::vector<actor::ActorImpl*> const& actors_list) = 0;
36 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
37 static std::function<ContextFactory*(void)> initializer;
40 template <class T, class... Args> T* new_context(Args&&... args)
42 auto* context = new T(std::forward<Args>(args)...);
43 context->declare_context(sizeof(T));
48 class XBT_PUBLIC Context {
49 friend ContextFactory;
51 static thread_local Context* current_context_;
53 std::function<void()> code_;
54 actor::ActorImpl* actor_ = nullptr;
56 void declare_context(std::size_t size);
60 static int install_sigsegv_stack(stack_t* old_stack, bool enable);
63 Context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
64 Context(const Context&) = delete;
65 Context& operator=(const Context&) = delete;
68 bool is_maestro() const { return is_maestro_; }
69 void operator()() const { code_(); }
70 bool has_code() const { return static_cast<bool>(code_); }
71 actor::ActorImpl* get_actor() const { return this->actor_; }
75 virtual void suspend() = 0;
77 // Retrieving the self() context
78 /** @brief Retrieves the current context of this thread */
79 static Context* self();
80 /** @brief Sets the current context of this thread */
81 static void set_current(Context* self);
84 class XBT_PUBLIC AttachContext : public Context {
86 AttachContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
87 : Context(std::move(code), actor, maestro)
90 AttachContext(const AttachContext&) = delete;
91 AttachContext& operator=(const AttachContext&) = delete;
92 ~AttachContext() override;
94 /** Called by the context when it is ready to give control to the maestro */
95 virtual void attach_start() = 0;
97 /** Called by the context when it has finished its job */
98 virtual void attach_stop() = 0;
101 XBT_PRIVATE ContextFactory* thread_factory();
102 XBT_PRIVATE ContextFactory* sysv_factory();
103 XBT_PRIVATE ContextFactory* raw_factory();
104 XBT_PRIVATE ContextFactory* boost_factory();
106 XBT_PUBLIC bool is_parallel();
107 XBT_PUBLIC int get_nthreads();
108 XBT_PUBLIC void set_nthreads(int nb_threads);
109 XBT_PUBLIC void set_parallel_mode(e_xbt_parmap_mode_t mode);
110 XBT_PUBLIC e_xbt_parmap_mode_t get_parallel_mode();
111 } // namespace simgrid::kernel::context