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 virtual void suspend();
42 virtual void resume();
44 virtual void swap_into(SwappedContext* to) = 0; // Defined in subclasses
46 static SwappedContext* get_maestro() { return maestro_context_; }
47 static void set_maestro(SwappedContext* maestro) { maestro_context_ = maestro; }
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_;
56 void* stack_ = nullptr; /* the thread stack */
59 static SwappedContext* maestro_context_;
60 /* For sequential and parallel run_all() */
61 SwappedContextFactory* factory_;
64 } // namespace context
66 } // namespace simgrid