Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
60d998777e93dd9d926b6fe0ba1874812b882f7a
[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   explicit SwappedContextFactory(std::string name);
22   ~SwappedContextFactory() override;
23   void run_all() override;
24
25 private:
26   bool parallel_;
27
28   unsigned long process_index_ = 0; // Next actor to execute during sequential run_all()
29
30   /* For the parallel execution */
31   simgrid::xbt::Parmap<smx_actor_t>* parmap_;
32   std::vector<SwappedContext*> workers_context_; /* space to save the worker's context in each thread */
33   std::atomic<uintptr_t> threads_working_{0};    /* number of threads that have started their work */
34 };
35
36 class SwappedContext : public Context {
37 public:
38   SwappedContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t get_actor,
39                  SwappedContextFactory* factory);
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   void* get_stack();
49
50   // FIXME: Killme
51   static thread_local uintptr_t worker_id_;
52
53 private:
54   void* stack_ = nullptr;                /* the thread stack */
55   SwappedContextFactory* const factory_; // for sequential and parallel run_all()
56 };
57
58 } // namespace context
59 } // namespace kernel
60 } // namespace simgrid
61 #endif