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)
8 #ifndef BOOST_FIBERS_BOUNDED_CHANNEL_H
9 #define BOOST_FIBERS_BOUNDED_CHANNEL_H
17 #include <system_error>
20 #include <boost/config.hpp>
21 #include <boost/intrusive_ptr.hpp>
23 #include <boost/fiber/detail/config.hpp>
24 #include <boost/fiber/exceptions.hpp>
25 #include <boost/fiber/exceptions.hpp>
26 #include <boost/fiber/condition_variable.hpp>
27 #include <boost/fiber/mutex.hpp>
28 #include <boost/fiber/channel_op_status.hpp>
30 #ifdef BOOST_HAS_ABI_HEADERS
31 # include BOOST_ABI_PREFIX
38 typename Allocator = std::allocator< T >
40 class bounded_channel {
46 typedef intrusive_ptr< node > ptr_t;
47 typedef typename std::allocator_traits< Allocator >::template rebind_alloc<
50 typedef std::allocator_traits< allocator_t > allocator_traits_t;
52 #if ! defined(BOOST_FIBERS_NO_ATOMICS)
53 std::atomic< std::size_t > use_count{ 0 };
55 std::size_t use_count{ 0 };
61 node( T const& t, allocator_t const& alloc_) noexcept :
66 node( T && t, allocator_t & alloc_) noexcept :
72 void intrusive_ptr_add_ref( node * p) noexcept {
77 void intrusive_ptr_release( node * p) noexcept {
78 if ( 0 == --p->use_count) {
79 allocator_t alloc( p->alloc);
80 allocator_traits_t::destroy( alloc, p);
81 allocator_traits_t::deallocate( alloc, p, 1);
86 using ptr_t = typename node::ptr_t;
87 using allocator_t = typename node::allocator_t;
88 using allocator_traits_t = typename node::allocator_traits_t;
90 enum class queue_status {
96 queue_status state_{ queue_status::open };
97 std::size_t count_{ 0 };
100 mutable mutex mtx_{};
101 condition_variable not_empty_cond_{};
102 condition_variable not_full_cond_{};
106 bool is_closed_() const noexcept {
107 return queue_status::closed == state_;
110 void close_( std::unique_lock< boost::fibers::mutex > & lk) noexcept {
111 state_ = queue_status::closed;
113 not_empty_cond_.notify_all();
114 not_full_cond_.notify_all();
117 std::size_t size_() const noexcept {
121 bool is_empty_() const noexcept {
125 bool is_full_() const noexcept {
126 return count_ >= hwm_;
129 channel_op_status push_( ptr_t new_node,
130 std::unique_lock< boost::fibers::mutex > & lk) {
131 if ( is_closed_() ) {
132 return channel_op_status::closed;
134 not_full_cond_.wait( lk,
138 return push_and_notify_( new_node, lk);
141 channel_op_status try_push_( ptr_t new_node,
142 std::unique_lock< boost::fibers::mutex > & lk) noexcept {
143 if ( is_closed_() ) {
144 return channel_op_status::closed;
147 return channel_op_status::full;
149 return push_and_notify_( new_node, lk);
152 template< typename Clock, typename Duration >
153 channel_op_status push_wait_until_( ptr_t new_node,
154 std::chrono::time_point< Clock, Duration > const& timeout_time,
155 std::unique_lock< boost::fibers::mutex > & lk) {
156 if ( is_closed_() ) {
157 return channel_op_status::closed;
159 if ( ! not_full_cond_.wait_until( lk, timeout_time,
163 return channel_op_status::timeout;
165 return push_and_notify_( new_node, lk);
168 channel_op_status push_and_notify_( ptr_t new_node,
169 std::unique_lock< boost::fibers::mutex > & lk) noexcept {
170 push_tail_( new_node);
172 not_empty_cond_.notify_one();
173 return channel_op_status::success;
176 void push_tail_( ptr_t new_node) noexcept {
178 tail_ = & new_node->nxt;
182 value_type value_pop_( std::unique_lock< boost::fibers::mutex > & lk) {
183 BOOST_ASSERT( ! is_empty_() );
184 auto old_head = pop_head_();
185 if ( size_() <= lwm_) {
188 not_full_cond_.notify_one();
191 // more than one producer could be waiting
193 not_full_cond_.notify_all();
196 return std::move( old_head->va);
199 ptr_t pop_head_() noexcept {
200 auto old_head = head_;
201 head_ = old_head->nxt;
205 old_head->nxt.reset();
211 bounded_channel( std::size_t hwm, std::size_t lwm,
212 Allocator const& alloc = Allocator() ) :
218 throw fiber_error( std::make_error_code( std::errc::invalid_argument),
219 "boost fiber: high-watermark is less than or equal to low-watermark for bounded_channel");
222 throw fiber_error( std::make_error_code( std::errc::invalid_argument),
223 "boost fiber: high-watermark is zero");
227 bounded_channel( std::size_t wm,
228 Allocator const& alloc = Allocator() ) :
234 throw fiber_error( std::make_error_code( std::errc::invalid_argument),
235 "boost fiber: watermark is zero");
239 bounded_channel( bounded_channel const&) = delete;
240 bounded_channel & operator=( bounded_channel const&) = delete;
242 std::size_t upper_bound() const noexcept {
246 std::size_t lower_bound() const noexcept {
250 void close() noexcept {
251 std::unique_lock< mutex > lk( mtx_);
255 channel_op_status push( value_type const& va) {
256 typename allocator_traits_t::pointer ptr{
257 allocator_traits_t::allocate( alloc_, 1) };
259 allocator_traits_t::construct( alloc_, ptr, va, alloc_);
261 allocator_traits_t::deallocate( alloc_, ptr, 1);
264 std::unique_lock< mutex > lk( mtx_);
265 return push_( { detail::convert( ptr) }, lk);
268 channel_op_status push( value_type && va) {
269 typename allocator_traits_t::pointer ptr{
270 allocator_traits_t::allocate( alloc_, 1) };
272 allocator_traits_t::construct(
273 alloc_, ptr, std::move( va), alloc_);
275 allocator_traits_t::deallocate( alloc_, ptr, 1);
278 std::unique_lock< mutex > lk( mtx_);
279 return push_( { detail::convert( ptr) }, lk);
282 template< typename Rep, typename Period >
283 channel_op_status push_wait_for( value_type const& va,
284 std::chrono::duration< Rep, Period > const& timeout_duration) {
285 return push_wait_until( va,
286 std::chrono::steady_clock::now() + timeout_duration);
289 template< typename Rep, typename Period >
290 channel_op_status push_wait_for( value_type && va,
291 std::chrono::duration< Rep, Period > const& timeout_duration) {
292 return push_wait_until( std::forward< value_type >( va),
293 std::chrono::steady_clock::now() + timeout_duration);
296 template< typename Clock, typename Duration >
297 channel_op_status push_wait_until( value_type const& va,
298 std::chrono::time_point< Clock, Duration > const& timeout_time) {
299 typename allocator_traits_t::pointer ptr{
300 allocator_traits_t::allocate( alloc_, 1) };
302 allocator_traits_t::construct( alloc_, ptr, va, alloc_);
304 allocator_traits_t::deallocate( alloc_, ptr, 1);
307 std::unique_lock< mutex > lk( mtx_);
308 return push_wait_until_( { detail::convert( ptr) }, timeout_time, lk);
311 template< typename Clock, typename Duration >
312 channel_op_status push_wait_until( value_type && va,
313 std::chrono::time_point< Clock, Duration > const& timeout_time) {
314 typename allocator_traits_t::pointer ptr{
315 allocator_traits_t::allocate( alloc_, 1) };
317 allocator_traits_t::construct(
318 alloc_, ptr, std::move( va), alloc_);
320 allocator_traits_t::deallocate( alloc_, ptr, 1);
323 std::unique_lock< mutex > lk( mtx_);
324 return push_wait_until_( { detail::convert( ptr) }, timeout_time, lk);
327 channel_op_status try_push( value_type const& va) {
328 typename allocator_traits_t::pointer ptr{
329 allocator_traits_t::allocate( alloc_, 1) };
331 allocator_traits_t::construct( alloc_, ptr, va, alloc_);
333 allocator_traits_t::deallocate( alloc_, ptr, 1);
336 std::unique_lock< mutex > lk( mtx_);
337 return try_push_( { detail::convert( ptr) }, lk);
340 channel_op_status try_push( value_type && va) {
341 typename allocator_traits_t::pointer ptr{
342 allocator_traits_t::allocate( alloc_, 1) };
344 allocator_traits_t::construct(
345 alloc_, ptr, std::move( va), alloc_);
347 allocator_traits_t::deallocate( alloc_, ptr, 1);
350 std::unique_lock< mutex > lk( mtx_);
351 return try_push_( { detail::convert( ptr) }, lk);
354 channel_op_status pop( value_type & va) {
355 std::unique_lock< mutex > lk( mtx_);
356 not_empty_cond_.wait( lk,
358 return is_closed_() || ! is_empty_();
360 if ( is_closed_() && is_empty_() ) {
361 return channel_op_status::closed;
363 va = value_pop_( lk);
364 return channel_op_status::success;
367 value_type value_pop() {
368 std::unique_lock< mutex > lk( mtx_);
369 not_empty_cond_.wait( lk,
371 return is_closed_() || ! is_empty_();
373 if ( is_closed_() && is_empty_() ) {
375 std::make_error_code( std::errc::operation_not_permitted),
376 "boost fiber: queue is closed");
378 return value_pop_( lk);
381 channel_op_status try_pop( value_type & va) {
382 std::unique_lock< mutex > lk( mtx_);
383 if ( is_closed_() && is_empty_() ) {
384 // let other fibers run
387 return channel_op_status::closed;
390 // let other fibers run
393 return channel_op_status::empty;
395 va = value_pop_( lk);
396 return channel_op_status::success;
399 template< typename Rep, typename Period >
400 channel_op_status pop_wait_for( value_type & va,
401 std::chrono::duration< Rep, Period > const& timeout_duration) {
402 return pop_wait_until( va,
403 std::chrono::steady_clock::now() + timeout_duration);
406 template< typename Clock, typename Duration >
407 channel_op_status pop_wait_until( value_type & va,
408 std::chrono::time_point< Clock, Duration > const& timeout_time) {
409 std::unique_lock< mutex > lk( mtx_);
410 if ( ! not_empty_cond_.wait_until( lk,
413 return is_closed_() || ! is_empty_();
415 return channel_op_status::timeout;
417 if ( is_closed_() && is_empty_() ) {
418 return channel_op_status::closed;
420 va = value_pop_( lk);
421 return channel_op_status::success;
427 #ifdef BOOST_HAS_ABI_HEADERS
428 # include BOOST_ABI_SUFFIX
431 #endif // BOOST_FIBERS_BOUNDED_CHANNEL_H