Logo AND Algorithmique Numérique Distribuée

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