execution_context_v2.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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_H
  6. #define BOOST_CONTEXT_EXECUTION_CONTEXT_H
  7. #include <boost/context/detail/config.hpp>
  8. #include <algorithm>
  9. #include <cstddef>
  10. #include <cstdint>
  11. #include <cstdlib>
  12. #include <functional>
  13. #include <memory>
  14. #include <ostream>
  15. #include <tuple>
  16. #include <utility>
  17. #include <boost/assert.hpp>
  18. #include <boost/config.hpp>
  19. #include <boost/intrusive_ptr.hpp>
  20. #include <boost/context/detail/apply.hpp>
  21. #include <boost/context/detail/disable_overload.hpp>
  22. #include <boost/context/detail/exception.hpp>
  23. #include <boost/context/detail/exchange.hpp>
  24. #include <boost/context/detail/fcontext.hpp>
  25. #include <boost/context/detail/tuple.hpp>
  26. #include <boost/context/fixedsize_stack.hpp>
  27. #include <boost/context/flags.hpp>
  28. #include <boost/context/preallocated.hpp>
  29. #include <boost/context/segmented_stack.hpp>
  30. #include <boost/context/stack_context.hpp>
  31. #ifdef BOOST_HAS_ABI_HEADERS
  32. # include BOOST_ABI_PREFIX
  33. #endif
  34. namespace boost {
  35. namespace context {
  36. namespace detail {
  37. inline
  38. transfer_t context_unwind( transfer_t t) {
  39. throw forced_unwind( t.fctx);
  40. return { nullptr, nullptr };
  41. }
  42. template< typename Rec >
  43. transfer_t context_exit( transfer_t t) noexcept {
  44. Rec * rec = static_cast< Rec * >( t.data);
  45. // destroy context stack
  46. rec->deallocate();
  47. return { nullptr, nullptr };
  48. }
  49. template< typename Rec >
  50. void context_entry( transfer_t t_) noexcept {
  51. // transfer control structure to the context-stack
  52. Rec * rec = static_cast< Rec * >( t_.data);
  53. BOOST_ASSERT( nullptr != rec);
  54. transfer_t t = { nullptr, nullptr };
  55. try {
  56. // jump back to `context_create()`
  57. t = jump_fcontext( t_.fctx, nullptr);
  58. // start executing
  59. t = rec->run( t);
  60. } catch ( forced_unwind const& e) {
  61. t = { e.fctx, nullptr };
  62. }
  63. BOOST_ASSERT( nullptr != t.fctx);
  64. // destroy context-stack of `this`context on next context
  65. ontop_fcontext( t.fctx, rec, context_exit< Rec >);
  66. BOOST_ASSERT_MSG( false, "context already terminated");
  67. }
  68. template< typename Ctx, typename Fn, typename ... Args >
  69. transfer_t context_ontop( transfer_t t) {
  70. auto tpl = static_cast< std::tuple< Fn, std::tuple< Args ... > > * >( t.data);
  71. BOOST_ASSERT( nullptr != tpl);
  72. typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 0 >( * tpl) );
  73. auto args = std::move( std::get< 1 >( * tpl) );
  74. Ctx ctx{ t.fctx };
  75. // execute function
  76. auto result = apply(
  77. fn,
  78. std::tuple_cat(
  79. std::forward_as_tuple( std::move( ctx) ),
  80. std::move( args) ) );
  81. ctx = std::move( std::get< 0 >( result) );
  82. // apply returned data
  83. detail::tail( args) = std::move( result);
  84. std::get< 1 >( * tpl) = std::move( args);
  85. return { exchange( ctx.fctx_, nullptr), & std::get< 1 >( * tpl) };
  86. }
  87. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  88. class record {
  89. private:
  90. StackAlloc salloc_;
  91. stack_context sctx_;
  92. typename std::decay< Fn >::type fn_;
  93. std::tuple< typename std::decay< Params >::type ... > params_;
  94. static void destroy( record * p) noexcept {
  95. StackAlloc salloc = p->salloc_;
  96. stack_context sctx = p->sctx_;
  97. // deallocate record
  98. p->~record();
  99. // destroy stack with stack allocator
  100. salloc.deallocate( sctx);
  101. }
  102. public:
  103. record( stack_context sctx, StackAlloc const& salloc,
  104. Fn && fn, Params && ... params) noexcept :
  105. salloc_( salloc),
  106. sctx_( sctx),
  107. fn_( std::forward< Fn >( fn) ),
  108. params_( std::forward< Params >( params) ... ) {
  109. }
  110. record( record const&) = delete;
  111. record & operator=( record const&) = delete;
  112. void deallocate() noexcept {
  113. destroy( this);
  114. }
  115. transfer_t run( transfer_t t) {
  116. Ctx from{ t.fctx };
  117. typename Ctx::args_tpl_t args = std::move( * static_cast< typename Ctx::args_tpl_t * >( t.data) );
  118. auto tpl = std::tuple_cat(
  119. params_,
  120. std::forward_as_tuple( std::move( from) ),
  121. std::move( args) );
  122. // invoke context-function
  123. Ctx cc = apply( std::move( fn_), std::move( tpl) );
  124. return { exchange( cc.fctx_, nullptr), nullptr };
  125. }
  126. };
  127. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  128. fcontext_t context_create( StackAlloc salloc, Fn && fn, Params && ... params) {
  129. typedef record< Ctx, StackAlloc, Fn, Params ... > record_t;
  130. auto sctx = salloc.allocate();
  131. // reserve space for control structure
  132. #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
  133. const std::size_t size = sctx.size - sizeof( record_t);
  134. void * sp = static_cast< char * >( sctx.sp) - sizeof( record_t);
  135. #else
  136. constexpr std::size_t func_alignment = 64; // alignof( record_t);
  137. constexpr std::size_t func_size = sizeof( record_t);
  138. // reserve space on stack
  139. void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
  140. // align sp pointer
  141. std::size_t space = func_size + func_alignment;
  142. sp = std::align( func_alignment, func_size, sp, space);
  143. BOOST_ASSERT( nullptr != sp);
  144. // calculate remaining size
  145. const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
  146. #endif
  147. // create fast-context
  148. const fcontext_t fctx = make_fcontext( sp, size, & context_entry< record_t >);
  149. BOOST_ASSERT( nullptr != fctx);
  150. // placment new for control structure on context-stack
  151. auto rec = ::new ( sp) record_t{
  152. sctx, salloc, std::forward< Fn >( fn), std::forward< Params >( params) ... };
  153. // transfer control structure to context-stack
  154. return jump_fcontext( fctx, rec).fctx;
  155. }
  156. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  157. fcontext_t context_create( preallocated palloc, StackAlloc salloc, Fn && fn, Params && ... params) {
  158. typedef record< Ctx, StackAlloc, Fn, Params ... > record_t;
  159. // reserve space for control structure
  160. #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
  161. const std::size_t size = palloc.size - sizeof( record_t);
  162. void * sp = static_cast< char * >( palloc.sp) - sizeof( record_t);
  163. #else
  164. constexpr std::size_t func_alignment = 64; // alignof( record_t);
  165. constexpr std::size_t func_size = sizeof( record_t);
  166. // reserve space on stack
  167. void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
  168. // align sp pointer
  169. std::size_t space = func_size + func_alignment;
  170. sp = std::align( func_alignment, func_size, sp, space);
  171. BOOST_ASSERT( nullptr != sp);
  172. // calculate remaining size
  173. const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
  174. #endif
  175. // create fast-context
  176. const fcontext_t fctx = make_fcontext( sp, size, & context_entry< record_t >);
  177. BOOST_ASSERT( nullptr != fctx);
  178. // placment new for control structure on context-stack
  179. auto rec = ::new ( sp) record_t{
  180. palloc.sctx, salloc, std::forward< Fn >( fn), std::forward< Params >( params) ... };
  181. // transfer control structure to context-stack
  182. return jump_fcontext( fctx, rec).fctx;
  183. }
  184. }
  185. template< typename ... Args >
  186. class execution_context {
  187. private:
  188. typedef std::tuple< Args ... > args_tpl_t;
  189. typedef std::tuple< execution_context, typename std::decay< Args >::type ... > ret_tpl_t;
  190. template< typename Ctx, typename StackAlloc, typename Fn, typename ... Params >
  191. friend class detail::record;
  192. template< typename Ctx, typename Fn, typename ... ArgsT >
  193. friend detail::transfer_t detail::context_ontop( detail::transfer_t);
  194. detail::fcontext_t fctx_{ nullptr };
  195. execution_context( detail::fcontext_t fctx) noexcept :
  196. fctx_( fctx) {
  197. }
  198. public:
  199. constexpr execution_context() noexcept = default;
  200. #if defined(BOOST_USE_SEGMENTED_STACKS)
  201. // segmented-stack requires to preserve the segments of the `current` context
  202. // which is not possible (no global pointer to current context)
  203. template< typename Fn, typename ... Params >
  204. execution_context( std::allocator_arg_t, segmented_stack, Fn &&, Params && ...) = delete;
  205. template< typename Fn, typename ... Params >
  206. execution_context( std::allocator_arg_t, preallocated, segmented_stack, Fn &&, Params && ...) = delete;
  207. #else
  208. template< typename Fn,
  209. typename ... Params,
  210. typename = detail::disable_overload< execution_context, Fn >
  211. >
  212. execution_context( Fn && fn, Params && ... params) :
  213. // deferred execution of fn and its arguments
  214. // arguments are stored in std::tuple<>
  215. // non-type template parameter pack via std::index_sequence_for<>
  216. // preserves the number of arguments
  217. // used to extract the function arguments from std::tuple<>
  218. fctx_( detail::context_create< execution_context >(
  219. fixedsize_stack(),
  220. std::forward< Fn >( fn),
  221. std::forward< Params >( params) ... ) ) {
  222. }
  223. template< typename StackAlloc,
  224. typename Fn,
  225. typename ... Params
  226. >
  227. execution_context( std::allocator_arg_t, StackAlloc salloc, Fn && fn, Params && ... params) :
  228. // deferred execution of fn and its arguments
  229. // arguments are stored in std::tuple<>
  230. // non-type template parameter pack via std::index_sequence_for<>
  231. // preserves the number of arguments
  232. // used to extract the function arguments from std::tuple<>
  233. fctx_( detail::context_create< execution_context >(
  234. salloc,
  235. std::forward< Fn >( fn),
  236. std::forward< Params >( params) ... ) ) {
  237. }
  238. template< typename StackAlloc,
  239. typename Fn,
  240. typename ... Params
  241. >
  242. execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc salloc, Fn && fn, Params && ... params) :
  243. // deferred execution of fn and its arguments
  244. // arguments are stored in std::tuple<>
  245. // non-type template parameter pack via std::index_sequence_for<>
  246. // preserves the number of arguments
  247. // used to extract the function arguments from std::tuple<>
  248. fctx_( detail::context_create< execution_context >(
  249. palloc, salloc,
  250. std::forward< Fn >( fn),
  251. std::forward< Params >( params) ... ) ) {
  252. }
  253. #endif
  254. ~execution_context() {
  255. if ( nullptr != fctx_) {
  256. detail::ontop_fcontext( detail::exchange( fctx_, nullptr), nullptr, detail::context_unwind);
  257. }
  258. }
  259. execution_context( execution_context && other) noexcept :
  260. fctx_( other.fctx_) {
  261. other.fctx_ = nullptr;
  262. }
  263. execution_context & operator=( execution_context && other) noexcept {
  264. if ( this != & other) {
  265. execution_context tmp = std::move( other);
  266. swap( tmp);
  267. }
  268. return * this;
  269. }
  270. execution_context( execution_context const& other) noexcept = delete;
  271. execution_context & operator=( execution_context const& other) noexcept = delete;
  272. ret_tpl_t operator()( Args ... args) {
  273. BOOST_ASSERT( nullptr != fctx_);
  274. args_tpl_t data( std::forward< Args >( args) ... );
  275. detail::transfer_t t = detail::jump_fcontext( detail::exchange( fctx_, nullptr), & data);
  276. if ( nullptr != t.data) {
  277. data = std::move( * static_cast< args_tpl_t * >( t.data) );
  278. }
  279. return std::tuple_cat( std::forward_as_tuple( execution_context( t.fctx) ), std::move( data) );
  280. }
  281. template< typename Fn >
  282. ret_tpl_t operator()( exec_ontop_arg_t, Fn && fn, Args ... args) {
  283. BOOST_ASSERT( nullptr != fctx_);
  284. args_tpl_t data{ std::forward< Args >( args) ... };
  285. auto p = std::make_tuple( fn, std::move( data) );
  286. detail::transfer_t t = detail::ontop_fcontext(
  287. detail::exchange( fctx_, nullptr),
  288. & p,
  289. detail::context_ontop< execution_context, Fn, Args ... >);
  290. if ( nullptr != t.data) {
  291. data = std::move( * static_cast< args_tpl_t * >( t.data) );
  292. }
  293. return std::tuple_cat( std::forward_as_tuple( execution_context( t.fctx) ), std::move( data) );
  294. }
  295. explicit operator bool() const noexcept {
  296. return nullptr != fctx_;
  297. }
  298. bool operator!() const noexcept {
  299. return nullptr == fctx_;
  300. }
  301. bool operator==( execution_context const& other) const noexcept {
  302. return fctx_ == other.fctx_;
  303. }
  304. bool operator!=( execution_context const& other) const noexcept {
  305. return fctx_ != other.fctx_;
  306. }
  307. bool operator<( execution_context const& other) const noexcept {
  308. return fctx_ < other.fctx_;
  309. }
  310. bool operator>( execution_context const& other) const noexcept {
  311. return other.fctx_ < fctx_;
  312. }
  313. bool operator<=( execution_context const& other) const noexcept {
  314. return ! ( * this > other);
  315. }
  316. bool operator>=( execution_context const& other) const noexcept {
  317. return ! ( * this < other);
  318. }
  319. template< typename charT, class traitsT >
  320. friend std::basic_ostream< charT, traitsT > &
  321. operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
  322. if ( nullptr != other.fctx_) {
  323. return os << other.fctx_;
  324. } else {
  325. return os << "{not-a-context}";
  326. }
  327. }
  328. void swap( execution_context & other) noexcept {
  329. std::swap( fctx_, other.fctx_);
  330. }
  331. };
  332. #include <boost/context/execution_context_v2_void.ipp>
  333. template< typename ... Args >
  334. void swap( execution_context< Args ... > & l, execution_context< Args ... > & r) noexcept {
  335. l.swap( r);
  336. }
  337. }}
  338. #ifdef BOOST_HAS_ABI_HEADERS
  339. # include BOOST_ABI_SUFFIX
  340. #endif
  341. #endif // BOOST_CONTEXT_EXECUTION_CONTEXT_H