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/context/all.hpp>
15 #include <simgrid/simix.hpp>
16 #include <xbt/parmap.hpp>
17 #include <xbt/xbt_os_thread.h>
19 #include "Context.hpp"
20 #include "src/internal_config.h"
21 #include "src/simix/smx_private.hpp"
27 /** @brief Userspace context switching implementation based on Boost.Context */
28 class BoostContext : public Context {
30 BoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process);
31 ~BoostContext() override;
33 virtual void resume() = 0;
35 static void swap(BoostContext* from, BoostContext* to);
36 static BoostContext* getMaestro() { return maestro_context_; }
37 static void setMaestro(BoostContext* maestro) { maestro_context_ = maestro; }
40 static BoostContext* maestro_context_;
41 void* stack_ = nullptr;
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_SANITIZE_ADDRESS_FIBER_SUPPORT
54 const void* asan_stack_ = nullptr;
55 size_t asan_stack_size_ = 0;
56 bool asan_stop_ = false;
59 static void wrapper(arg_type arg);
62 class SerialBoostContext : public BoostContext {
64 SerialBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
65 : BoostContext(std::move(code), cleanup_func, process)
68 void suspend() override;
69 void resume() override;
71 static void run_all();
74 static unsigned long process_index_;
77 #if HAVE_THREAD_CONTEXTS
78 class ParallelBoostContext : public BoostContext {
80 ParallelBoostContext(std::function<void()> code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
81 : BoostContext(std::move(code), cleanup_func, process)
84 void suspend() override;
85 void resume() override;
87 static void initialize();
88 static void finalize();
89 static void run_all();
92 static simgrid::xbt::Parmap<smx_actor_t>* parmap_;
93 static std::vector<ParallelBoostContext*> workers_context_;
94 static uintptr_t threads_working_;
95 static xbt_os_thread_key_t worker_id_key_;
99 class BoostContextFactory : public ContextFactory {
101 BoostContextFactory();
102 ~BoostContextFactory() override;
103 Context* create_context(std::function<void()> code, void_pfn_smxprocess_t, smx_actor_t process) override;
104 void run_all() override;