1 /* Copyright (c) 2009-2020. 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_KERNEL_CONTEXT_SWAPPED_CONTEXT_HPP
7 #define SIMGRID_KERNEL_CONTEXT_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() = default;
22 SwappedContextFactory(const SwappedContextFactory&) = delete;
23 SwappedContextFactory& operator=(const SwappedContextFactory&) = delete;
24 void run_all() override;
27 /* For the sequential execution */
28 unsigned long process_index_ = 0; // next actor to execute
29 SwappedContext* maestro_context_ = nullptr; // save maestro's context
31 /* For the parallel execution, will be created lazily with the right parameters if needed (ie, in parallel) */
32 std::unique_ptr<simgrid::xbt::Parmap<smx_actor_t>> parmap_{nullptr};
35 class SwappedContext : public Context {
37 SwappedContext(std::function<void()>&& code, smx_actor_t get_actor, SwappedContextFactory* factory);
38 SwappedContext(const SwappedContext&) = delete;
39 SwappedContext& operator=(const SwappedContext&) = delete;
40 virtual ~SwappedContext();
42 void suspend() override;
43 virtual void resume();
44 XBT_ATTRIB_NORETURN void stop() override;
46 virtual void swap_into(SwappedContext* to) = 0; // Defined in Raw, Boost and UContext subclasses
48 unsigned char* get_stack() const { return stack_; }
49 // Return the address for the bottom of the stack. Depending on the stack direction it may be the lower or higher
51 unsigned char* get_stack_bottom() const { return PTH_STACKGROWTH == -1 ? stack_ + smx_context_stack_size : stack_; }
53 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
54 const void* asan_stack_ = nullptr;
55 size_t asan_stack_size_ = 0;
56 SwappedContext* asan_ctx_ = nullptr;
57 bool asan_stop_ = false;
61 // With ASan, after a context switch, check that the originating context is the expected one (see BoostContext)
62 void verify_previous_context(const SwappedContext* context) const;
65 static thread_local SwappedContext* worker_context_;
67 unsigned char* stack_ = nullptr; // the thread stack
68 SwappedContextFactory& factory_; // for sequential and parallel run_all()
71 unsigned int valgrind_stack_id_;
75 inline void SwappedContext::verify_previous_context(XBT_ATTRIB_UNUSED const SwappedContext* context) const
77 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
78 xbt_assert(this->asan_ctx_ == context);
82 } // namespace context
84 } // namespace simgrid