execution_context_v2.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // Copyright Oliver Kowalke 2014.
  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_EXECUTION_CONTEXT_V2_H
  6. #define BOOST_CONTEXT_EXECUTION_CONTEXT_V2_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_CXX17_STD_APPLY)
  22. #include <boost/context/detail/apply.hpp>
  23. #endif
  24. #include <boost/context/detail/disable_overload.hpp>
  25. #include <boost/context/detail/exception.hpp>
  26. #include <boost/context/detail/exchange.hpp>
  27. #include <boost/context/detail/fcontext.hpp>
  28. #include <boost/context/detail/tuple.hpp>
  29. #include <boost/context/fixedsize_stack.hpp>
  30. #include <boost/context/flags.hpp>
  31. #include <boost/context/preallocated.hpp>
  32. #include <boost/context/segmented_stack.hpp>
  33. #include <boost/context/stack_context.hpp>
  34. #ifdef BOOST_HAS_ABI_HEADERS
  35. # include BOOST_ABI_PREFIX
  36. #endif
  37. #if defined(BOOST_MSVC)
  38. # pragma warning(push)
  39. # pragma warning(disable: 4702)
  40. #endif
  41. namespace boost {
  42. namespace context {
  43. namespace detail {
  44. transfer_t ecv2_context_unwind( transfer_t);
  45. template< typename Rec >
  46. transfer_t ecv2_context_exit( transfer_t) noexcept;
  47. template< typename Rec >
  48. void ecv2_context_etry( transfer_t) noexcept;
  49. template< typename Ctx, typename Fn, typename ... Args >
  50. transfer_t ecv2_context_ontop( transfer_t);
  51. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  52. fcontext_t ecv2_context_create( StackAlloc &&, Fn &&, Params && ...);
  53. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  54. fcontext_t ecv2_context_create( preallocated, StackAlloc &&, Fn &&, Params && ...);
  55. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  56. class ecv2_record {
  57. private:
  58. typename std::decay< StackAlloc >::type salloc_;
  59. stack_context sctx_;
  60. typename std::decay< Fn >::type fn_;
  61. std::tuple< typename std::decay< Params >::type ... > params_;
  62. static void destroy( ecv2_record * p) noexcept {
  63. typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
  64. stack_context sctx = p->sctx_;
  65. // deallocate ecv2_record
  66. p->~ecv2_record();
  67. // destroy stack with stack allocator
  68. salloc.deallocate( sctx);
  69. }
  70. public:
  71. ecv2_record( stack_context sctx, StackAlloc && salloc,
  72. Fn && fn, Params && ... params) noexcept :
  73. salloc_( std::forward< StackAlloc >( salloc)),
  74. sctx_( sctx),
  75. fn_( std::forward< Fn >( fn) ),
  76. params_( std::forward< Params >( params) ... ) {
  77. }
  78. ecv2_record( ecv2_record const&) = delete;
  79. ecv2_record & operator=( ecv2_record const&) = delete;
  80. void deallocate() noexcept {
  81. destroy( this);
  82. }
  83. transfer_t run( transfer_t t) {
  84. Ctx from{ t.fctx };
  85. typename Ctx::args_tpl_t args = std::move( std::get<1>( * static_cast< std::tuple< std::exception_ptr, typename Ctx::args_tpl_t > * >( t.data) ) );
  86. auto tpl = std::tuple_cat(
  87. params_,
  88. std::forward_as_tuple( std::move( from) ),
  89. std::move( args) );
  90. // invoke context-function
  91. #if defined(BOOST_NO_CXX17_STD_APPLY)
  92. Ctx cc = boost::context::detail::apply( std::move( fn_), std::move( tpl) );
  93. #else
  94. Ctx cc = std::apply( std::move( fn_), std::move( tpl) );
  95. #endif
  96. return { exchange( cc.fctx_, nullptr), nullptr };
  97. }
  98. };
  99. }
  100. inline namespace v2 {
  101. template< typename ... Args >
  102. class execution_context {
  103. private:
  104. friend class ontop_error;
  105. typedef std::tuple< Args ... > args_tpl_t;
  106. typedef std::tuple< execution_context, typename std::decay< Args >::type ... > ret_tpl_t;
  107. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  108. friend class detail::ecv2_record;
  109. template< typename Ctx, typename Fn, typename ... ArgsT >
  110. friend detail::transfer_t detail::ecv2_context_ontop( detail::transfer_t);
  111. detail::fcontext_t fctx_{ nullptr };
  112. execution_context( detail::fcontext_t fctx) noexcept :
  113. fctx_( fctx) {
  114. }
  115. public:
  116. execution_context() noexcept = default;
  117. #if defined(BOOST_USE_SEGMENTED_STACKS)
  118. // segmented-stack requires to preserve the segments of the `current` context
  119. // which is not possible (no global pointer to current context)
  120. template< typename Fn, typename ... Params >
  121. execution_context( std::allocator_arg_t, segmented_stack, Fn &&, Params && ...) = delete;
  122. template< typename Fn, typename ... Params >
  123. execution_context( std::allocator_arg_t, preallocated, segmented_stack, Fn &&, Params && ...) = delete;
  124. #else
  125. template< typename Fn,
  126. typename ... Params,
  127. typename = detail::disable_overload< execution_context, Fn >
  128. >
  129. execution_context( Fn && fn, Params && ... params) :
  130. // deferred execution of fn and its arguments
  131. // arguments are stored in std::tuple<>
  132. // non-type template parameter pack via std::index_sequence_for<>
  133. // preserves the number of arguments
  134. // used to extract the function arguments from std::tuple<>
  135. fctx_( detail::ecv2_context_create< execution_context >(
  136. fixedsize_stack(),
  137. std::forward< Fn >( fn),
  138. std::forward< Params >( params) ... ) ) {
  139. }
  140. template< typename StackAlloc,
  141. typename Fn,
  142. typename ... Params
  143. >
  144. execution_context( std::allocator_arg_t, StackAlloc && salloc, Fn && fn, Params && ... params) :
  145. // deferred execution of fn and its arguments
  146. // arguments are stored in std::tuple<>
  147. // non-type template parameter pack via std::index_sequence_for<>
  148. // preserves the number of arguments
  149. // used to extract the function arguments from std::tuple<>
  150. fctx_( detail::ecv2_context_create< execution_context >(
  151. std::forward< StackAlloc >( salloc),
  152. std::forward< Fn >( fn),
  153. std::forward< Params >( params) ... ) ) {
  154. }
  155. template< typename StackAlloc,
  156. typename Fn,
  157. typename ... Params
  158. >
  159. execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn, Params && ... params) :
  160. // deferred execution of fn and its arguments
  161. // arguments are stored in std::tuple<>
  162. // non-type template parameter pack via std::index_sequence_for<>
  163. // preserves the number of arguments
  164. // used to extract the function arguments from std::tuple<>
  165. fctx_( detail::ecv2_context_create< execution_context >(
  166. palloc, std::forward< StackAlloc >( salloc),
  167. std::forward< Fn >( fn),
  168. std::forward< Params >( params) ... ) ) {
  169. }
  170. #endif
  171. ~execution_context() {
  172. if ( nullptr != fctx_) {
  173. detail::ontop_fcontext( detail::exchange( fctx_, nullptr), nullptr, detail::ecv2_context_unwind);
  174. }
  175. }
  176. execution_context( execution_context && other) noexcept :
  177. fctx_( other.fctx_) {
  178. other.fctx_ = nullptr;
  179. }
  180. execution_context & operator=( execution_context && other) noexcept {
  181. if ( this != & other) {
  182. execution_context tmp = std::move( other);
  183. swap( tmp);
  184. }
  185. return * this;
  186. }
  187. execution_context( execution_context const& other) noexcept = delete;
  188. execution_context & operator=( execution_context const& other) noexcept = delete;
  189. ret_tpl_t operator()( Args ... args);
  190. template< typename Fn >
  191. ret_tpl_t operator()( exec_ontop_arg_t, Fn && fn, Args ... args);
  192. explicit operator bool() const noexcept {
  193. return nullptr != fctx_;
  194. }
  195. bool operator!() const noexcept {
  196. return nullptr == fctx_;
  197. }
  198. bool operator<( execution_context const& other) const noexcept {
  199. return fctx_ < other.fctx_;
  200. }
  201. template< typename charT, class traitsT >
  202. friend std::basic_ostream< charT, traitsT > &
  203. operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
  204. if ( nullptr != other.fctx_) {
  205. return os << other.fctx_;
  206. } else {
  207. return os << "{not-a-context}";
  208. }
  209. }
  210. void swap( execution_context & other) noexcept {
  211. std::swap( fctx_, other.fctx_);
  212. }
  213. };
  214. class ontop_error : public std::exception {
  215. private:
  216. detail::fcontext_t fctx_;
  217. public:
  218. ontop_error( detail::fcontext_t fctx) noexcept :
  219. fctx_{ fctx } {
  220. }
  221. template< typename ... Args >
  222. execution_context< Args ... > get_context() const noexcept {
  223. return execution_context< Args ... >{ fctx_ };
  224. }
  225. };
  226. template< typename ... Args >
  227. typename execution_context< Args ... >::ret_tpl_t
  228. execution_context< Args ... >::operator()( Args ... args) {
  229. BOOST_ASSERT( nullptr != fctx_);
  230. args_tpl_t data( std::forward< Args >( args) ... );
  231. auto p = std::make_tuple( std::exception_ptr{}, std::move( data) );
  232. detail::transfer_t t = detail::jump_fcontext( detail::exchange( fctx_, nullptr), & p);
  233. if ( nullptr != t.data) {
  234. auto p = static_cast< std::tuple< std::exception_ptr, args_tpl_t > * >( t.data);
  235. std::exception_ptr eptr = std::get< 0 >( * p);
  236. if ( eptr) {
  237. try {
  238. std::rethrow_exception( eptr);
  239. } catch (...) {
  240. std::throw_with_nested( ontop_error{ t.fctx } );
  241. }
  242. }
  243. data = std::move( std::get< 1 >( * p) );
  244. }
  245. return std::tuple_cat( std::forward_as_tuple( execution_context( t.fctx) ), std::move( data) );
  246. }
  247. template< typename ... Args >
  248. template< typename Fn >
  249. typename execution_context< Args ... >::ret_tpl_t
  250. execution_context< Args ... >::operator()( exec_ontop_arg_t, Fn && fn, Args ... args) {
  251. BOOST_ASSERT( nullptr != fctx_);
  252. args_tpl_t data{ std::forward< Args >( args) ... };
  253. auto p = std::make_tuple( fn, std::make_tuple( std::exception_ptr{}, std::move( data) ) );
  254. detail::transfer_t t = detail::ontop_fcontext(
  255. detail::exchange( fctx_, nullptr),
  256. & p,
  257. detail::ecv2_context_ontop< execution_context, Fn, Args ... >);
  258. if ( nullptr != t.data) {
  259. auto p = static_cast< std::tuple< std::exception_ptr, args_tpl_t > * >( t.data);
  260. std::exception_ptr eptr = std::get< 0 >( * p);
  261. if ( eptr) {
  262. try {
  263. std::rethrow_exception( eptr);
  264. } catch (...) {
  265. std::throw_with_nested( ontop_error{ t.fctx } );
  266. }
  267. }
  268. data = std::move( std::get< 1 >( * p) );
  269. }
  270. return std::tuple_cat( std::forward_as_tuple( execution_context( t.fctx) ), std::move( data) );
  271. }
  272. }
  273. namespace detail {
  274. template< int N >
  275. struct helper {
  276. template< typename T >
  277. static T convert( T && t) noexcept {
  278. return std::forward< T >( t);
  279. }
  280. };
  281. template<>
  282. struct helper< 1 > {
  283. template< typename T >
  284. static std::tuple< T > convert( T && t) noexcept {
  285. return std::make_tuple( std::forward< T >( t) );
  286. }
  287. };
  288. inline
  289. transfer_t ecv2_context_unwind( transfer_t t) {
  290. throw forced_unwind( t.fctx);
  291. return { nullptr, nullptr };
  292. }
  293. template< typename Rec >
  294. transfer_t ecv2_context_exit( transfer_t t) noexcept {
  295. Rec * rec = static_cast< Rec * >( t.data);
  296. // destroy context stack
  297. rec->deallocate();
  298. return { nullptr, nullptr };
  299. }
  300. template< typename Rec >
  301. void ecv2_context_etry( transfer_t t_) noexcept {
  302. // transfer control structure to the context-stack
  303. Rec * rec = static_cast< Rec * >( t_.data);
  304. BOOST_ASSERT( nullptr != rec);
  305. transfer_t t = { nullptr, nullptr };
  306. try {
  307. // jump back to `ecv2_context_create()`
  308. t = jump_fcontext( t_.fctx, nullptr);
  309. // start executing
  310. t = rec->run( t);
  311. } catch ( forced_unwind const& ex) {
  312. t = { ex.fctx, nullptr };
  313. #ifndef BOOST_ASSERT_IS_VOID
  314. const_cast< forced_unwind & >( ex).caught = true;
  315. #endif
  316. }
  317. BOOST_ASSERT( nullptr != t.fctx);
  318. // destroy context-stack of `this`context on next context
  319. ontop_fcontext( t.fctx, rec, ecv2_context_exit< Rec >);
  320. BOOST_ASSERT_MSG( false, "context already terminated");
  321. }
  322. template< typename Ctx, typename Fn, typename ... Args >
  323. transfer_t ecv2_context_ontop( transfer_t t) {
  324. auto p = static_cast< std::tuple< Fn, std::tuple< std::exception_ptr, std::tuple< Args ... > > > * >( t.data);
  325. BOOST_ASSERT( nullptr != p);
  326. typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 0 >( * p) );
  327. auto args = std::move( std::get< 1 >( std::get< 1 >( * p) ) );
  328. try {
  329. // execute function
  330. #if defined(BOOST_NO_CXX17_STD_APPLY)
  331. std::get< 1 >( std::get< 1 >( * p) ) = helper< sizeof ... (Args) >::convert( boost::context::detail::apply( fn, std::move( args) ) );
  332. #else
  333. std::get< 1 >( std::get< 1 >( * p) ) = helper< sizeof ... (Args) >::convert( std::apply( fn, std::move( args) ) );
  334. #endif
  335. } catch (...) {
  336. std::get< 0 >( std::get< 1 >( * p) ) = std::current_exception();
  337. }
  338. // apply returned data
  339. return { t.fctx, & std::get< 1 >( * p) };
  340. }
  341. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  342. fcontext_t ecv2_context_create( StackAlloc && salloc, Fn && fn, Params && ... params) {
  343. typedef ecv2_record< Ctx, StackAlloc, Fn, Params ... > ecv2_record_t;
  344. auto sctx = salloc.allocate();
  345. // reserve space for control structure
  346. #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
  347. const std::size_t size = sctx.size - sizeof( ecv2_record_t);
  348. void * sp = static_cast< char * >( sctx.sp) - sizeof( ecv2_record_t);
  349. #else
  350. constexpr std::size_t func_alignment = 64; // alignof( ecv2_record_t);
  351. constexpr std::size_t func_size = sizeof( ecv2_record_t);
  352. // reserve space on stack
  353. void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
  354. // align sp pointer
  355. std::size_t space = func_size + func_alignment;
  356. sp = std::align( func_alignment, func_size, sp, space);
  357. BOOST_ASSERT( nullptr != sp);
  358. // calculate remaining size
  359. const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
  360. #endif
  361. // create fast-context
  362. const fcontext_t fctx = make_fcontext( sp, size, & ecv2_context_etry< ecv2_record_t >);
  363. BOOST_ASSERT( nullptr != fctx);
  364. // placment new for control structure on context-stack
  365. auto rec = ::new ( sp) ecv2_record_t{
  366. sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn), std::forward< Params >( params) ... };
  367. // transfer control structure to context-stack
  368. return jump_fcontext( fctx, rec).fctx;
  369. }
  370. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  371. fcontext_t ecv2_context_create( preallocated palloc, StackAlloc && salloc, Fn && fn, Params && ... params) {
  372. typedef ecv2_record< Ctx, StackAlloc, Fn, Params ... > ecv2_record_t;
  373. // reserve space for control structure
  374. #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
  375. const std::size_t size = palloc.size - sizeof( ecv2_record_t);
  376. void * sp = static_cast< char * >( palloc.sp) - sizeof( ecv2_record_t);
  377. #else
  378. constexpr std::size_t func_alignment = 64; // alignof( ecv2_record_t);
  379. constexpr std::size_t func_size = sizeof( ecv2_record_t);
  380. // reserve space on stack
  381. void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
  382. // align sp pointer
  383. std::size_t space = func_size + func_alignment;
  384. sp = std::align( func_alignment, func_size, sp, space);
  385. BOOST_ASSERT( nullptr != sp);
  386. // calculate remaining size
  387. const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
  388. #endif
  389. // create fast-context
  390. const fcontext_t fctx = make_fcontext( sp, size, & ecv2_context_etry< ecv2_record_t >);
  391. BOOST_ASSERT( nullptr != fctx);
  392. // placment new for control structure on context-stack
  393. auto rec = ::new ( sp) ecv2_record_t{
  394. palloc.sctx, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn), std::forward< Params >( params) ... };
  395. // transfer control structure to context-stack
  396. return jump_fcontext( fctx, rec).fctx;
  397. }
  398. }
  399. #include <boost/context/execution_context_v2_void.ipp>
  400. inline namespace v2 {
  401. template< typename ... Args >
  402. void swap( execution_context< Args ... > & l, execution_context< Args ... > & r) noexcept {
  403. l.swap( r);
  404. }
  405. }}}
  406. #if defined(BOOST_MSVC)
  407. # pragma warning(pop)
  408. #endif
  409. #ifdef BOOST_HAS_ABI_HEADERS
  410. # include BOOST_ABI_SUFFIX
  411. #endif
  412. #endif // BOOST_CONTEXT_EXECUTION_CONTEXT_V2_H