fiber_fcontext.hpp 11 KB

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