fiber.hpp
6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright Oliver Kowalke 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_FIBERS_FIBER_H
#define BOOST_FIBERS_FIBER_H
#include <algorithm>
#include <exception>
#include <memory>
#include <utility>
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/predef.h>
#include <boost/fiber/detail/config.hpp>
#include <boost/fiber/detail/disable_overload.hpp>
#include <boost/fiber/context.hpp>
#include <boost/fiber/fixedsize_stack.hpp>
#include <boost/fiber/policy.hpp>
#include <boost/fiber/properties.hpp>
#include <boost/fiber/segmented_stack.hpp>
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable:4251)
#endif
namespace boost {
namespace fibers {
class BOOST_FIBERS_DECL fiber {
private:
friend class context;
using ptr_t = intrusive_ptr<context>;
ptr_t impl_{};
void start_() noexcept;
public:
using id = context::id;
fiber() = default;
template< typename Fn,
typename ... Arg,
typename = detail::disable_overload< fiber, Fn >,
typename = detail::disable_overload< launch, Fn >,
typename = detail::disable_overload< std::allocator_arg_t, Fn >
>
#if BOOST_COMP_GNUC < 50000000
explicit fiber( Fn && fn, Arg && ... arg) :
#else
fiber( Fn && fn, Arg ... arg) :
#endif
fiber{ launch::post,
std::allocator_arg, default_stack(),
std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
}
template< typename Fn,
typename ... Arg,
typename = detail::disable_overload< fiber, Fn >
>
#if BOOST_COMP_GNUC < 50000000
fiber( launch policy, Fn && fn, Arg && ... arg) :
#else
fiber( launch policy, Fn && fn, Arg ... arg) :
#endif
fiber{ policy,
std::allocator_arg, default_stack(),
std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
}
template< typename StackAllocator,
typename Fn,
typename ... Arg
>
#if BOOST_COMP_GNUC < 50000000
fiber( std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg && ... arg) :
#else
fiber( std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg ... arg) :
#endif
fiber{ launch::post,
std::allocator_arg, std::forward< StackAllocator >( salloc),
std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
}
template< typename StackAllocator,
typename Fn,
typename ... Arg
>
#if BOOST_COMP_GNUC < 50000000
fiber( launch policy, std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg && ... arg) :
#else
fiber( launch policy, std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg ... arg) :
#endif
fiber{ policy,
static_cast<fiber_properties*>(nullptr),
std::allocator_arg, std::forward< StackAllocator >( salloc),
std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
}
template< typename Fn,
typename ... Arg,
typename = detail::disable_overload< fiber, Fn >,
typename = detail::disable_overload< launch, Fn >,
typename = detail::disable_overload< std::allocator_arg_t, Fn >
>
#if BOOST_COMP_GNUC < 50000000
explicit fiber( fiber_properties* properties, Fn && fn, Arg && ... arg) :
#else
fiber( fiber_properties* properties, Fn && fn, Arg ... arg) :
#endif
fiber{ launch::post,
properties,
std::allocator_arg, default_stack(),
std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
}
template< typename Fn,
typename ... Arg,
typename = detail::disable_overload< fiber, Fn >
>
#if BOOST_COMP_GNUC < 50000000
fiber( launch policy, fiber_properties* properties, Fn && fn, Arg && ... arg) :
#else
fiber( launch policy, fiber_properties* properties, Fn && fn, Arg ... arg) :
#endif
fiber{ policy,
properties,
std::allocator_arg, default_stack(),
std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
}
template< typename StackAllocator,
typename Fn,
typename ... Arg
>
#if BOOST_COMP_GNUC < 50000000
fiber( fiber_properties* properties, std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg && ... arg) :
#else
fiber( fiber_properties* properties, std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg ... arg) :
#endif
fiber{ launch::post,
properties,
std::allocator_arg, std::forward< StackAllocator >( salloc),
std::forward< Fn >( fn), std::forward< Arg >( arg) ... } {
}
template< typename StackAllocator,
typename Fn,
typename ... Arg
>
#if BOOST_COMP_GNUC < 50000000
fiber( launch policy, fiber_properties* properties, std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg && ... arg) :
#else
fiber( launch policy, fiber_properties* properties, std::allocator_arg_t, StackAllocator && salloc, Fn && fn, Arg ... arg) :
#endif
impl_{ make_worker_context_with_properties( policy, properties, std::forward< StackAllocator >( salloc), std::forward< Fn >( fn), std::forward< Arg >( arg) ... ) } {
start_();
}
~fiber() {
if ( joinable() ) {
std::terminate();
}
}
fiber( fiber const&) = delete;
fiber & operator=( fiber const&) = delete;
fiber( fiber && other) noexcept :
impl_{} {
swap( other);
}
fiber & operator=( fiber && other) noexcept {
if ( joinable() ) {
std::terminate();
}
if ( BOOST_UNLIKELY( this == & other) ) {
return * this;
}
impl_.swap( other.impl_);
return * this;
}
void swap( fiber & other) noexcept {
impl_.swap( other.impl_);
}
id get_id() const noexcept {
return impl_ ? impl_->get_id() : id();
}
bool joinable() const noexcept {
return nullptr != impl_;
}
void join();
void detach();
template< typename PROPS >
PROPS & properties() {
auto props = impl_->get_properties();
BOOST_ASSERT_MSG( props, "fiber::properties not set");
return dynamic_cast< PROPS & >( * props );
}
};
inline
bool operator<( fiber const& l, fiber const& r) noexcept {
return l.get_id() < r.get_id();
}
inline
void swap( fiber & l, fiber & r) noexcept {
return l.swap( r);
}
}}
#ifdef _MSC_VER
# pragma warning(pop)
#endif
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif
#endif // BOOST_FIBERS_FIBER_H