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;
28 unsigned long process_index_ = 0; // Next actor to execute during sequential run_all()
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_; /* number of threads that have started their work */
36 class SwappedContext : public Context {
38 SwappedContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process,
39 SwappedContextFactory* factory);
40 virtual ~SwappedContext();
42 void suspend() override;
43 virtual void resume();
46 virtual void swap_into(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses
48 static SwappedContext* get_maestro() { return maestro_context_; }
49 static void set_maestro(SwappedContext* maestro) { maestro_context_ = maestro; }
52 static thread_local uintptr_t worker_id_;
55 void* stack_ = nullptr; /* the thread stack */
58 static SwappedContext* maestro_context_;
59 SwappedContextFactory* factory_; // for sequential and parallel run_all()
62 } // namespace context
64 } // namespace simgrid