Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a8973d159a0462fb9fd1e2a23c0ea1f3067ffb15
[simgrid.git] / src / kernel / context / ContextSwapped.hpp
1 /* Copyright (c) 2009-2019. 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_SIMIX_SWAPPED_CONTEXT_HPP
7 #define SIMGRID_SIMIX_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();
22   SwappedContextFactory(const SwappedContextFactory&) = delete;
23   SwappedContextFactory& operator=(const SwappedContextFactory&) = delete;
24   void run_all() override;
25
26 private:
27   bool parallel_;
28
29   /* For the sequential execution */
30   unsigned long process_index_     = 0;       // next actor to execute
31   SwappedContext* maestro_context_ = nullptr; // save maestro's context
32
33   /* For the parallel execution */
34   std::unique_ptr<simgrid::xbt::Parmap<smx_actor_t>> parmap_;
35 };
36
37 class SwappedContext : public Context {
38 public:
39   SwappedContext(std::function<void()>&& code, smx_actor_t get_actor, SwappedContextFactory* factory);
40   SwappedContext(const SwappedContext&) = delete;
41   SwappedContext& operator=(const SwappedContext&) = delete;
42   virtual ~SwappedContext();
43
44   void suspend() override;
45   virtual void resume();
46   void stop() override;
47
48   virtual void swap_into(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses
49
50   unsigned char* get_stack();
51
52   static thread_local SwappedContext* worker_context_;
53
54 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
55   const void* asan_stack_   = nullptr;
56   size_t asan_stack_size_   = 0;
57   SwappedContext* asan_ctx_ = nullptr;
58   bool asan_stop_           = false;
59 #endif
60
61 private:
62   unsigned char* stack_ = nullptr;       /* the thread stack */
63   SwappedContextFactory* const factory_; // for sequential and parallel run_all()
64
65 #if HAVE_VALGRIND_H
66   unsigned int valgrind_stack_id_;
67 #endif
68 };
69
70 } // namespace context
71 } // namespace kernel
72 } // namespace simgrid
73 #endif