Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0195e1b7c0b35facfe84434ac2c3242b41cf93bb
[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 <vector>
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   ~SwappedContextFactory() override;
25   void run_all() override;
26
27 private:
28   bool parallel_;
29
30   unsigned long process_index_ = 0; // Next actor to execute during sequential run_all()
31
32   /* For the parallel execution */
33   simgrid::xbt::Parmap<smx_actor_t>* parmap_;
34   std::vector<SwappedContext*> workers_context_; /* space to save the worker's context in each thread */
35   std::atomic<uintptr_t> threads_working_{0};    /* number of threads that have started their work */
36 };
37
38 class SwappedContext : public Context {
39 public:
40   SwappedContext(std::function<void()> code, smx_actor_t get_actor, SwappedContextFactory* factory);
41   SwappedContext(const SwappedContext&) = delete;
42   SwappedContext& operator=(const SwappedContext&) = delete;
43   virtual ~SwappedContext();
44
45   void suspend() override;
46   virtual void resume();
47   void stop() override;
48
49   virtual void swap_into(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses
50
51   void* get_stack();
52
53   static thread_local uintptr_t worker_id_;
54
55 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
56   const void* asan_stack_   = nullptr;
57   size_t asan_stack_size_   = 0;
58   SwappedContext* asan_ctx_ = nullptr;
59   bool asan_stop_           = false;
60 #endif
61
62 private:
63   void* stack_ = nullptr;                /* the thread stack */
64   SwappedContextFactory* const factory_; // for sequential and parallel run_all()
65 };
66
67 } // namespace context
68 } // namespace kernel
69 } // namespace simgrid
70 #endif