execution_context_v1.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 <atomic>
  10. #include <cstddef>
  11. #include <cstdint>
  12. #include <cstdlib>
  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. #include <boost/context/detail/apply.hpp>
  22. #include <boost/context/detail/disable_overload.hpp>
  23. #include <boost/context/detail/fcontext.hpp>
  24. #include <boost/context/fixedsize_stack.hpp>
  25. #include <boost/context/flags.hpp>
  26. #include <boost/context/preallocated.hpp>
  27. #include <boost/context/segmented_stack.hpp>
  28. #include <boost/context/stack_context.hpp>
  29. #ifdef BOOST_HAS_ABI_HEADERS
  30. # include BOOST_ABI_PREFIX
  31. #endif
  32. #if defined(BOOST_USE_SEGMENTED_STACKS)
  33. extern "C" {
  34. void __splitstack_getcontext( void * [BOOST_CONTEXT_SEGMENTS]);
  35. void __splitstack_setcontext( void * [BOOST_CONTEXT_SEGMENTS]);
  36. }
  37. #endif
  38. namespace boost {
  39. namespace context {
  40. namespace detail {
  41. template< typename Fn >
  42. transfer_t context_ontop( transfer_t);
  43. struct activation_record;
  44. struct data_t {
  45. activation_record * from;
  46. void * data;
  47. };
  48. struct activation_record {
  49. typedef boost::intrusive_ptr< activation_record > ptr_t;
  50. thread_local static ptr_t current_rec;
  51. std::atomic< std::size_t > use_count{ 0 };
  52. fcontext_t fctx{ nullptr };
  53. stack_context sctx{};
  54. bool main_ctx{ true };
  55. // used for toplevel-context
  56. // (e.g. main context, thread-entry context)
  57. constexpr activation_record() = default;
  58. activation_record( fcontext_t fctx_, stack_context sctx_) noexcept :
  59. fctx{ fctx_ },
  60. sctx( sctx_ ), // sctx{ sctx_ } - clang-3.6: no viable conversion from 'boost::context::stack_context' to 'std::size_t'
  61. main_ctx{ false } {
  62. }
  63. virtual ~activation_record() = default;
  64. bool is_main_context() const noexcept {
  65. return main_ctx;
  66. }
  67. void * resume( void * vp) {
  68. // store current activation record in local variable
  69. auto from = current_rec.get();
  70. // store `this` in static, thread local pointer
  71. // `this` will become the active (running) context
  72. // returned by execution_context::current()
  73. current_rec = this;
  74. #if defined(BOOST_USE_SEGMENTED_STACKS)
  75. // adjust segmented stack properties
  76. __splitstack_getcontext( from->sctx.segments_ctx);
  77. __splitstack_setcontext( sctx.segments_ctx);
  78. #endif
  79. data_t d = { from, vp };
  80. // context switch from parent context to `this`-context
  81. transfer_t t = jump_fcontext( fctx, & d);
  82. data_t * dp = reinterpret_cast< data_t * >( t.data);
  83. dp->from->fctx = t.fctx;
  84. // parent context resumed
  85. return dp->data;
  86. }
  87. template< typename Fn >
  88. void * resume_ontop( void * data, Fn && fn) {
  89. // store current activation record in local variable
  90. activation_record * from = current_rec.get();
  91. // store `this` in static, thread local pointer
  92. // `this` will become the active (running) context
  93. // returned by execution_context::current()
  94. current_rec = this;
  95. #if defined(BOOST_USE_SEGMENTED_STACKS)
  96. // adjust segmented stack properties
  97. __splitstack_getcontext( from->sctx.segments_ctx);
  98. __splitstack_setcontext( sctx.segments_ctx);
  99. #endif
  100. std::tuple< void *, Fn > p = std::forward_as_tuple( data, fn);
  101. data_t d = { from, & p };
  102. // context switch from parent context to `this`-context
  103. // execute Fn( Tpl) on top of `this`
  104. transfer_t t = ontop_fcontext( fctx, & d, context_ontop< Fn >);
  105. data_t * dp = reinterpret_cast< data_t * >( t.data);
  106. dp->from->fctx = t.fctx;
  107. // parent context resumed
  108. return dp->data;
  109. }
  110. virtual void deallocate() noexcept {
  111. }
  112. friend void intrusive_ptr_add_ref( activation_record * ar) noexcept {
  113. ++ar->use_count;
  114. }
  115. friend void intrusive_ptr_release( activation_record * ar) noexcept {
  116. BOOST_ASSERT( nullptr != ar);
  117. if ( 0 == --ar->use_count) {
  118. ar->deallocate();
  119. }
  120. }
  121. };
  122. struct activation_record_initializer {
  123. activation_record_initializer() noexcept;
  124. ~activation_record_initializer();
  125. };
  126. template< typename Fn >
  127. transfer_t context_ontop( transfer_t t) {
  128. data_t * dp = reinterpret_cast< data_t * >( t.data);
  129. dp->from->fctx = t.fctx;
  130. auto tpl = reinterpret_cast< std::tuple< void *, Fn > * >( dp->data);
  131. BOOST_ASSERT( nullptr != tpl);
  132. auto data = std::get< 0 >( * tpl);
  133. typename std::decay< Fn >::type fn = std::forward< Fn >( std::get< 1 >( * tpl) );
  134. dp->data = apply( fn, std::tie( data) );
  135. return { t.fctx, dp };
  136. }
  137. template< typename StackAlloc, typename Fn, typename ... Args >
  138. class capture_record : public activation_record {
  139. private:
  140. StackAlloc salloc_;
  141. typename std::decay< Fn >::type fn_;
  142. std::tuple< typename std::decay< Args >::type ... > args_;
  143. activation_record * caller_;
  144. static void destroy( capture_record * p) noexcept {
  145. StackAlloc salloc = p->salloc_;
  146. stack_context sctx = p->sctx;
  147. // deallocate activation record
  148. p->~capture_record();
  149. // destroy stack with stack allocator
  150. salloc.deallocate( sctx);
  151. }
  152. public:
  153. capture_record( stack_context sctx, StackAlloc const& salloc,
  154. fcontext_t fctx,
  155. activation_record * caller,
  156. Fn && fn, Args && ... args) noexcept :
  157. activation_record{ fctx, sctx },
  158. salloc_{ salloc },
  159. fn_( std::forward< Fn >( fn) ),
  160. args_( std::forward< Args >( args) ... ),
  161. caller_{ caller } {
  162. }
  163. void deallocate() noexcept override final {
  164. destroy( this);
  165. }
  166. void run() {
  167. auto data = caller_->resume( nullptr);
  168. apply( fn_, std::tuple_cat( args_, std::tie( data) ) );
  169. BOOST_ASSERT_MSG( ! main_ctx, "main-context does not execute activation-record::run()");
  170. }
  171. };
  172. }
  173. class BOOST_CONTEXT_DECL execution_context {
  174. private:
  175. // tampoline function
  176. // entered if the execution context
  177. // is resumed for the first time
  178. template< typename AR >
  179. static void entry_func( detail::transfer_t t) noexcept {
  180. detail::data_t * dp = reinterpret_cast< detail::data_t * >( t.data);
  181. AR * ar = static_cast< AR * >( dp->data);
  182. BOOST_ASSERT( nullptr != ar);
  183. dp->from->fctx = t.fctx;
  184. // start execution of toplevel context-function
  185. ar->run();
  186. }
  187. typedef boost::intrusive_ptr< detail::activation_record > ptr_t;
  188. ptr_t ptr_;
  189. template< typename StackAlloc, typename Fn, typename ... Args >
  190. static detail::activation_record * create_context( StackAlloc salloc,
  191. Fn && fn, Args && ... args) {
  192. typedef detail::capture_record<
  193. StackAlloc, Fn, Args ...
  194. > capture_t;
  195. auto sctx = salloc.allocate();
  196. // reserve space for control structure
  197. #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
  198. const std::size_t size = sctx.size - sizeof( capture_t);
  199. void * sp = static_cast< char * >( sctx.sp) - sizeof( capture_t);
  200. #else
  201. constexpr std::size_t func_alignment = 64; // alignof( capture_t);
  202. constexpr std::size_t func_size = sizeof( capture_t);
  203. // reserve space on stack
  204. void * sp = static_cast< char * >( sctx.sp) - func_size - func_alignment;
  205. // align sp pointer
  206. std::size_t space = func_size + func_alignment;
  207. sp = std::align( func_alignment, func_size, sp, space);
  208. BOOST_ASSERT( nullptr != sp);
  209. // calculate remaining size
  210. const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) );
  211. #endif
  212. // create fast-context
  213. const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
  214. BOOST_ASSERT( nullptr != fctx);
  215. // get current activation record
  216. auto curr = execution_context::current().ptr_;
  217. // placment new for control structure on fast-context stack
  218. return ::new ( sp) capture_t{
  219. sctx, salloc, fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
  220. }
  221. template< typename StackAlloc, typename Fn, typename ... Args >
  222. static detail::activation_record * create_context( preallocated palloc, StackAlloc salloc,
  223. Fn && fn, Args && ... args) {
  224. typedef detail::capture_record<
  225. StackAlloc, Fn, Args ...
  226. > capture_t;
  227. // reserve space for control structure
  228. #if defined(BOOST_NO_CXX11_CONSTEXPR) || defined(BOOST_NO_CXX11_STD_ALIGN)
  229. const std::size_t size = palloc.size - sizeof( capture_t);
  230. void * sp = static_cast< char * >( palloc.sp) - sizeof( capture_t);
  231. #else
  232. constexpr std::size_t func_alignment = 64; // alignof( capture_t);
  233. constexpr std::size_t func_size = sizeof( capture_t);
  234. // reserve space on stack
  235. void * sp = static_cast< char * >( palloc.sp) - func_size - func_alignment;
  236. // align sp pointer
  237. std::size_t space = func_size + func_alignment;
  238. sp = std::align( func_alignment, func_size, sp, space);
  239. BOOST_ASSERT( nullptr != sp);
  240. // calculate remaining size
  241. const std::size_t size = palloc.size - ( static_cast< char * >( palloc.sp) - static_cast< char * >( sp) );
  242. #endif
  243. // create fast-context
  244. const detail::fcontext_t fctx = detail::make_fcontext( sp, size, & execution_context::entry_func< capture_t >);
  245. BOOST_ASSERT( nullptr != fctx);
  246. // get current activation record
  247. auto curr = execution_context::current().ptr_;
  248. // placment new for control structure on fast-context stack
  249. return ::new ( sp) capture_t{
  250. palloc.sctx, salloc, fctx, curr.get(), std::forward< Fn >( fn), std::forward< Args >( args) ... };
  251. }
  252. execution_context() noexcept :
  253. // default constructed with current activation_record
  254. ptr_{ detail::activation_record::current_rec } {
  255. }
  256. public:
  257. static execution_context current() noexcept;
  258. #if defined(BOOST_USE_SEGMENTED_STACKS)
  259. template< typename Fn,
  260. typename ... Args,
  261. typename = detail::disable_overload< execution_context, Fn >
  262. >
  263. execution_context( Fn && fn, Args && ... args) :
  264. // deferred execution of fn and its arguments
  265. // arguments are stored in std::tuple<>
  266. // non-type template parameter pack via std::index_sequence_for<>
  267. // preserves the number of arguments
  268. // used to extract the function arguments from std::tuple<>
  269. ptr_{ create_context( segmented_stack(),
  270. std::forward< Fn >( fn),
  271. std::forward< Args >( args) ...) } {
  272. ptr_->resume( ptr_.get() );
  273. }
  274. template< typename Fn,
  275. typename ... Args
  276. >
  277. execution_context( std::allocator_arg_t, segmented_stack salloc, Fn && fn, Args && ... args) :
  278. // deferred execution of fn and its arguments
  279. // arguments are stored in std::tuple<>
  280. // non-type template parameter pack via std::index_sequence_for<>
  281. // preserves the number of arguments
  282. // used to extract the function arguments from std::tuple<>
  283. ptr_{ create_context( salloc,
  284. std::forward< Fn >( fn),
  285. std::forward< Args >( args) ...) } {
  286. ptr_->resume( ptr_.get() );
  287. }
  288. template< typename Fn,
  289. typename ... Args
  290. >
  291. execution_context( std::allocator_arg_t, preallocated palloc, segmented_stack salloc, Fn && fn, Args && ... args) :
  292. // deferred execution of fn and its arguments
  293. // arguments are stored in std::tuple<>
  294. // non-type template parameter pack via std::index_sequence_for<>
  295. // preserves the number of arguments
  296. // used to extract the function arguments from std::tuple<>
  297. ptr_{ create_context( palloc, salloc,
  298. std::forward< Fn >( fn),
  299. std::forward< Args >( args) ...) } {
  300. ptr_->resume( ptr_.get() );
  301. }
  302. #else
  303. template< typename Fn,
  304. typename ... Args,
  305. typename = detail::disable_overload< execution_context, Fn >
  306. >
  307. execution_context( Fn && fn, Args && ... args) :
  308. // deferred execution of fn and its arguments
  309. // arguments are stored in std::tuple<>
  310. // non-type template parameter pack via std::index_sequence_for<>
  311. // preserves the number of arguments
  312. // used to extract the function arguments from std::tuple<>
  313. ptr_{ create_context( fixedsize_stack(),
  314. std::forward< Fn >( fn),
  315. std::forward< Args >( args) ...) } {
  316. ptr_->resume( ptr_.get() );
  317. }
  318. template< typename StackAlloc,
  319. typename Fn,
  320. typename ... Args
  321. >
  322. execution_context( std::allocator_arg_t, StackAlloc salloc, Fn && fn, Args && ... args) :
  323. // deferred execution of fn and its arguments
  324. // arguments are stored in std::tuple<>
  325. // non-type template parameter pack via std::index_sequence_for<>
  326. // preserves the number of arguments
  327. // used to extract the function arguments from std::tuple<>
  328. ptr_{ create_context( salloc,
  329. std::forward< Fn >( fn),
  330. std::forward< Args >( args) ...) } {
  331. ptr_->resume( ptr_.get() );
  332. }
  333. template< typename StackAlloc,
  334. typename Fn,
  335. typename ... Args
  336. >
  337. execution_context( std::allocator_arg_t, preallocated palloc, StackAlloc salloc, Fn && fn, Args && ... args) :
  338. // deferred execution of fn and its arguments
  339. // arguments are stored in std::tuple<>
  340. // non-type template parameter pack via std::index_sequence_for<>
  341. // preserves the number of arguments
  342. // used to extract the function arguments from std::tuple<>
  343. ptr_{ create_context( palloc, salloc,
  344. std::forward< Fn >( fn),
  345. std::forward< Args >( args) ...) } {
  346. ptr_->resume( ptr_.get() );
  347. }
  348. #endif
  349. execution_context( execution_context const& other) noexcept :
  350. ptr_{ other.ptr_ } {
  351. }
  352. execution_context( execution_context && other) noexcept :
  353. ptr_{ other.ptr_ } {
  354. other.ptr_.reset();
  355. }
  356. execution_context & operator=( execution_context const& other) noexcept {
  357. // intrusive_ptr<> does not test for self-assignment
  358. if ( this == & other) return * this;
  359. ptr_ = other.ptr_;
  360. return * this;
  361. }
  362. execution_context & operator=( execution_context && other) noexcept {
  363. if ( this == & other) return * this;
  364. execution_context tmp{ std::move( other) };
  365. swap( tmp);
  366. return * this;
  367. }
  368. void * operator()( void * vp = nullptr) {
  369. return ptr_->resume( vp);
  370. }
  371. template< typename Fn >
  372. void * operator()( exec_ontop_arg_t, Fn && fn, void * vp = nullptr) {
  373. return ptr_->resume_ontop( vp,
  374. std::forward< Fn >( fn) );
  375. }
  376. explicit operator bool() const noexcept {
  377. return nullptr != ptr_.get();
  378. }
  379. bool operator!() const noexcept {
  380. return nullptr == ptr_.get();
  381. }
  382. bool operator==( execution_context const& other) const noexcept {
  383. return ptr_ == other.ptr_;
  384. }
  385. bool operator!=( execution_context const& other) const noexcept {
  386. return ptr_ != other.ptr_;
  387. }
  388. bool operator<( execution_context const& other) const noexcept {
  389. return ptr_ < other.ptr_;
  390. }
  391. bool operator>( execution_context const& other) const noexcept {
  392. return other.ptr_ < ptr_;
  393. }
  394. bool operator<=( execution_context const& other) const noexcept {
  395. return ! ( * this > other);
  396. }
  397. bool operator>=( execution_context const& other) const noexcept {
  398. return ! ( * this < other);
  399. }
  400. template< typename charT, class traitsT >
  401. friend std::basic_ostream< charT, traitsT > &
  402. operator<<( std::basic_ostream< charT, traitsT > & os, execution_context const& other) {
  403. if ( nullptr != other.ptr_) {
  404. return os << other.ptr_;
  405. } else {
  406. return os << "{not-a-context}";
  407. }
  408. }
  409. void swap( execution_context & other) noexcept {
  410. ptr_.swap( other.ptr_);
  411. }
  412. };
  413. inline
  414. void swap( execution_context & l, execution_context & r) noexcept {
  415. l.swap( r);
  416. }
  417. }}
  418. #ifdef BOOST_HAS_ABI_HEADERS
  419. # include BOOST_ABI_SUFFIX
  420. #endif
  421. #endif // BOOST_CONTEXT_EXECUTION_CONTEXT_H