context.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Copyright Oliver Kowalke 2013.
  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_FIBERS_CONTEXT_H
  6. #define BOOST_FIBERS_CONTEXT_H
  7. #include <atomic>
  8. #include <chrono>
  9. #include <cstdint>
  10. #include <exception>
  11. #include <functional>
  12. #include <iostream>
  13. #include <map>
  14. #include <memory>
  15. #include <tuple>
  16. #include <type_traits>
  17. #include <boost/assert.hpp>
  18. #include <boost/config.hpp>
  19. #if defined(BOOST_NO_CXX17_STD_APPLY)
  20. #include <boost/context/detail/apply.hpp>
  21. #endif
  22. #include <boost/context/fiber.hpp>
  23. #include <boost/context/stack_context.hpp>
  24. #include <boost/intrusive/list.hpp>
  25. #include <boost/intrusive/parent_from_member.hpp>
  26. #include <boost/intrusive_ptr.hpp>
  27. #include <boost/intrusive/set.hpp>
  28. #include <boost/intrusive/slist.hpp>
  29. #include <boost/fiber/detail/config.hpp>
  30. #include <boost/fiber/detail/data.hpp>
  31. #include <boost/fiber/detail/decay_copy.hpp>
  32. #include <boost/fiber/detail/fss.hpp>
  33. #include <boost/fiber/detail/spinlock.hpp>
  34. #include <boost/fiber/exceptions.hpp>
  35. #include <boost/fiber/fixedsize_stack.hpp>
  36. #include <boost/fiber/policy.hpp>
  37. #include <boost/fiber/properties.hpp>
  38. #include <boost/fiber/segmented_stack.hpp>
  39. #include <boost/fiber/type.hpp>
  40. #ifdef BOOST_HAS_ABI_HEADERS
  41. # include BOOST_ABI_PREFIX
  42. #endif
  43. #ifdef _MSC_VER
  44. # pragma warning(push)
  45. # pragma warning(disable:4251)
  46. #endif
  47. namespace boost {
  48. namespace fibers {
  49. class context;
  50. class fiber;
  51. class scheduler;
  52. namespace detail {
  53. struct wait_tag;
  54. typedef intrusive::list_member_hook<
  55. intrusive::tag< wait_tag >,
  56. intrusive::link_mode<
  57. intrusive::auto_unlink
  58. >
  59. > wait_hook;
  60. // declaration of the functor that converts between
  61. // the context class and the wait-hook
  62. struct wait_functor {
  63. // required types
  64. typedef wait_hook hook_type;
  65. typedef hook_type * hook_ptr;
  66. typedef const hook_type * const_hook_ptr;
  67. typedef context value_type;
  68. typedef value_type * pointer;
  69. typedef const value_type * const_pointer;
  70. // required static functions
  71. static hook_ptr to_hook_ptr( value_type &value);
  72. static const_hook_ptr to_hook_ptr( value_type const& value);
  73. static pointer to_value_ptr( hook_ptr n);
  74. static const_pointer to_value_ptr( const_hook_ptr n);
  75. };
  76. struct ready_tag;
  77. typedef intrusive::list_member_hook<
  78. intrusive::tag< ready_tag >,
  79. intrusive::link_mode<
  80. intrusive::auto_unlink
  81. >
  82. > ready_hook;
  83. struct sleep_tag;
  84. typedef intrusive::set_member_hook<
  85. intrusive::tag< sleep_tag >,
  86. intrusive::link_mode<
  87. intrusive::auto_unlink
  88. >
  89. > sleep_hook;
  90. struct worker_tag;
  91. typedef intrusive::list_member_hook<
  92. intrusive::tag< worker_tag >,
  93. intrusive::link_mode<
  94. intrusive::auto_unlink
  95. >
  96. > worker_hook;
  97. struct terminated_tag;
  98. typedef intrusive::slist_member_hook<
  99. intrusive::tag< terminated_tag >,
  100. intrusive::link_mode<
  101. intrusive::safe_link
  102. >
  103. > terminated_hook;
  104. struct remote_ready_tag;
  105. typedef intrusive::slist_member_hook<
  106. intrusive::tag< remote_ready_tag >,
  107. intrusive::link_mode<
  108. intrusive::safe_link
  109. >
  110. > remote_ready_hook;
  111. }
  112. class BOOST_FIBERS_DECL context {
  113. public:
  114. typedef intrusive::list<
  115. context,
  116. intrusive::function_hook< detail::wait_functor >,
  117. intrusive::constant_time_size< false >
  118. > wait_queue_t;
  119. private:
  120. friend class dispatcher_context;
  121. friend class main_context;
  122. template< typename Fn, typename ... Arg > friend class worker_context;
  123. friend class scheduler;
  124. struct fss_data {
  125. void * vp{ nullptr };
  126. detail::fss_cleanup_function::ptr_t cleanup_function{};
  127. fss_data() noexcept {
  128. }
  129. fss_data( void * vp_,
  130. detail::fss_cleanup_function::ptr_t const& fn) noexcept :
  131. vp( vp_),
  132. cleanup_function( fn) {
  133. BOOST_ASSERT( cleanup_function);
  134. }
  135. void do_cleanup() {
  136. ( * cleanup_function)( vp);
  137. }
  138. };
  139. typedef std::map< uintptr_t, fss_data > fss_data_t;
  140. #if ! defined(BOOST_FIBERS_NO_ATOMICS)
  141. std::atomic< std::size_t > use_count_;
  142. #else
  143. std::size_t use_count_;
  144. #endif
  145. #if ! defined(BOOST_FIBERS_NO_ATOMICS)
  146. detail::remote_ready_hook remote_ready_hook_{};
  147. #endif
  148. detail::spinlock splk_{};
  149. bool terminated_{ false };
  150. wait_queue_t wait_queue_{};
  151. public:
  152. detail::wait_hook wait_hook_{};
  153. #if ! defined(BOOST_FIBERS_NO_ATOMICS)
  154. std::atomic< std::intptr_t > twstatus{ 0 };
  155. #endif
  156. private:
  157. scheduler * scheduler_{ nullptr };
  158. fss_data_t fss_data_{};
  159. detail::sleep_hook sleep_hook_{};
  160. detail::ready_hook ready_hook_{};
  161. detail::terminated_hook terminated_hook_{};
  162. detail::worker_hook worker_hook_{};
  163. fiber_properties * properties_{ nullptr };
  164. boost::context::fiber c_{};
  165. std::chrono::steady_clock::time_point tp_;
  166. type type_;
  167. launch policy_;
  168. context( std::size_t initial_count, type t, launch policy) noexcept :
  169. use_count_{ initial_count },
  170. tp_{ (std::chrono::steady_clock::time_point::max)() },
  171. type_{ t },
  172. policy_{ policy } {
  173. }
  174. public:
  175. class id {
  176. private:
  177. context * impl_{ nullptr };
  178. public:
  179. id() = default;
  180. explicit id( context * impl) noexcept :
  181. impl_{ impl } {
  182. }
  183. bool operator==( id const& other) const noexcept {
  184. return impl_ == other.impl_;
  185. }
  186. bool operator!=( id const& other) const noexcept {
  187. return impl_ != other.impl_;
  188. }
  189. bool operator<( id const& other) const noexcept {
  190. return impl_ < other.impl_;
  191. }
  192. bool operator>( id const& other) const noexcept {
  193. return other.impl_ < impl_;
  194. }
  195. bool operator<=( id const& other) const noexcept {
  196. return ! ( * this > other);
  197. }
  198. bool operator>=( id const& other) const noexcept {
  199. return ! ( * this < other);
  200. }
  201. template< typename charT, class traitsT >
  202. friend std::basic_ostream< charT, traitsT > &
  203. operator<<( std::basic_ostream< charT, traitsT > & os, id const& other) {
  204. if ( nullptr != other.impl_) {
  205. return os << other.impl_;
  206. } else {
  207. return os << "{not-valid}";
  208. }
  209. }
  210. explicit operator bool() const noexcept {
  211. return nullptr != impl_;
  212. }
  213. bool operator!() const noexcept {
  214. return nullptr == impl_;
  215. }
  216. };
  217. static context * active() noexcept;
  218. static void reset_active() noexcept;
  219. context( context const&) = delete;
  220. context( context &&) = delete;
  221. context & operator=( context const&) = delete;
  222. context & operator=( context &&) = delete;
  223. friend bool
  224. operator==( context const& lhs, context const& rhs) noexcept {
  225. return & lhs == & rhs;
  226. }
  227. virtual ~context();
  228. scheduler * get_scheduler() const noexcept {
  229. return scheduler_;
  230. }
  231. id get_id() const noexcept;
  232. bool is_resumable() const noexcept {
  233. if ( c_) return true;
  234. else return false;
  235. }
  236. void resume() noexcept;
  237. void resume( detail::spinlock_lock &) noexcept;
  238. void resume( context *) noexcept;
  239. void suspend() noexcept;
  240. void suspend( detail::spinlock_lock &) noexcept;
  241. boost::context::fiber suspend_with_cc() noexcept;
  242. boost::context::fiber terminate() noexcept;
  243. void join();
  244. void yield() noexcept;
  245. bool wait_until( std::chrono::steady_clock::time_point const&) noexcept;
  246. bool wait_until( std::chrono::steady_clock::time_point const&,
  247. detail::spinlock_lock &) noexcept;
  248. void schedule( context *) noexcept;
  249. bool is_context( type t) const noexcept {
  250. return type::none != ( type_ & t);
  251. }
  252. void * get_fss_data( void const * vp) const;
  253. void set_fss_data(
  254. void const * vp,
  255. detail::fss_cleanup_function::ptr_t const& cleanup_fn,
  256. void * data,
  257. bool cleanup_existing);
  258. void set_properties( fiber_properties * props) noexcept;
  259. fiber_properties * get_properties() const noexcept {
  260. return properties_;
  261. }
  262. launch get_policy() const noexcept {
  263. return policy_;
  264. }
  265. bool worker_is_linked() const noexcept;
  266. bool ready_is_linked() const noexcept;
  267. bool remote_ready_is_linked() const noexcept;
  268. bool sleep_is_linked() const noexcept;
  269. bool terminated_is_linked() const noexcept;
  270. bool wait_is_linked() const noexcept;
  271. template< typename List >
  272. void worker_link( List & lst) noexcept {
  273. static_assert( std::is_same< typename List::value_traits::hook_type, detail::worker_hook >::value, "not a worker-queue");
  274. BOOST_ASSERT( ! worker_is_linked() );
  275. lst.push_back( * this);
  276. }
  277. template< typename List >
  278. void ready_link( List & lst) noexcept {
  279. static_assert( std::is_same< typename List::value_traits::hook_type, detail::ready_hook >::value, "not a ready-queue");
  280. BOOST_ASSERT( ! ready_is_linked() );
  281. lst.push_back( * this);
  282. }
  283. template< typename List >
  284. void remote_ready_link( List & lst) noexcept {
  285. static_assert( std::is_same< typename List::value_traits::hook_type, detail::remote_ready_hook >::value, "not a remote-ready-queue");
  286. BOOST_ASSERT( ! remote_ready_is_linked() );
  287. lst.push_back( * this);
  288. }
  289. template< typename Set >
  290. void sleep_link( Set & set) noexcept {
  291. static_assert( std::is_same< typename Set::value_traits::hook_type,detail::sleep_hook >::value, "not a sleep-queue");
  292. BOOST_ASSERT( ! sleep_is_linked() );
  293. set.insert( * this);
  294. }
  295. template< typename List >
  296. void terminated_link( List & lst) noexcept {
  297. static_assert( std::is_same< typename List::value_traits::hook_type, detail::terminated_hook >::value, "not a terminated-queue");
  298. BOOST_ASSERT( ! terminated_is_linked() );
  299. lst.push_back( * this);
  300. }
  301. template< typename List >
  302. void wait_link( List & lst) noexcept {
  303. static_assert( std::is_same< typename List::value_traits::hook_type, detail::wait_hook >::value, "not a wait-queue");
  304. BOOST_ASSERT( ! wait_is_linked() );
  305. lst.push_back( * this);
  306. }
  307. void worker_unlink() noexcept;
  308. void ready_unlink() noexcept;
  309. void sleep_unlink() noexcept;
  310. void wait_unlink() noexcept;
  311. void detach() noexcept;
  312. void attach( context *) noexcept;
  313. friend void intrusive_ptr_add_ref( context * ctx) noexcept {
  314. BOOST_ASSERT( nullptr != ctx);
  315. ctx->use_count_.fetch_add( 1, std::memory_order_relaxed);
  316. }
  317. friend void intrusive_ptr_release( context * ctx) noexcept {
  318. BOOST_ASSERT( nullptr != ctx);
  319. if ( 1 == ctx->use_count_.fetch_sub( 1, std::memory_order_release) ) {
  320. std::atomic_thread_fence( std::memory_order_acquire);
  321. boost::context::fiber c = std::move( ctx->c_);
  322. // destruct context
  323. ctx->~context();
  324. // deallocated stack
  325. std::move( c).resume();
  326. }
  327. }
  328. };
  329. inline
  330. bool operator<( context const& l, context const& r) noexcept {
  331. return l.get_id() < r.get_id();
  332. }
  333. template< typename Fn, typename ... Arg >
  334. class worker_context final : public context {
  335. private:
  336. typename std::decay< Fn >::type fn_;
  337. std::tuple< Arg ... > arg_;
  338. boost::context::fiber
  339. run_( boost::context::fiber && c) {
  340. {
  341. // fn and tpl must be destroyed before calling terminate()
  342. auto fn = std::move( fn_);
  343. auto arg = std::move( arg_);
  344. #if (defined(BOOST_USE_UCONTEXT)||defined(BOOST_USE_WINFIB))
  345. std::move( c).resume();
  346. #endif
  347. #if defined(BOOST_NO_CXX17_STD_APPLY)
  348. boost::context::detail::apply( std::move( fn), std::move( arg) );
  349. #else
  350. std::apply( std::move( fn), std::move( arg) );
  351. #endif
  352. }
  353. // terminate context
  354. return terminate();
  355. }
  356. public:
  357. template< typename StackAlloc >
  358. worker_context( launch policy,
  359. boost::context::preallocated const& palloc, StackAlloc && salloc,
  360. Fn && fn, Arg ... arg) :
  361. context{ 1, type::worker_context, policy },
  362. fn_( std::forward< Fn >( fn) ),
  363. arg_( std::forward< Arg >( arg) ... ) {
  364. c_ = boost::context::fiber{ std::allocator_arg, palloc, std::forward< StackAlloc >( salloc),
  365. std::bind( & worker_context::run_, this, std::placeholders::_1) };
  366. #if (defined(BOOST_USE_UCONTEXT)||defined(BOOST_USE_WINFIB))
  367. c_ = std::move( c_).resume();
  368. #endif
  369. }
  370. };
  371. template< typename StackAlloc, typename Fn, typename ... Arg >
  372. static intrusive_ptr< context > make_worker_context( launch policy,
  373. StackAlloc && salloc,
  374. Fn && fn, Arg ... arg) {
  375. typedef worker_context< Fn, Arg ... > context_t;
  376. auto sctx = salloc.allocate();
  377. // reserve space for control structure
  378. void * storage = reinterpret_cast< void * >(
  379. ( reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sizeof( context_t) ) )
  380. & ~ static_cast< uintptr_t >( 0xff) );
  381. void * stack_bottom = reinterpret_cast< void * >(
  382. reinterpret_cast< uintptr_t >( sctx.sp) - static_cast< uintptr_t >( sctx.size) );
  383. const std::size_t size = reinterpret_cast< uintptr_t >( storage) - reinterpret_cast< uintptr_t >( stack_bottom);
  384. // placement new of context on top of fiber's stack
  385. return intrusive_ptr< context >{
  386. new ( storage) context_t{
  387. policy,
  388. boost::context::preallocated{ storage, size, sctx },
  389. std::forward< StackAlloc >( salloc),
  390. std::forward< Fn >( fn),
  391. std::forward< Arg >( arg) ... } };
  392. }
  393. namespace detail {
  394. inline
  395. wait_functor::hook_ptr wait_functor::to_hook_ptr( wait_functor::value_type & value) {
  396. return & value.wait_hook_;
  397. }
  398. inline
  399. wait_functor::const_hook_ptr wait_functor::to_hook_ptr( wait_functor::value_type const& value) {
  400. return & value.wait_hook_;
  401. }
  402. inline
  403. wait_functor::pointer wait_functor::to_value_ptr( wait_functor::hook_ptr n) {
  404. return intrusive::get_parent_from_member< context >( n, & context::wait_hook_);
  405. }
  406. inline
  407. wait_functor::const_pointer wait_functor::to_value_ptr( wait_functor::const_hook_ptr n) {
  408. return intrusive::get_parent_from_member< context >( n, & context::wait_hook_);
  409. }
  410. }}}
  411. #ifdef _MSC_VER
  412. # pragma warning(pop)
  413. #endif
  414. #ifdef BOOST_HAS_ABI_HEADERS
  415. # include BOOST_ABI_SUFFIX
  416. #endif
  417. #endif // BOOST_FIBERS_CONTEXT_H