Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move a static field of SwappedContext into SwappedContextFactory
[simgrid.git] / src / kernel / context / ContextSwapped.hpp
1 /* Copyright (c) 2009-2018. 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(std::string name);
22   ~SwappedContextFactory() override;
23   void run_all() override;
24
25 protected:
26   bool parallel_;
27
28 private:
29   unsigned long process_index_ = 0; // Next actor to execute during sequential run_all()
30 };
31
32 class SwappedContext : public Context {
33 public:
34   SwappedContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
35                  SwappedContextFactory* factory);
36   virtual ~SwappedContext();
37
38   static void initialize(bool parallel); // Initialize the module, using the options
39   static void finalize();   // Finalize the module
40
41   virtual void suspend();
42   virtual void resume();
43
44   virtual void swap_into(SwappedContext* to) = 0; // Defined in subclasses
45
46   static SwappedContext* get_maestro() { return maestro_context_; }
47   static void set_maestro(SwappedContext* maestro) { maestro_context_ = maestro; }
48
49   /* For the parallel execution */ // FIXME killme
50   static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
51   static std::vector<SwappedContext*> workers_context_;
52   static std::atomic<uintptr_t> threads_working_;
53   static thread_local uintptr_t worker_id_;
54
55 protected:
56   void* stack_ = nullptr; /* the thread stack */
57
58 private:
59   static SwappedContext* maestro_context_;
60   /* For sequential and parallel run_all() */
61   SwappedContextFactory* factory_;
62 };
63
64 } // namespace context
65 } // namespace kernel
66 } // namespace simgrid
67 #endif