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"
19 class XBT_PUBLIC ContextFactory {
21 explicit ContextFactory() {}
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() = 0;
35 template <class T, class... Args> T* new_context(Args&&... args)
37 T* context = new T(std::forward<Args>(args)...);
38 context->declare_context(sizeof(T));
43 class XBT_PUBLIC Context {
44 friend ContextFactory;
46 std::function<void()> code_;
47 actor::ActorImpl* actor_ = nullptr;
48 bool iwannadie_ = false;
49 void declare_context(std::size_t size);
52 Context(std::function<void()>&& code, actor::ActorImpl* actor);
53 Context(const Context&) = delete;
54 Context& operator=(const Context&) = delete;
57 bool wannadie() const { return iwannadie_; }
58 void set_wannadie(bool value = true) { iwannadie_ = value; }
59 void operator()() const { code_(); }
60 bool has_code() const { return static_cast<bool>(code_); }
61 actor::ActorImpl* get_actor() const { return this->actor_; }
65 virtual void suspend() = 0;
67 // Retrieving the self() context
68 /** @brief Retrieves the current context of this thread */
69 static Context* self();
70 /** @brief Sets the current context of this thread */
71 static void set_current(Context* self);
74 class XBT_PUBLIC AttachContext : public Context {
76 AttachContext(std::function<void()>&& code, actor::ActorImpl* actor) : Context(std::move(code), actor) {}
77 AttachContext(const AttachContext&) = delete;
78 AttachContext& operator=(const AttachContext&) = delete;
79 ~AttachContext() override;
81 /** Called by the context when it is ready to give control
84 virtual void attach_start() = 0;
86 /** Called by the context when it has finished its job */
87 virtual void attach_stop() = 0;
91 /* This allows Java to hijack the context factory (Java induces factories of factory :) */
92 typedef ContextFactory* (*ContextFactoryInitializer)();
93 XBT_PUBLIC_DATA ContextFactoryInitializer factory_initializer;
95 XBT_PRIVATE ContextFactory* thread_factory();
96 XBT_PRIVATE ContextFactory* sysv_factory();
97 XBT_PRIVATE ContextFactory* raw_factory();
98 XBT_PRIVATE ContextFactory* boost_factory();
100 } // namespace context
101 } // namespace kernel
102 } // namespace simgrid
104 typedef simgrid::kernel::context::ContextFactory *smx_context_factory_t;
106 XBT_PRIVATE void SIMIX_context_mod_init();
107 XBT_PRIVATE void SIMIX_context_mod_exit();
110 XBT_PUBLIC_DATA unsigned char sigsegv_stack[SIGSTKSZ];