From db180f08daa5076788dbaf6346de680baecb96a4 Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Sat, 12 Dec 2015 23:19:33 +0100 Subject: [PATCH] relax to C++11 --- examples/work_stealing.cpp | 23 +- include/boost/fiber/context.hpp | 30 ++- include/boost/fiber/future/async.hpp | 8 +- include/boost/fiber/future/packaged_task.hpp | 12 +- src/context.cpp | 11 +- test/Jamfile.v2 | 254 +++++++++---------- test/test_condition_mt.cpp | 9 +- test/test_fss.cpp | 6 +- test/test_future.cpp | 5 +- test/test_mutex_mt.cpp | 4 +- 10 files changed, 168 insertions(+), 194 deletions(-) diff --git a/examples/work_stealing.cpp b/examples/work_stealing.cpp index d599dc45..a90f69e3 100644 --- a/examples/work_stealing.cpp +++ b/examples/work_stealing.cpp @@ -91,25 +91,25 @@ class victim_algo : public boost::fibers::sched_algorithm { private: typedef work_stealing_queue rqueue_t; - rqueue_t rqueue_{}; + std::shared_ptr< rqueue_t > rqueue_{}; boost::fibers::detail::autoreset_event ev_{}; public: - victim_algo( rqueue_t * & rqueue) { - rqueue = & rqueue_; + victim_algo( std::shared_ptr< rqueue_t > rqueue) : + rqueue_( rqueue) { } virtual void awakened( boost::fibers::context * ctx) noexcept { BOOST_ASSERT( nullptr != ctx); - rqueue_.push_back( ctx); + rqueue_->push_back( ctx); } virtual boost::fibers::context * pick_next() noexcept { - return rqueue_.pick_next(); + return rqueue_->pick_next(); } virtual bool has_ready_fibers() const noexcept { - return ! rqueue_.empty(); + return ! rqueue_->empty(); } void suspend_until( std::chrono::steady_clock::time_point const& suspend_time) noexcept { @@ -124,15 +124,15 @@ public: class tief_algo : public boost::fibers::sched_algorithm { private: typedef boost::fibers::scheduler::ready_queue_t rqueue_t; - typedef work_stealing_queue ws_rqueue_t; + typedef work_stealing_queue ws_rqueue_t; rqueue_t rqueue_{}; - ws_rqueue_t * ws_rqueue_; + std::shared_ptr< ws_rqueue_t > ws_rqueue_; std::atomic< int > * count_; boost::fibers::detail::autoreset_event ev_{}; public: - tief_algo( ws_rqueue_t * ws_rqueue, std::atomic< int > * count) : + tief_algo( std::shared_ptr< ws_rqueue_t > ws_rqueue, std::atomic< int > * count) : ws_rqueue_( ws_rqueue), count_( count) { } @@ -198,7 +198,7 @@ boost::fibers::future< int > fibonacci( int n) { return f; } -void thread( work_stealing_queue * ws_queue, std::atomic< int > * count, std::atomic< bool > * fini) { +void thread( std::shared_ptr< work_stealing_queue > ws_queue, std::atomic< int > * count, std::atomic< bool > * fini) { boost::fibers::use_scheduling_algorithm< tief_algo >( ws_queue, count); while ( ! ( * fini) ) { @@ -214,7 +214,8 @@ void thread( work_stealing_queue * ws_queue, std::atomic< int > * count, std::at } int main() { - work_stealing_queue * ws_queue = nullptr; + std::shared_ptr< work_stealing_queue > ws_queue( + new work_stealing_queue() ); boost::fibers::use_scheduling_algorithm< victim_algo >( ws_queue); for ( int i = 0; i < 10; ++i) { diff --git a/include/boost/fiber/context.hpp b/include/boost/fiber/context.hpp index 76e58add..c58a7587 100644 --- a/include/boost/fiber/context.hpp +++ b/include/boost/fiber/context.hpp @@ -208,31 +208,34 @@ private: fiber_properties * properties_{ nullptr }; #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) - template< typename Fn, typename Tpl > - std::function< void( void*) > create_( Fn && fn__, Tpl && tpl__) { - return std::bind( + template< typename StackAlloc, typename Fn, typename Tpl > + boost::context::execution_context create_( boost::context::preallocated palloc, + StackAlloc salloc, Fn && fn, Tpl && tpl) { + return boost::context::execution_context{ + std::allocator_arg, palloc, salloc, + std::bind( [this]( typename std::decay< Fn >::type & fn_, typename std::decay< Tpl >::type & tpl_, boost::context::execution_context ctx, void * vp) mutable noexcept { try { + auto fn = std::move( fn_); + auto tpl = std::move( tpl_); data_t * dp = static_cast< data_t * >( vp); if ( nullptr != dp->lk) { dp->lk->unlock(); } else if ( nullptr != dp->ctx) { active_->set_ready_( dp->ctx); } - auto fn = std::move( fn_); - auto tpl = std::move( tpl_); - boost::context::detail::apply( fn, tpl); + boost::context::detail::apply( std::move( fn), std::move( tpl) ); } catch ( fiber_interrupted const&) { } // terminate context terminate(); BOOST_ASSERT_MSG( false, "fiber already terminated"); }, - std::forward< Fn >( fn__), - std::forward< Tpl >( tpl__), + std::forward< Fn >( fn), + std::forward< Tpl >( tpl), boost::context::execution_context::current(), - std::placeholders::_1); + std::placeholders::_1) }; } #endif @@ -310,8 +313,7 @@ public: use_count_{ 1 }, // fiber instance or scheduler owner flags_{ flag_worker_context }, #if defined(BOOST_NO_CXX14_GENERIC_LAMBDAS) - ctx_{ std::allocator_arg, palloc, salloc, - create_( std::forward< Fn >( fn), std::make_tuple( std::forward< Args >( args) ... ) ) } + ctx_{ create_( palloc, salloc, std::forward< Fn >( fn), std::make_tuple( std::forward< Args >( args) ... ) ) } #else ctx_{ std::allocator_arg, palloc, salloc, // mutable: generated operator() is not const -> enables std::move( fn) @@ -319,15 +321,15 @@ public: [this,fn_=std::forward< Fn >( fn),tpl_=std::make_tuple( std::forward< Args >( args) ...), ctx=boost::context::execution_context::current()] (void * vp) mutable noexcept { try { + auto fn = std::move( fn_); + auto tpl = std::move( tpl_); data_t * dp = static_cast< data_t * >( vp); if ( nullptr != dp->lk) { dp->lk->unlock(); } else if ( nullptr != dp->ctx) { active_->set_ready_( dp->ctx); } - auto fn = std::move( fn_); - auto tpl = std::move( tpl_); - boost::context::detail::apply( fn, tpl); + boost::context::detail::apply( std::move( fn), std::move( tpl) ); } catch ( fiber_interrupted const&) { } // terminate context diff --git a/include/boost/fiber/future/async.hpp b/include/boost/fiber/future/async.hpp index e7087f33..b6792a8d 100644 --- a/include/boost/fiber/future/async.hpp +++ b/include/boost/fiber/future/async.hpp @@ -21,9 +21,9 @@ namespace boost { namespace fibers { template< typename Fn, typename ... Args > -future< typename std::result_of< Fn( Args ... ) >::type > +future< typename std::result_of< Fn &&( Args && ... ) >::type > async( Fn && fn, Args && ... args) { - typedef typename std::result_of< Fn( Args ... ) >::type result_type; + typedef typename std::result_of< Fn &&( Args && ... ) >::type result_type; packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{ std::forward< Fn >( fn) }; @@ -33,9 +33,9 @@ async( Fn && fn, Args && ... args) { } template< typename StackAllocator, typename Fn, typename ... Args > -future< typename std::result_of< Fn( Args ... ) >::type > +future< typename std::result_of< Fn &&( Args && ... ) >::type > async( std::allocator_arg_t, StackAllocator salloc, Fn && fn, Args && ... args) { - typedef typename std::result_of< Fn( Args ... ) >::type result_type; + typedef typename std::result_of< Fn &&( Args && ... ) >::type result_type; packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{ std::forward< Fn >( fn) }; diff --git a/include/boost/fiber/future/packaged_task.hpp b/include/boost/fiber/future/packaged_task.hpp index de5aab4d..a0c8baeb 100644 --- a/include/boost/fiber/future/packaged_task.hpp +++ b/include/boost/fiber/future/packaged_task.hpp @@ -36,12 +36,6 @@ private: public: constexpr packaged_task() noexcept = default; - ~packaged_task() { - if ( task_) { - task_->owner_destroyed(); - } - } - template< typename Fn > explicit packaged_task( Fn && fn) : packaged_task{ std::allocator_arg, @@ -69,6 +63,12 @@ public: task_.reset( convert( ptr) ); } + ~packaged_task() { + if ( task_) { + task_->owner_destroyed(); + } + } + packaged_task( packaged_task const&) = delete; packaged_task & operator=( packaged_task const&) = delete; diff --git a/src/context.cpp b/src/context.cpp index 3d5ef844..53eb7ea5 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -47,7 +47,7 @@ static intrusive_ptr< context > make_dispatcher_context( scheduler * sched) { #endif // placement new of context on top of fiber's stack return intrusive_ptr< context >( - new ( sp) context( + ::new ( sp) context( dispatcher_context, boost::context::preallocated( sp, size, sctx), salloc, @@ -68,9 +68,9 @@ context_initializer::context_initializer() { 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 @@ -97,13 +97,13 @@ context_initializer::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 @@ -190,7 +190,6 @@ context::context( dispatcher_context_t, boost::context::preallocated const& pall context::~context() { BOOST_ASSERT( wait_queue_.empty() ); BOOST_ASSERT( ! ready_is_linked() ); - BOOST_ASSERT( ! remote_ready_is_linked() ); BOOST_ASSERT( ! sleep_is_linked() ); BOOST_ASSERT( ! wait_is_linked() ); delete properties_; diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 4bac255b..deafd2fd 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,178 +27,156 @@ project boost/fiber/test multi ; -run test_future.cpp : -: : -[ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators - cxx11_hdr_tuple - cxx11_lambdas - cxx11_noexcept - cxx11_nullptr - cxx11_template_aliases - cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; - run test_fiber.cpp : : : - [ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final cxx11_hdr_tuple cxx11_lambdas cxx11_noexcept cxx11_nullptr - cxx11_template_aliases cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + cxx11_template_aliases + cxx11_variadic_templates ] ; run test_mutex.cpp : : : - [ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final cxx11_hdr_tuple cxx11_lambdas cxx11_noexcept cxx11_nullptr - cxx11_template_aliases cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + cxx11_template_aliases + cxx11_variadic_templates ] ; run test_condition.cpp : -: : -[ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators - cxx11_hdr_tuple - cxx11_lambdas - cxx11_noexcept - cxx11_nullptr - cxx11_template_aliases - cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + : : + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final + cxx11_hdr_tuple + cxx11_lambdas + cxx11_noexcept + cxx11_nullptr + cxx11_rvalue_references + cxx11_template_aliases + cxx11_variadic_templates ] ; run test_barrier.cpp : -: : -[ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators - cxx11_hdr_tuple - cxx11_lambdas - cxx11_noexcept - cxx11_nullptr - cxx11_template_aliases - cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + : : + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final + cxx11_hdr_tuple + cxx11_lambdas + cxx11_noexcept + cxx11_nullptr + cxx11_rvalue_references + cxx11_template_aliases + cxx11_variadic_templates ] ; run test_unbounded_channel.cpp : -: : -[ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators - cxx11_hdr_tuple - cxx11_lambdas - cxx11_noexcept - cxx11_nullptr - cxx11_template_aliases - cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + : : + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final + cxx11_hdr_tuple + cxx11_lambdas + cxx11_noexcept + cxx11_nullptr + cxx11_rvalue_references + cxx11_template_aliases + cxx11_variadic_templates ] ; run test_bounded_channel.cpp : -: : -[ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators - cxx11_hdr_tuple - cxx11_lambdas - cxx11_noexcept - cxx11_nullptr - cxx11_template_aliases - cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + : : + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final + cxx11_hdr_tuple + cxx11_lambdas + cxx11_noexcept + cxx11_nullptr + cxx11_rvalue_references + cxx11_template_aliases + cxx11_variadic_templates ] ; run test_fss.cpp : -: : -[ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators - cxx11_hdr_tuple - cxx11_lambdas - cxx11_noexcept - cxx11_nullptr - cxx11_template_aliases - cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + : : + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final + cxx11_hdr_tuple + cxx11_lambdas + cxx11_noexcept + cxx11_nullptr + cxx11_rvalue_references + cxx11_template_aliases + cxx11_variadic_templates ] ; + +run test_future.cpp : + : : + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final + cxx11_hdr_tuple + cxx11_lambdas + cxx11_noexcept + cxx11_nullptr + cxx11_rvalue_references + cxx11_template_aliases + cxx11_variadic_templates ] ; run test_mutex_mt.cpp : : : - [ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final cxx11_hdr_tuple cxx11_lambdas cxx11_noexcept cxx11_nullptr - cxx11_template_aliases cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + cxx11_template_aliases + cxx11_variadic_templates ] ; run test_condition_mt.cpp : -: : -[ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators - cxx11_hdr_tuple - cxx11_lambdas - cxx11_noexcept - cxx11_nullptr - cxx11_template_aliases - cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + : : + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final + cxx11_hdr_tuple + cxx11_lambdas + cxx11_noexcept + cxx11_nullptr + cxx11_rvalue_references + cxx11_template_aliases + cxx11_variadic_templates ] ; run test_future_mt.cpp : -: : -[ requires cxx11_constexpr - cxx11_decltype - cxx11_deleted_functions - cxx11_explicit_conversion_operators - cxx11_hdr_tuple - cxx11_lambdas - cxx11_noexcept - cxx11_nullptr - cxx11_template_aliases - cxx11_rvalue_references - cxx11_variadic_macros - cxx11_variadic_templates - cxx14_initialized_lambda_captures ] ; + : : + [ requires cxx11_auto_declarations + cxx11_constexpr + cxx11_defaulted_functions + cxx11_final + cxx11_hdr_tuple + cxx11_lambdas + cxx11_noexcept + cxx11_nullptr + cxx11_rvalue_references + cxx11_template_aliases + cxx11_variadic_templates ] ; diff --git a/test/test_condition_mt.cpp b/test/test_condition_mt.cpp index 207fc4f3..077761e3 100644 --- a/test/test_condition_mt.cpp +++ b/test/test_condition_mt.cpp @@ -66,12 +66,11 @@ void fn1( boost::barrier & b, boost::fibers::condition & cond, bool & flag) { boost::fibers::fiber( - std::bind( wait_fn, std::ref( b), std::ref( mtx), std::ref( cond), - std::ref( flag) ) ).join(); + std::ref( flag) ).join(); } void fn2( boost::barrier & b, @@ -79,12 +78,11 @@ void fn2( boost::barrier & b, boost::fibers::condition & cond, bool & flag) { boost::fibers::fiber( - std::bind( notify_one_fn, std::ref( b), std::ref( mtx), std::ref( cond), - std::ref( flag) ) ).join(); + std::ref( flag) ).join(); } void fn3( boost::barrier & b, @@ -92,12 +90,11 @@ void fn3( boost::barrier & b, boost::fibers::condition & cond, bool & flag) { boost::fibers::fiber( - std::bind( notify_all_fn, std::ref( b), std::ref( mtx), std::ref( cond), - std::ref( flag) ) ).join(); + std::ref( flag) ).join(); } void test_one_waiter_notify_one() { diff --git a/test/test_fss.cpp b/test/test_fss.cpp index de107ba9..561b72b2 100644 --- a/test/test_fss.cpp +++ b/test/test_fss.cpp @@ -106,7 +106,6 @@ void test_fss_with_custom_cleanup() { boost::fibers::fiber( fss_with_custom_cleanup).join(); } - Dummy* fss_object=new Dummy; void fss_fiber_with_custom_cleanup_and_release() { @@ -149,8 +148,8 @@ void fss_fiber_with_null_cleanup(dummy_class_tracks_deletions* delete_tracker) { void do_test_fss_does_no_cleanup_with_null_cleanup_function() { dummy_class_tracks_deletions* delete_tracker=new dummy_class_tracks_deletions; - boost::fibers::fiber f( - std::bind( fss_fiber_with_null_cleanup,delete_tracker) ); + boost::fibers::fiber f([&delete_tracker](){ + fss_fiber_with_null_cleanup( delete_tracker); }); try { f.join(); } catch(...) { @@ -225,7 +224,6 @@ void test_fss_at_the_same_adress() { boost::fibers::fiber( fss_at_the_same_adress).join(); } - boost::unit_test::test_suite* init_unit_test_suite(int, char*[]) { boost::unit_test::test_suite* test = BOOST_TEST_SUITE("Boost.Fiber: fss test suite"); diff --git a/test/test_future.cpp b/test/test_future.cpp index f753d446..4e2b170c 100644 --- a/test/test_future.cpp +++ b/test/test_future.cpp @@ -42,7 +42,7 @@ void fn2() { boost::fibers::promise< int > p; boost::fibers::future< int > f( p.get_future() ); boost::this_fiber::yield(); - boost::fibers::fiber( std::bind( fn1, & p, 7) ).detach(); + boost::fibers::fiber( fn1, & p, 7).detach(); boost::this_fiber::yield(); BOOST_CHECK( 7 == f.get() ); } @@ -717,8 +717,7 @@ void test_future_wait_void() { void test_future_wait_with_fiber_1() { boost::fibers::promise< int > p1; - boost::fibers::fiber( - std::bind( fn1, & p1, 7) ).detach(); + boost::fibers::fiber( fn1, & p1, 7).detach(); boost::fibers::future< int > f1 = p1.get_future(); diff --git a/test/test_mutex_mt.cpp b/test/test_mutex_mt.cpp index f54bda5a..6a459d2c 100644 --- a/test/test_mutex_mt.cpp +++ b/test/test_mutex_mt.cpp @@ -43,12 +43,12 @@ void f( boost::barrier & b, Mtx & m) { template< typename Mtx > void fn1( boost::barrier & b, Mtx & m) { - boost::fibers::fiber( std::bind( g< Mtx >, std::ref( b), std::ref( m) ) ).join(); + boost::fibers::fiber( g< Mtx >, std::ref( b), std::ref( m) ).join(); } template< typename Mtx > void fn2( boost::barrier & b, Mtx & m) { - boost::fibers::fiber( std::bind( f< Mtx >, std::ref( b), std::ref( m) ) ).join(); + boost::fibers::fiber( f< Mtx >, std::ref( b), std::ref( m) ).join(); } void test_mutex() {