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 {
19 class XBT_PUBLIC ContextFactory {
21 explicit ContextFactory() = default;
22 ContextFactory(const ContextFactory&) = delete;
23 ContextFactory& operator=(const ContextFactory&) = delete;
24 virtual ~ContextFactory();
25 virtual Context* create_context(std::function<void()>&& code, actor::ActorImpl* actor) = 0;
27 /** Turn the current thread into a simulation context */
28 virtual Context* attach(actor::ActorImpl* actor);
29 /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
30 virtual Context* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor);
32 virtual void run_all(std::vector<actor::ActorImpl*> const& actors_list) = 0;
35 template <class T, class... Args> T* new_context(Args&&... args)
37 auto* context = new T(std::forward<Args>(args)...);
42 class XBT_PUBLIC Context {
43 friend ContextFactory;
45 static int parallel_contexts;
46 static thread_local Context* current_context_;
48 std::function<void()> code_;
49 actor::ActorImpl* actor_ = nullptr;
53 static e_xbt_parmap_mode_t parallel_mode;
54 static unsigned stack_size;
55 static unsigned guard_size;
57 static int install_sigsegv_stack(bool enable);
59 Context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
60 Context(const Context&) = delete;
61 Context& operator=(const Context&) = delete;
64 bool is_maestro() const { return is_maestro_; }
65 void operator()() const { code_(); }
66 bool has_code() const { return static_cast<bool>(code_); }
67 actor::ActorImpl* get_actor() const { return this->actor_; }
69 /** @brief Returns whether some parallel threads are used for the user contexts. */
70 static bool is_parallel() { return parallel_contexts > 1; }
71 /** @brief Returns the number of parallel threads used for the user contexts (1 means no parallelism). */
72 static int get_nthreads() { return parallel_contexts; }
74 * @brief Sets the number of parallel threads to use for the user contexts.
76 * This function should be called before initializing SIMIX.
77 * A value of 1 means no parallelism (1 thread only).
78 * If the value is greater than 1, the thread support must be enabled.
79 * If the value is less than 1, the optimal number of threads is chosen automatically.
81 * @param nb_threads the number of threads to use
83 static void set_nthreads(int nb_threads);
87 virtual void suspend() = 0;
89 // Retrieving the self() context
90 /** @brief Retrieves the current context of this thread */
91 static Context* self();
92 /** @brief Sets the current context of this thread */
93 static void set_current(Context* self);
96 class XBT_PUBLIC AttachContext : public Context {
98 AttachContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
99 : Context(std::move(code), actor, maestro)
102 AttachContext(const AttachContext&) = delete;
103 AttachContext& operator=(const AttachContext&) = delete;
104 ~AttachContext() override;
106 /** Called by the context when it is ready to give control to the maestro */
107 virtual void attach_start() = 0;
109 /** Called by the context when it has finished its job */
110 virtual void attach_stop() = 0;
113 XBT_PRIVATE ContextFactory* thread_factory();
114 XBT_PRIVATE ContextFactory* sysv_factory();
115 XBT_PRIVATE ContextFactory* raw_factory();
116 XBT_PRIVATE ContextFactory* boost_factory();
118 } // namespace simgrid::kernel::context