Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / kernel / context / ContextSwapped.hpp
1 /* Copyright (c) 2009-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_KERNEL_CONTEXT_SWAPPED_CONTEXT_HPP
7 #define SIMGRID_KERNEL_CONTEXT_SWAPPED_CONTEXT_HPP
8
9 #include "src/internal_config.h" // HAVE_SANITIZER_*
10 #include "src/kernel/context/Context.hpp"
11 #include "src/xbt/parmap.hpp"
12
13 #include <memory>
14
15 namespace simgrid::kernel::context {
16 class SwappedContext;
17 } // namespace simgrid::kernel::context
18
19 /* Use extern "C" to make sure that this symbol is easy to recognize by name, even on exotic platforms */
20 extern "C" XBT_ATTRIB_NORETURN void smx_ctx_wrapper(simgrid::kernel::context::SwappedContext* context);
21
22 namespace simgrid::kernel::context {
23
24 class SwappedContextFactory : public ContextFactory {
25   friend SwappedContext; // Reads whether we are in parallel mode
26 public:
27   SwappedContextFactory()                             = default;
28   SwappedContextFactory(const SwappedContextFactory&) = delete;
29   SwappedContextFactory& operator=(const SwappedContextFactory&) = delete;
30   void run_all(std::vector<actor::ActorImpl*> const& actors) override;
31
32 private:
33   /* For the sequential execution */
34   unsigned long process_index_     = 0;       // next actor to execute
35   SwappedContext* maestro_context_ = nullptr; // save maestro's context
36
37   /* For the parallel execution, will be created lazily with the right parameters if needed (ie, in parallel) */
38   std::unique_ptr<simgrid::xbt::Parmap<actor::ActorImpl*>> parmap_{nullptr};
39 };
40
41 class SwappedContext : public Context {
42   friend void ::smx_ctx_wrapper(simgrid::kernel::context::SwappedContext*);
43
44 public:
45   SwappedContext(std::function<void()>&& code, actor::ActorImpl* get_actor, SwappedContextFactory* factory);
46   SwappedContext(const SwappedContext&) = delete;
47   SwappedContext& operator=(const SwappedContext&) = delete;
48   ~SwappedContext() override;
49
50   void suspend() override;
51   virtual void resume();
52
53   void swap_into(SwappedContext* to);
54
55 protected:
56   unsigned char* get_stack() const { return stack_; }
57   unsigned char* get_stack_bottom() const; // Depending on the stack direction, its bottom (that Boost::make_fcontext
58                                            // needs) may be the lower or higher end
59
60   // With ASan, after a context switch, check that the originating context is the expected one (see BoostContext)
61   void verify_previous_context(const SwappedContext* context) const;
62
63 private:
64   static thread_local SwappedContext* worker_context_;
65
66   unsigned char* stack_ = nullptr; // the thread stack
67   SwappedContextFactory& factory_; // for sequential and parallel run_all()
68
69 #if HAVE_VALGRIND_H
70   unsigned int valgrind_stack_id_ = 0;
71 #endif
72 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
73   const void* asan_stack_   = nullptr;
74   size_t asan_stack_size_   = 0;
75   SwappedContext* asan_ctx_ = nullptr;
76   bool asan_stop_           = false;
77 #endif
78 #if HAVE_SANITIZER_THREAD_FIBER_SUPPORT
79   void* tsan_fiber_;
80 #endif
81
82   virtual void swap_into_for_real(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses
83 };
84
85 inline void SwappedContext::verify_previous_context(XBT_ATTRIB_UNUSED const SwappedContext* context) const
86 {
87 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
88   xbt_assert(this->asan_ctx_ == context);
89 #endif
90 }
91
92 } // namespace simgrid::kernel::context
93 #endif