1 /* Copyright (c) 2015-2017. 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>
18 #include <xbt/parmap.hpp>
20 #include <simgrid/simix.hpp>
28 class BoostSerialContext;
29 class BoostParallelContext;
30 class BoostContextFactory;
32 /** @brief Userspace context switching implementation based on Boost.Context */
33 class BoostContext : public Context {
35 static bool parallel_;
36 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
37 static std::vector<BoostContext*> workers_context_;
38 static uintptr_t threads_working_;
39 static xbt_os_thread_key_t worker_id_key_;
40 static unsigned long process_index_;
41 static BoostContext* maestro_context_;
43 #if BOOST_VERSION < 105600
44 boost::context::fcontext_t* fc_ = nullptr;
45 typedef intptr_t ctx_arg_type;
46 #elif BOOST_VERSION < 106100
47 boost::context::fcontext_t fc_;
48 typedef intptr_t ctx_arg_type;
50 boost::context::detail::fcontext_t fc_;
51 typedef boost::context::detail::transfer_t ctx_arg_type;
53 static void smx_ctx_boost_wrapper(ctx_arg_type);
54 static void smx_ctx_boost_jump_fcontext(BoostContext*, BoostContext*);
56 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
57 const void* asan_stack_ = nullptr;
58 size_t asan_stack_size_ = 0;
59 bool asan_stop_ = false;
62 void* stack_ = nullptr;
64 friend BoostContextFactory;
65 BoostContext(std::function<void()> code,
66 void_pfn_smxprocess_t cleanup_func,
68 ~BoostContext() override;
70 virtual void resume();
72 static void wrapper(int first, ...);
75 class BoostContextFactory : public ContextFactory {
78 friend BoostSerialContext;
79 friend BoostParallelContext;
81 BoostContextFactory();
82 ~BoostContextFactory() override;
83 Context* create_context(std::function<void()> code,
84 void_pfn_smxprocess_t, smx_actor_t process) override;
85 void run_all() override;