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