]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
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) | |
6 | ||
7 | #ifndef BOOST_FIBERS_FIBER_H | |
8 | #define BOOST_FIBERS_FIBER_H | |
9 | ||
10 | #include <algorithm> | |
11 | #include <exception> | |
12 | #include <memory> | |
13 | #include <utility> | |
14 | ||
15 | #include <boost/assert.hpp> | |
16 | #include <boost/config.hpp> | |
17 | #include <boost/intrusive_ptr.hpp> | |
18 | ||
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> | |
26 | ||
27 | #ifdef BOOST_HAS_ABI_HEADERS | |
28 | # include BOOST_ABI_PREFIX | |
29 | #endif | |
30 | ||
31 | #ifdef _MSC_VER | |
32 | # pragma warning(push) | |
33 | # pragma warning(disable:4251) | |
34 | #endif | |
35 | ||
36 | namespace boost { | |
37 | namespace fibers { | |
38 | ||
39 | class BOOST_FIBERS_DECL fiber { | |
40 | private: | |
41 | friend class context; | |
42 | ||
43 | typedef intrusive_ptr< context > ptr_t; | |
44 | ||
45 | ptr_t impl_{}; | |
46 | ||
47 | void start_() noexcept; | |
48 | ||
49 | public: | |
50 | typedef context::id id; | |
51 | ||
52 | constexpr fiber() noexcept = default; | |
53 | ||
54 | template< typename Fn, | |
55 | typename ... Args, | |
56 | typename = detail::disable_overload< fiber, Fn > | |
57 | > | |
58 | fiber( Fn && fn, Args && ... args) : | |
59 | fiber{ launch::post, | |
60 | std::allocator_arg, default_stack(), | |
61 | std::forward< Fn >( fn), std::forward< Args >( args) ... } { | |
62 | } | |
63 | ||
64 | template< typename Fn, | |
65 | typename ... Args, | |
66 | typename = detail::disable_overload< fiber, Fn > | |
67 | > | |
68 | fiber( launch policy, Fn && fn, Args && ... args) : | |
69 | fiber{ policy, | |
70 | std::allocator_arg, default_stack(), | |
71 | std::forward< Fn >( fn), std::forward< Args >( args) ... } { | |
72 | } | |
73 | ||
74 | template< typename StackAllocator, | |
75 | typename Fn, | |
76 | typename ... Args | |
77 | > | |
78 | fiber( std::allocator_arg_t, StackAllocator salloc, Fn && fn, Args && ... args) : | |
79 | fiber{ launch::post, | |
80 | std::allocator_arg, salloc, | |
81 | std::forward< Fn >( fn), std::forward< Args >( args) ... } { | |
82 | } | |
83 | ||
84 | template< typename StackAllocator, | |
85 | typename Fn, | |
86 | typename ... Args | |
87 | > | |
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) ... ) } { | |
90 | start_(); | |
91 | } | |
92 | ||
93 | ~fiber() { | |
94 | if ( joinable() ) { | |
95 | std::terminate(); | |
96 | } | |
97 | } | |
98 | ||
99 | fiber( fiber const&) = delete; | |
100 | fiber & operator=( fiber const&) = delete; | |
101 | ||
102 | fiber( fiber && other) noexcept : | |
103 | impl_{} { | |
104 | impl_.swap( other.impl_); | |
105 | } | |
106 | ||
107 | fiber & operator=( fiber && other) noexcept { | |
108 | if ( joinable() ) { | |
109 | std::terminate(); | |
110 | } | |
111 | if ( this == & other) return * this; | |
112 | impl_.swap( other.impl_); | |
113 | return * this; | |
114 | } | |
115 | ||
116 | void swap( fiber & other) noexcept { | |
117 | impl_.swap( other.impl_); | |
118 | } | |
119 | ||
120 | id get_id() const noexcept { | |
121 | return impl_ ? impl_->get_id() : id(); | |
122 | } | |
123 | ||
124 | bool joinable() const noexcept { | |
125 | return nullptr != impl_; | |
126 | } | |
127 | ||
128 | void join(); | |
129 | ||
130 | void detach(); | |
131 | ||
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 ); | |
137 | } | |
138 | }; | |
139 | ||
140 | inline | |
141 | bool operator<( fiber const& l, fiber const& r) noexcept { | |
142 | return l.get_id() < r.get_id(); | |
143 | } | |
144 | ||
145 | inline | |
146 | void swap( fiber & l, fiber & r) noexcept { | |
147 | return l.swap( r); | |
148 | } | |
149 | ||
150 | }} | |
151 | ||
152 | #ifdef _MSC_VER | |
153 | # pragma warning(pop) | |
154 | #endif | |
155 | ||
156 | #ifdef BOOST_HAS_ABI_HEADERS | |
157 | # include BOOST_ABI_SUFFIX | |
158 | #endif | |
159 | ||
160 | #endif // BOOST_FIBERS_FIBER_H |