pull_control_block_cc.ipp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // Copyright Oliver Kowalke 2016.
  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_COROUTINES2_DETAIL_PULL_CONTROL_BLOCK_IPP
  6. #define BOOST_COROUTINES2_DETAIL_PULL_CONTROL_BLOCK_IPP
  7. #include <algorithm>
  8. #include <exception>
  9. #include <memory>
  10. #include <tuple>
  11. #include <boost/assert.hpp>
  12. #include <boost/config.hpp>
  13. #include <boost/context/detail/config.hpp>
  14. #include <boost/context/fiber.hpp>
  15. #include <boost/coroutine2/detail/config.hpp>
  16. #include <boost/coroutine2/detail/forced_unwind.hpp>
  17. #include <boost/coroutine2/detail/wrap.hpp>
  18. #ifdef BOOST_HAS_ABI_HEADERS
  19. # include BOOST_ABI_PREFIX
  20. #endif
  21. namespace boost {
  22. namespace coroutines2 {
  23. namespace detail {
  24. // pull_coroutine< T >
  25. template< typename T >
  26. void
  27. pull_coroutine< T >::control_block::destroy( control_block * cb) noexcept {
  28. boost::context::fiber c = std::move( cb->c);
  29. // destroy control structure
  30. cb->~control_block();
  31. // destroy coroutine's stack
  32. cb->state |= state_t::destroy;
  33. }
  34. template< typename T >
  35. template< typename StackAllocator, typename Fn >
  36. pull_coroutine< T >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc,
  37. Fn && fn) :
  38. #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS)
  39. c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc),
  40. wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::fiber && c) mutable {
  41. // create synthesized push_coroutine< T >
  42. typename push_coroutine< T >::control_block synthesized_cb{ this, c };
  43. push_coroutine< T > synthesized{ & synthesized_cb };
  44. other = & synthesized_cb;
  45. if ( state_t::none == ( state & state_t::destroy) ) {
  46. try {
  47. auto fn = std::move( fn_);
  48. // call coroutine-fn with synthesized push_coroutine as argument
  49. fn( synthesized);
  50. } catch ( boost::context::detail::forced_unwind const&) {
  51. throw;
  52. #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
  53. } catch ( abi::__forced_unwind const&) {
  54. throw;
  55. #endif
  56. } catch (...) {
  57. // store other exceptions in exception-pointer
  58. except = std::current_exception();
  59. }
  60. }
  61. // set termination flags
  62. state |= state_t::complete;
  63. // jump back
  64. return std::move( other->c).resume();
  65. },
  66. std::forward< Fn >( fn) ) },
  67. #else
  68. c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc),
  69. [this,fn_=std::forward< Fn >( fn)](boost::context::fiber && c) mutable {
  70. // create synthesized push_coroutine< T >
  71. typename push_coroutine< T >::control_block synthesized_cb{ this, c };
  72. push_coroutine< T > synthesized{ & synthesized_cb };
  73. other = & synthesized_cb;
  74. if ( state_t::none == ( state & state_t::destroy) ) {
  75. try {
  76. auto fn = std::move( fn_);
  77. // call coroutine-fn with synthesized push_coroutine as argument
  78. fn( synthesized);
  79. } catch ( boost::context::detail::forced_unwind const&) {
  80. throw;
  81. #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
  82. } catch ( abi::__forced_unwind const&) {
  83. throw;
  84. #endif
  85. } catch (...) {
  86. // store other exceptions in exception-pointer
  87. except = std::current_exception();
  88. }
  89. }
  90. // set termination flags
  91. state |= state_t::complete;
  92. // jump back
  93. return std::move( other->c).resume();
  94. } },
  95. #endif
  96. other{ nullptr },
  97. state{ state_t::unwind },
  98. except{},
  99. bvalid{ false },
  100. storage{} {
  101. c = std::move( c).resume();
  102. if ( except) {
  103. std::rethrow_exception( except);
  104. }
  105. }
  106. template< typename T >
  107. pull_coroutine< T >::control_block::control_block( typename push_coroutine< T >::control_block * cb,
  108. boost::context::fiber & c_) noexcept :
  109. c{ std::move( c_) },
  110. other{ cb },
  111. state{ state_t::none },
  112. except{},
  113. bvalid{ false },
  114. storage{} {
  115. }
  116. template< typename T >
  117. pull_coroutine< T >::control_block::~control_block() {
  118. // destroy data if set
  119. if ( bvalid) {
  120. reinterpret_cast< T * >( std::addressof( storage) )->~T();
  121. }
  122. }
  123. template< typename T >
  124. void
  125. pull_coroutine< T >::control_block::deallocate() noexcept {
  126. if ( state_t::none != ( state & state_t::unwind) ) {
  127. destroy( this);
  128. }
  129. }
  130. template< typename T >
  131. void
  132. pull_coroutine< T >::control_block::resume() {
  133. c = std::move( c).resume();
  134. if ( except) {
  135. std::rethrow_exception( except);
  136. }
  137. }
  138. template< typename T >
  139. void
  140. pull_coroutine< T >::control_block::set( T const& t) {
  141. // destroy data if set
  142. if ( bvalid) {
  143. reinterpret_cast< T * >( std::addressof( storage) )->~T();
  144. }
  145. ::new ( static_cast< void * >( std::addressof( storage) ) ) T( t);
  146. bvalid = true;
  147. }
  148. template< typename T >
  149. void
  150. pull_coroutine< T >::control_block::set( T && t) {
  151. // destroy data if set
  152. if ( bvalid) {
  153. reinterpret_cast< T * >( std::addressof( storage) )->~T();
  154. }
  155. ::new ( static_cast< void * >( std::addressof( storage) ) ) T( std::move( t) );
  156. bvalid = true;
  157. }
  158. template< typename T >
  159. T &
  160. pull_coroutine< T >::control_block::get() noexcept {
  161. return * reinterpret_cast< T * >( std::addressof( storage) );
  162. }
  163. template< typename T >
  164. bool
  165. pull_coroutine< T >::control_block::valid() const noexcept {
  166. return nullptr != other && state_t::none == ( state & state_t::complete) && bvalid;
  167. }
  168. // pull_coroutine< T & >
  169. template< typename T >
  170. void
  171. pull_coroutine< T & >::control_block::destroy( control_block * cb) noexcept {
  172. boost::context::fiber c = std::move( cb->c);
  173. // destroy control structure
  174. cb->~control_block();
  175. // destroy coroutine's stack
  176. cb->state |= state_t::destroy;
  177. }
  178. template< typename T >
  179. template< typename StackAllocator, typename Fn >
  180. pull_coroutine< T & >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc,
  181. Fn && fn) :
  182. #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS)
  183. c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc),
  184. wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::fiber && c) mutable {
  185. // create synthesized push_coroutine< T & >
  186. typename push_coroutine< T & >::control_block synthesized_cb{ this, c };
  187. push_coroutine< T & > synthesized{ & synthesized_cb };
  188. other = & synthesized_cb;
  189. if ( state_t::none == ( state & state_t::destroy) ) {
  190. try {
  191. auto fn = std::move( fn_);
  192. // call coroutine-fn with synthesized push_coroutine as argument
  193. fn( synthesized);
  194. } catch ( boost::context::detail::forced_unwind const&) {
  195. throw;
  196. #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
  197. } catch ( abi::__forced_unwind const&) {
  198. throw;
  199. #endif
  200. } catch (...) {
  201. // store other exceptions in exception-pointer
  202. except = std::current_exception();
  203. }
  204. }
  205. // set termination flags
  206. state |= state_t::complete;
  207. // jump back
  208. return std::move( other->c).resume();
  209. },
  210. std::forward< Fn >( fn) ) },
  211. #else
  212. c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc),
  213. [this,fn_=std::forward< Fn >( fn)](boost::context::fiber && c) mutable {
  214. // create synthesized push_coroutine< T & >
  215. typename push_coroutine< T & >::control_block synthesized_cb{ this, c };
  216. push_coroutine< T & > synthesized{ & synthesized_cb };
  217. other = & synthesized_cb;
  218. if ( state_t::none == ( state & state_t::destroy) ) {
  219. try {
  220. auto fn = std::move( fn_);
  221. // call coroutine-fn with synthesized push_coroutine as argument
  222. fn( synthesized);
  223. } catch ( boost::context::detail::forced_unwind const&) {
  224. throw;
  225. #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
  226. } catch ( abi::__forced_unwind const&) {
  227. throw;
  228. #endif
  229. } catch (...) {
  230. // store other exceptions in exception-pointer
  231. except = std::current_exception();
  232. }
  233. }
  234. // set termination flags
  235. state |= state_t::complete;
  236. // jump back
  237. return std::move( other->c).resume();
  238. } },
  239. #endif
  240. other{ nullptr },
  241. state{ state_t::unwind },
  242. except{},
  243. bvalid{ false },
  244. storage{} {
  245. c = std::move( c).resume();
  246. if ( except) {
  247. std::rethrow_exception( except);
  248. }
  249. }
  250. template< typename T >
  251. pull_coroutine< T & >::control_block::control_block( typename push_coroutine< T & >::control_block * cb,
  252. boost::context::fiber & c_) noexcept :
  253. c{ std::move( c_) },
  254. other{ cb },
  255. state{ state_t::none },
  256. except{},
  257. bvalid{ false },
  258. storage{} {
  259. }
  260. template< typename T >
  261. void
  262. pull_coroutine< T & >::control_block::deallocate() noexcept {
  263. if ( state_t::none != ( state & state_t::unwind) ) {
  264. destroy( this);
  265. }
  266. }
  267. template< typename T >
  268. void
  269. pull_coroutine< T & >::control_block::resume() {
  270. c = std::move( c).resume();
  271. if ( except) {
  272. std::rethrow_exception( except);
  273. }
  274. }
  275. template< typename T >
  276. void
  277. pull_coroutine< T & >::control_block::set( T & t) {
  278. ::new ( static_cast< void * >( std::addressof( storage) ) ) holder{ t };
  279. bvalid = true;
  280. }
  281. template< typename T >
  282. T &
  283. pull_coroutine< T & >::control_block::get() noexcept {
  284. return reinterpret_cast< holder * >( std::addressof( storage) )->t;
  285. }
  286. template< typename T >
  287. bool
  288. pull_coroutine< T & >::control_block::valid() const noexcept {
  289. return nullptr != other && state_t::none == ( state & state_t::complete) && bvalid;
  290. }
  291. // pull_coroutine< void >
  292. inline
  293. void
  294. pull_coroutine< void >::control_block::destroy( control_block * cb) noexcept {
  295. boost::context::fiber c = std::move( cb->c);
  296. // destroy control structure
  297. cb->~control_block();
  298. // destroy coroutine's stack
  299. cb->state |= state_t::destroy;
  300. }
  301. template< typename StackAllocator, typename Fn >
  302. pull_coroutine< void >::control_block::control_block( context::preallocated palloc, StackAllocator && salloc,
  303. Fn && fn) :
  304. #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS)
  305. c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc),
  306. wrap( [this](typename std::decay< Fn >::type & fn_,boost::context::fiber && c) mutable {
  307. // create synthesized push_coroutine< void >
  308. typename push_coroutine< void >::control_block synthesized_cb{ this, c };
  309. push_coroutine< void > synthesized{ & synthesized_cb };
  310. other = & synthesized_cb;
  311. if ( state_t::none == ( state & state_t::destroy) ) {
  312. try {
  313. auto fn = std::move( fn_);
  314. // call coroutine-fn with synthesized push_coroutine as argument
  315. fn( synthesized);
  316. } catch ( boost::context::detail::forced_unwind const&) {
  317. throw;
  318. #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
  319. } catch ( abi::__forced_unwind const&) {
  320. throw;
  321. #endif
  322. } catch (...) {
  323. // store other exceptions in exception-pointer
  324. except = std::current_exception();
  325. }
  326. }
  327. // set termination flags
  328. state |= state_t::complete;
  329. // jump back
  330. return std::move( other->c).resume();
  331. },
  332. std::forward< Fn >( fn) ) },
  333. #else
  334. c{ std::allocator_arg, palloc, std::forward< StackAllocator >( salloc),
  335. [this,fn_=std::forward< Fn >( fn)]( boost::context::fiber && c) mutable {
  336. // create synthesized push_coroutine< void >
  337. typename push_coroutine< void >::control_block synthesized_cb{ this, c };
  338. push_coroutine< void > synthesized{ & synthesized_cb };
  339. other = & synthesized_cb;
  340. if ( state_t::none == ( state & state_t::destroy) ) {
  341. try {
  342. auto fn = std::move( fn_);
  343. // call coroutine-fn with synthesized push_coroutine as argument
  344. fn( synthesized);
  345. } catch ( boost::context::detail::forced_unwind const&) {
  346. throw;
  347. #if defined( BOOST_CONTEXT_HAS_CXXABI_H )
  348. } catch ( abi::__forced_unwind const&) {
  349. throw;
  350. #endif
  351. } catch (...) {
  352. // store other exceptions in exception-pointer
  353. except = std::current_exception();
  354. }
  355. }
  356. // set termination flags
  357. state |= state_t::complete;
  358. // jump back to ctx
  359. return std::move( other->c).resume();
  360. } },
  361. #endif
  362. other{ nullptr },
  363. state{ state_t::unwind },
  364. except{} {
  365. c = std::move( c).resume();
  366. if ( except) {
  367. std::rethrow_exception( except);
  368. }
  369. }
  370. inline
  371. pull_coroutine< void >::control_block::control_block( push_coroutine< void >::control_block * cb,
  372. boost::context::fiber & c_) noexcept :
  373. c{ std::move( c_) },
  374. other{ cb },
  375. state{ state_t::none },
  376. except{} {
  377. }
  378. inline
  379. void
  380. pull_coroutine< void >::control_block::deallocate() noexcept {
  381. if ( state_t::none != ( state & state_t::unwind) ) {
  382. destroy( this);
  383. }
  384. }
  385. inline
  386. void
  387. pull_coroutine< void >::control_block::resume() {
  388. c = std::move( c).resume();
  389. if ( except) {
  390. std::rethrow_exception( except);
  391. }
  392. }
  393. inline
  394. bool
  395. pull_coroutine< void >::control_block::valid() const noexcept {
  396. return nullptr != other && state_t::none == ( state & state_t::complete);
  397. }
  398. }}}
  399. #ifdef BOOST_HAS_ABI_HEADERS
  400. # include BOOST_ABI_SUFFIX
  401. #endif
  402. #endif // BOOST_COROUTINES2_DETAIL_PULL_CONTROL_BLOCK_IPP