context.hpp 15 KB

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