Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[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
18 class SwappedContextFactory : public ContextFactory {
19   friend SwappedContext; // Reads whether we are in parallel mode
20 public:
21   SwappedContextFactory()                             = default;
22   SwappedContextFactory(const SwappedContextFactory&) = delete;
23   SwappedContextFactory& operator=(const SwappedContextFactory&) = delete;
24   void run_all() override;
25
26 private:
27   /* For the sequential execution */
28   unsigned long process_index_     = 0;       // next actor to execute
29   SwappedContext* maestro_context_ = nullptr; // save maestro's context
30
31   /* For the parallel execution, will be created lazily with the right parameters if needed (ie, in parallel) */
32   std::unique_ptr<simgrid::xbt::Parmap<smx_actor_t>> parmap_{nullptr};
33 };
34
35 class SwappedContext : public Context {
36 public:
37   SwappedContext(std::function<void()>&& code, smx_actor_t get_actor, SwappedContextFactory* factory);
38   SwappedContext(const SwappedContext&) = delete;
39   SwappedContext& operator=(const SwappedContext&) = delete;
40   virtual ~SwappedContext();
41
42   void suspend() override;
43   virtual void resume();
44   void stop() override;
45
46   virtual void swap_into(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses
47
48   unsigned char* get_stack();
49
50   static thread_local SwappedContext* worker_context_;
51
52 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
53   const void* asan_stack_   = nullptr;
54   size_t asan_stack_size_   = 0;
55   SwappedContext* asan_ctx_ = nullptr;
56   bool asan_stop_           = false;
57 #endif
58
59 private:
60   unsigned char* stack_ = nullptr;       /* the thread stack */
61   SwappedContextFactory* const factory_; // for sequential and parallel run_all()
62
63 #if HAVE_VALGRIND_H
64   unsigned int valgrind_stack_id_;
65 #endif
66 };
67
68 } // namespace context
69 } // namespace kernel
70 } // namespace simgrid
71 #endif