1 /* Copyright (c) 2009-2019. 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"
19 class SwappedContextFactory : public ContextFactory {
20 friend SwappedContext; // Reads whether we are in parallel mode
22 SwappedContextFactory();
23 SwappedContextFactory(const SwappedContextFactory&) = delete;
24 SwappedContextFactory& operator=(const SwappedContextFactory&) = delete;
25 void run_all() override;
30 /* For the sequential execution */
31 unsigned long process_index_ = 0; // next actor to execute
32 SwappedContext* maestro_context_ = nullptr; // save maestro's context
34 /* For the parallel execution */
35 std::unique_ptr<simgrid::xbt::Parmap<smx_actor_t>> parmap_;
38 class SwappedContext : public Context {
40 SwappedContext(std::function<void()>&& code, smx_actor_t get_actor, SwappedContextFactory* factory);
41 SwappedContext(const SwappedContext&) = delete;
42 SwappedContext& operator=(const SwappedContext&) = delete;
43 virtual ~SwappedContext();
45 void suspend() override;
46 virtual void resume();
49 virtual void swap_into(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses
51 unsigned char* get_stack();
53 static thread_local SwappedContext* worker_context_;
55 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
56 const void* asan_stack_ = nullptr;
57 size_t asan_stack_size_ = 0;
58 SwappedContext* asan_ctx_ = nullptr;
59 bool asan_stop_ = false;
63 unsigned char* stack_ = nullptr; /* the thread stack */
64 SwappedContextFactory* const factory_; // for sequential and parallel run_all()
67 unsigned int valgrind_stack_id_;
71 } // namespace context
73 } // namespace simgrid