1 /* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_SIMIX_SWAPPED_CONTEXT_HPP
7 #define SIMGRID_SIMIX_SWAPPED_CONTEXT_HPP
9 #include "src/kernel/context/Context.hpp"
18 class SwappedContextFactory : public ContextFactory {
19 friend SwappedContext; // Reads whether we are in parallel mode
21 SwappedContextFactory(std::string name);
22 ~SwappedContextFactory() override;
23 void run_all() override;
29 unsigned long process_index_ = 0; // Next actor to execute during sequential run_all()
32 class SwappedContext : public Context {
34 SwappedContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
35 SwappedContextFactory* factory);
36 virtual ~SwappedContext();
38 static void initialize(bool parallel); // Initialize the module, using the options
39 static void finalize(); // Finalize the module
41 void suspend() override;
42 virtual void resume();
45 virtual void swap_into(SwappedContext* to) = 0; // Defined in subclasses
47 static SwappedContext* get_maestro() { return maestro_context_; }
48 static void set_maestro(SwappedContext* maestro) { maestro_context_ = maestro; }
50 /* For the parallel execution */ // FIXME killme
51 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
52 static std::vector<SwappedContext*> workers_context_;
53 static std::atomic<uintptr_t> threads_working_;
54 static thread_local uintptr_t worker_id_;
57 void* stack_ = nullptr; /* the thread stack */
60 static SwappedContext* maestro_context_;
61 /* For sequential and parallel run_all() */
62 SwappedContextFactory* factory_;
65 } // namespace context
67 } // namespace simgrid