push_control_block_cc.ipp 14 KB

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