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>
18 #include <boost/predef.h>
20 #include <boost/fiber/detail/config.hpp>
21 #include <boost/fiber/detail/disable_overload.hpp>
22 #include <boost/fiber/context.hpp>
23 #include <boost/fiber/fixedsize_stack.hpp>
24 #include <boost/fiber/policy.hpp>
25 #include <boost/fiber/properties.hpp>
26 #include <boost/fiber/segmented_stack.hpp>
28 #ifdef BOOST_HAS_ABI_HEADERS
29 # include BOOST_ABI_PREFIX
33 # pragma warning(push)
34 # pragma warning(disable:4251)
40 class BOOST_FIBERS_DECL fiber {
44 using ptr_t = intrusive_ptr<context>;
48 void start_() noexcept;
51 using id = context::id;
55 template< typename Fn,
57 typename = detail::disable_overload< fiber, Fn >,
58 typename = detail::disable_overload< launch, Fn >,
59 typename = detail::disable_overload< std::allocator_arg_t, Fn >
61 #if BOOST_COMP_GNUC < 50000000
62 explicit fiber( Fn && fn, Arg && ... arg) :
64 fiber( Fn && fn, Arg ... arg) :
67 std::allocator_arg, default_stack(),
68 std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
71 template< typename Fn,
73 typename = detail::disable_overload< fiber, Fn >
75 #if BOOST_COMP_GNUC < 50000000
76 fiber( launch policy, Fn && fn, Arg && ... arg) :
78 fiber( launch policy, Fn && fn, Arg ... arg) :
81 std::allocator_arg, default_stack(),
82 std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
85 template< typename StackAllocator,
89 #if BOOST_COMP_GNUC < 50000000
90 fiber( std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg && ... arg) :
92 fiber( std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg ... arg) :
95 std::allocator_arg, std::forward< StackAllocator >( salloc),
96 std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
99 template< typename StackAllocator,
103 #if BOOST_COMP_GNUC < 50000000
104 fiber( launch policy, std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg && ... arg) :
106 fiber( launch policy, std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg ... arg) :
108 impl_{ make_worker_context( policy, std::forward< StackAllocator >( salloc), std::forward< Fn >( fn), std::forward< Arg >( arg) ... ) } {
118 fiber( fiber const&) = delete;
119 fiber & operator=( fiber const&) = delete;
121 fiber( fiber && other) noexcept :
126 fiber & operator=( fiber && other) noexcept {
130 if ( BOOST_UNLIKELY( this == & other) ) {
133 impl_.swap( other.impl_);
137 void swap( fiber & other) noexcept {
138 impl_.swap( other.impl_);
141 id get_id() const noexcept {
142 return impl_ ? impl_->get_id() : id();
145 bool joinable() const noexcept {
146 return nullptr != impl_;
153 template< typename PROPS >
154 PROPS & properties() {
155 auto props = impl_->get_properties();
156 BOOST_ASSERT_MSG( props, "fiber::properties not set");
157 return dynamic_cast< PROPS & >( * props );
162 bool operator<( fiber const& l, fiber const& r) noexcept {
163 return l.get_id() < r.get_id();
167 void swap( fiber & l, fiber & r) noexcept {
174 # pragma warning(pop)
177 #ifdef BOOST_HAS_ABI_HEADERS
178 # include BOOST_ABI_SUFFIX
181 #endif // BOOST_FIBERS_FIBER_H