Logo AND Algorithmique Numérique Distribuée

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