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 "Context.hpp"
26 #include "src/internal_config.h"
32 /** @brief Userspace context switching implementation based on Boost.Context */
33 class BoostContext : public Context {
35 BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
36 ~BoostContext() override;
38 virtual void resume() = 0;
40 static void swap(BoostContext* from, BoostContext* to);
41 static BoostContext* getMaestro() { return maestro_context_; }
42 static void setMaestro(BoostContext* maestro) { maestro_context_ = maestro; }
45 static BoostContext* maestro_context_;
46 void* stack_ = nullptr;
48 #if BOOST_VERSION < 105600
49 boost::context::fcontext_t* fc_ = nullptr;
50 typedef intptr_t arg_type;
51 #elif BOOST_VERSION < 106100
52 boost::context::fcontext_t fc_;
53 typedef intptr_t arg_type;
55 boost::context::detail::fcontext_t fc_;
56 typedef boost::context::detail::transfer_t arg_type;
58 #if HAVE_SANITIZE_ADDRESS_FIBER_SUPPORT
59 const void* asan_stack_ = nullptr;
60 size_t asan_stack_size_ = 0;
61 bool asan_stop_ = false;
64 static void wrapper(arg_type arg);
67 class SerialBoostContext : public BoostContext {
69 SerialBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
70 : BoostContext(std::move(code), cleanup_func, process)
73 void suspend() override;
74 void resume() override;
76 static void run_all();
79 static unsigned long process_index_;
82 #if HAVE_THREAD_CONTEXTS
83 class ParallelBoostContext : public BoostContext {
85 ParallelBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
86 : BoostContext(std::move(code), cleanup_func, process)
89 void suspend() override;
90 void resume() override;
92 static void initialize();
93 static void finalize();
94 static void run_all();
97 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
98 static std::vector<ParallelBoostContext*> workers_context_;
99 static std::atomic<uintptr_t> threads_working_;
100 static xbt_os_thread_key_t worker_id_key_;
104 class BoostContextFactory : public ContextFactory {
106 BoostContextFactory();
107 ~BoostContextFactory() override;
108 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup, smx_actor_t process) override;
109 void run_all() override;