1 /* Copyright (c) 2007-2020. 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/kernel/activity/ActivityImpl.hpp"
20 class XBT_PUBLIC ContextFactory {
22 explicit ContextFactory() = default;
23 ContextFactory(const ContextFactory&) = delete;
24 ContextFactory& operator=(const ContextFactory&) = delete;
25 virtual ~ContextFactory();
26 virtual Context* create_context(std::function<void()>&& code, actor::ActorImpl* actor) = 0;
28 /** Turn the current thread into a simulation context */
29 virtual Context* attach(actor::ActorImpl* actor);
30 /** Turn the current thread into maestro (the old maestro becomes a regular actor) */
31 virtual Context* create_maestro(std::function<void()>&& code, actor::ActorImpl* actor);
33 virtual void run_all() = 0;
36 template <class T, class... Args> T* new_context(Args&&... args)
38 auto* context = new T(std::forward<Args>(args)...);
39 context->declare_context(sizeof(T));
44 class XBT_PUBLIC Context {
45 friend ContextFactory;
47 std::function<void()> code_;
48 actor::ActorImpl* actor_ = nullptr;
49 bool iwannadie_ = false;
50 void declare_context(std::size_t size);
53 Context(std::function<void()>&& code, actor::ActorImpl* actor);
54 Context(const Context&) = delete;
55 Context& operator=(const Context&) = delete;
58 bool wannadie() const { return iwannadie_; }
59 void set_wannadie(bool value = true) { iwannadie_ = value; }
60 void operator()() const { code_(); }
61 bool has_code() const { return static_cast<bool>(code_); }
62 actor::ActorImpl* get_actor() const { return this->actor_; }
66 virtual void suspend() = 0;
68 // Retrieving the self() context
69 /** @brief Retrieves the current context of this thread */
70 static Context* self();
71 /** @brief Sets the current context of this thread */
72 static void set_current(Context* self);
75 class XBT_PUBLIC AttachContext : public Context {
77 AttachContext(std::function<void()>&& code, actor::ActorImpl* actor) : Context(std::move(code), actor) {}
78 AttachContext(const AttachContext&) = delete;
79 AttachContext& operator=(const AttachContext&) = delete;
80 ~AttachContext() override;
82 /** Called by the context when it is ready to give control
85 virtual void attach_start() = 0;
87 /** Called by the context when it has finished its job */
88 virtual void attach_stop() = 0;
92 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
93 typedef ContextFactory* (*ContextFactoryInitializer)();
94 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
96 XBT_PRIVATE ContextFactory* thread_factory();
97 XBT_PRIVATE ContextFactory* sysv_factory();
98 XBT_PRIVATE ContextFactory* raw_factory();
99 XBT_PRIVATE ContextFactory* boost_factory();
101 } // namespace context
102 } // namespace kernel
103 } // namespace simgrid
105 XBT_PRIVATE void SIMIX_context_mod_init();
106 XBT_PRIVATE void SIMIX_context_mod_exit();
109 XBT_PUBLIC_DATA std::array<unsigned char, SIGSTKSZ> sigsegv_stack;