1 /* Copyright (c) 2007-2022. 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"
20 extern unsigned stack_size;
21 extern unsigned guard_size;
23 class XBT_PUBLIC ContextFactory {
25 explicit ContextFactory() = default;
26 ContextFactory(const ContextFactory&) = delete;
27 ContextFactory& operator=(const ContextFactory&) = delete;
28 virtual ~ContextFactory();
29 virtual Context* create_context(std::function<void()>&& code, actor::ActorImpl* actor) = 0;
31 /** Turn the current thread into a simulation context */
32 virtual Context* attach(actor::ActorImpl* actor);
33 /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
34 virtual Context* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor);
36 virtual void run_all() = 0;
39 template <class T, class... Args> T* new_context(Args&&... args)
41 auto* context = new T(std::forward<Args>(args)...);
42 context->declare_context(sizeof(T));
47 class XBT_PUBLIC Context {
48 friend ContextFactory;
50 static thread_local Context* current_context_;
52 std::function<void()> code_;
53 actor::ActorImpl* actor_ = nullptr;
54 bool iwannadie_ = false;
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 wannadie() const { return iwannadie_; }
69 void set_wannadie(bool value = true) { iwannadie_ = value; }
70 bool is_maestro() const { return is_maestro_; }
71 void operator()() const { code_(); }
72 bool has_code() const { return static_cast<bool>(code_); }
73 actor::ActorImpl* get_actor() const { return this->actor_; }
77 virtual void suspend() = 0;
79 // Retrieving the self() context
80 /** @brief Retrieves the current context of this thread */
81 static Context* self();
82 /** @brief Sets the current context of this thread */
83 static void set_current(Context* self);
86 class XBT_PUBLIC AttachContext : public Context {
88 AttachContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
89 : Context(std::move(code), actor, maestro)
92 AttachContext(const AttachContext&) = delete;
93 AttachContext& operator=(const AttachContext&) = delete;
94 ~AttachContext() override;
96 /** Called by the context when it is ready to give control to the maestro */
97 virtual void attach_start() = 0;
99 /** Called by the context when it has finished its job */
100 virtual void attach_stop() = 0;
104 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
105 using ContextFactoryInitializer = ContextFactory* (*)();
106 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
108 XBT_PRIVATE ContextFactory* thread_factory();
109 XBT_PRIVATE ContextFactory* sysv_factory();
110 XBT_PRIVATE ContextFactory* raw_factory();
111 XBT_PRIVATE ContextFactory* boost_factory();
113 XBT_PUBLIC int is_parallel();
114 XBT_PUBLIC int get_nthreads();
115 XBT_PUBLIC void set_nthreads(int nb_threads);
116 XBT_PUBLIC void set_parallel_mode(e_xbt_parmap_mode_t mode);
117 XBT_PUBLIC e_xbt_parmap_mode_t get_parallel_mode();
118 } // namespace context
119 } // namespace kernel
120 } // namespace simgrid