2 // Copyright Oliver Kowalke 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_FIBERS_FIBER_H
8 #define BOOST_FIBERS_FIBER_H
15 #include <boost/assert.hpp>
16 #include <boost/config.hpp>
17 #include <boost/intrusive_ptr.hpp>
19 #include <boost/fiber/detail/config.hpp>
20 #include <boost/fiber/detail/disable_overload.hpp>
21 #include <boost/fiber/context.hpp>
22 #include <boost/fiber/fixedsize_stack.hpp>
23 #include <boost/fiber/policy.hpp>
24 #include <boost/fiber/properties.hpp>
25 #include <boost/fiber/segmented_stack.hpp>
27 #ifdef BOOST_HAS_ABI_HEADERS
28 # include BOOST_ABI_PREFIX
32 # pragma warning(push)
33 # pragma warning(disable:4251)
39 class BOOST_FIBERS_DECL fiber {
43 typedef intrusive_ptr< context > ptr_t;
47 void start_() noexcept;
50 typedef context::id id;
52 constexpr fiber() noexcept = default;
54 template< typename Fn,
56 typename = detail::disable_overload< fiber, Fn >
58 fiber( Fn && fn, Args && ... args) :
60 std::allocator_arg, default_stack(),
61 std::forward< Fn >( fn), std::forward< Args >( args) ... } {
64 template< typename Fn,
66 typename = detail::disable_overload< fiber, Fn >
68 fiber( launch policy, Fn && fn, Args && ... args) :
70 std::allocator_arg, default_stack(),
71 std::forward< Fn >( fn), std::forward< Args >( args) ... } {
74 template< typename StackAllocator,
78 fiber( std::allocator_arg_t, StackAllocator salloc, Fn && fn, Args && ... args) :
80 std::allocator_arg, salloc,
81 std::forward< Fn >( fn), std::forward< Args >( args) ... } {
84 template< typename StackAllocator,
88 fiber( launch policy, std::allocator_arg_t, StackAllocator salloc, Fn && fn, Args && ... args) :
89 impl_{ make_worker_context( policy, salloc, std::forward< Fn >( fn), std::forward< Args >( args) ... ) } {
99 fiber( fiber const&) = delete;
100 fiber & operator=( fiber const&) = delete;
102 fiber( fiber && other) noexcept :
104 impl_.swap( other.impl_);
107 fiber & operator=( fiber && other) noexcept {
111 if ( this == & other) return * this;
112 impl_.swap( other.impl_);
116 void swap( fiber & other) noexcept {
117 impl_.swap( other.impl_);
120 id get_id() const noexcept {
121 return impl_ ? impl_->get_id() : id();
124 bool joinable() const noexcept {
125 return nullptr != impl_;
132 template< typename PROPS >
133 PROPS & properties() {
134 auto props = impl_->get_properties();
135 BOOST_ASSERT_MSG(props, "fiber::properties not set");
136 return dynamic_cast< PROPS & >( * props );
141 bool operator<( fiber const& l, fiber const& r) noexcept {
142 return l.get_id() < r.get_id();
146 void swap( fiber & l, fiber & r) noexcept {
153 # pragma warning(pop)
156 #ifdef BOOST_HAS_ABI_HEADERS
157 # include BOOST_ABI_SUFFIX
160 #endif // BOOST_FIBERS_FIBER_H