Logo AND Algorithmique Numérique Distribuée

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