fiber_fcontext.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. }
  72. BOOST_ASSERT( nullptr != t.fctx);
  73. // destroy context-stack of `this`context on next context
  74. ontop_fcontext( t.fctx, rec, fiber_exit< Rec >);
  75. BOOST_ASSERT_MSG( false, "context already terminated");
  76. }
  77. template< typename Ctx, typename Fn >
  78. transfer_t fiber_ontop( transfer_t t) {
  79. BOOST_ASSERT( nullptr != t.data);
  80. auto p = *static_cast< Fn * >( t.data);
  81. t.data = nullptr;
  82. // execute function, pass fiber via reference
  83. Ctx c = p( Ctx{ t.fctx } );
  84. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  85. return { exchange( c.fctx_, nullptr), nullptr };
  86. #else
  87. return { std::exchange( c.fctx_, nullptr), nullptr };
  88. #endif
  89. }
  90. template< typename Ctx, typename StackAlloc, typename Fn >
  91. class fiber_record {
  92. private:
  93. stack_context sctx_;
  94. typename std::decay< StackAlloc >::type salloc_;
  95. typename std::decay< Fn >::type fn_;
  96. static void destroy( fiber_record * p) noexcept {
  97. typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
  98. stack_context sctx = p->sctx_;
  99. // deallocate fiber_record
  100. p->~fiber_record();
  101. // destroy stack with stack allocator
  102. salloc.deallocate( sctx);
  103. }
  104. public:
  105. fiber_record( stack_context sctx, StackAlloc && salloc,
  106. Fn && fn) noexcept :
  107. sctx_( sctx),
  108. salloc_( std::forward< StackAlloc >( salloc)),
  109. fn_( std::forward< Fn >( fn) ) {
  110. }
  111. fiber_record( fiber_record const&) = delete;
  112. fiber_record & operator=( fiber_record const&) = delete;
  113. void deallocate() noexcept {
  114. destroy( this);
  115. }
  116. fcontext_t run( fcontext_t fctx) {
  117. // invoke context-function
  118. #if defined(BOOST_NO_CXX17_STD_INVOKE)
  119. Ctx c = boost::context::detail::invoke( fn_, Ctx{ fctx } );
  120. #else
  121. Ctx c = std::invoke( fn_, Ctx{ fctx } );
  122. #endif
  123. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  124. return exchange( c.fctx_, nullptr);
  125. #else
  126. return std::exchange( c.fctx_, nullptr);
  127. #endif
  128. }
  129. };
  130. template< typename Record, typename StackAlloc, typename Fn >
  131. fcontext_t create_fiber1( StackAlloc && salloc, Fn && fn) {
  132. auto sctx = salloc.allocate();
  133. // reserve space for control structure
  134. void * storage = reinterpret_cast< void * >(
  135. ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
  136. & ~static_cast< uintptr_t >( 0xff) );
  137. // placment new for control structure on context stack
  138. Record * record = new ( storage) Record{
  139. sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
  140. // 64byte gab between control structure and stack top
  141. // should be 16byte aligned
  142. void * stack_top = reinterpret_cast< void * >(
  143. reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) );
  144. void * stack_bottom = reinterpret_cast< void * >(
  145. reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) );
  146. // create fast-context
  147. const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
  148. const fcontext_t fctx = make_fcontext( stack_top, size, & fiber_entry< Record >);
  149. BOOST_ASSERT( nullptr != fctx);
  150. // transfer control structure to context-stack
  151. return jump_fcontext( fctx, record).fctx;
  152. }
  153. template< typename Record, typename StackAlloc, typename Fn >
  154. fcontext_t create_fiber2( preallocated palloc, StackAlloc && salloc, Fn && fn) {
  155. // reserve space for control structure
  156. void * storage = reinterpret_cast< void * >(
  157. ( reinterpret_cast< uintptr_t >( palloc.sp) - static_cast< uintptr_t >( sizeof( Record) ) )
  158. & ~ static_cast< uintptr_t >( 0xff) );
  159. // placment new for control structure on context-stack
  160. Record * record = new ( storage) Record{
  161. palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) };
  162. // 64byte gab between control structure and stack top
  163. void * stack_top = reinterpret_cast< void * >(
  164. reinterpret_cast< uintptr_t >( storage) - static_cast< uintptr_t >( 64) );
  165. void * stack_bottom = reinterpret_cast< void * >(
  166. reinterpret_cast< uintptr_t >( palloc.sctx.sp) - static_cast< uintptr_t >( palloc.sctx.size) );
  167. // create fast-context
  168. const std::size_t size = reinterpret_cast< uintptr_t >( stack_top) - reinterpret_cast< uintptr_t >( stack_bottom);
  169. const fcontext_t fctx = make_fcontext( stack_top, size, & fiber_entry< Record >);
  170. BOOST_ASSERT( nullptr != fctx);
  171. // transfer control structure to context-stack
  172. return jump_fcontext( fctx, record).fctx;
  173. }
  174. }
  175. class fiber {
  176. private:
  177. template< typename Ctx, typename StackAlloc, typename Fn >
  178. friend class detail::fiber_record;
  179. template< typename Ctx, typename Fn >
  180. friend detail::transfer_t
  181. detail::fiber_ontop( detail::transfer_t);
  182. template< typename StackAlloc, typename Fn >
  183. friend fiber
  184. callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
  185. template< typename StackAlloc, typename Fn >
  186. friend fiber
  187. callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
  188. detail::fcontext_t fctx_{ nullptr };
  189. fiber( detail::fcontext_t fctx) noexcept :
  190. fctx_{ fctx } {
  191. }
  192. public:
  193. fiber() noexcept = default;
  194. template< typename Fn, typename = detail::disable_overload< fiber, Fn > >
  195. fiber( Fn && fn) :
  196. fiber{ std::allocator_arg, fixedsize_stack(), std::forward< Fn >( fn) } {
  197. }
  198. template< typename StackAlloc, typename Fn >
  199. fiber( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) :
  200. fctx_{ detail::create_fiber1< detail::fiber_record< fiber, StackAlloc, Fn > >(
  201. std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {
  202. }
  203. template< typename StackAlloc, typename Fn >
  204. fiber( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) :
  205. fctx_{ detail::create_fiber2< detail::fiber_record< fiber, StackAlloc, Fn > >(
  206. palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) } {
  207. }
  208. #if defined(BOOST_USE_SEGMENTED_STACKS)
  209. template< typename Fn >
  210. fiber( std::allocator_arg_t, segmented_stack, Fn &&);
  211. template< typename StackAlloc, typename Fn >
  212. fiber( std::allocator_arg_t, preallocated, segmented_stack, Fn &&);
  213. #endif
  214. ~fiber() {
  215. if ( BOOST_UNLIKELY( nullptr != fctx_) ) {
  216. detail::ontop_fcontext(
  217. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  218. detail::exchange( fctx_, nullptr),
  219. #else
  220. std::exchange( fctx_, nullptr),
  221. #endif
  222. nullptr,
  223. detail::fiber_unwind);
  224. }
  225. }
  226. fiber( fiber && other) noexcept {
  227. swap( other);
  228. }
  229. fiber & operator=( fiber && other) noexcept {
  230. if ( BOOST_LIKELY( this != & other) ) {
  231. fiber tmp = std::move( other);
  232. swap( tmp);
  233. }
  234. return * this;
  235. }
  236. fiber( fiber const& other) noexcept = delete;
  237. fiber & operator=( fiber const& other) noexcept = delete;
  238. fiber resume() && {
  239. BOOST_ASSERT( nullptr != fctx_);
  240. return { detail::jump_fcontext(
  241. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  242. detail::exchange( fctx_, nullptr),
  243. #else
  244. std::exchange( fctx_, nullptr),
  245. #endif
  246. nullptr).fctx };
  247. }
  248. template< typename Fn >
  249. fiber resume_with( Fn && fn) && {
  250. BOOST_ASSERT( nullptr != fctx_);
  251. auto p = std::forward< Fn >( fn);
  252. return { detail::ontop_fcontext(
  253. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  254. detail::exchange( fctx_, nullptr),
  255. #else
  256. std::exchange( fctx_, nullptr),
  257. #endif
  258. & p,
  259. detail::fiber_ontop< fiber, decltype(p) >).fctx };
  260. }
  261. explicit operator bool() const noexcept {
  262. return nullptr != fctx_;
  263. }
  264. bool operator!() const noexcept {
  265. return nullptr == fctx_;
  266. }
  267. bool operator<( fiber const& other) const noexcept {
  268. return fctx_ < other.fctx_;
  269. }
  270. #if !defined(BOOST_EMBTC)
  271. template< typename charT, class traitsT >
  272. friend std::basic_ostream< charT, traitsT > &
  273. operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
  274. if ( nullptr != other.fctx_) {
  275. return os << other.fctx_;
  276. } else {
  277. return os << "{not-a-context}";
  278. }
  279. }
  280. #else
  281. template< typename charT, class traitsT >
  282. friend std::basic_ostream< charT, traitsT > &
  283. operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other);
  284. #endif
  285. void swap( fiber & other) noexcept {
  286. std::swap( fctx_, other.fctx_);
  287. }
  288. };
  289. #if defined(BOOST_EMBTC)
  290. template< typename charT, class traitsT >
  291. inline std::basic_ostream< charT, traitsT > &
  292. operator<<( std::basic_ostream< charT, traitsT > & os, fiber const& other) {
  293. if ( nullptr != other.fctx_) {
  294. return os << other.fctx_;
  295. } else {
  296. return os << "{not-a-context}";
  297. }
  298. }
  299. #endif
  300. inline
  301. void swap( fiber & l, fiber & r) noexcept {
  302. l.swap( r);
  303. }
  304. typedef fiber fiber_context;
  305. }}
  306. #if defined(BOOST_MSVC)
  307. # pragma warning(pop)
  308. #endif
  309. #ifdef BOOST_HAS_ABI_HEADERS
  310. # include BOOST_ABI_SUFFIX
  311. #endif
  312. #endif // BOOST_CONTEXT_FIBER_H