diff --git a/include/boost/fiber/algo/algorithm.hpp b/include/boost/fiber/algo/algorithm.hpp index 515fc5ed..9b846e77 100644 --- a/include/boost/fiber/algo/algorithm.hpp +++ b/include/boost/fiber/algo/algorithm.hpp @@ -43,11 +43,11 @@ struct BOOST_FIBERS_DECL algorithm { class BOOST_FIBERS_DECL algorithm_with_properties_base : public algorithm { public: // called by fiber_properties::notify() -- don't directly call - virtual void property_change_( context * f, fiber_properties * props) noexcept = 0; + virtual void property_change_( context * ctx, fiber_properties * props) noexcept = 0; protected: - static fiber_properties* get_properties( context * f) noexcept; - static void set_properties( context * f, fiber_properties * p) noexcept; + static fiber_properties* get_properties( context * ctx) noexcept; + static void set_properties( context * ctx, fiber_properties * p) noexcept; }; template< typename PROPS > @@ -58,18 +58,18 @@ struct algorithm_with_properties : public algorithm_with_properties_base { // must override awakened() with properties parameter instead. Otherwise // you'd have to remember to start every subclass awakened() override // with: algorithm_with_properties::awakened(fb); - virtual void awakened( context * f) noexcept override final { - fiber_properties * props = super::get_properties( f); + virtual void awakened( context * ctx) noexcept override final { + fiber_properties * props = super::get_properties( ctx); if ( nullptr == props) { // TODO: would be great if PROPS could be allocated on the new // fiber's stack somehow - props = new_properties( f); + props = new_properties( ctx); // It is not good for new_properties() to return 0. - BOOST_ASSERT_MSG(props, "new_properties() must return non-NULL"); + BOOST_ASSERT_MSG( props, "new_properties() must return non-NULL"); // new_properties() must return instance of (a subclass of) PROPS BOOST_ASSERT_MSG( dynamic_cast< PROPS * >( props), "new_properties() must return properties class"); - super::set_properties( f, props); + super::set_properties( ctx, props); } // Set algo_ again every time this fiber becomes READY. That // handles the case of a fiber migrating to a new thread with a new @@ -77,31 +77,31 @@ struct algorithm_with_properties : public algorithm_with_properties_base { props->set_algorithm( this); // Okay, now forward the call to subclass override. - awakened( f, properties(f) ); + awakened( ctx, properties( ctx) ); } // subclasses override this method instead of the original awakened() - virtual void awakened( context *, PROPS& ) noexcept = 0; + virtual void awakened( context *, PROPS &) noexcept = 0; // used for all internal calls - PROPS & properties( context * f) noexcept { - return static_cast< PROPS & >( * super::get_properties( f) ); + PROPS & properties( context * ctx) noexcept { + return static_cast< PROPS & >( * super::get_properties( ctx) ); } // override this to be notified by PROPS::notify() - virtual void property_change( context * f, PROPS & props) noexcept { + virtual void property_change( context * ctx, PROPS & props) noexcept { } // implementation for algorithm_with_properties_base method - void property_change_( context * f, fiber_properties * props ) noexcept override final { - property_change( f, * static_cast< PROPS * >( props) ); + void property_change_( context * ctx, fiber_properties * props) noexcept override final { + property_change( ctx, * static_cast< PROPS * >( props) ); } // Override this to customize instantiation of PROPS, e.g. use a different // allocator. Each PROPS instance is associated with a particular // context. - virtual fiber_properties * new_properties( context * f) { - return new PROPS( f); + virtual fiber_properties * new_properties( context * ctx) { + return new PROPS( ctx); } }; diff --git a/include/boost/fiber/algo/round_robin.hpp b/include/boost/fiber/algo/round_robin.hpp index 038b4245..e3849825 100644 --- a/include/boost/fiber/algo/round_robin.hpp +++ b/include/boost/fiber/algo/round_robin.hpp @@ -32,9 +32,9 @@ namespace algo { class BOOST_FIBERS_DECL round_robin : public algorithm { private: - typedef scheduler::ready_queue_t rqueue_t; + typedef scheduler::ready_queue_type rqueue_type; - rqueue_t rqueue_{}; + rqueue_type rqueue_{}; std::mutex mtx_{}; std::condition_variable cnd_{}; bool flag_{ false }; diff --git a/include/boost/fiber/algo/shared_work.hpp b/include/boost/fiber/algo/shared_work.hpp index e648c5b1..23bc926e 100644 --- a/include/boost/fiber/algo/shared_work.hpp +++ b/include/boost/fiber/algo/shared_work.hpp @@ -34,17 +34,17 @@ namespace algo { class BOOST_FIBERS_DECL shared_work : public algorithm { private: - typedef std::deque< context * > rqueue_t; - typedef scheduler::ready_queue_t lqueue_t; + typedef std::deque< context * > rqueue_type; + typedef scheduler::ready_queue_type lqueue_type; - static rqueue_t rqueue_; + static rqueue_type rqueue_; static std::mutex rqueue_mtx_; - lqueue_t lqueue_{}; + lqueue_type lqueue_{}; std::mutex mtx_{}; std::condition_variable cnd_{}; bool flag_{ false }; - bool suspend_; + bool suspend_{ false }; public: shared_work() = default; @@ -64,7 +64,7 @@ public: context * pick_next() noexcept; bool has_ready_fibers() const noexcept { - std::unique_lock< std::mutex > lock( rqueue_mtx_); + std::unique_lock< std::mutex > lock{ rqueue_mtx_ }; return ! rqueue_.empty() || ! lqueue_.empty(); } diff --git a/include/boost/fiber/algo/work_stealing.hpp b/include/boost/fiber/algo/work_stealing.hpp index 36adcddb..7a16f00e 100644 --- a/include/boost/fiber/algo/work_stealing.hpp +++ b/include/boost/fiber/algo/work_stealing.hpp @@ -32,14 +32,14 @@ namespace algo { class work_stealing : public algorithm { private: - typedef scheduler::ready_queue_t lqueue_t; + typedef scheduler::ready_queue_type lqueue_type; static std::vector< work_stealing * > schedulers_; std::size_t idx_; std::size_t max_idx_; detail::context_spmc_queue rqueue_{}; - lqueue_t lqueue_{}; + lqueue_type lqueue_{}; std::mutex mtx_{}; std::condition_variable cnd_{}; bool flag_{ false }; diff --git a/include/boost/fiber/buffered_channel.hpp b/include/boost/fiber/buffered_channel.hpp index d9e95a00..ef3477fc 100644 --- a/include/boost/fiber/buffered_channel.hpp +++ b/include/boost/fiber/buffered_channel.hpp @@ -64,23 +64,23 @@ private: char pad_[cacheline_length]; bool is_full_() { - std::size_t idx{ producer_idx_.load( std::memory_order_relaxed) }; + std::size_t idx = producer_idx_.load( std::memory_order_relaxed); return 0 > static_cast< std::intptr_t >( slots_[idx & (capacity_ - 1)].cycle.load( std::memory_order_acquire) ) - static_cast< std::intptr_t >( idx); } bool is_empty_() { - std::size_t idx{ consumer_idx_.load( std::memory_order_relaxed) }; + std::size_t idx = consumer_idx_.load( std::memory_order_relaxed); return 0 > static_cast< std::intptr_t >( slots_[idx & (capacity_ - 1)].cycle.load( std::memory_order_acquire) ) - static_cast< std::intptr_t >( idx + 1); } template< typename ValueType > channel_op_status try_push_( ValueType && value) { - slot * s{ nullptr }; - std::size_t idx{ producer_idx_.load( std::memory_order_relaxed) }; + slot * s = nullptr; + std::size_t idx = producer_idx_.load( std::memory_order_relaxed); for (;;) { s = & slots_[idx & (capacity_ - 1)]; - std::size_t cycle{ s->cycle.load( std::memory_order_acquire) }; - std::intptr_t diff{ static_cast< std::intptr_t >( cycle) - static_cast< std::intptr_t >( idx) }; + std::size_t cycle = s->cycle.load( std::memory_order_acquire); + std::intptr_t diff = static_cast< std::intptr_t >( cycle) - static_cast< std::intptr_t >( idx); if ( 0 == diff) { if ( producer_idx_.compare_exchange_weak( idx, idx + 1, std::memory_order_relaxed) ) { break; @@ -101,7 +101,7 @@ private: for (;;) { s = & slots_[idx & (capacity_ - 1)]; std::size_t cycle = s->cycle.load( std::memory_order_acquire); - std::intptr_t diff{ static_cast< std::intptr_t >( cycle) - static_cast< std::intptr_t >( idx + 1) }; + std::intptr_t diff = static_cast< std::intptr_t >( cycle) - static_cast< std::intptr_t >( idx + 1); if ( 0 == diff) { if ( consumer_idx_.compare_exchange_weak( idx, idx + 1, std::memory_order_relaxed) ) { break; @@ -118,9 +118,9 @@ private: } channel_op_status try_pop_( value_type & value) { - slot * s{ nullptr }; - std::size_t idx{ 0 }; - channel_op_status status{ try_value_pop_( s, idx) }; + slot * s = nullptr; + std::size_t idx = 0; + channel_op_status status = try_value_pop_( s, idx); if ( channel_op_status::success == status) { value = std::move( * reinterpret_cast< value_type * >( std::addressof( s->storage) ) ); s->cycle.store( idx + capacity_, std::memory_order_release); @@ -132,8 +132,8 @@ public: explicit buffered_channel( std::size_t capacity) : capacity_{ capacity } { if ( 2 > capacity_ || 0 != ( capacity_ & (capacity_ - 1) ) ) { - throw fiber_error( std::make_error_code( std::errc::invalid_argument), - "boost fiber: buffer capacity is invalid"); + throw fiber_error{ std::make_error_code( std::errc::invalid_argument), + "boost fiber: buffer capacity is invalid" }; } slots_ = new slot[capacity_](); for ( std::size_t i = 0; i < capacity_; ++i) { @@ -144,8 +144,8 @@ public: ~buffered_channel() { close(); for (;;) { - slot * s{ nullptr }; - std::size_t idx{ 0 }; + slot * s = nullptr; + std::size_t idx= 0; if ( channel_op_status::success == try_value_pop_( s, idx) ) { reinterpret_cast< value_type * >( std::addressof( s->storage) )->~value_type(); s->cycle.store( idx + capacity_, std::memory_order_release); @@ -164,20 +164,22 @@ public: } void close() noexcept { - context * ctx{ context::active() }; + context * active_ctx = context::active(); detail::spinlock_lock lk{ splk_ }; closed_.store( true, std::memory_order_release); // notify all waiting producers + // FIXME: swap queue while ( ! waiting_producers_.empty() ) { - context * producer_ctx{ & waiting_producers_.front() }; + context * producer_ctx = & waiting_producers_.front(); waiting_producers_.pop_front(); - ctx->set_ready( producer_ctx); + active_ctx->set_ready( producer_ctx); } // notify all waiting consumers + // FIXME: swap queue, then unlock lock while ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } } @@ -196,24 +198,23 @@ public: } channel_op_status push( value_type const& value) { - context * ctx{ context::active() }; + context * active_ctx = context::active(); for (;;) { if ( is_closed() ) { return channel_op_status::closed; } - channel_op_status status{ try_push_( value) }; + channel_op_status status = try_push_( value); if ( channel_op_status::success == status) { detail::spinlock_lock lk{ splk_ }; // notify one waiting consumer if ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); lk.unlock(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } return status; } else if ( channel_op_status::full == status) { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -221,9 +222,9 @@ public: if ( ! is_full_() ) { continue; } - ctx->wait_link( waiting_producers_); + active_ctx->wait_link( waiting_producers_); // suspend this producer - ctx->suspend( lk); + active_ctx->suspend( lk); } else { BOOST_ASSERT( channel_op_status::closed == status); return status; @@ -232,24 +233,23 @@ public: } channel_op_status push( value_type && value) { - context * ctx{ context::active() }; + context * active_ctx = context::active(); for (;;) { if ( is_closed() ) { return channel_op_status::closed; } - channel_op_status status{ try_push_( std::move( value) ) }; + channel_op_status status = try_push_( std::move( value) ); if ( channel_op_status::success == status) { detail::spinlock_lock lk{ splk_ }; // notify one waiting consumer if ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); lk.unlock(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } return status; } else if ( channel_op_status::full == status) { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -257,9 +257,9 @@ public: if ( ! is_full_() ) { continue; } - ctx->wait_link( waiting_producers_); + active_ctx->wait_link( waiting_producers_); // suspend this producer - ctx->suspend( lk); + active_ctx->suspend( lk); } else { BOOST_ASSERT( channel_op_status::closed == status); return status; @@ -284,25 +284,24 @@ public: template< typename Clock, typename Duration > channel_op_status push_wait_until( value_type const& value, std::chrono::time_point< Clock, Duration > const& timeout_time_) { - std::chrono::steady_clock::time_point timeout_time( detail::convert( timeout_time_) ); - context * ctx{ context::active() }; + context * active_ctx = context::active(); + std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); for (;;) { if ( is_closed() ) { return channel_op_status::closed; } - channel_op_status status{ try_push_( value) }; + channel_op_status status = try_push_( value); if ( channel_op_status::success == status) { detail::spinlock_lock lk{ splk_ }; // notify one waiting consumer if ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); lk.unlock(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } return status; } else if ( channel_op_status::full == status) { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -310,13 +309,13 @@ public: if ( ! is_full_() ) { continue; } - ctx->wait_link( waiting_producers_); + active_ctx->wait_link( waiting_producers_); // suspend this producer - if ( ! ctx->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // relock local lk lk.lock(); // remove from waiting-queue - ctx->wait_unlink(); + active_ctx->wait_unlink(); return channel_op_status::timeout; } } else { @@ -329,25 +328,24 @@ public: template< typename Clock, typename Duration > channel_op_status push_wait_until( value_type && value, std::chrono::time_point< Clock, Duration > const& timeout_time_) { - std::chrono::steady_clock::time_point timeout_time( detail::convert( timeout_time_) ); - context * ctx{ context::active() }; + context * active_ctx = context::active(); + std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); for (;;) { if ( is_closed() ) { return channel_op_status::closed; } - channel_op_status status{ try_push_( std::move( value) ) }; + channel_op_status status = try_push_( std::move( value) ); if ( channel_op_status::success == status) { detail::spinlock_lock lk{ splk_ }; // notify one waiting consumer if ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); lk.unlock(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } return status; } else if ( channel_op_status::full == status) { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -355,13 +353,13 @@ public: if ( ! is_full_() ) { continue; } - ctx->wait_link( waiting_producers_); + active_ctx->wait_link( waiting_producers_); // suspend this producer - if ( ! ctx->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // relock local lk lk.lock(); // remove from waiting-queue - ctx->wait_unlink(); + active_ctx->wait_unlink(); return channel_op_status::timeout; } } else { @@ -372,7 +370,7 @@ public: } channel_op_status try_pop( value_type & value) { - channel_op_status status{ try_pop_( value) }; + channel_op_status status = try_pop_( value); if ( channel_op_status::success != status) { if ( is_closed() ) { status = channel_op_status::closed; @@ -382,21 +380,20 @@ public: } channel_op_status pop( value_type & value) { - context * ctx{ context::active() }; + context * active_ctx = context::active(); for (;;) { - channel_op_status status{ try_pop_( value) }; + channel_op_status status = try_pop_( value); if ( channel_op_status::success == status) { detail::spinlock_lock lk{ splk_ }; // notify one waiting producer if ( ! waiting_producers_.empty() ) { - context * producer_ctx{ & waiting_producers_.front() }; + context * producer_ctx = & waiting_producers_.front(); waiting_producers_.pop_front(); lk.unlock(); - ctx->set_ready( producer_ctx); + active_ctx->set_ready( producer_ctx); } return status; } else if ( channel_op_status::empty == status) { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -404,9 +401,9 @@ public: if ( ! is_empty_() ) { continue; } - ctx->wait_link( waiting_consumers_); + active_ctx->wait_link( waiting_consumers_); // suspend this consumer - ctx->suspend( lk); + active_ctx->suspend( lk); } else { BOOST_ASSERT( channel_op_status::closed == status); return status; @@ -415,25 +412,24 @@ public: } value_type value_pop() { - context * ctx{ context::active() }; + context * active_ctx = context::active(); for (;;) { - slot * s{ nullptr }; - std::size_t idx{ 0 }; - channel_op_status status{ try_value_pop_( s, idx) }; + slot * s = nullptr; + std::size_t idx = 0; + channel_op_status status = try_value_pop_( s, idx); if ( channel_op_status::success == status) { - value_type value{ std::move( * reinterpret_cast< value_type * >( std::addressof( s->storage) ) ) }; + value_type value = std::move( * reinterpret_cast< value_type * >( std::addressof( s->storage) ) ); s->cycle.store( idx + capacity_, std::memory_order_release); detail::spinlock_lock lk{ splk_ }; // notify one waiting producer if ( ! waiting_producers_.empty() ) { - context * producer_ctx{ & waiting_producers_.front() }; + context * producer_ctx = & waiting_producers_.front(); waiting_producers_.pop_front(); lk.unlock(); - ctx->set_ready( producer_ctx); + active_ctx->set_ready( producer_ctx); } return std::move( value); } else if ( channel_op_status::empty == status) { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { throw fiber_error{ @@ -443,9 +439,9 @@ public: if ( ! is_empty_() ) { continue; } - ctx->wait_link( waiting_consumers_); + active_ctx->wait_link( waiting_consumers_); // suspend this consumer - ctx->suspend( lk); + active_ctx->suspend( lk); } else { BOOST_ASSERT( channel_op_status::closed == status); throw fiber_error{ @@ -465,22 +461,21 @@ public: template< typename Clock, typename Duration > channel_op_status pop_wait_until( value_type & value, std::chrono::time_point< Clock, Duration > const& timeout_time_) { - std::chrono::steady_clock::time_point timeout_time( detail::convert( timeout_time_) ); - context * ctx{ context::active() }; + context * active_ctx = context::active(); + std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); for (;;) { - channel_op_status status{ try_pop_( value) }; + channel_op_status status = try_pop_( value); if ( channel_op_status::success == status) { detail::spinlock_lock lk{ splk_ }; // notify one waiting producer if ( ! waiting_producers_.empty() ) { - context * producer_ctx{ & waiting_producers_.front() }; + context * producer_ctx = & waiting_producers_.front(); waiting_producers_.pop_front(); lk.unlock(); - context::active()->set_ready( producer_ctx); + active_ctx->set_ready( producer_ctx); } return status; } else if ( channel_op_status::empty == status) { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -488,13 +483,13 @@ public: if ( ! is_empty_() ) { continue; } - ctx->wait_link( waiting_consumers_); + active_ctx->wait_link( waiting_consumers_); // suspend this consumer - if ( ! ctx->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // relock local lk lk.lock(); // remove from waiting-queue - ctx->wait_unlink(); + active_ctx->wait_unlink(); return channel_op_status::timeout; } } else { diff --git a/include/boost/fiber/condition_variable.hpp b/include/boost/fiber/condition_variable.hpp index a9c2a259..fcef9b47 100644 --- a/include/boost/fiber/condition_variable.hpp +++ b/include/boost/fiber/condition_variable.hpp @@ -64,16 +64,16 @@ public: template< typename LockType > void wait( LockType & lt) { - context * ctx = context::active(); + context * active_ctx = context::active(); // atomically call lt.unlock() and block on *this // store this fiber in waiting-queue - detail::spinlock_lock lk( wait_queue_splk_); - BOOST_ASSERT( ! ctx->wait_is_linked() ); - ctx->wait_link( wait_queue_); + detail::spinlock_lock lk{ wait_queue_splk_ }; + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); + active_ctx->wait_link( wait_queue_); // unlock external lt lt.unlock(); // suspend this fiber - ctx->suspend( lk); + active_ctx->suspend( lk); // relock external again before returning try { lt.lock(); @@ -81,7 +81,7 @@ public: std::terminate(); } // post-conditions - BOOST_ASSERT( ! ctx->wait_is_linked() ); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); } template< typename LockType, typename Pred > @@ -93,24 +93,23 @@ public: template< typename LockType, typename Clock, typename Duration > cv_status wait_until( LockType & lt, std::chrono::time_point< Clock, Duration > const& timeout_time_) { + context * active_ctx = context::active(); cv_status status = cv_status::no_timeout; - std::chrono::steady_clock::time_point timeout_time( - detail::convert( timeout_time_) ); - context * ctx = context::active(); + std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); // atomically call lt.unlock() and block on *this // store this fiber in waiting-queue - detail::spinlock_lock lk( wait_queue_splk_); - BOOST_ASSERT( ! ctx->wait_is_linked() ); - ctx->wait_link( wait_queue_); + detail::spinlock_lock lk{ wait_queue_splk_ }; + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); + active_ctx->wait_link( wait_queue_); // unlock external lt lt.unlock(); // suspend this fiber - if ( ! ctx->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { status = cv_status::timeout; // relock local lk lk.lock(); // remove from waiting-queue - ctx->wait_unlink(); + active_ctx->wait_unlink(); // unlock local lk lk.unlock(); } @@ -121,7 +120,7 @@ public: std::terminate(); } // post-conditions - BOOST_ASSERT( ! ctx->wait_is_linked() ); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); return status; } diff --git a/include/boost/fiber/context.hpp b/include/boost/fiber/context.hpp index 41cd4ef5..dc85fb19 100644 --- a/include/boost/fiber/context.hpp +++ b/include/boost/fiber/context.hpp @@ -248,11 +248,10 @@ public: context * impl_{ nullptr }; public: - id() noexcept { - } + id() = default; explicit id( context * impl) noexcept : - impl_( impl) { + impl_{ impl } { } bool operator==( id const& other) const noexcept { @@ -498,12 +497,12 @@ public: BOOST_ASSERT( nullptr != ctx); if ( 0 == --ctx->use_count_) { #if (BOOST_EXECUTION_CONTEXT==1) - boost::context::execution_context ec( ctx->ctx_); + boost::context::execution_context ec = ctx->ctx_; // destruct context // deallocates stack (execution_context is ref counted) ctx->~context(); #else - boost::context::continuation cc( std::move( ctx->c_) ); + boost::context::continuation cc = std::move( ctx->c_); // destruct context ctx->~context(); // deallocated stack @@ -540,14 +539,14 @@ static intrusive_ptr< context > make_worker_context( launch policy, const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) ); #endif // placement new of context on top of fiber's stack - return intrusive_ptr< context >( - ::new ( sp) context( + return intrusive_ptr< context >{ + ::new ( sp) context{ worker_context, policy, - boost::context::preallocated( sp, size, sctx), + boost::context::preallocated{ sp, size, sctx }, salloc, std::forward< Fn >( fn), - std::make_tuple( std::forward< Args >( args) ... ) ) ); + std::make_tuple( std::forward< Args >( args) ... ) } }; } namespace detail { diff --git a/include/boost/fiber/detail/context_spmc_queue.hpp b/include/boost/fiber/detail/context_spmc_queue.hpp index 2543d6cc..b0aa5616 100644 --- a/include/boost/fiber/detail/context_spmc_queue.hpp +++ b/include/boost/fiber/detail/context_spmc_queue.hpp @@ -48,7 +48,7 @@ private: sizeof( atomic_type), cache_alignment >::type storage_type; - std::size_t size_; + std::size_t size_; storage_type * storage_; public: @@ -115,19 +115,19 @@ public: context_spmc_queue & operator=( context_spmc_queue const&) = delete; bool empty() const noexcept { - std::size_t bottom{ bottom_.load( std::memory_order_relaxed) }; - std::size_t top{ top_.load( std::memory_order_relaxed) }; + std::size_t bottom = bottom_.load( std::memory_order_relaxed); + std::size_t top = top_.load( std::memory_order_relaxed); return bottom <= top; } void push( context * ctx) { - std::size_t bottom{ bottom_.load( std::memory_order_relaxed) }; - std::size_t top{ top_.load( std::memory_order_acquire) }; - array * a{ array_.load( std::memory_order_relaxed) }; + std::size_t bottom = bottom_.load( std::memory_order_relaxed); + std::size_t top = top_.load( std::memory_order_acquire); + array * a = array_.load( std::memory_order_relaxed); if ( (a->size() - 1) < (bottom - top) ) { // queue is full // resize - array * tmp{ a->resize( bottom, top) }; + array * tmp = a->resize( bottom, top); old_arrays_.push_back( a); std::swap( a, tmp); array_.store( a, std::memory_order_relaxed); @@ -138,18 +138,19 @@ public: } context * pop() { - std::size_t top{ top_.load( std::memory_order_acquire) }; + std::size_t top = top_.load( std::memory_order_acquire); std::atomic_thread_fence( std::memory_order_seq_cst); - std::size_t bottom{ bottom_.load( std::memory_order_acquire) }; - context * ctx{ nullptr }; + std::size_t bottom = bottom_.load( std::memory_order_acquire); + context * ctx = nullptr; if ( top < bottom) { // queue is not empty - array * a{ array_.load( std::memory_order_consume) }; + array * a = array_.load( std::memory_order_consume); ctx = a->pop( top); if ( ctx->is_context( type::pinned_context) || ! top_.compare_exchange_strong( top, top + 1, std::memory_order_seq_cst, std::memory_order_relaxed) ) { + // FIXME: what about the popped piinded context? // lose the race return nullptr; } diff --git a/include/boost/fiber/detail/data.hpp b/include/boost/fiber/detail/data.hpp index 24e833a9..e2b119ec 100644 --- a/include/boost/fiber/detail/data.hpp +++ b/include/boost/fiber/detail/data.hpp @@ -28,7 +28,7 @@ struct data_t { spinlock_lock * lk{ nullptr }; context * ctx{ nullptr }; - data_t() noexcept = default; + data_t() = default; explicit data_t( spinlock_lock * lk_) noexcept : lk{ lk_ } { diff --git a/include/boost/fiber/detail/spinlock_ttas_adaptive_futex.hpp b/include/boost/fiber/detail/spinlock_ttas_adaptive_futex.hpp index a81c4668..34cb1755 100644 --- a/include/boost/fiber/detail/spinlock_ttas_adaptive_futex.hpp +++ b/include/boost/fiber/detail/spinlock_ttas_adaptive_futex.hpp @@ -33,7 +33,7 @@ class spinlock_ttas_adaptive_futex { private: // align shared variable 'value_' at cache line to prevent false sharing alignas(cache_alignment) std::atomic< std::int32_t > value_{ 0 }; - std::atomic< std::int32_t > tests_{ 0 }; + std::atomic< std::int32_t > tests_{ 0 }; // padding to avoid other data one the cacheline of shared variable 'value_' char pad_[cacheline_length]; diff --git a/include/boost/fiber/detail/wrap.hpp b/include/boost/fiber/detail/wrap.hpp index 6cab305d..66e60222 100644 --- a/include/boost/fiber/detail/wrap.hpp +++ b/include/boost/fiber/detail/wrap.hpp @@ -40,9 +40,9 @@ private: public: wrapper( Fn1 && fn1, Fn2 && fn2, Tpl && tpl, boost::context::execution_context const& ctx) : - fn1_( std::move( fn1) ), - fn2_( std::move( fn2) ), - tpl_( std::move( tpl) ), + fn1_{ std::move( fn1) }, + fn2_{ std::move( fn2) }, + tpl_{ std::move( tpl) }, ctx_{ ctx } { } @@ -53,6 +53,7 @@ public: wrapper & operator=( wrapper && other) = default; void operator()( void * vp) { + // FIXME: use std::invoke() if available boost::context::detail::invoke( std::move( fn1_), fn2_, tpl_, ctx_, vp); @@ -63,11 +64,11 @@ template< typename Fn1, typename Fn2, typename Tpl > wrapper< Fn1, Fn2, Tpl > wrap( Fn1 && fn1, Fn2 && fn2, Tpl && tpl, boost::context::execution_context const& ctx) { - return wrapper< Fn1, Fn2, Tpl >( + return wrapper< Fn1, Fn2, Tpl >{ std::forward< Fn1 >( fn1), std::forward< Fn2 >( fn2), std::forward< Tpl >( tpl), - ctx); + ctx }; } #else template< typename Fn1, typename Fn2, typename Tpl > @@ -79,9 +80,9 @@ private: public: wrapper( Fn1 && fn1, Fn2 && fn2, Tpl && tpl) : - fn1_( std::move( fn1) ), - fn2_( std::move( fn2) ), - tpl_( std::move( tpl) ) { + fn1_{ std::move( fn1) }, + fn2_{ std::move( fn2) }, + tpl_{ std::move( tpl) } { } wrapper( wrapper const&) = delete; @@ -92,6 +93,7 @@ public: boost::context::continuation operator()( boost::context::continuation && c) { + // FIXME: use std::invoke() if available return boost::context::detail::invoke( std::move( fn1_), fn2_, @@ -103,10 +105,10 @@ public: template< typename Fn1, typename Fn2, typename Tpl > wrapper< Fn1, Fn2, Tpl > wrap( Fn1 && fn1, Fn2 && fn2, Tpl && tpl) { - return wrapper< Fn1, Fn2, Tpl >( + return wrapper< Fn1, Fn2, Tpl >{ std::forward< Fn1 >( fn1), std::forward< Fn2 >( fn2), - std::forward< Tpl >( tpl) ); + std::forward< Tpl >( tpl) }; } #endif diff --git a/include/boost/fiber/exceptions.hpp b/include/boost/fiber/exceptions.hpp index ddbf45ea..5a5f7e4f 100644 --- a/include/boost/fiber/exceptions.hpp +++ b/include/boost/fiber/exceptions.hpp @@ -28,15 +28,15 @@ namespace fibers { class fiber_error : public std::system_error { public: fiber_error( std::error_code ec) : - std::system_error( ec) { + std::system_error{ ec } { } fiber_error( std::error_code ec, const char * what_arg) : - std::system_error( ec, what_arg) { + std::system_error{ ec, what_arg } { } fiber_error( std::error_code ec, std::string const& what_arg) : - std::system_error( ec, what_arg) { + std::system_error{ ec, what_arg } { } virtual ~fiber_error() = default; @@ -45,15 +45,15 @@ public: class lock_error : public fiber_error { public: lock_error( std::error_code ec) : - fiber_error( ec) { + fiber_error{ ec } { } lock_error( std::error_code ec, const char * what_arg) : - fiber_error( ec, what_arg) { + fiber_error{ ec, what_arg } { } lock_error( std::error_code ec, std::string const& what_arg) : - fiber_error( ec, what_arg) { + fiber_error{ ec, what_arg } { } }; @@ -77,12 +77,12 @@ struct is_error_code_enum< boost::fibers::future_errc > : public true_type { inline std::error_code make_error_code( boost::fibers::future_errc e) noexcept { - return std::error_code( static_cast< int >( e), boost::fibers::future_category() ); + return std::error_code{ static_cast< int >( e), boost::fibers::future_category() }; } inline std::error_condition make_error_condition( boost::fibers::future_errc e) noexcept { - return std::error_condition( static_cast< int >( e), boost::fibers::future_category() ); + return std::error_condition{ static_cast< int >( e), boost::fibers::future_category() }; } } @@ -93,49 +93,49 @@ namespace fibers { class future_error : public fiber_error { public: future_error( std::error_code ec) : - fiber_error( ec) { + fiber_error{ ec } { } }; class future_uninitialized : public future_error { public: future_uninitialized() : - future_error( std::make_error_code( future_errc::no_state) ) { + future_error{ std::make_error_code( future_errc::no_state) } { } }; class future_already_retrieved : public future_error { public: future_already_retrieved() : - future_error( std::make_error_code( future_errc::future_already_retrieved) ) { + future_error{ std::make_error_code( future_errc::future_already_retrieved) } { } }; class broken_promise : public future_error { public: broken_promise() : - future_error( std::make_error_code( future_errc::broken_promise) ) { + future_error{ std::make_error_code( future_errc::broken_promise) } { } }; class promise_already_satisfied : public future_error { public: promise_already_satisfied() : - future_error( std::make_error_code( future_errc::promise_already_satisfied) ) { + future_error{ std::make_error_code( future_errc::promise_already_satisfied) } { } }; class promise_uninitialized : public future_error { public: promise_uninitialized() : - future_error( std::make_error_code( future_errc::no_state) ) { + future_error{ std::make_error_code( future_errc::no_state) } { } }; class packaged_task_uninitialized : public future_error { public: packaged_task_uninitialized() : - future_error( std::make_error_code( future_errc::no_state) ) { + future_error{ std::make_error_code( future_errc::no_state) } { } }; diff --git a/include/boost/fiber/fiber.hpp b/include/boost/fiber/fiber.hpp index 0fbc84ad..1508a9b5 100644 --- a/include/boost/fiber/fiber.hpp +++ b/include/boost/fiber/fiber.hpp @@ -49,7 +49,7 @@ private: public: typedef context::id id; - fiber() noexcept = default; + fiber() = default; template< typename Fn, typename ... Args, @@ -108,7 +108,9 @@ public: if ( joinable() ) { std::terminate(); } - if ( this == & other) return * this; + if ( this == & other) { + return * this; + } impl_.swap( other.impl_); return * this; } @@ -132,7 +134,7 @@ public: template< typename PROPS > PROPS & properties() { auto props = impl_->get_properties(); - BOOST_ASSERT_MSG(props, "fiber::properties not set"); + BOOST_ASSERT_MSG( props, "fiber::properties not set"); return dynamic_cast< PROPS & >( * props ); } }; diff --git a/include/boost/fiber/fss.hpp b/include/boost/fiber/fss.hpp index a578d40a..f65d7353 100644 --- a/include/boost/fiber/fss.hpp +++ b/include/boost/fiber/fss.hpp @@ -58,9 +58,9 @@ public: } ~fiber_specific_ptr() { - context * f = context::active(); - if ( nullptr != f) { - f->set_fss_data( + context * active_ctx = context::active(); + if ( nullptr != active_ctx) { + active_ctx->set_fss_data( this, cleanup_fn_, nullptr, true); } } diff --git a/include/boost/fiber/future/async.hpp b/include/boost/fiber/future/async.hpp index 28b06ab6..e68b2c28 100644 --- a/include/boost/fiber/future/async.hpp +++ b/include/boost/fiber/future/async.hpp @@ -33,11 +33,11 @@ future< async( Fn && fn, Args && ... args) { typedef typename std::result_of< typename std::decay< Fn >::type( typename std::decay< Args >::type ... ) - >::type result_t; + >::type result_type; - packaged_task< result_t( typename std::decay< Args >::type ... ) > pt{ + packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{ std::forward< Fn >( fn) }; - future< result_t > f{ pt.get_future() }; + future< result_type > f{ pt.get_future() }; fiber{ std::move( pt), std::forward< Args >( args) ... }.detach(); return f; } @@ -54,11 +54,11 @@ future< async( Policy policy, Fn && fn, Args && ... args) { typedef typename std::result_of< typename std::decay< Fn >::type( typename std::decay< Args >::type ... ) - >::type result_t; + >::type result_type; - packaged_task< result_t( typename std::decay< Args >::type ... ) > pt{ + packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{ std::forward< Fn >( fn) }; - future< result_t > f{ pt.get_future() }; + future< result_type > f{ pt.get_future() }; fiber{ policy, std::move( pt), std::forward< Args >( args) ... }.detach(); return f; } @@ -75,11 +75,11 @@ future< async( Policy policy, std::allocator_arg_t, StackAllocator salloc, Fn && fn, Args && ... args) { typedef typename std::result_of< typename std::decay< Fn >::type( typename std::decay< Args >::type ... ) - >::type result_t; + >::type result_type; - packaged_task< result_t( typename std::decay< Args >::type ... ) > pt{ + packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{ std::forward< Fn >( fn) }; - future< result_t > f{ pt.get_future() }; + future< result_type > f{ pt.get_future() }; fiber{ policy, std::allocator_arg, salloc, std::move( pt), std::forward< Args >( args) ... }.detach(); return f; @@ -97,11 +97,11 @@ future< async( Policy policy, std::allocator_arg_t, StackAllocator salloc, Allocator alloc, Fn && fn, Args && ... args) { typedef typename std::result_of< typename std::decay< Fn >::type( typename std::decay< Args >::type ... ) - >::type result_t; + >::type result_type; - packaged_task< result_t( typename std::decay< Args >::type ... ) > pt{ + packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{ std::allocator_arg, alloc, std::forward< Fn >( fn) }; - future< result_t > f{ pt.get_future() }; + future< result_type > f{ pt.get_future() }; fiber{ policy, std::allocator_arg, salloc, std::move( pt), std::forward< Args >( args) ... }.detach(); return f; diff --git a/include/boost/fiber/future/detail/shared_state.hpp b/include/boost/fiber/future/detail/shared_state.hpp index 5ec6858c..51ef7cef 100644 --- a/include/boost/fiber/future/detail/shared_state.hpp +++ b/include/boost/fiber/future/detail/shared_state.hpp @@ -109,34 +109,34 @@ public: shared_state_base & operator=( shared_state_base const&) = delete; void owner_destroyed() { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; owner_destroyed_( lk); } void set_exception( std::exception_ptr except) { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; set_exception_( except, lk); } std::exception_ptr get_exception_ptr() { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; return get_exception_ptr_( lk); } void wait() const { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; wait_( lk); } template< typename Rep, typename Period > future_status wait_for( std::chrono::duration< Rep, Period > const& timeout_duration) const { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; return wait_for_( lk, timeout_duration); } template< typename Clock, typename Duration > future_status wait_until( std::chrono::time_point< Clock, Duration > const& timeout_time) const { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; return wait_until_( lk, timeout_time); } @@ -161,18 +161,18 @@ private: void set_value_( R const& value, std::unique_lock< mutex > & lk) { BOOST_ASSERT( lk.owns_lock() ); if ( ready_) { - throw promise_already_satisfied(); + throw promise_already_satisfied{}; } - ::new ( static_cast< void * >( std::addressof( storage_) ) ) R( value); + ::new ( static_cast< void * >( std::addressof( storage_) ) ) R{ value }; mark_ready_and_notify_( lk); } void set_value_( R && value, std::unique_lock< mutex > & lk) { BOOST_ASSERT( lk.owns_lock() ); if ( ready_) { - throw promise_already_satisfied(); + throw promise_already_satisfied{}; } - ::new ( static_cast< void * >( std::addressof( storage_) ) ) R( std::move( value) ); + ::new ( static_cast< void * >( std::addressof( storage_) ) ) R{ std::move( value) }; mark_ready_and_notify_( lk); } @@ -186,7 +186,7 @@ private: } public: - typedef intrusive_ptr< shared_state > ptr_t; + typedef intrusive_ptr< shared_state > ptr_type; shared_state() = default; @@ -200,17 +200,17 @@ public: shared_state & operator=( shared_state const&) = delete; void set_value( R const& value) { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; set_value_( value, lk); } void set_value( R && value) { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; set_value_( std::move( value), lk); } R & get() { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; return get_( lk); } }; @@ -239,7 +239,7 @@ private: } public: - typedef intrusive_ptr< shared_state > ptr_t; + typedef intrusive_ptr< shared_state > ptr_type; shared_state() = default; @@ -249,12 +249,12 @@ public: shared_state & operator=( shared_state const&) = delete; void set_value( R & value) { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; set_value_( value, lk); } R & get() { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; return get_( lk); } }; @@ -281,7 +281,7 @@ private: } public: - typedef intrusive_ptr< shared_state > ptr_t; + typedef intrusive_ptr< shared_state > ptr_type; shared_state() = default; @@ -292,13 +292,13 @@ public: inline void set_value() { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; set_value_( lk); } inline void get() { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; get_( lk); } }; diff --git a/include/boost/fiber/future/detail/shared_state_object.hpp b/include/boost/fiber/future/detail/shared_state_object.hpp index dcdafef6..50e08a63 100644 --- a/include/boost/fiber/future/detail/shared_state_object.hpp +++ b/include/boost/fiber/future/detail/shared_state_object.hpp @@ -27,10 +27,11 @@ class shared_state_object : public shared_state< R > { public: typedef typename std::allocator_traits< Allocator >::template rebind_alloc< shared_state_object - > allocator_t; + > allocator_type; - shared_state_object( allocator_t const& alloc) : - shared_state< R >(), alloc_( alloc) { + shared_state_object( allocator_type const& alloc) : + shared_state< R >{}, + alloc_{ alloc } { } protected: @@ -39,10 +40,10 @@ protected: } private: - allocator_t alloc_; + allocator_type alloc_; - static void destroy_( allocator_t const& alloc, shared_state_object * p) noexcept { - allocator_t a{ alloc }; + static void destroy_( allocator_type const& alloc, shared_state_object * p) noexcept { + allocator_type a{ alloc }; a.destroy( p); a.deallocate( p, 1); } diff --git a/include/boost/fiber/future/detail/task_base.hpp b/include/boost/fiber/future/detail/task_base.hpp index 907e8204..83abd7e5 100644 --- a/include/boost/fiber/future/detail/task_base.hpp +++ b/include/boost/fiber/future/detail/task_base.hpp @@ -22,14 +22,14 @@ namespace detail { template< typename R, typename ... Args > struct task_base : public shared_state< R > { - typedef intrusive_ptr< task_base > ptr_t; + typedef intrusive_ptr< task_base > ptr_type; virtual ~task_base() { } virtual void run( Args && ... args) = 0; - virtual ptr_t reset() = 0; + virtual ptr_type reset() = 0; }; }}} diff --git a/include/boost/fiber/future/detail/task_object.hpp b/include/boost/fiber/future/detail/task_object.hpp index 37bfd3fd..df13777d 100644 --- a/include/boost/fiber/future/detail/task_object.hpp +++ b/include/boost/fiber/future/detail/task_object.hpp @@ -28,28 +28,29 @@ namespace detail { template< typename Fn, typename Allocator, typename R, typename ... Args > class task_object : public task_base< R, Args ... > { private: - typedef task_base< R, Args ... > base_t; + typedef task_base< R, Args ... > base_type; public: typedef typename std::allocator_traits< Allocator >::template rebind_alloc< task_object - > allocator_t; + > allocator_type; - task_object( allocator_t const& alloc, Fn const& fn) : - base_t(), - fn_( fn), - alloc_( alloc) { + task_object( allocator_type const& alloc, Fn const& fn) : + base_type{}, + fn_{ fn }, + alloc_{ alloc } { } - task_object( allocator_t const& alloc, Fn && fn) : - base_t(), - fn_( std::move( fn) ), - alloc_( alloc) { + task_object( allocator_type const& alloc, Fn && fn) : + base_type{}, + fn_{ std::move( fn) }, + alloc_{ alloc } { } void run( Args && ... args) override final { try { this->set_value( + // FIXME : use std::apply() if available boost::context::detail::apply( fn_, std::make_tuple( std::forward< Args >( args) ... ) ) ); } catch (...) { @@ -57,14 +58,14 @@ public: } } - typename base_t::ptr_t reset() override final { - typedef std::allocator_traits< allocator_t > traits_t; + typename base_type::ptr_type reset() override final { + typedef std::allocator_traits< allocator_type > traity_type; - typename traits_t::pointer ptr{ traits_t::allocate( alloc_, 1) }; + typename traity_type::pointer ptr{ traity_type::allocate( alloc_, 1) }; try { - traits_t::construct( alloc_, ptr, alloc_, std::move( fn_) ); + traity_type::construct( alloc_, ptr, alloc_, std::move( fn_) ); } catch (...) { - traits_t::deallocate( alloc_, ptr, 1); + traity_type::deallocate( alloc_, ptr, 1); throw; } return { convert( ptr) }; @@ -77,10 +78,10 @@ protected: private: Fn fn_; - allocator_t alloc_; + allocator_type alloc_; - static void destroy_( allocator_t const& alloc, task_object * p) noexcept { - allocator_t a{ alloc }; + static void destroy_( allocator_type const& alloc, task_object * p) noexcept { + allocator_type a{ alloc }; a.destroy( p); a.deallocate( p, 1); } @@ -89,27 +90,28 @@ private: template< typename Fn, typename Allocator, typename ... Args > class task_object< Fn, Allocator, void, Args ... > : public task_base< void, Args ... > { private: - typedef task_base< void, Args ... > base_t; + typedef task_base< void, Args ... > base_type; public: typedef typename Allocator::template rebind< task_object< Fn, Allocator, void, Args ... > - >::other allocator_t; + >::other allocator_type; - task_object( allocator_t const& alloc, Fn const& fn) : - base_t(), - fn_( fn), - alloc_( alloc) { + task_object( allocator_type const& alloc, Fn const& fn) : + base_type{}, + fn_{ fn }, + alloc_{ alloc } { } - task_object( allocator_t const& alloc, Fn && fn) : - base_t(), - fn_( std::move( fn) ), - alloc_( alloc) { + task_object( allocator_type const& alloc, Fn && fn) : + base_type{}, + fn_{ std::move( fn) }, + alloc_{ alloc } { } void run( Args && ... args) override final { try { + // FIXME : use std::apply() if available boost::context::detail::apply( fn_, std::make_tuple( std::forward< Args >( args) ... ) ); this->set_value(); @@ -118,14 +120,14 @@ public: } } - typename base_t::ptr_t reset() override final { - typedef std::allocator_traits< allocator_t > traits_t; + typename base_type::ptr_type reset() override final { + typedef std::allocator_traits< allocator_type > traity_type; - typename traits_t::pointer ptr{ traits_t::allocate( alloc_, 1) }; + typename traity_type::pointer ptr{ traity_type::allocate( alloc_, 1) }; try { - traits_t::construct( alloc_, ptr, alloc_, std::move( fn_) ); + traity_type::construct( alloc_, ptr, alloc_, std::move( fn_) ); } catch (...) { - traits_t::deallocate( alloc_, ptr, 1); + traity_type::deallocate( alloc_, ptr, 1); throw; } return { convert( ptr) }; @@ -138,10 +140,10 @@ protected: private: Fn fn_; - allocator_t alloc_; + allocator_type alloc_; - static void destroy_( allocator_t const& alloc, task_object * p) noexcept { - allocator_t a{ alloc }; + static void destroy_( allocator_type const& alloc, task_object * p) noexcept { + allocator_type a{ alloc }; a.destroy( p); a.deallocate( p, 1); } diff --git a/include/boost/fiber/future/future.hpp b/include/boost/fiber/future/future.hpp index d92820eb..5d4ad78a 100644 --- a/include/boost/fiber/future/future.hpp +++ b/include/boost/fiber/future/future.hpp @@ -24,13 +24,13 @@ namespace detail { template< typename R > struct future_base { - typedef typename shared_state< R >::ptr_t ptr_t; + typedef typename shared_state< R >::ptr_type ptr_type; - ptr_t state_{}; + ptr_type state_{}; - future_base() noexcept = default; + future_base() = default; - explicit future_base( ptr_t const& p) noexcept : + explicit future_base( ptr_type const& p) noexcept : state_{ p } { } @@ -46,15 +46,17 @@ struct future_base { } future_base & operator=( future_base const& other) noexcept { - if ( this == & other) return * this; - state_ = other.state_; + if ( this != & other) { + state_ = other.state_; + } return * this; } future_base & operator=( future_base && other) noexcept { - if ( this == & other) return * this; - state_ = other.state_; - other.state_.reset(); + if ( this != & other) { + state_ = other.state_; + other.state_.reset(); + } return * this; } @@ -107,128 +109,131 @@ class packaged_task; template< typename R > class future : private detail::future_base< R > { private: - typedef detail::future_base< R > base_t; + typedef detail::future_base< R > base_type; friend struct detail::promise_base< R >; friend class shared_future< R >; template< typename Signature > friend class packaged_task; - explicit future( typename base_t::ptr_t const& p) noexcept : - base_t{ p } { + explicit future( typename base_type::ptr_type const& p) noexcept : + base_type{ p } { } public: - future() noexcept = default; + future() = default; future( future const&) = delete; future & operator=( future const&) = delete; future( future && other) noexcept : - base_t{ std::move( other) } { + base_type{ std::move( other) } { } future & operator=( future && other) noexcept { - if ( this == & other) return * this; - base_t::operator=( std::move( other) ); + if ( this != & other) { + base_type::operator=( std::move( other) ); + } return * this; } shared_future< R > share(); R get() { - if ( ! base_t::valid() ) { + if ( ! base_type::valid() ) { throw future_uninitialized{}; } - typename base_t::ptr_t tmp{}; - tmp.swap( base_t::state_); + typename base_type::ptr_type tmp{}; + tmp.swap( base_type::state_); return std::move( tmp->get() ); } - using base_t::valid; - using base_t::get_exception_ptr; - using base_t::wait; - using base_t::wait_for; - using base_t::wait_until; + using base_type::valid; + using base_type::get_exception_ptr; + using base_type::wait; + using base_type::wait_for; + using base_type::wait_until; }; template< typename R > class future< R & > : private detail::future_base< R & > { private: - typedef detail::future_base< R & > base_t; + typedef detail::future_base< R & > base_type; friend struct detail::promise_base< R & >; friend class shared_future< R & >; template< typename Signature > friend class packaged_task; - explicit future( typename base_t::ptr_t const& p) noexcept : - base_t{ p } { + explicit future( typename base_type::ptr_type const& p) noexcept : + base_type{ p } { } public: - future() noexcept = default; + future() = default; future( future const&) = delete; future & operator=( future const&) = delete; future( future && other) noexcept : - base_t{ std::move( other) } { + base_type{ std::move( other) } { } future & operator=( future && other) noexcept { - if ( this == & other) return * this; - base_t::operator=( std::move( other) ); + if ( this != & other) { + base_type::operator=( std::move( other) ); + } return * this; } shared_future< R & > share(); R & get() { - if ( ! base_t::valid() ) { + if ( ! base_type::valid() ) { throw future_uninitialized{}; } - typename base_t::ptr_t tmp{}; - tmp.swap( base_t::state_); + typename base_type::ptr_type tmp{}; + tmp.swap( base_type::state_); return tmp->get(); } - using base_t::valid; - using base_t::get_exception_ptr; - using base_t::wait; - using base_t::wait_for; - using base_t::wait_until; + using base_type::valid; + using base_type::get_exception_ptr; + using base_type::wait; + using base_type::wait_for; + using base_type::wait_until; }; template<> class future< void > : private detail::future_base< void > { private: - typedef detail::future_base< void > base_t; + typedef detail::future_base< void > base_type; friend struct detail::promise_base< void >; friend class shared_future< void >; template< typename Signature > friend class packaged_task; - explicit future( base_t::ptr_t const& p) noexcept : - base_t{ p } { + explicit future( base_type::ptr_type const& p) noexcept : + base_type{ p } { } public: - future() noexcept = default; + future() = default; future( future const&) = delete; future & operator=( future const&) = delete; inline future( future && other) noexcept : - base_t{ std::move( other) } { + base_type{ std::move( other) } { } inline future & operator=( future && other) noexcept { - if ( this == & other) return * this; - base_t::operator=( std::move( other) ); + if ( this != & other) { + base_type::operator=( std::move( other) ); + } return * this; } @@ -236,62 +241,64 @@ public: inline void get() { - if ( ! base_t::valid() ) { + if ( ! base_type::valid() ) { throw future_uninitialized{}; } - base_t::ptr_t tmp{}; - tmp.swap( base_t::state_); + base_type::ptr_type tmp{}; + tmp.swap( base_type::state_); tmp->get(); } - using base_t::valid; - using base_t::get_exception_ptr; - using base_t::wait; - using base_t::wait_for; - using base_t::wait_until; + using base_type::valid; + using base_type::get_exception_ptr; + using base_type::wait; + using base_type::wait_for; + using base_type::wait_until; }; template< typename R > class shared_future : private detail::future_base< R > { private: - typedef detail::future_base< R > base_t; + typedef detail::future_base< R > base_type; - explicit shared_future( typename base_t::ptr_t const& p) noexcept : - base_t{ p } { + explicit shared_future( typename base_type::ptr_type const& p) noexcept : + base_type{ p } { } public: - shared_future() noexcept = default; + shared_future() = default; ~shared_future() = default; shared_future( shared_future const& other) : - base_t{ other } { + base_type{ other } { } shared_future( shared_future && other) noexcept : - base_t{ std::move( other) } { + base_type{ std::move( other) } { } shared_future( future< R > && other) noexcept : - base_t{ std::move( other) } { + base_type{ std::move( other) } { } shared_future & operator=( shared_future const& other) noexcept { - if ( this == & other) return * this; - base_t::operator=( other); + if ( this != & other) { + base_type::operator=( other); + } return * this; } shared_future & operator=( shared_future && other) noexcept { - if ( this == & other) return * this; - base_t::operator=( std::move( other) ); + if ( this != & other) { + base_type::operator=( std::move( other) ); + } return * this; } shared_future & operator=( future< R > && other) noexcept { - base_t::operator=( std::move( other) ); + base_type::operator=( std::move( other) ); return * this; } @@ -299,56 +306,58 @@ public: if ( ! valid() ) { throw future_uninitialized{}; } - return base_t::state_->get(); + return base_type::state_->get(); } - using base_t::valid; - using base_t::get_exception_ptr; - using base_t::wait; - using base_t::wait_for; - using base_t::wait_until; + using base_type::valid; + using base_type::get_exception_ptr; + using base_type::wait; + using base_type::wait_for; + using base_type::wait_until; }; template< typename R > class shared_future< R & > : private detail::future_base< R & > { private: - typedef detail::future_base< R & > base_t; + typedef detail::future_base< R & > base_type; - explicit shared_future( typename base_t::ptr_t const& p) noexcept : - base_t{ p } { + explicit shared_future( typename base_type::ptr_type const& p) noexcept : + base_type{ p } { } public: - shared_future() noexcept = default; + shared_future() = default; ~shared_future() = default; shared_future( shared_future const& other) : - base_t{ other } { + base_type{ other } { } shared_future( shared_future && other) noexcept : - base_t{ std::move( other) } { + base_type{ std::move( other) } { } shared_future( future< R & > && other) noexcept : - base_t{ std::move( other) } { + base_type{ std::move( other) } { } shared_future & operator=( shared_future const& other) noexcept { - if ( this == & other) return * this; - base_t::operator=( other); + if ( this != & other) { + base_type::operator=( other); + } return * this; } shared_future & operator=( shared_future && other) noexcept { - if ( this == & other) return * this; - base_t::operator=( std::move( other) ); + if ( this != & other) { + base_type::operator=( std::move( other) ); + } return * this; } shared_future & operator=( future< R & > && other) noexcept { - base_t::operator=( std::move( other) ); + base_type::operator=( std::move( other) ); return * this; } @@ -356,62 +365,64 @@ public: if ( ! valid() ) { throw future_uninitialized{}; } - return base_t::state_->get(); + return base_type::state_->get(); } - using base_t::valid; - using base_t::get_exception_ptr; - using base_t::wait; - using base_t::wait_for; - using base_t::wait_until; + using base_type::valid; + using base_type::get_exception_ptr; + using base_type::wait; + using base_type::wait_for; + using base_type::wait_until; }; template<> class shared_future< void > : private detail::future_base< void > { private: - typedef detail::future_base< void > base_t; + typedef detail::future_base< void > base_type; - explicit shared_future( base_t::ptr_t const& p) noexcept : - base_t{ p } { + explicit shared_future( base_type::ptr_type const& p) noexcept : + base_type{ p } { } public: - shared_future() noexcept = default; + shared_future() = default; ~shared_future() = default; inline shared_future( shared_future const& other) : - base_t{ other } { + base_type{ other } { } inline shared_future( shared_future && other) noexcept : - base_t{ std::move( other) } { + base_type{ std::move( other) } { } inline shared_future( future< void > && other) noexcept : - base_t{ std::move( other) } { + base_type{ std::move( other) } { } inline shared_future & operator=( shared_future const& other) noexcept { - if ( this == & other) return * this; - base_t::operator=( other); + if ( this != & other) { + base_type::operator=( other); + } return * this; } inline shared_future & operator=( shared_future && other) noexcept { - if ( this == & other) return * this; - base_t::operator=( std::move( other) ); + if ( this != & other) { + base_type::operator=( std::move( other) ); + } return * this; } inline shared_future & operator=( future< void > && other) noexcept { - base_t::operator=( std::move( other) ); + base_type::operator=( std::move( other) ); return * this; } @@ -420,21 +431,21 @@ public: if ( ! valid() ) { throw future_uninitialized{}; } - base_t::state_->get(); + base_type::state_->get(); } - using base_t::valid; - using base_t::get_exception_ptr; - using base_t::wait; - using base_t::wait_for; - using base_t::wait_until; + using base_type::valid; + using base_type::get_exception_ptr; + using base_type::wait; + using base_type::wait_for; + using base_type::wait_until; }; template< typename R > shared_future< R > future< R >::share() { - if ( ! base_t::valid() ) { + if ( ! base_type::valid() ) { throw future_uninitialized{}; } return shared_future< R >{ std::move( * this) }; @@ -443,7 +454,7 @@ future< R >::share() { template< typename R > shared_future< R & > future< R & >::share() { - if ( ! base_t::valid() ) { + if ( ! base_type::valid() ) { throw future_uninitialized{}; } return shared_future< R & >{ std::move( * this) }; @@ -452,7 +463,7 @@ future< R & >::share() { inline shared_future< void > future< void >::share() { - if ( ! base_t::valid() ) { + if ( ! base_type::valid() ) { throw future_uninitialized{}; } return shared_future< void >{ std::move( * this) }; diff --git a/include/boost/fiber/future/packaged_task.hpp b/include/boost/fiber/future/packaged_task.hpp index 7ea16bfe..31838ee4 100644 --- a/include/boost/fiber/future/packaged_task.hpp +++ b/include/boost/fiber/future/packaged_task.hpp @@ -30,13 +30,13 @@ class packaged_task; template< typename R, typename ... Args > class packaged_task< R( Args ... ) > { private: - typedef typename detail::task_base< R, Args ... >::ptr_t ptr_t; + typedef typename detail::task_base< R, Args ... >::ptr_type ptr_type; bool obtained_{ false }; - ptr_t task_{}; + ptr_type task_{}; public: - constexpr packaged_task() noexcept = default; + packaged_task() = default; template< typename Fn, typename = detail::disable_overload< packaged_task, Fn > @@ -53,17 +53,17 @@ public: explicit packaged_task( std::allocator_arg_t, Allocator const& alloc, Fn && fn) { typedef detail::task_object< typename std::decay< Fn >::type, Allocator, R, Args ... - > object_t; + > object_type; typedef std::allocator_traits< - typename object_t::allocator_t - > traits_t; + typename object_type::allocator_type + > traits_type; - typename object_t::allocator_t a{ alloc }; - typename traits_t::pointer ptr{ traits_t::allocate( a, 1) }; + typename object_type::allocator_type a{ alloc }; + typename traits_type::pointer ptr{ traits_type::allocate( a, 1) }; try { - traits_t::construct( a, ptr, a, std::forward< Fn >( fn) ); + traits_type::construct( a, ptr, a, std::forward< Fn >( fn) ); } catch (...) { - traits_t::deallocate( a, ptr, 1); + traits_type::deallocate( a, ptr, 1); throw; } task_.reset( convert( ptr) ); @@ -85,9 +85,10 @@ public: } packaged_task & operator=( packaged_task && other) noexcept { - if ( this == & other) return * this; - packaged_task tmp{ std::move( other) }; - swap( tmp); + if ( this != & other) { + packaged_task tmp{ std::move( other) }; + swap( tmp); + } return * this; } diff --git a/include/boost/fiber/future/promise.hpp b/include/boost/fiber/future/promise.hpp index 278c8397..83ba63fb 100644 --- a/include/boost/fiber/future/promise.hpp +++ b/include/boost/fiber/future/promise.hpp @@ -25,10 +25,10 @@ namespace detail { template< typename R > struct promise_base { - typedef typename shared_state< R >::ptr_t ptr_t; + typedef typename shared_state< R >::ptr_type ptr_type; bool obtained_{ false }; - ptr_t future_{}; + ptr_type future_{}; promise_base() : promise_base{ std::allocator_arg, std::allocator< promise_base >{} } { @@ -36,15 +36,15 @@ struct promise_base { template< typename Allocator > promise_base( std::allocator_arg_t, Allocator alloc) { - typedef detail::shared_state_object< R, Allocator > object_t; - typedef std::allocator_traits< typename object_t::allocator_t > traits_t; - typename object_t::allocator_t a{ alloc }; - typename traits_t::pointer ptr{ traits_t::allocate( a, 1) }; + typedef detail::shared_state_object< R, Allocator > object_type; + typedef std::allocator_traits< typename object_type::allocator_type > traits_type; + typename object_type::allocator_type a{ alloc }; + typename traits_type::pointer ptr{ traits_type::allocate( a, 1) }; try { - traits_t::construct( a, ptr, a); + traits_type::construct( a, ptr, a); } catch (...) { - traits_t::deallocate( a, ptr, 1); + traits_type::deallocate( a, ptr, 1); throw; } future_.reset( convert( ptr) ); @@ -66,9 +66,10 @@ struct promise_base { } promise_base & operator=( promise_base && other) noexcept { - if ( this == & other) return * this; - promise_base tmp{ std::move( other) }; - swap( tmp); + if ( this != & other) { + promise_base tmp{ std::move( other) }; + swap( tmp); + } return * this; } @@ -101,14 +102,14 @@ struct promise_base { template< typename R > class promise : private detail::promise_base< R > { private: - typedef detail::promise_base< R > base_t; + typedef detail::promise_base< R > base_type; public: promise() = default; template< typename Allocator > promise( std::allocator_arg_t, Allocator alloc) : - base_t{ std::allocator_arg, alloc } { + base_type{ std::allocator_arg, alloc } { } promise( promise const&) = delete; @@ -118,38 +119,38 @@ public: promise & operator=( promise && other) = default; void set_value( R const& value) { - if ( ! base_t::future_) { + if ( ! base_type::future_) { throw promise_uninitialized{}; } - base_t::future_->set_value( value); + base_type::future_->set_value( value); } void set_value( R && value) { - if ( ! base_t::future_) { + if ( ! base_type::future_) { throw promise_uninitialized{}; } - base_t::future_->set_value( std::move( value) ); + base_type::future_->set_value( std::move( value) ); } void swap( promise & other) noexcept { - base_t::swap( other); + base_type::swap( other); } - using base_t::get_future; - using base_t::set_exception; + using base_type::get_future; + using base_type::set_exception; }; template< typename R > class promise< R & > : private detail::promise_base< R & > { private: - typedef detail::promise_base< R & > base_t; + typedef detail::promise_base< R & > base_type; public: promise() = default; template< typename Allocator > promise( std::allocator_arg_t, Allocator alloc) : - base_t{ std::allocator_arg, alloc } { + base_type{ std::allocator_arg, alloc } { } promise( promise const&) = delete; @@ -159,31 +160,31 @@ public: promise & operator=( promise && other) noexcept = default; void set_value( R & value) { - if ( ! base_t::future_) { + if ( ! base_type::future_) { throw promise_uninitialized{}; } - base_t::future_->set_value( value); + base_type::future_->set_value( value); } void swap( promise & other) noexcept { - base_t::swap( other); + base_type::swap( other); } - using base_t::get_future; - using base_t::set_exception; + using base_type::get_future; + using base_type::set_exception; }; template<> class promise< void > : private detail::promise_base< void > { private: - typedef detail::promise_base< void > base_t; + typedef detail::promise_base< void > base_type; public: promise() = default; template< typename Allocator > promise( std::allocator_arg_t, Allocator alloc) : - base_t{ std::allocator_arg, alloc } { + base_type{ std::allocator_arg, alloc } { } promise( promise const&) = delete; @@ -194,19 +195,19 @@ public: inline void set_value() { - if ( ! base_t::future_) { + if ( ! base_type::future_) { throw promise_uninitialized{}; } - base_t::future_->set_value(); + base_type::future_->set_value(); } inline void swap( promise & other) noexcept { - base_t::swap( other); + base_type::swap( other); } - using base_t::get_future; - using base_t::set_exception; + using base_type::get_future; + using base_type::set_exception; }; template< typename R > diff --git a/include/boost/fiber/mutex.hpp b/include/boost/fiber/mutex.hpp index b56e9680..d18e1277 100644 --- a/include/boost/fiber/mutex.hpp +++ b/include/boost/fiber/mutex.hpp @@ -33,10 +33,10 @@ class BOOST_FIBERS_DECL mutex { private: friend class condition_variable; - typedef context::wait_queue_t wait_queue_t; + typedef context::wait_queue_t wait_queue_type; context * owner_{ nullptr }; - wait_queue_t wait_queue_{}; + wait_queue_type wait_queue_{}; detail::spinlock wait_queue_splk_{}; public: diff --git a/include/boost/fiber/operations.hpp b/include/boost/fiber/operations.hpp index 490ba462..95ab9ba4 100644 --- a/include/boost/fiber/operations.hpp +++ b/include/boost/fiber/operations.hpp @@ -35,8 +35,7 @@ void yield() noexcept { template< typename Clock, typename Duration > void sleep_until( std::chrono::time_point< Clock, Duration > const& sleep_time_) { - std::chrono::steady_clock::time_point sleep_time( - boost::fibers::detail::convert( sleep_time_) ); + std::chrono::steady_clock::time_point sleep_time = boost::fibers::detail::convert( sleep_time_); fibers::context::active()->wait_until( sleep_time); } @@ -48,8 +47,7 @@ void sleep_for( std::chrono::duration< Rep, Period > const& timeout_duration) { template< typename PROPS > PROPS & properties() { - fibers::fiber_properties * props = - fibers::context::active()->get_properties(); + fibers::fiber_properties * props = fibers::context::active()->get_properties(); if ( ! props) { // props could be nullptr if the thread's main fiber has not yet // yielded (not yet passed through algorithm_with_properties:: @@ -61,7 +59,7 @@ PROPS & properties() { props = fibers::context::active()->get_properties(); // Could still be hosed if the running manager isn't a subclass of // algorithm_with_properties. - BOOST_ASSERT_MSG(props, "this_fiber::properties not set"); + BOOST_ASSERT_MSG( props, "this_fiber::properties not set"); } return dynamic_cast< PROPS & >( * props ); } diff --git a/include/boost/fiber/recursive_mutex.hpp b/include/boost/fiber/recursive_mutex.hpp index 66fb5aa7..5201092f 100644 --- a/include/boost/fiber/recursive_mutex.hpp +++ b/include/boost/fiber/recursive_mutex.hpp @@ -37,11 +37,11 @@ class BOOST_FIBERS_DECL recursive_mutex { private: friend class condition_variable; - typedef context::wait_queue_t wait_queue_t; + typedef context::wait_queue_t wait_queue_type; context * owner_{ nullptr }; std::size_t count_{ 0 }; - wait_queue_t wait_queue_{}; + wait_queue_type wait_queue_{}; detail::spinlock wait_queue_splk_{}; public: diff --git a/include/boost/fiber/recursive_timed_mutex.hpp b/include/boost/fiber/recursive_timed_mutex.hpp index 2852252c..a0473437 100644 --- a/include/boost/fiber/recursive_timed_mutex.hpp +++ b/include/boost/fiber/recursive_timed_mutex.hpp @@ -39,11 +39,11 @@ class BOOST_FIBERS_DECL recursive_timed_mutex { private: friend class condition_variable; - typedef context::wait_queue_t wait_queue_t; + typedef context::wait_queue_t wait_queue_type; context * owner_{ nullptr }; std::size_t count_{ 0 }; - wait_queue_t wait_queue_{}; + wait_queue_type wait_queue_{}; detail::spinlock wait_queue_splk_{}; bool try_lock_until_( std::chrono::steady_clock::time_point const& timeout_time) noexcept; @@ -66,8 +66,7 @@ public: template< typename Clock, typename Duration > bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time_) { - std::chrono::steady_clock::time_point timeout_time( - detail::convert( timeout_time_) ); + std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); return try_lock_until_( timeout_time); } diff --git a/include/boost/fiber/scheduler.hpp b/include/boost/fiber/scheduler.hpp index 50179e07..b5fe15a8 100644 --- a/include/boost/fiber/scheduler.hpp +++ b/include/boost/fiber/scheduler.hpp @@ -53,24 +53,24 @@ public: context, intrusive::member_hook< context, detail::ready_hook, & context::ready_hook_ >, - intrusive::constant_time_size< false > > ready_queue_t; + intrusive::constant_time_size< false > > ready_queue_type; private: typedef intrusive::multiset< context, intrusive::member_hook< context, detail::sleep_hook, & context::sleep_hook_ >, intrusive::constant_time_size< false >, - intrusive::compare< timepoint_less > > sleep_queue_t; + intrusive::compare< timepoint_less > > sleep_queue_type; typedef intrusive::list< context, intrusive::member_hook< context, detail::terminated_hook, & context::terminated_hook_ >, - intrusive::constant_time_size< false > > terminated_queue_t; + intrusive::constant_time_size< false > > terminated_queue_type; typedef intrusive::list< context, intrusive::member_hook< context, detail::worker_hook, & context::worker_hook_ >, - intrusive::constant_time_size< false > > worker_queue_t; + intrusive::constant_time_size< false > > worker_queue_type; std::unique_ptr< algo::algorithm > algo_; context * main_ctx_{ nullptr }; @@ -78,9 +78,9 @@ private: // worker-queue contains all context' mananged by this scheduler // except main-context and dispatcher-context // unlink happens on destruction of a context - worker_queue_t worker_queue_{}; + worker_queue_type worker_queue_{}; // terminated-queue contains context' which have been terminated - terminated_queue_t terminated_queue_{}; + terminated_queue_type terminated_queue_{}; #if ! defined(BOOST_FIBERS_NO_ATOMICS) // remote ready-queue contains context' signaled by schedulers // running in other threads @@ -88,7 +88,7 @@ private: // sleep-queue contains context' which have been called #endif // scheduler::wait_until() - sleep_queue_t sleep_queue_{}; + sleep_queue_type sleep_queue_{}; bool shutdown_{ false }; void release_terminated_() noexcept; diff --git a/include/boost/fiber/timed_mutex.hpp b/include/boost/fiber/timed_mutex.hpp index ed6dbd45..e2a6949f 100644 --- a/include/boost/fiber/timed_mutex.hpp +++ b/include/boost/fiber/timed_mutex.hpp @@ -35,10 +35,10 @@ class BOOST_FIBERS_DECL timed_mutex { private: friend class condition_variable; - typedef context::wait_queue_t wait_queue_t; + typedef context::wait_queue_t wait_queue_type; context * owner_{ nullptr }; - wait_queue_t wait_queue_{}; + wait_queue_type wait_queue_{}; detail::spinlock wait_queue_splk_{}; bool try_lock_until_( std::chrono::steady_clock::time_point const& timeout_time) noexcept; @@ -60,8 +60,7 @@ public: template< typename Clock, typename Duration > bool try_lock_until( std::chrono::time_point< Clock, Duration > const& timeout_time_) { - std::chrono::steady_clock::time_point timeout_time( - detail::convert( timeout_time_) ); + std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); return try_lock_until_( timeout_time); } diff --git a/include/boost/fiber/unbuffered_channel.hpp b/include/boost/fiber/unbuffered_channel.hpp index 5e49158d..6f843eba 100644 --- a/include/boost/fiber/unbuffered_channel.hpp +++ b/include/boost/fiber/unbuffered_channel.hpp @@ -68,7 +68,7 @@ private: bool try_push_( slot * own_slot) { for (;;) { - slot * s{ slot_.load( std::memory_order_acquire) }; + slot * s = slot_.load( std::memory_order_acquire); if ( nullptr == s) { if ( ! slot_.compare_exchange_strong( s, own_slot, std::memory_order_acq_rel) ) { continue; @@ -81,9 +81,9 @@ private: } slot * try_pop_() { - slot * nil_slot{ nullptr }; + slot * nil_slot = nullptr; for (;;) { - slot * s{ slot_.load( std::memory_order_acquire) }; + slot * s = slot_.load( std::memory_order_acquire); if ( nullptr != s) { if ( ! slot_.compare_exchange_strong( s, nil_slot, std::memory_order_acq_rel) ) { continue;} @@ -97,7 +97,7 @@ public: ~unbuffered_channel() { close(); - slot * s{ nullptr }; + slot * s = nullptr; if ( nullptr != ( s = try_pop_() ) ) { BOOST_ASSERT( nullptr != s); BOOST_ASSERT( nullptr != s->ctx); @@ -114,26 +114,26 @@ public: } void close() noexcept { - context * ctx{ context::active() }; + context * active_ctx = context::active(); detail::spinlock_lock lk{ splk_ }; closed_.store( true, std::memory_order_release); // notify all waiting producers while ( ! waiting_producers_.empty() ) { - context * producer_ctx{ & waiting_producers_.front() }; + context * producer_ctx = & waiting_producers_.front(); waiting_producers_.pop_front(); - ctx->set_ready( producer_ctx); + active_ctx->set_ready( producer_ctx); } // notify all waiting consumers while ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } } channel_op_status push( value_type const& value) { - context * ctx{ context::active() }; - slot s{ value, ctx }; + context * active_ctx = context::active(); + slot s{ value, active_ctx }; for (;;) { if ( is_closed() ) { return channel_op_status::closed; @@ -142,16 +142,15 @@ public: detail::spinlock_lock lk{ splk_ }; // notify one waiting consumer if ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } // suspend till value has been consumed - ctx->suspend( lk); + active_ctx->suspend( lk); // resumed, value has been consumed return channel_op_status::success; } else { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -159,17 +158,17 @@ public: if ( is_empty_() ) { continue; } - ctx->wait_link( waiting_producers_); + active_ctx->wait_link( waiting_producers_); // suspend this producer - ctx->suspend( lk); + active_ctx->suspend( lk); // resumed, slot mabye free } } } channel_op_status push( value_type && value) { - context * ctx{ context::active() }; - slot s{ std::move( value), ctx }; + context * active_ctx = context::active(); + slot s{ std::move( value), active_ctx }; for (;;) { if ( is_closed() ) { return channel_op_status::closed; @@ -178,16 +177,15 @@ public: detail::spinlock_lock lk{ splk_ }; // notify one waiting consumer if ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } // suspend till value has been consumed - ctx->suspend( lk); + active_ctx->suspend( lk); // resumed, value has been consumed return channel_op_status::success; } else { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -195,9 +193,9 @@ public: if ( is_empty_() ) { continue; } - ctx->wait_link( waiting_producers_); + active_ctx->wait_link( waiting_producers_); // suspend this producer - ctx->suspend( lk); + active_ctx->suspend( lk); // resumed, slot mabye free } } @@ -220,9 +218,9 @@ public: template< typename Clock, typename Duration > channel_op_status push_wait_until( value_type const& value, std::chrono::time_point< Clock, Duration > const& timeout_time_) { - std::chrono::steady_clock::time_point timeout_time( detail::convert( timeout_time_) ); - context * ctx{ context::active() }; - slot s{ value, ctx }; + context * active_ctx = context::active(); + slot s{ value, active_ctx }; + std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); for (;;) { if ( is_closed() ) { return channel_op_status::closed; @@ -231,14 +229,14 @@ public: detail::spinlock_lock lk{ splk_ }; // notify one waiting consumer if ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } // suspend this producer - if ( ! ctx->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // clear slot - slot * nil_slot{ nullptr }, * own_slot{ & s }; + slot * nil_slot = nullptr, * own_slot = & s; slot_.compare_exchange_strong( own_slot, nil_slot, std::memory_order_acq_rel); // resumed, value has not been consumed return channel_op_status::timeout; @@ -246,7 +244,6 @@ public: // resumed, value has been consumed return channel_op_status::success; } else { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -254,13 +251,13 @@ public: if ( is_empty_() ) { continue; } - ctx->wait_link( waiting_producers_); + active_ctx->wait_link( waiting_producers_); // suspend this producer - if ( ! ctx->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // relock local lk lk.lock(); // remove from waiting-queue - ctx->wait_unlink(); + active_ctx->wait_unlink(); return channel_op_status::timeout; } // resumed, slot maybe free @@ -271,9 +268,9 @@ public: template< typename Clock, typename Duration > channel_op_status push_wait_until( value_type && value, std::chrono::time_point< Clock, Duration > const& timeout_time_) { - std::chrono::steady_clock::time_point timeout_time( detail::convert( timeout_time_) ); - context * ctx{ context::active() }; - slot s{ std::move( value), ctx }; + context * active_ctx = context::active(); + slot s{ std::move( value), active_ctx }; + std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); for (;;) { if ( is_closed() ) { return channel_op_status::closed; @@ -282,14 +279,14 @@ public: detail::spinlock_lock lk{ splk_ }; // notify one waiting consumer if ( ! waiting_consumers_.empty() ) { - context * consumer_ctx{ & waiting_consumers_.front() }; + context * consumer_ctx = & waiting_consumers_.front(); waiting_consumers_.pop_front(); - ctx->set_ready( consumer_ctx); + active_ctx->set_ready( consumer_ctx); } // suspend this producer - if ( ! ctx->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // clear slot - slot * nil_slot{ nullptr }, * own_slot{ & s }; + slot * nil_slot = nullptr, * own_slot = & s; slot_.compare_exchange_strong( own_slot, nil_slot, std::memory_order_acq_rel); // resumed, value has not been consumed return channel_op_status::timeout; @@ -297,7 +294,6 @@ public: // resumed, value has been consumed return channel_op_status::success; } else { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -305,13 +301,13 @@ public: if ( is_empty_() ) { continue; } - ctx->wait_link( waiting_producers_); + active_ctx->wait_link( waiting_producers_); // suspend this producer - if ( ! ctx->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // relock local lk lk.lock(); // remove from waiting-queue - ctx->wait_unlink(); + active_ctx->wait_unlink(); return channel_op_status::timeout; } // resumed, slot maybe free @@ -320,27 +316,26 @@ public: } channel_op_status pop( value_type & value) { - context * ctx{ context::active() }; - slot * s{ nullptr }; + context * active_ctx = context::active(); + slot * s = nullptr; for (;;) { if ( nullptr != ( s = try_pop_() ) ) { { detail::spinlock_lock lk{ splk_ }; // notify one waiting producer if ( ! waiting_producers_.empty() ) { - context * producer_ctx{ & waiting_producers_.front() }; + context * producer_ctx = & waiting_producers_.front(); waiting_producers_.pop_front(); lk.unlock(); - ctx->set_ready( producer_ctx); + active_ctx->set_ready( producer_ctx); } } // consume value value = std::move( s->value); // resume suspended producer - ctx->set_ready( s->ctx); + active_ctx->set_ready( s->ctx); return channel_op_status::success; } else { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -348,36 +343,35 @@ public: if ( ! is_empty_() ) { continue; } - ctx->wait_link( waiting_consumers_); + active_ctx->wait_link( waiting_consumers_); // suspend this consumer - ctx->suspend( lk); + active_ctx->suspend( lk); // resumed, slot mabye set } } } value_type value_pop() { - context * ctx{ context::active() }; - slot * s{ nullptr }; + context * active_ctx = context::active(); + slot * s = nullptr; for (;;) { if ( nullptr != ( s = try_pop_() ) ) { { detail::spinlock_lock lk{ splk_ }; // notify one waiting producer if ( ! waiting_producers_.empty() ) { - context * producer_ctx{ & waiting_producers_.front() }; + context * producer_ctx = & waiting_producers_.front(); waiting_producers_.pop_front(); lk.unlock(); - ctx->set_ready( producer_ctx); + active_ctx->set_ready( producer_ctx); } } // consume value value_type value{ std::move( s->value) }; // resume suspended producer - ctx->set_ready( s->ctx); + active_ctx->set_ready( s->ctx); return std::move( value); } else { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { throw fiber_error{ @@ -387,9 +381,9 @@ public: if ( ! is_empty_() ) { continue; } - ctx->wait_link( waiting_consumers_); + active_ctx->wait_link( waiting_consumers_); // suspend this consumer - ctx->suspend( lk); + active_ctx->suspend( lk); // resumed, slot mabye set } } @@ -405,28 +399,27 @@ public: template< typename Clock, typename Duration > channel_op_status pop_wait_until( value_type & value, std::chrono::time_point< Clock, Duration > const& timeout_time_) { - std::chrono::steady_clock::time_point timeout_time( detail::convert( timeout_time_) ); - context * ctx{ context::active() }; - slot * s{ nullptr }; + context * active_ctx = context::active(); + slot * s = nullptr; + std::chrono::steady_clock::time_point timeout_time = detail::convert( timeout_time_); for (;;) { if ( nullptr != ( s = try_pop_() ) ) { { detail::spinlock_lock lk{ splk_ }; // notify one waiting producer if ( ! waiting_producers_.empty() ) { - context * producer_ctx{ & waiting_producers_.front() }; + context * producer_ctx = & waiting_producers_.front(); waiting_producers_.pop_front(); lk.unlock(); - ctx->set_ready( producer_ctx); + active_ctx->set_ready( producer_ctx); } } // consume value value = std::move( s->value); // resume suspended producer - ctx->set_ready( s->ctx); + active_ctx->set_ready( s->ctx); return channel_op_status::success; } else { - BOOST_ASSERT( ! ctx->wait_is_linked() ); detail::spinlock_lock lk{ splk_ }; if ( is_closed() ) { return channel_op_status::closed; @@ -434,13 +427,13 @@ public: if ( ! is_empty_() ) { continue; } - ctx->wait_link( waiting_consumers_); + active_ctx->wait_link( waiting_consumers_); // suspend this consumer - if ( ! ctx->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // relock local lk lk.lock(); // remove from waiting-queue - ctx->wait_unlink(); + active_ctx->wait_unlink(); return channel_op_status::timeout; } } @@ -451,8 +444,8 @@ public: private: typedef typename std::aligned_storage< sizeof( value_type), alignof( value_type) >::type storage_type; - unbuffered_channel * chan_{ nullptr }; - storage_type storage_; + unbuffered_channel * chan_{ nullptr }; + storage_type storage_; void increment_() { BOOST_ASSERT( nullptr != chan_); diff --git a/src/algo/round_robin.cpp b/src/algo/round_robin.cpp index ea69a8f2..beed7a96 100644 --- a/src/algo/round_robin.cpp +++ b/src/algo/round_robin.cpp @@ -26,7 +26,7 @@ round_robin::awakened( context * ctx) noexcept { context * round_robin::pick_next() noexcept { - context * victim{ nullptr }; + context * victim = nullptr; if ( ! rqueue_.empty() ) { victim = & rqueue_.front(); rqueue_.pop_front(); @@ -44,11 +44,11 @@ round_robin::has_ready_fibers() const noexcept { void round_robin::suspend_until( std::chrono::steady_clock::time_point const& time_point) noexcept { if ( (std::chrono::steady_clock::time_point::max)() == time_point) { - std::unique_lock< std::mutex > lk( mtx_); + std::unique_lock< std::mutex > lk{ mtx_ }; cnd_.wait( lk, [&](){ return flag_; }); flag_ = false; } else { - std::unique_lock< std::mutex > lk( mtx_); + std::unique_lock< std::mutex > lk{ mtx_ }; cnd_.wait_until( lk, time_point, [&](){ return flag_; }); flag_ = false; } @@ -56,7 +56,7 @@ round_robin::suspend_until( std::chrono::steady_clock::time_point const& time_po void round_robin::notify() noexcept { - std::unique_lock< std::mutex > lk( mtx_); + std::unique_lock< std::mutex > lk{ mtx_ }; flag_ = true; lk.unlock(); cnd_.notify_all(); diff --git a/src/algo/shared_work.cpp b/src/algo/shared_work.cpp index 1790a6c3..a055c1e9 100644 --- a/src/algo/shared_work.cpp +++ b/src/algo/shared_work.cpp @@ -29,7 +29,7 @@ shared_work::awakened( context * ctx) noexcept { lqueue_.push_back( * ctx); } else { ctx->detach(); - std::unique_lock< std::mutex > lk( rqueue_mtx_); /*< + std::unique_lock< std::mutex > lk{ rqueue_mtx_ }; /*< worker fiber, enqueue on shared queue >*/ rqueue_.push_back( ctx); @@ -40,8 +40,8 @@ shared_work::awakened( context * ctx) noexcept { //[pick_next_ws context * shared_work::pick_next() noexcept { - context * ctx( nullptr); - std::unique_lock< std::mutex > lk( rqueue_mtx_); + context * ctx = nullptr; + std::unique_lock< std::mutex > lk{ rqueue_mtx_ }; if ( ! rqueue_.empty() ) { /*< pop an item from the ready queue >*/ @@ -70,11 +70,11 @@ void shared_work::suspend_until( std::chrono::steady_clock::time_point const& time_point) noexcept { if ( suspend_) { if ( (std::chrono::steady_clock::time_point::max)() == time_point) { - std::unique_lock< std::mutex > lk( mtx_); + std::unique_lock< std::mutex > lk{ mtx_ }; cnd_.wait( lk, [this](){ return flag_; }); flag_ = false; } else { - std::unique_lock< std::mutex > lk( mtx_); + std::unique_lock< std::mutex > lk{ mtx_ }; cnd_.wait_until( lk, time_point, [this](){ return flag_; }); flag_ = false; } @@ -84,14 +84,14 @@ shared_work::suspend_until( std::chrono::steady_clock::time_point const& time_po void shared_work::notify() noexcept { if ( suspend_) { - std::unique_lock< std::mutex > lk( mtx_); + std::unique_lock< std::mutex > lk{ mtx_ }; flag_ = true; lk.unlock(); cnd_.notify_all(); } } -shared_work::rqueue_t shared_work::rqueue_{}; +shared_work::rqueue_type shared_work::rqueue_{}; std::mutex shared_work::rqueue_mtx_{}; }}} diff --git a/src/algo/work_stealing.cpp b/src/algo/work_stealing.cpp index 1ff4a464..b1570a1e 100644 --- a/src/algo/work_stealing.cpp +++ b/src/algo/work_stealing.cpp @@ -72,11 +72,11 @@ void work_stealing::suspend_until( std::chrono::steady_clock::time_point const& time_point) noexcept { if ( suspend_) { if ( (std::chrono::steady_clock::time_point::max)() == time_point) { - std::unique_lock< std::mutex > lk( mtx_); + std::unique_lock< std::mutex > lk{ mtx_ }; cnd_.wait( lk, [this](){ return flag_; }); flag_ = false; } else { - std::unique_lock< std::mutex > lk( mtx_); + std::unique_lock< std::mutex > lk{ mtx_ }; cnd_.wait_until( lk, time_point, [this](){ return flag_; }); flag_ = false; } @@ -86,7 +86,7 @@ work_stealing::suspend_until( std::chrono::steady_clock::time_point const& time_ void work_stealing::notify() noexcept { if ( suspend_) { - std::unique_lock< std::mutex > lk( mtx_); + std::unique_lock< std::mutex > lk{ mtx_ }; flag_ = true; lk.unlock(); cnd_.notify_all(); diff --git a/src/barrier.cpp b/src/barrier.cpp index e6deed87..acad32d2 100644 --- a/src/barrier.cpp +++ b/src/barrier.cpp @@ -22,14 +22,14 @@ barrier::barrier( std::size_t initial) : initial_{ initial }, current_{ initial_ } { if ( 0 == initial) { - throw fiber_error( std::make_error_code( std::errc::invalid_argument), - "boost fiber: zero initial barrier count"); + throw fiber_error{ std::make_error_code( std::errc::invalid_argument), + "boost fiber: zero initial barrier count" }; } } bool barrier::wait() { - std::unique_lock< mutex > lk( mtx_); + std::unique_lock< mutex > lk{ mtx_ }; const bool cycle = cycle_; if ( 0 == --current_) { cycle_ = ! cycle_; diff --git a/src/condition_variable.cpp b/src/condition_variable.cpp index 010891ab..1f7b3e60 100644 --- a/src/condition_variable.cpp +++ b/src/condition_variable.cpp @@ -18,7 +18,7 @@ namespace fibers { void condition_variable_any::notify_one() noexcept { // get one context' from wait-queue - detail::spinlock_lock lk( wait_queue_splk_); + detail::spinlock_lock lk{ wait_queue_splk_ }; if ( wait_queue_.empty() ) { return; } @@ -31,7 +31,8 @@ condition_variable_any::notify_one() noexcept { void condition_variable_any::notify_all() noexcept { // get all context' from wait-queue - detail::spinlock_lock lk( wait_queue_splk_); + detail::spinlock_lock lk{ wait_queue_splk_ }; + // FIXME : swap wait-queue and unlock lock // notify all context' while ( ! wait_queue_.empty() ) { context * ctx = & wait_queue_.front(); diff --git a/src/context.cpp b/src/context.cpp index e74fbf71..feaff84e 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -41,12 +41,12 @@ static intrusive_ptr< context > make_dispatcher_context( scheduler * sched) { const std::size_t size = sctx.size - ( static_cast< char * >( sctx.sp) - static_cast< char * >( sp) ); #endif // placement new of context on top of fiber's stack - return intrusive_ptr< context >( - ::new ( sp) context( + return intrusive_ptr< context >{ + ::new ( sp) context{ dispatcher_context, - boost::context::preallocated( sp, size, sctx), + boost::context::preallocated{ sp, size, sctx }, salloc, - sched) ); + sched } }; } // schwarz counter @@ -61,17 +61,16 @@ struct context_initializer { constexpr std::size_t size = sizeof( context) + sizeof( scheduler); void * vp = std::malloc( size); if ( nullptr == vp) { - throw std::bad_alloc(); + throw std::bad_alloc{}; } // main fiber context of this thread - context * main_ctx = ::new ( vp) context( main_context); + context * main_ctx = ::new ( vp) context{ main_context }; // scheduler of this thread - scheduler * sched = ::new ( static_cast< char * >( vp) + sizeof( context) ) scheduler(); + scheduler * sched = ::new ( static_cast< char * >( vp) + sizeof( context) ) scheduler{}; // attach main context to scheduler sched->attach_main_context( main_ctx); // create and attach dispatcher context to scheduler - sched->attach_dispatcher_context( - make_dispatcher_context( sched) ); + sched->attach_dispatcher_context( make_dispatcher_context( sched) ); // make main context to active context active_ = main_ctx; # else @@ -81,7 +80,7 @@ struct context_initializer { constexpr std::size_t size = 2 * alignment + ctx_size + sched_size; void * vp = std::malloc( size); if ( nullptr == vp) { - throw std::bad_alloc(); + throw std::bad_alloc{}; } // reserve space for shift void * vp1 = static_cast< char * >( vp) + sizeof( int); @@ -93,18 +92,17 @@ struct context_initializer { // store shifted size in fornt of context * shift = static_cast< char * >( vp1) - static_cast< char * >( vp); // main fiber context of this thread - context * main_ctx = ::new ( vp1) context( main_context); + context * main_ctx = ::new ( vp1) context{ main_context }; vp1 = static_cast< char * >( vp1) + ctx_size; // align scheduler pointer space = sched_size + alignment; vp1 = std::align( alignment, sched_size, vp1, space); // scheduler of this thread - scheduler * sched = ::new ( vp1) scheduler(); + scheduler * sched = ::new ( vp1) scheduler{}; // attach main context to scheduler sched->attach_main_context( main_ctx); // create and attach dispatcher context to scheduler - sched->attach_dispatcher_context( - make_dispatcher_context( sched) ); + sched->attach_dispatcher_context( make_dispatcher_context( sched) ); // make main context to active context active_ = main_ctx; # endif @@ -256,7 +254,7 @@ context::~context() { context::id context::get_id() const noexcept { - return id( const_cast< context * >( this) ); + return id{ const_cast< context * >( this) }; } void @@ -316,7 +314,7 @@ context::join() { // get active context context * active_ctx = context::active(); // protect for concurrent access - std::unique_lock< detail::spinlock > lk( splk_); + std::unique_lock< detail::spinlock > lk{ splk_ }; // wait for context which is not terminated if ( 0 == ( flags_ & flag_terminated) ) { // push active context to wait-queue, member @@ -341,7 +339,7 @@ context::yield() noexcept { void context::set_terminated() noexcept { // protect for concurrent access - std::unique_lock< detail::spinlock > lk( splk_); + std::unique_lock< detail::spinlock > lk{ splk_ }; // mark as terminated flags_ |= flag_terminated; // notify all waiting fibers @@ -376,7 +374,7 @@ context::suspend_with_cc() noexcept { boost::context::continuation context::set_terminated() noexcept { // protect for concurrent access - std::unique_lock< detail::spinlock > lk( splk_); + std::unique_lock< detail::spinlock > lk{ splk_ }; // mark as terminated flags_ |= flag_terminated; // notify all waiting fibers @@ -438,8 +436,8 @@ context::set_ready( context * ctx) noexcept { void * context::get_fss_data( void const * vp) const { - uintptr_t key( reinterpret_cast< uintptr_t >( vp) ); - fss_data_t::const_iterator i( fss_data_.find( key) ); + uintptr_t key = reinterpret_cast< uintptr_t >( vp); + fss_data_t::const_iterator i = fss_data_.find( key); return fss_data_.end() != i ? i->second.vp : nullptr; } @@ -449,8 +447,8 @@ context::set_fss_data( void const * vp, void * data, bool cleanup_existing) { BOOST_ASSERT( cleanup_fn); - uintptr_t key( reinterpret_cast< uintptr_t >( vp) ); - fss_data_t::iterator i( fss_data_.find( key) ); + uintptr_t key = reinterpret_cast< uintptr_t >( vp); + fss_data_t::iterator i = fss_data_.find( key); if ( fss_data_.end() != i) { if( cleanup_existing) { i->second.do_cleanup(); @@ -460,7 +458,7 @@ context::set_fss_data( void const * vp, i, std::make_pair( key, - fss_data( data, cleanup_fn) ) ); + fss_data{ data, cleanup_fn } ) ); } else { fss_data_.erase( i); } @@ -468,7 +466,7 @@ context::set_fss_data( void const * vp, fss_data_.insert( std::make_pair( key, - fss_data( data, cleanup_fn) ) ); + fss_data{ data, cleanup_fn } ) ); } } diff --git a/src/fiber.cpp b/src/fiber.cpp index 1f312579..3aa17435 100644 --- a/src/fiber.cpp +++ b/src/fiber.cpp @@ -44,14 +44,13 @@ void fiber::join() { // FIXME: must fiber::join() be synchronized? if ( context::active()->get_id() == get_id() ) { - throw fiber_error( std::make_error_code( std::errc::resource_deadlock_would_occur), - "boost fiber: trying to join itself"); + throw fiber_error{ std::make_error_code( std::errc::resource_deadlock_would_occur), + "boost fiber: trying to join itself" }; } if ( ! joinable() ) { - throw fiber_error( std::make_error_code( std::errc::invalid_argument), - "boost fiber: fiber not joinable"); + throw fiber_error{ std::make_error_code( std::errc::invalid_argument), + "boost fiber: fiber not joinable" }; } - impl_->join(); impl_.reset(); } @@ -59,8 +58,8 @@ fiber::join() { void fiber::detach() { if ( ! joinable() ) { - throw fiber_error( std::make_error_code( std::errc::invalid_argument), - "boost fiber: fiber not joinable"); + throw fiber_error{ std::make_error_code( std::errc::invalid_argument), + "boost fiber: fiber not joinable" }; } impl_.reset(); } diff --git a/src/future.cpp b/src/future.cpp index 0059b457..d8f9b5c4 100644 --- a/src/future.cpp +++ b/src/future.cpp @@ -18,26 +18,24 @@ public: virtual std::error_condition default_error_condition( int ev) const noexcept { switch ( static_cast< future_errc >( ev) ) { case future_errc::broken_promise: - return std::error_condition( + return std::error_condition{ static_cast< int >( future_errc::broken_promise), - future_category() ); + future_category() }; case future_errc::future_already_retrieved: - return std::error_condition( + return std::error_condition{ static_cast< int >( future_errc::future_already_retrieved), - future_category() ); + future_category() }; case future_errc::promise_already_satisfied: - return std::error_condition( + return std::error_condition{ static_cast< int >( future_errc::promise_already_satisfied), - future_category() ); + future_category() }; case future_errc::no_state: - return std::error_condition( + return std::error_condition{ static_cast< int >( future_errc::no_state), - future_category() ); + future_category() }; default: - return std::error_condition( - ev, - * this); + return std::error_condition{ ev, * this }; } } @@ -49,18 +47,18 @@ public: virtual std::string message( int ev) const { switch ( static_cast< future_errc >( ev) ) { case future_errc::broken_promise: - return std::string("The associated promise has been destructed prior " - "to the associated state becoming ready."); + return std::string{ "The associated promise has been destructed prior " + "to the associated state becoming ready." }; case future_errc::future_already_retrieved: - return std::string("The future has already been retrieved from " - "the promise or packaged_task."); + return std::string{ "The future has already been retrieved from " + "the promise or packaged_task." }; case future_errc::promise_already_satisfied: - return std::string("The state of the promise has already been set."); + return std::string{ "The state of the promise has already been set." }; case future_errc::no_state: - return std::string("Operation not permitted on an object without " - "an associated state."); + return std::string{ "Operation not permitted on an object without " + "an associated state." }; } - return std::string("unspecified future_errc value\n"); + return std::string{ "unspecified future_errc value\n" }; } }; diff --git a/src/mutex.cpp b/src/mutex.cpp index 1ec3c636..82d50a8d 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -22,55 +22,55 @@ namespace fibers { void mutex::lock() { - context * ctx = context::active(); + context * active_ctx = context::active(); // store this fiber in order to be notified later - detail::spinlock_lock lk( wait_queue_splk_); - if ( ctx == owner_) { - throw lock_error( + detail::spinlock_lock lk{ wait_queue_splk_ }; + if ( active_ctx == owner_) { + throw lock_error{ std::make_error_code( std::errc::resource_deadlock_would_occur), - "boost fiber: a deadlock is detected"); + "boost fiber: a deadlock is detected" }; } else if ( nullptr == owner_) { - owner_ = ctx; + owner_ = active_ctx; return; } - BOOST_ASSERT( ! ctx->wait_is_linked() ); - ctx->wait_link( wait_queue_); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); + active_ctx->wait_link( wait_queue_); // suspend this fiber - ctx->suspend( lk); - BOOST_ASSERT( ! ctx->wait_is_linked() ); + active_ctx->suspend( lk); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); } bool mutex::try_lock() { - context * ctx = context::active(); - detail::spinlock_lock lk( wait_queue_splk_); - if ( ctx == owner_) { - throw lock_error( + context * active_ctx = context::active(); + detail::spinlock_lock lk{ wait_queue_splk_ }; + if ( active_ctx == owner_) { + throw lock_error{ std::make_error_code( std::errc::resource_deadlock_would_occur), - "boost fiber: a deadlock is detected"); + "boost fiber: a deadlock is detected" }; } else if ( nullptr == owner_) { - owner_ = ctx; + owner_ = active_ctx; } lk.unlock(); // let other fiber release the lock - context::active()->yield(); - return ctx == owner_; + active_ctx->yield(); + return active_ctx == owner_; } void mutex::unlock() { - context * ctx = context::active(); - detail::spinlock_lock lk( wait_queue_splk_); - if ( ctx != owner_) { - throw lock_error( + context * active_ctx = context::active(); + detail::spinlock_lock lk{ wait_queue_splk_ }; + if ( active_ctx != owner_) { + throw lock_error{ std::make_error_code( std::errc::operation_not_permitted), - "boost fiber: no privilege to perform the operation"); + "boost fiber: no privilege to perform the operation" }; } if ( ! wait_queue_.empty() ) { context * ctx = & wait_queue_.front(); wait_queue_.pop_front(); owner_ = ctx; - context::active()->set_ready( ctx); + active_ctx->set_ready( ctx); } else { owner_ = nullptr; return; diff --git a/src/recursive_mutex.cpp b/src/recursive_mutex.cpp index d04680cb..0a83fb4c 100644 --- a/src/recursive_mutex.cpp +++ b/src/recursive_mutex.cpp @@ -23,7 +23,7 @@ void recursive_mutex::lock() { context * ctx = context::active(); // store this fiber in order to be notified later - detail::spinlock_lock lk( wait_queue_splk_); + detail::spinlock_lock lk{ wait_queue_splk_ }; if ( ctx == owner_) { ++count_; return; @@ -42,7 +42,7 @@ recursive_mutex::lock() { bool recursive_mutex::try_lock() noexcept { context * ctx = context::active(); - detail::spinlock_lock lk( wait_queue_splk_); + detail::spinlock_lock lk{ wait_queue_splk_ }; if ( nullptr == owner_) { owner_ = ctx; count_ = 1; diff --git a/src/recursive_timed_mutex.cpp b/src/recursive_timed_mutex.cpp index af6c9b19..3c2f2b5f 100644 --- a/src/recursive_timed_mutex.cpp +++ b/src/recursive_timed_mutex.cpp @@ -24,74 +24,74 @@ recursive_timed_mutex::try_lock_until_( std::chrono::steady_clock::time_point co if ( std::chrono::steady_clock::now() > timeout_time) { return false; } - context * ctx = context::active(); + context * active_ctx = context::active(); // store this fiber in order to be notified later - detail::spinlock_lock lk( wait_queue_splk_); - if ( ctx == owner_) { + detail::spinlock_lock lk{ wait_queue_splk_ }; + if ( active_ctx == owner_) { ++count_; return true; } else if ( nullptr == owner_) { - owner_ = ctx; + owner_ = active_ctx; count_ = 1; return true; } - BOOST_ASSERT( ! ctx->wait_is_linked() ); - ctx->wait_link( wait_queue_); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); + active_ctx->wait_link( wait_queue_); // suspend this fiber until notified or timed-out - if ( ! context::active()->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // remove fiber from wait-queue lk.lock(); - ctx->wait_unlink(); + active_ctx->wait_unlink(); return false; } - BOOST_ASSERT( ! ctx->wait_is_linked() ); - return ctx == owner_; + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); + return active_ctx == owner_; } void recursive_timed_mutex::lock() { - context * ctx = context::active(); + context * active_ctx = context::active(); // store this fiber in order to be notified later - detail::spinlock_lock lk( wait_queue_splk_); - if ( ctx == owner_) { + detail::spinlock_lock lk{ wait_queue_splk_ }; + if ( active_ctx == owner_) { ++count_; return; } else if ( nullptr == owner_) { - owner_ = ctx; + owner_ = active_ctx; count_ = 1; return; } - BOOST_ASSERT( ! ctx->wait_is_linked() ); - ctx->wait_link( wait_queue_); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); + active_ctx->wait_link( wait_queue_); // suspend this fiber - ctx->suspend( lk); - BOOST_ASSERT( ! ctx->wait_is_linked() ); + active_ctx->suspend( lk); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); } bool recursive_timed_mutex::try_lock() noexcept { - context * ctx = context::active(); - detail::spinlock_lock lk( wait_queue_splk_); + context * active_ctx = context::active(); + detail::spinlock_lock lk{ wait_queue_splk_ }; if ( nullptr == owner_) { - owner_ = ctx; + owner_ = active_ctx; count_ = 1; - } else if ( ctx == owner_) { + } else if ( active_ctx == owner_) { ++count_; } lk.unlock(); // let other fiber release the lock - context::active()->yield(); - return ctx == owner_; + active_ctx->yield(); + return active_ctx == owner_; } void recursive_timed_mutex::unlock() { - context * ctx = context::active(); - detail::spinlock_lock lk( wait_queue_splk_); - if ( ctx != owner_) { - throw lock_error( + context * active_ctx = context::active(); + detail::spinlock_lock lk{ wait_queue_splk_ }; + if ( active_ctx != owner_) { + throw lock_error{ std::make_error_code( std::errc::operation_not_permitted), - "boost fiber: no privilege to perform the operation"); + "boost fiber: no privilege to perform the operation" }; } if ( 0 == --count_) { if ( ! wait_queue_.empty() ) { @@ -99,7 +99,7 @@ recursive_timed_mutex::unlock() { wait_queue_.pop_front(); owner_ = ctx; count_ = 1; - context::active()->set_ready( ctx); + active_ctx->set_ready( ctx); } else { owner_ = nullptr; return; diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 96cd8421..42b1bffd 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -24,8 +24,8 @@ namespace fibers { void scheduler::release_terminated_() noexcept { - terminated_queue_t::iterator e( terminated_queue_.end() ); - for ( terminated_queue_t::iterator i( terminated_queue_.begin() ); + terminated_queue_type::iterator e = terminated_queue_.end(); + for ( terminated_queue_type::iterator i = terminated_queue_.begin(); i != e;) { context * ctx = & ( * i); // remove context from terminated-queue @@ -47,9 +47,9 @@ scheduler::release_terminated_() noexcept { #if ! defined(BOOST_FIBERS_NO_ATOMICS) void scheduler::remote_ready2ready_() noexcept { - context * ctx = nullptr; + context * ctx = remote_ready_queue_.pop(); // get context from remote ready-queue - while ( nullptr != ( ctx = remote_ready_queue_.pop() ) ) { + while ( nullptr != ctx) { // store context in local queues set_ready( ctx); } @@ -61,10 +61,9 @@ scheduler::sleep2ready_() noexcept { // move context which the deadline has reached // to ready-queue // sleep-queue is sorted (ascending) - std::chrono::steady_clock::time_point now = - std::chrono::steady_clock::now(); - sleep_queue_t::iterator e = sleep_queue_.end(); - for ( sleep_queue_t::iterator i = sleep_queue_.begin(); i != e;) { + std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); + sleep_queue_type::iterator e = sleep_queue_.end(); + for ( sleep_queue_type::iterator i = sleep_queue_.begin(); i != e;) { context * ctx = & ( * i); // dipatcher context must never be pushed to sleep-queue BOOST_ASSERT( ! ctx->is_context( type::dispatcher_context) ); @@ -149,7 +148,7 @@ scheduler::dispatch() noexcept { std::chrono::steady_clock::time_point suspend_time = (std::chrono::steady_clock::time_point::max)(); // get lowest deadline from sleep-queue - sleep_queue_t::iterator i = sleep_queue_.begin(); + sleep_queue_type::iterator i = sleep_queue_.begin(); if ( sleep_queue_.end() != i) { suspend_time = i->tp_; } @@ -195,7 +194,7 @@ scheduler::dispatch() noexcept { std::chrono::steady_clock::time_point suspend_time = (std::chrono::steady_clock::time_point::max)(); // get lowest deadline from sleep-queue - sleep_queue_t::iterator i = sleep_queue_.begin(); + sleep_queue_type::iterator i = sleep_queue_.begin(); if ( sleep_queue_.end() != i) { suspend_time = i->tp_; } diff --git a/src/timed_mutex.cpp b/src/timed_mutex.cpp index e9addae4..15b405f8 100644 --- a/src/timed_mutex.cpp +++ b/src/timed_mutex.cpp @@ -24,77 +24,77 @@ timed_mutex::try_lock_until_( std::chrono::steady_clock::time_point const& timeo if ( std::chrono::steady_clock::now() > timeout_time) { return false; } - context * ctx = context::active(); + context * active_ctx = context::active(); // store this fiber in order to be notified later - detail::spinlock_lock lk( wait_queue_splk_); + detail::spinlock_lock lk{ wait_queue_splk_ }; if ( nullptr == owner_) { - owner_ = ctx; + owner_ = active_ctx; return true; } - BOOST_ASSERT( ! ctx->wait_is_linked() ); - ctx->wait_link( wait_queue_); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); + active_ctx->wait_link( wait_queue_); // suspend this fiber until notified or timed-out - if ( ! context::active()->wait_until( timeout_time, lk) ) { + if ( ! active_ctx->wait_until( timeout_time, lk) ) { // remove fiber from wait-queue lk.lock(); - ctx->wait_unlink(); + active_ctx->wait_unlink(); return false; } - BOOST_ASSERT( ! ctx->wait_is_linked() ); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); return true; } void timed_mutex::lock() { - context * ctx = context::active(); + context * active_ctx = context::active(); // store this fiber in order to be notified later - detail::spinlock_lock lk( wait_queue_splk_); - if ( ctx == owner_) { - throw lock_error( + detail::spinlock_lock lk{ wait_queue_splk_ }; + if ( active_ctx == owner_) { + throw lock_error{ std::make_error_code( std::errc::resource_deadlock_would_occur), - "boost fiber: a deadlock is detected"); + "boost fiber: a deadlock is detected" }; } else if ( nullptr == owner_) { - owner_ = ctx; + owner_ = active_ctx; return; } - BOOST_ASSERT( ! ctx->wait_is_linked() ); - ctx->wait_link( wait_queue_); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); + active_ctx->wait_link( wait_queue_); // suspend this fiber - ctx->suspend( lk); - BOOST_ASSERT( ! ctx->wait_is_linked() ); + active_ctx->suspend( lk); + BOOST_ASSERT( ! active_ctx->wait_is_linked() ); } bool timed_mutex::try_lock() { - context * ctx = context::active(); - detail::spinlock_lock lk( wait_queue_splk_); - if ( ctx == owner_) { - throw lock_error( + context * active_ctx = context::active(); + detail::spinlock_lock lk{ wait_queue_splk_ }; + if ( active_ctx == owner_) { + throw lock_error{ std::make_error_code( std::errc::resource_deadlock_would_occur), - "boost fiber: a deadlock is detected"); + "boost fiber: a deadlock is detected" }; } else if ( nullptr == owner_) { - owner_ = ctx; + owner_ = active_ctx; } lk.unlock(); // let other fiber release the lock - context::active()->yield(); - return ctx == owner_; + active_ctx->yield(); + return active_ctx == owner_; } void timed_mutex::unlock() { - context * ctx = context::active(); - detail::spinlock_lock lk( wait_queue_splk_); - if ( ctx != owner_) { - throw lock_error( + context * active_ctx = context::active(); + detail::spinlock_lock lk{ wait_queue_splk_ }; + if ( active_ctx != owner_) { + throw lock_error{ std::make_error_code( std::errc::operation_not_permitted), - "boost fiber: no privilege to perform the operation"); + "boost fiber: no privilege to perform the operation" }; } if ( ! wait_queue_.empty() ) { context * ctx = & wait_queue_.front(); wait_queue_.pop_front(); owner_ = ctx; - context::active()->set_ready( ctx); + active_ctx->set_ready( ctx); } else { owner_ = nullptr; return;