fiber_fcontext.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // Copyright Oliver Kowalke 2017.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_CONTEXT_FIBER_H
  6. #define BOOST_CONTEXT_FIBER_H
  7. #include <boost/context/detail/config.hpp>
  8. #include <algorithm>
  9. #include <cstddef>
  10. #include <cstdint>
  11. #include <cstdlib>
  12. #include <exception>
  13. #include <functional>
  14. #include <memory>
  15. #include <ostream>
  16. #include <tuple>
  17. #include <utility>
  18. #include <boost/assert.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/intrusive_ptr.hpp>
  21. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  22. #include <boost/context/detail/exchange.hpp>
  23. #endif
  24. #if defined(BOOST_NO_CXX17_STD_INVOKE)
  25. #include <boost/context/detail/invoke.hpp>
  26. #endif
  27. #include <boost/context/detail/disable_overload.hpp>
  28. #include <boost/context/detail/exception.hpp>
  29. #include <boost/context/detail/fcontext.hpp>
  30. #include <boost/context/detail/tuple.hpp>
  31. #include <boost/context/fixedsize_stack.hpp>
  32. #include <boost/context/flags.hpp>
  33. #include <boost/context/preallocated.hpp>
  34. #include <boost/context/segmented_stack.hpp>
  35. #include <boost/context/stack_context.hpp>
  36. #ifdef BOOST_HAS_ABI_HEADERS
  37. # include BOOST_ABI_PREFIX
  38. #endif
  39. #if defined(BOOST_MSVC)
  40. # pragma warning(push)
  41. # pragma warning(disable: 4702)
  42. #endif
  43. namespace boost {
  44. namespace context {
  45. namespace detail {
  46. inline
  47. transfer_t fiber_unwind( transfer_t t) {
  48. throw forced_unwind( t.fctx);
  49. return { nullptr, nullptr };
  50. }
  51. template< typename Rec >
  52. transfer_t fiber_exit( transfer_t t) noexcept {
  53. Rec * rec = static_cast< Rec * >( t.data);
  54. // destroy context stack
  55. rec->deallocate();
  56. return { nullptr, nullptr };
  57. }
  58. template< typename Rec >
  59. void fiber_entry( transfer_t t) noexcept {
  60. // transfer control structure to the context-stack
  61. Rec * rec = static_cast< Rec * >( t.data);
  62. BOOST_ASSERT( nullptr != t.fctx);
  63. BOOST_ASSERT( nullptr != rec);
  64. try {
  65. // jump back to `create_context()`
  66. t = jump_fcontext( t.fctx, nullptr);
  67. // start executing
  68. t.fctx = rec->run( t.fctx);
  69. } catch ( forced_unwind const& ex) {
  70. t = { ex.fctx, nullptr };
  71. #ifndef BOOST_ASSERT_IS_VOID
  72. const_cast< forced_unwind & >( ex).caught = true;
  73. #endif
  74. }
  75. BOOST_ASSERT( nullptr != t.fctx);
  76. // destroy context-stack of `this`context on next context
  77. ontop_fcontext( t.fctx, rec, fiber_exit< Rec >);
  78. BOOST_ASSERT_MSG( false, "context already terminated");
  79. }
  80. template< typename Ctx, typename Fn >
  81. transfer_t fiber_ontop( transfer_t t) {
  82. auto p = static_cast< std::tuple< Fn > * >( t.data);
  83. BOOST_ASSERT( nullptr != p);
  84. typename std::decay< Fn >::type fn = std::get< 0 >( * p);
  85. t.data = nullptr;
  86. // execute function, pass fiber via reference
  87. Ctx c = fn( Ctx{ t.fctx } );
  88. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  89. return { exchange( c.fctx_, nullptr), nullptr };
  90. #else
  91. return { std::exchange( c.fctx_, nullptr), nullptr };
  92. #endif
  93. }
  94. template< typename Ctx, typename StackAlloc, typename Fn >
  95. class fiber_record {
  96. private:
  97. stack_context sctx_;
  98. typename std::decay< StackAlloc >::type salloc_;
  99. typename std::decay< Fn >::type fn_;
  100. static void destroy( fiber_record * p) noexcept {
  101. typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
  102. stack_context sctx = p->sctx_;
  103. // deallocate fiber_record
  104. p->~fiber_record();
  105. // destroy stack with stack allocator
  106. salloc.deallocate( sctx);
  107. }
  108. public:
  109. fiber_record( stack_context sctx, StackAlloc && salloc,
  110. Fn && fn) noexcept :
  111. sctx_( sctx),
  112. salloc_( std::forward< StackAlloc >( salloc)),
  113. fn_( std::forward< Fn >( fn) ) {
  114. }
  115. fiber_record( fiber_record const&) = delete;
  116. fiber_record & operator=( fiber_record const&) = delete;
  117. void deallocate() noexcept {
  118. destroy( this);
  119. }
  120. fcontext_t run( fcontext_t fctx) {
  121. // invoke context-function
  122. #if defined(BOOST_NO_CXX17_STD_INVOKE)
  123. Ctx c = boost::context::detail::invoke( fn_, Ctx{ fctx } );
  124. #else
  125. Ctx c = std::invoke( fn_, Ctx{ fctx } );
  126. #endif
  127. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  128. return exchange( c.fctx_, nullptr);
  129. #else
  130. return std::exchange( c.fctx_, nullptr);
  131. #endif
  132. }
  133. };
  134. template< typename Record, typename StackAlloc, typename Fn >
  135. fcontext_t create_fiber1( StackAlloc && salloc, Fn && fn) {
  136. auto sctx = salloc.allocate();
  137. // reserve space for control structure
  138. void * storage = reinterpret_cast< void * >(
  139. ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
  140. & ~static_cast< uintptr_t >( 0xff) );
  141. // placment new for control structure on context stack
  142. Record * record = new ( storage) Record{
  143. sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
  144. // 64byte gab between control structure and stack top
  145. // should be 16byte aligned
  146. void * stack_top = reinterpret_cast< void * >(
  147. reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) );
  148. void * stack_bottom = reinterpret_cast< void * >(
  149. reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) );
  150. // create fast-context
  151. const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
  152. const fcontext_t fctx = make_fcontext( stack_top, size, & fiber_entry< Record >);
  153. BOOST_ASSERT( nullptr != fctx);
  154. // transfer control structure to context-stack
  155. return jump_fcontext( fctx, record).fctx;
  156. }
  157. template< typename Record, typename StackAlloc, typename Fn >
  158. fcontext_t create_fiber2( preallocated palloc, StackAlloc && salloc, Fn && fn) {
  159. // reserve space for control structure
  160. void * storage = reinterpret_cast< void * >(
  161. ( reinterpret_cast< uintptr_t >( palloc.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
  162. & ~ static_cast< uintptr_t >( 0xff) );
  163. // placment new for control structure on context-stack
  164. Record * record = new ( storage) Record{
  165. palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
  166. // 64byte gab between control structure and stack top
  167. void * stack_top = reinterpret_cast< void * >(
  168. reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) );
  169. void * stack_bottom = reinterpret_cast< void * >(
  170. reinterpret_cast< uintptr_t >( palloc.sctx.sp) - static_cast< uintptr_t >( palloc.sctx.size) );
  171. // create fast-context
  172. const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
  173. const fcontext_t fctx = make_fcontext( stack_top, size, & fiber_entry< Record >);
  174. BOOST_ASSERT( nullptr != fctx);
  175. // transfer control structure to context-stack
  176. return jump_fcontext( fctx, record).fctx;
  177. }
  178. }
  179. class fiber {
  180. private:
  181. template< typename Ctx, typename StackAlloc, typename Fn >
  182. friend class detail::fiber_record;
  183. template< typename Ctx, typename Fn >
  184. friend detail::transfer_t
  185. detail::fiber_ontop( detail::transfer_t);
  186. template< typename StackAlloc, typename Fn >
  187. friend fiber
  188. callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
  189. template< typename StackAlloc, typename Fn >
  190. friend fiber
  191. callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
  192. detail::fcontext_t fctx_{ nullptr };
  193. fiber( detail::fcontext_t fctx) noexcept :
  194. fctx_{ fctx } {
  195. }
  196. public:
  197. fiber() noexcept = default;
  198. template< typename Fn, typename = detail::disable_overload< fiber, Fn > >
  199. fiber( Fn && fn) :
  200. fiber{ std::allocator_arg, fixedsize_stack(), std::forward< Fn >( fn) } {
  201. }
  202. template< typename StackAlloc, typename Fn >
  203. fiber( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) :
  204. fctx_{ detail::create_fiber1< detail::fiber_record< fiber, StackAlloc, Fn > >(
  205. std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {
  206. }
  207. template< typename StackAlloc, typename Fn >
  208. fiber( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) :
  209. fctx_{ detail::create_fiber2< detail::fiber_record< fiber, StackAlloc, Fn > >(
  210. palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {
  211. }
  212. #if defined(BOOST_USE_SEGMENTED_STACKS)
  213. template< typename Fn >
  214. fiber( std::allocator_arg_t, segmented_stack, Fn &&);
  215. template< typename StackAlloc, typename Fn >
  216. fiber( std::allocator_arg_t, preallocated, segmented_stack, Fn &&);
  217. #endif
  218. ~fiber() {
  219. if ( BOOST_UNLIKELY( nullptr != fctx_) ) {
  220. detail::ontop_fcontext(
  221. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  222. detail::exchange( fctx_, nullptr),
  223. #else
  224. std::exchange( fctx_, nullptr),
  225. #endif
  226. nullptr,
  227. detail::fiber_unwind);
  228. }
  229. }
  230. fiber( fiber && other) noexcept {
  231. swap( other);
  232. }
  233. fiber & operator=( fiber && other) noexcept {
  234. if ( BOOST_LIKELY( this != & other) ) {
  235. fiber tmp = std::move( other);
  236. swap( tmp);
  237. }
  238. return * this;
  239. }
  240. fiber( fiber const& other) noexcept = delete;
  241. fiber & operator=( fiber const& other) noexcept = delete;
  242. fiber resume() && {
  243. BOOST_ASSERT( nullptr != fctx_);
  244. return { detail::jump_fcontext(
  245. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  246. detail::exchange( fctx_, nullptr),
  247. #else
  248. std::exchange( fctx_, nullptr),
  249. #endif
  250. nullptr).fctx };
  251. }
  252. template< typename Fn >
  253. fiber resume_with( Fn && fn) && {
  254. BOOST_ASSERT( nullptr != fctx_);
  255. auto p = std::make_tuple( std::forward< Fn >( fn) );
  256. return { detail::ontop_fcontext(
  257. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  258. detail::exchange( fctx_, nullptr),
  259. #else
  260. std::exchange( fctx_, nullptr),
  261. #endif
  262. & p,
  263. detail::fiber_ontop< fiber, Fn >).fctx };
  264. }
  265. explicit operator bool() const noexcept {
  266. return nullptr != fctx_;
  267. }
  268. bool operator!() const noexcept {
  269. return nullptr == fctx_;
  270. }
  271. bool operator<( fiber const& other) const noexcept {
  272. return fctx_ < other.fctx_;
  273. }
  274. template< typename charT, class traitsT >
  275. friend std::basic_ostream< charT, traitsT > &
  276. operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
  277. if ( nullptr != other.fctx_) {
  278. return os << other.fctx_;
  279. } else {
  280. return os << "{not-a-context}";
  281. }
  282. }
  283. void swap( fiber & other) noexcept {
  284. std::swap( fctx_, other.fctx_);
  285. }
  286. };
  287. inline
  288. void swap( fiber & l, fiber & r) noexcept {
  289. l.swap( r);
  290. }
  291. typedef fiber fiber_context;
  292. }}
  293. #if defined(BOOST_MSVC)
  294. # pragma warning(pop)
  295. #endif
  296. #ifdef BOOST_HAS_ABI_HEADERS
  297. # include BOOST_ABI_SUFFIX
  298. #endif
  299. #endif // BOOST_CONTEXT_FIBER_H