1 /* Copyright (c) 2015-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_BOOST_CONTEXT_HPP
7 #define SIMGRID_SIMIX_BOOST_CONTEXT_HPP
9 #include <boost/version.hpp>
10 #if BOOST_VERSION < 106100
11 #include <boost/context/fcontext.hpp>
13 #include <boost/context/detail/fcontext.hpp>
21 #include <simgrid/simix.hpp>
22 #include <xbt/parmap.hpp>
23 #include <xbt/xbt_os_thread.h>
25 #include "src/internal_config.h"
26 #include "src/kernel/context/Context.hpp"
27 #include "src/kernel/context/ContextSwapped.hpp"
33 /** @brief Userspace context switching implementation based on Boost.Context */
34 class BoostContext : public SwappedContext {
36 BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
37 ~BoostContext() override;
40 void swap_into(SwappedContext* to) override;
43 #if BOOST_VERSION < 105600
44 boost::context::fcontext_t* fc_ = nullptr;
45 typedef intptr_t arg_type;
46 #elif BOOST_VERSION < 106100
47 boost::context::fcontext_t fc_;
48 typedef intptr_t arg_type;
50 boost::context::detail::fcontext_t fc_;
51 typedef boost::context::detail::transfer_t arg_type;
53 #if HAVE_SANITIZER_ADDRESS_FIBER_SUPPORT
54 const void* asan_stack_ = nullptr;
55 size_t asan_stack_size_ = 0;
56 BoostContext* asan_ctx_ = nullptr;
57 bool asan_stop_ = false;
60 static void wrapper(arg_type arg);
63 class ParallelBoostContext : public BoostContext {
65 ParallelBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
66 : BoostContext(std::move(code), cleanup_func, process)
69 void suspend() override;
70 void resume() override;
72 static void initialize();
73 static void finalize();
74 static void run_all();
77 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
78 static std::vector<ParallelBoostContext*> workers_context_;
79 static std::atomic<uintptr_t> threads_working_;
80 static thread_local uintptr_t worker_id_;
83 class BoostContextFactory : public ContextFactory {
85 BoostContextFactory();
86 ~BoostContextFactory() override;
87 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
88 void run_all() override;