continuation_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_CONTINUATION_H
  6. #define BOOST_CONTEXT_CONTINUATION_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 context_unwind( transfer_t t) {
  48. throw forced_unwind( t.fctx);
  49. return { nullptr, nullptr };
  50. }
  51. template< typename Rec >
  52. transfer_t context_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 context_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, context_exit< Rec >);
  75. BOOST_ASSERT_MSG( false, "context already terminated");
  76. }
  77. template< typename Ctx, typename Fn >
  78. transfer_t context_ontop( transfer_t t) {
  79. auto p = static_cast< std::tuple< Fn > * >( t.data);
  80. BOOST_ASSERT( nullptr != p);
  81. typename std::decay< Fn >::type fn = std::get< 0 >( * p);
  82. t.data = nullptr;
  83. Ctx c{ t.fctx };
  84. // execute function, pass continuation via reference
  85. c = fn( std::move( c) );
  86. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  87. return { exchange( c.fctx_, nullptr), nullptr };
  88. #else
  89. return { std::exchange( c.fctx_, nullptr), nullptr };
  90. #endif
  91. }
  92. template< typename Ctx, typename StackAlloc, typename Fn >
  93. class record {
  94. private:
  95. stack_context sctx_;
  96. typename std::decay< StackAlloc >::type salloc_;
  97. typename std::decay< Fn >::type fn_;
  98. static void destroy( record * p) noexcept {
  99. typename std::decay< StackAlloc >::type salloc = std::move( p->salloc_);
  100. stack_context sctx = p->sctx_;
  101. // deallocate record
  102. p->~record();
  103. // destroy stack with stack allocator
  104. salloc.deallocate( sctx);
  105. }
  106. public:
  107. record( stack_context sctx, StackAlloc && salloc,
  108. Fn && fn) noexcept :
  109. sctx_( sctx),
  110. salloc_( std::forward< StackAlloc >( salloc)),
  111. fn_( std::forward< Fn >( fn) ) {
  112. }
  113. record( record const&) = delete;
  114. record & operator=( record const&) = delete;
  115. void deallocate() noexcept {
  116. destroy( this);
  117. }
  118. fcontext_t run( fcontext_t fctx) {
  119. Ctx c{ fctx };
  120. // invoke context-function
  121. #if defined(BOOST_NO_CXX17_STD_INVOKE)
  122. c = boost::context::detail::invoke( fn_, std::move( c) );
  123. #else
  124. c = std::invoke( fn_, std::move( c) );
  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_context1( 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, & context_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_context2( 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, & context_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 continuation {
  179. private:
  180. template< typename Ctx, typename StackAlloc, typename Fn >
  181. friend class detail::record;
  182. template< typename Ctx, typename Fn >
  183. friend detail::transfer_t
  184. detail::context_ontop( detail::transfer_t);
  185. template< typename StackAlloc, typename Fn >
  186. friend continuation
  187. callcc( std::allocator_arg_t, StackAlloc &&, Fn &&);
  188. template< typename StackAlloc, typename Fn >
  189. friend continuation
  190. callcc( std::allocator_arg_t, preallocated, StackAlloc &&, Fn &&);
  191. detail::fcontext_t fctx_{ nullptr };
  192. continuation( detail::fcontext_t fctx) noexcept :
  193. fctx_{ fctx } {
  194. }
  195. public:
  196. continuation() noexcept = default;
  197. ~continuation() {
  198. if ( BOOST_UNLIKELY( nullptr != fctx_) ) {
  199. detail::ontop_fcontext(
  200. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  201. detail::exchange( fctx_, nullptr),
  202. #else
  203. std::exchange( fctx_, nullptr),
  204. #endif
  205. nullptr,
  206. detail::context_unwind);
  207. }
  208. }
  209. continuation( continuation && other) noexcept {
  210. swap( other);
  211. }
  212. continuation & operator=( continuation && other) noexcept {
  213. if ( BOOST_LIKELY( this != & other) ) {
  214. continuation tmp = std::move( other);
  215. swap( tmp);
  216. }
  217. return * this;
  218. }
  219. continuation( continuation const& other) noexcept = delete;
  220. continuation & operator=( continuation const& other) noexcept = delete;
  221. continuation resume() & {
  222. return std::move( * this).resume();
  223. }
  224. continuation resume() && {
  225. BOOST_ASSERT( nullptr != fctx_);
  226. return { detail::jump_fcontext(
  227. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  228. detail::exchange( fctx_, nullptr),
  229. #else
  230. std::exchange( fctx_, nullptr),
  231. #endif
  232. nullptr).fctx };
  233. }
  234. template< typename Fn >
  235. continuation resume_with( Fn && fn) & {
  236. return std::move( * this).resume_with( std::forward< Fn >( fn) );
  237. }
  238. template< typename Fn >
  239. continuation resume_with( Fn && fn) && {
  240. BOOST_ASSERT( nullptr != fctx_);
  241. auto p = std::make_tuple( std::forward< Fn >( fn) );
  242. return { detail::ontop_fcontext(
  243. #if defined(BOOST_NO_CXX14_STD_EXCHANGE)
  244. detail::exchange( fctx_, nullptr),
  245. #else
  246. std::exchange( fctx_, nullptr),
  247. #endif
  248. & p,
  249. detail::context_ontop< continuation, Fn >).fctx };
  250. }
  251. explicit operator bool() const noexcept {
  252. return nullptr != fctx_;
  253. }
  254. bool operator!() const noexcept {
  255. return nullptr == fctx_;
  256. }
  257. bool operator<( continuation const& other) const noexcept {
  258. return fctx_ < other.fctx_;
  259. }
  260. template< typename charT, class traitsT >
  261. friend std::basic_ostream< charT, traitsT > &
  262. operator<<( std::basic_ostream< charT, traitsT > & os, continuation const& other) {
  263. if ( nullptr != other.fctx_) {
  264. return os << other.fctx_;
  265. } else {
  266. return os << "{not-a-context}";
  267. }
  268. }
  269. void swap( continuation & other) noexcept {
  270. std::swap( fctx_, other.fctx_);
  271. }
  272. };
  273. template<
  274. typename Fn,
  275. typename = detail::disable_overload< continuation, Fn >
  276. >
  277. continuation
  278. callcc( Fn && fn) {
  279. return callcc(
  280. std::allocator_arg, fixedsize_stack(),
  281. std::forward< Fn >( fn) );
  282. }
  283. template< typename StackAlloc, typename Fn >
  284. continuation
  285. callcc( std::allocator_arg_t, StackAlloc && salloc, Fn && fn) {
  286. using Record = detail::record< continuation, StackAlloc, Fn >;
  287. return continuation{
  288. detail::create_context1< Record >(
  289. std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
  290. }
  291. template< typename StackAlloc, typename Fn >
  292. continuation
  293. callcc( std::allocator_arg_t, preallocated palloc, StackAlloc && salloc, Fn && fn) {
  294. using Record = detail::record< continuation, StackAlloc, Fn >;
  295. return continuation{
  296. detail::create_context2< Record >(
  297. palloc, std::forward< StackAlloc >( salloc), std::forward< Fn >( fn) ) }.resume();
  298. }
  299. #if defined(BOOST_USE_SEGMENTED_STACKS)
  300. template< typename Fn >
  301. continuation
  302. callcc( std::allocator_arg_t, segmented_stack, Fn &&);
  303. template< typename StackAlloc, typename Fn >
  304. continuation
  305. callcc( std::allocator_arg_t, preallocated, segmented_stack, Fn &&);
  306. #endif
  307. inline
  308. void swap( continuation & l, continuation & r) noexcept {
  309. l.swap( r);
  310. }
  311. }}
  312. #if defined(BOOST_MSVC)
  313. # pragma warning(pop)
  314. #endif
  315. #ifdef BOOST_HAS_ABI_HEADERS
  316. # include BOOST_ABI_SUFFIX
  317. #endif
  318. #endif // BOOST_CONTEXT_CONTINUATION_H