From 228aae683356f78b46dc6c2ba44d374b3ca4d9c5 Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Wed, 19 Mar 2014 19:55:56 +0100 Subject: [PATCH] do not use reference-counting --- build/Jamfile.v2 | 3 +- include/boost/fiber/algorithm.hpp | 10 ++-- include/boost/fiber/all.hpp | 8 +-- include/boost/fiber/condition.hpp | 6 +-- include/boost/fiber/detail/fiber_base.hpp | 6 +-- include/boost/fiber/detail/fifo.hpp | 53 +++++++++---------- include/boost/fiber/detail/flags.hpp | 3 +- include/boost/fiber/detail/main_fiber.hpp | 9 +--- include/boost/fiber/detail/scheduler.hpp | 2 +- include/boost/fiber/detail/worker_fiber.hpp | 16 ++++-- include/boost/fiber/detail/worker_object.hpp | 2 +- include/boost/fiber/fiber.hpp | 12 +++-- include/boost/fiber/mutex.hpp | 10 ++-- include/boost/fiber/operations.hpp | 10 ++-- include/boost/fiber/recursive_mutex.hpp | 12 ++--- include/boost/fiber/recursive_timed_mutex.hpp | 12 ++--- include/boost/fiber/round_robin.hpp | 21 ++++---- include/boost/fiber/timed_mutex.hpp | 10 ++-- src/condition.cpp | 8 +-- src/detail/spinlock.cpp | 2 +- src/detail/worker_fiber.cpp | 8 --- src/fiber.cpp | 11 ++-- src/interruption.cpp | 8 +-- src/mutex.cpp | 8 +-- src/recursive_mutex.cpp | 8 +-- src/recursive_timed_mutex.cpp | 10 ++-- src/round_robin.cpp | 27 +++++----- src/timed_mutex.cpp | 8 +-- 28 files changed, 148 insertions(+), 155 deletions(-) diff --git a/build/Jamfile.v2 b/build/Jamfile.v2 index 02e39f2b..f49120d7 100644 --- a/build/Jamfile.v2 +++ b/build/Jamfile.v2 @@ -31,8 +31,7 @@ project boost/fiber ; lib boost_fiber - : asio/round_robin.cpp - barrier.cpp + : barrier.cpp condition.cpp detail/worker_fiber.cpp detail/scheduler.cpp diff --git a/include/boost/fiber/algorithm.hpp b/include/boost/fiber/algorithm.hpp index ea9cc356..ef4cd802 100644 --- a/include/boost/fiber/algorithm.hpp +++ b/include/boost/fiber/algorithm.hpp @@ -31,13 +31,13 @@ namespace fibers { struct algorithm : private noncopyable { - virtual void spawn( detail::worker_fiber::ptr_t const&) = 0; + virtual void spawn( detail::worker_fiber *) = 0; - virtual void priority( detail::worker_fiber::ptr_t const&, int) BOOST_NOEXCEPT = 0; + virtual void priority( detail::worker_fiber *, int) BOOST_NOEXCEPT = 0; - virtual void join( detail::worker_fiber::ptr_t const&) = 0; + virtual void join( detail::worker_fiber *) = 0; - virtual detail::worker_fiber::ptr_t active() BOOST_NOEXCEPT = 0; + virtual detail::worker_fiber * active() BOOST_NOEXCEPT = 0; virtual void run() = 0; @@ -51,7 +51,7 @@ struct algorithm : private noncopyable virtual void yield() = 0; - virtual detail::fiber_base::ptr_t get_main_fiber() = 0; + virtual detail::fiber_base * get_main_fiber() = 0; virtual ~algorithm() {} }; diff --git a/include/boost/fiber/all.hpp b/include/boost/fiber/all.hpp index de5d0472..7b70fb0f 100644 --- a/include/boost/fiber/all.hpp +++ b/include/boost/fiber/all.hpp @@ -8,10 +8,10 @@ #define BOOST_FIBERS_H #include -#include -#include -#include -#include +//#include +//#include +//#include +//#include #include #include #include diff --git a/include/boost/fiber/condition.hpp b/include/boost/fiber/condition.hpp index f1f72c67..6a1a6408 100644 --- a/include/boost/fiber/condition.hpp +++ b/include/boost/fiber/condition.hpp @@ -51,7 +51,7 @@ class BOOST_FIBERS_DECL condition : private noncopyable { private: detail::spinlock splk_; - std::deque< detail::fiber_base::ptr_t > waiting_; + std::deque< detail::fiber_base * > waiting_; public: condition(); @@ -72,7 +72,7 @@ public: template< typename LockType > void wait( LockType & lt) { - detail::fiber_base::ptr_t n( detail::scheduler::instance()->active() ); + detail::fiber_base * n( detail::scheduler::instance()->active() ); try { if ( n) @@ -143,7 +143,7 @@ public: { cv_status status = cv_status::no_timeout; - detail::fiber_base::ptr_t n( detail::scheduler::instance()->active() ); + detail::fiber_base * n( detail::scheduler::instance()->active() ); try { if ( n) diff --git a/include/boost/fiber/detail/fiber_base.hpp b/include/boost/fiber/detail/fiber_base.hpp index ee47dfeb..00e11eb6 100644 --- a/include/boost/fiber/detail/fiber_base.hpp +++ b/include/boost/fiber/detail/fiber_base.hpp @@ -44,7 +44,7 @@ public: impl_( 0) {} - explicit id( fiber_base::ptr_t impl) BOOST_NOEXCEPT : + explicit id( fiber_base * impl) BOOST_NOEXCEPT : impl_( impl) {} @@ -83,7 +83,7 @@ public: { return 0 == impl_; } }; - fiber_base() : + fiber_base() {} virtual ~fiber_base() {}; @@ -93,8 +93,6 @@ public: virtual void set_ready() BOOST_NOEXCEPT = 0; virtual id get_id() const BOOST_NOEXCEPT = 0; - - virtual void deallocate_object() = 0; }; }}} diff --git a/include/boost/fiber/detail/fifo.hpp b/include/boost/fiber/detail/fifo.hpp index 5a2d1dbf..c0a672cb 100644 --- a/include/boost/fiber/detail/fifo.hpp +++ b/include/boost/fiber/detail/fifo.hpp @@ -7,6 +7,7 @@ #ifndef BOOST_FIBERS_DETAIL_FIFO_H #define BOOST_FIBERS_DETAIL_FIFO_H +#include #include #include @@ -28,27 +29,25 @@ namespace detail { class fifo : private noncopyable { public: - typedef worker_fiber::ptr_t ptr_t; - fifo() BOOST_NOEXCEPT : - head_(), - tail_() + head_( 0), + tail_( 0) {} bool empty() const BOOST_NOEXCEPT - { return 0 == head_.get(); } + { return 0 == head_; } std::size_t size() const BOOST_NOEXCEPT { std::size_t counter = 0; - for ( ptr_t x = head_; x; x = x->next() ) + for ( worker_fiber * x = head_; x; x = x->next() ) ++counter; return counter; } - void push( ptr_t const& item) BOOST_NOEXCEPT + void push( worker_fiber * item) BOOST_NOEXCEPT { - BOOST_ASSERT( item); + BOOST_ASSERT( 0 != item); if ( empty() ) { @@ -59,23 +58,23 @@ public: tail_ = tail_->next(); } - ptr_t head() const BOOST_NOEXCEPT + worker_fiber * head() const BOOST_NOEXCEPT { return head_; } - void top( ptr_t const& item) BOOST_NOEXCEPT + void top( worker_fiber * item) BOOST_NOEXCEPT { head_ = item; } - ptr_t tail() const BOOST_NOEXCEPT + worker_fiber * tail() const BOOST_NOEXCEPT { return tail_; } - void tail( ptr_t const& item) BOOST_NOEXCEPT + void tail( worker_fiber * item) BOOST_NOEXCEPT { tail_ = item; } - ptr_t pop() BOOST_NOEXCEPT + worker_fiber * pop() BOOST_NOEXCEPT { BOOST_ASSERT( ! empty() ); - ptr_t item = head_; + worker_fiber * item = head_; head_ = head_->next(); if ( ! head_) tail_ = head_; @@ -84,16 +83,16 @@ public: return item; } - ptr_t find( ptr_t const& item) BOOST_NOEXCEPT + worker_fiber * find( worker_fiber * item) BOOST_NOEXCEPT { - BOOST_ASSERT( item); + BOOST_ASSERT( 0 != item); - for ( ptr_t x = head_; x; x = x->next() ) + for ( worker_fiber * x = head_; x; x = x->next() ) if ( item == x) return x; - return ptr_t(); + return 0; } - void erase( ptr_t const& item) BOOST_NOEXCEPT + void erase( worker_fiber * item) BOOST_NOEXCEPT { BOOST_ASSERT( item); BOOST_ASSERT( ! empty() ); @@ -103,9 +102,9 @@ public: pop(); return; } - for ( ptr_t x = head_; x; x = x->next() ) + for ( worker_fiber * x = head_; x; x = x->next() ) { - ptr_t nxt = x->next(); + worker_fiber * nxt = x->next(); if ( ! nxt) return; if ( item == nxt) { @@ -120,9 +119,9 @@ public: template< typename Queue, typename Fn > void move_to( Queue & queue, Fn fn) { - for ( ptr_t f = head_, prev = head_; f; ) + for ( worker_fiber * f = head_, * prev = head_; f; ) { - ptr_t nxt = f->next(); + worker_fiber * nxt = f->next(); if ( fn( f) ) { if ( f == head_) @@ -154,13 +153,13 @@ public: void swap( fifo & other) { - head_.swap( other.head_); - tail_.swap( other.tail_); + std::swap( head_, other.head_); + std::swap( tail_, other.tail_); } private: - ptr_t head_; - ptr_t tail_; + worker_fiber * head_; + worker_fiber * tail_; }; }}} diff --git a/include/boost/fiber/detail/flags.hpp b/include/boost/fiber/detail/flags.hpp index ff9d533e..559990e7 100644 --- a/include/boost/fiber/detail/flags.hpp +++ b/include/boost/fiber/detail/flags.hpp @@ -23,7 +23,8 @@ enum flag_t { flag_interruption_blocked = 1 << 0, flag_interruption_requested = 1 << 1, - flag_thread_affinity = 1 << 2 + flag_thread_affinity = 1 << 2, + flag_detached = 1 << 3 }; }}} diff --git a/include/boost/fiber/detail/main_fiber.hpp b/include/boost/fiber/detail/main_fiber.hpp index 916a9cb8..baeeff96 100644 --- a/include/boost/fiber/detail/main_fiber.hpp +++ b/include/boost/fiber/detail/main_fiber.hpp @@ -23,10 +23,8 @@ namespace detail { class main_fiber : public fiber_base { public: - static ptr_t make_pointer( main_fiber & n) { - ptr_t p( & n); - intrusive_ptr_add_ref( p.get() ); - return p; + static main_fiber * make_pointer( main_fiber & n) { + return & n; } main_fiber() : @@ -40,9 +38,6 @@ public: void set_ready() BOOST_NOEXCEPT { ready_ = true; } - void deallocate_object() - {} - id get_id() const BOOST_NOEXCEPT { return id( const_cast< main_fiber * >( this) ); } diff --git a/include/boost/fiber/detail/scheduler.hpp b/include/boost/fiber/detail/scheduler.hpp index 8b7c8cd8..ce62c7a1 100644 --- a/include/boost/fiber/detail/scheduler.hpp +++ b/include/boost/fiber/detail/scheduler.hpp @@ -79,7 +79,7 @@ private: public: template< typename F > - static fiber_base::ptr_t extract( F const& f) BOOST_NOEXCEPT + static fiber_base * extract( F const& f) BOOST_NOEXCEPT { return f.impl_; } static algorithm * instance() diff --git a/include/boost/fiber/detail/worker_fiber.hpp b/include/boost/fiber/detail/worker_fiber.hpp index 32ec1f25..65848eb6 100644 --- a/include/boost/fiber/detail/worker_fiber.hpp +++ b/include/boost/fiber/detail/worker_fiber.hpp @@ -115,6 +115,12 @@ public: bool join( worker_fiber *); + bool detached() const BOOST_NOEXCEPT + { return 0 != ( flags_.load() & flag_detached); } + + void detach() BOOST_NOEXCEPT + { flags_ |= flag_detached; } + bool interruption_blocked() const BOOST_NOEXCEPT { return 0 != ( flags_.load() & flag_interruption_blocked); } @@ -174,11 +180,9 @@ public: void * data, bool cleanup_existing); - bool has_exception() const BOOST_NOEXCEPT + exception_ptr exception() const BOOST_NOEXCEPT { return except_; } - void rethrow() const; - void resume( worker_fiber * f) { if ( 0 == f) @@ -210,10 +214,10 @@ public: BOOST_ASSERT( is_running() ); // set by the scheduler-algorithm } - ptr_t const& next() const + worker_fiber * next() const { return nxt_; } - void next( ptr_t const& nxt) + void next( worker_fiber * nxt) { nxt_ = nxt; } void next_reset() @@ -227,6 +231,8 @@ public: void time_point_reset() { tp_ = (clock_type::time_point::max)(); } + + virtual void deallocate() = 0; }; }}} diff --git a/include/boost/fiber/detail/worker_object.hpp b/include/boost/fiber/detail/worker_object.hpp index 83dc778d..888f833b 100644 --- a/include/boost/fiber/detail/worker_object.hpp +++ b/include/boost/fiber/detail/worker_object.hpp @@ -95,7 +95,7 @@ public: } #endif - void deallocate_object() + void deallocate() { destroy_( alloc_, this); } private: diff --git a/include/boost/fiber/fiber.hpp b/include/boost/fiber/fiber.hpp index 8c1da360..8aae85cf 100644 --- a/include/boost/fiber/fiber.hpp +++ b/include/boost/fiber/fiber.hpp @@ -50,9 +50,8 @@ private: friend class detail::scheduler; typedef detail::worker_fiber base_t; - typedef base_t * ptr_t; - ptr_t impl_; + detail::worker_fiber * impl_; BOOST_MOVABLE_BUT_NOT_COPYABLE( fiber); @@ -65,7 +64,7 @@ public: impl_( 0) {} - explicit fiber( ptr_t imp) BOOST_NOEXCEPT : + explicit fiber( detail::worker_fiber * imp) BOOST_NOEXCEPT : impl_( imp) {} @@ -244,8 +243,11 @@ public: ~fiber() { if ( joinable() ) std::terminate(); - impl_->deallocate_object(); - impl_ = 0; + if ( 0 != impl_) + { + impl_->deallocate(); + impl_ = 0; + } } fiber( BOOST_RV_REF( fiber) other) BOOST_NOEXCEPT : diff --git a/include/boost/fiber/mutex.hpp b/include/boost/fiber/mutex.hpp index 8c3f8557..6862fb9f 100644 --- a/include/boost/fiber/mutex.hpp +++ b/include/boost/fiber/mutex.hpp @@ -39,12 +39,10 @@ private: UNLOCKED }; - detail::spinlock splk_; - state_t state_; - detail::worker_fiber::id owner_; - std::deque< - detail::fiber_base::ptr_t - > waiting_; + detail::spinlock splk_; + state_t state_; + detail::worker_fiber::id owner_; + std::deque< detail::fiber_base * > waiting_; public: typedef unique_lock< mutex > scoped_lock; diff --git a/include/boost/fiber/operations.hpp b/include/boost/fiber/operations.hpp index f53e8aaf..ad1b9099 100644 --- a/include/boost/fiber/operations.hpp +++ b/include/boost/fiber/operations.hpp @@ -25,7 +25,7 @@ namespace this_fiber { inline fibers::fiber::id get_id() BOOST_NOEXCEPT { - return fibers::detail::scheduler::instance()->active() + return 0 != fibers::detail::scheduler::instance()->active() ? fibers::detail::scheduler::instance()->active()->get_id() : fibers::fiber::id(); } @@ -33,7 +33,7 @@ fibers::fiber::id get_id() BOOST_NOEXCEPT inline void yield() { - if ( fibers::detail::scheduler::instance()->active() ) + if ( 0 != fibers::detail::scheduler::instance()->active() ) fibers::detail::scheduler::instance()->yield(); else fibers::detail::scheduler::instance()->run(); @@ -42,7 +42,7 @@ void yield() inline void sleep_until( fibers::clock_type::time_point const& sleep_time) { - if ( fibers::detail::scheduler::instance()->active() ) + if ( 0 != fibers::detail::scheduler::instance()->active() ) { fibers::detail::spinlock splk; unique_lock< fibers::detail::spinlock > lk( splk); @@ -65,7 +65,7 @@ void sleep_for( chrono::duration< Rep, Period > const& timeout_duration) inline bool thread_affinity() BOOST_NOEXCEPT { - return fibers::detail::scheduler::instance()->active() + return 0 != fibers::detail::scheduler::instance()->active() ? fibers::detail::scheduler::instance()->active()->thread_affinity() : true; } @@ -73,7 +73,7 @@ bool thread_affinity() BOOST_NOEXCEPT inline void thread_affinity( bool req) BOOST_NOEXCEPT { - if ( fibers::detail::scheduler::instance()->active() ) + if ( 0 != fibers::detail::scheduler::instance()->active() ) fibers::detail::scheduler::instance()->active()->thread_affinity( req); } diff --git a/include/boost/fiber/recursive_mutex.hpp b/include/boost/fiber/recursive_mutex.hpp index fcb053da..941843ee 100644 --- a/include/boost/fiber/recursive_mutex.hpp +++ b/include/boost/fiber/recursive_mutex.hpp @@ -42,13 +42,11 @@ private: UNLOCKED }; - detail::spinlock splk_; - state_t state_; - detail::worker_fiber::id owner_; - std::size_t count_; - std::deque< - detail::fiber_base::ptr_t - > waiting_; + detail::spinlock splk_; + state_t state_; + detail::worker_fiber::id owner_; + std::size_t count_; + std::deque< detail::fiber_base * > waiting_; public: typedef unique_lock< recursive_mutex > scoped_lock; diff --git a/include/boost/fiber/recursive_timed_mutex.hpp b/include/boost/fiber/recursive_timed_mutex.hpp index 8630e43f..c37905a1 100644 --- a/include/boost/fiber/recursive_timed_mutex.hpp +++ b/include/boost/fiber/recursive_timed_mutex.hpp @@ -42,13 +42,11 @@ private: UNLOCKED }; - detail::spinlock splk_; - state_t state_; - detail::worker_fiber::id owner_; - std::size_t count_; - std::deque< - detail::fiber_base::ptr_t - > waiting_; + detail::spinlock splk_; + state_t state_; + detail::worker_fiber::id owner_; + std::size_t count_; + std::deque< detail::fiber_base * > waiting_; public: typedef unique_lock< recursive_timed_mutex > scoped_lock; diff --git a/include/boost/fiber/round_robin.hpp b/include/boost/fiber/round_robin.hpp index b8d2b657..46ae3776 100644 --- a/include/boost/fiber/round_robin.hpp +++ b/include/boost/fiber/round_robin.hpp @@ -46,27 +46,27 @@ private: typedef detail::fifo wqueue_t; typedef detail::fifo rqueue_t; - detail::worker_fiber::ptr_t active_fiber_; + detail::worker_fiber * active_fiber_; wqueue_t wqueue_; rqueue_t rqueue_; detail::main_fiber mn_; - detail::worker_fiber::ptr_t pick_next_(); + detail::worker_fiber * pick_next_(); - void resume_( detail::worker_fiber::ptr_t const&); + void resume_( detail::worker_fiber *); public: round_robin() BOOST_NOEXCEPT; ~round_robin() BOOST_NOEXCEPT; - void spawn( detail::worker_fiber::ptr_t const&); + void spawn( detail::worker_fiber *); - void priority( detail::worker_fiber::ptr_t const&, int) BOOST_NOEXCEPT; + void priority( detail::worker_fiber *, int) BOOST_NOEXCEPT; - void join( detail::worker_fiber::ptr_t const&); + void join( detail::worker_fiber *); - detail::worker_fiber::ptr_t active() BOOST_NOEXCEPT + detail::worker_fiber * active() BOOST_NOEXCEPT { return active_fiber_; } void run(); @@ -77,8 +77,11 @@ public: void yield(); - detail::fiber_base::ptr_t get_main_fiber() - { return detail::fiber_base::ptr_t( new detail::main_fiber() ); } + // FIXME: must be removed + // allocate main_fiber on stack of mutext, condition, etc. + // and pass address of main_fiber to wait-container + detail::fiber_base * get_main_fiber() + { return new detail::main_fiber(); } }; }} diff --git a/include/boost/fiber/timed_mutex.hpp b/include/boost/fiber/timed_mutex.hpp index e17b8c0c..e67fe265 100644 --- a/include/boost/fiber/timed_mutex.hpp +++ b/include/boost/fiber/timed_mutex.hpp @@ -39,12 +39,10 @@ private: UNLOCKED }; - detail::spinlock splk_; - state_t state_; - detail::worker_fiber::id owner_; - std::deque< - detail::fiber_base::ptr_t - > waiting_; + detail::spinlock splk_; + state_t state_; + detail::worker_fiber::id owner_; + std::deque< detail::fiber_base * > waiting_; public: typedef unique_lock< timed_mutex > scoped_lock; diff --git a/src/condition.cpp b/src/condition.cpp index 9b6dcd30..4e0fa82d 100644 --- a/src/condition.cpp +++ b/src/condition.cpp @@ -28,12 +28,12 @@ condition::~condition() void condition::notify_one() { - detail::fiber_base::ptr_t n; + detail::fiber_base * n = 0; unique_lock< detail::spinlock > lk( splk_); // get one waiting fiber if ( ! waiting_.empty() ) { - n.swap( waiting_.front() ); + n = waiting_.front(); waiting_.pop_front(); } lk.unlock(); @@ -45,7 +45,7 @@ condition::notify_one() void condition::notify_all() { - std::deque< detail::fiber_base::ptr_t > waiting; + std::deque< detail::fiber_base * > waiting; unique_lock< detail::spinlock > lk( splk_); // get all waiting fibers @@ -55,7 +55,7 @@ condition::notify_all() // notify all waiting fibers while ( ! waiting.empty() ) { - detail::fiber_base::ptr_t n( waiting.front() ); + detail::fiber_base * n( waiting.front() ); waiting.pop_front(); BOOST_ASSERT( n); n->set_ready(); diff --git a/src/detail/spinlock.cpp b/src/detail/spinlock.cpp index 8bf3a7e0..a8a7fc67 100644 --- a/src/detail/spinlock.cpp +++ b/src/detail/spinlock.cpp @@ -22,7 +22,7 @@ spinlock::spinlock() : void spinlock::lock() { - bool is_fiber = 0 != scheduler::instance()->active().get(); + bool is_fiber = 0 != scheduler::instance()->active(); for (;;) { // access to CPU's cache diff --git a/src/detail/worker_fiber.cpp b/src/detail/worker_fiber.cpp index e4ba3b3a..19d34bcc 100644 --- a/src/detail/worker_fiber.cpp +++ b/src/detail/worker_fiber.cpp @@ -174,14 +174,6 @@ worker_fiber::set_fss_data( fss_data( data, cleanup_fn) ) ); } -void -worker_fiber::rethrow() const -{ - BOOST_ASSERT( has_exception() ); - - rethrow_exception( except_); -} - }}} #ifdef BOOST_HAS_ABI_HEADERS diff --git a/src/fiber.cpp b/src/fiber.cpp index c76cf1c1..658c14dd 100644 --- a/src/fiber.cpp +++ b/src/fiber.cpp @@ -77,11 +77,13 @@ fiber::join() detail::scheduler::instance()->join( impl_); - ptr_t tmp( 0); + detail::worker_fiber * tmp = 0; std::swap( tmp, impl_); // check if joined fiber was interrupted - if ( tmp->has_exception() ) - tmp->rethrow(); + exception_ptr except( tmp->exception() ); + tmp->deallocate(); + if ( except) + rethrow_exception( except); } void @@ -96,7 +98,8 @@ fiber::detach() BOOST_NOEXCEPT system::errc::invalid_argument, "boost fiber: fiber not joinable") ); } - impl_.reset(); + impl_->detach(); + impl_ = 0; } void diff --git a/src/interruption.cpp b/src/interruption.cpp index c789282b..c4587706 100644 --- a/src/interruption.cpp +++ b/src/interruption.cpp @@ -50,14 +50,14 @@ restore_interruption::~restore_interruption() BOOST_NOEXCEPT bool interruption_enabled() BOOST_NOEXCEPT { - fibers::detail::worker_fiber::ptr_t f( fibers::detail::scheduler::instance()->active() ); - return f && ! f->interruption_blocked(); + fibers::detail::worker_fiber * f = fibers::detail::scheduler::instance()->active(); + return 0 != f && ! f->interruption_blocked(); } bool interruption_requested() BOOST_NOEXCEPT { - fibers::detail::worker_fiber::ptr_t f( fibers::detail::scheduler::instance()->active() ); - if ( ! f) return false; + fibers::detail::worker_fiber * f = fibers::detail::scheduler::instance()->active(); + if ( 0 == f) return false; return f->interruption_requested(); } diff --git a/src/mutex.cpp b/src/mutex.cpp index 465f0bf4..d78e8ecd 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -37,8 +37,8 @@ mutex::~mutex() void mutex::lock() { - detail::fiber_base::ptr_t n( detail::scheduler::instance()->active() ); - if ( n) + detail::fiber_base * n( detail::scheduler::instance()->active() ); + if ( 0 != n) { for (;;) { @@ -118,10 +118,10 @@ mutex::unlock() BOOST_ASSERT( this_fiber::get_id() == owner_); unique_lock< detail::spinlock > lk( splk_); - detail::fiber_base::ptr_t n; + detail::fiber_base * n = 0; if ( ! waiting_.empty() ) { - n.swap( waiting_.front() ); + n = waiting_.front(); waiting_.pop_front(); } owner_ = detail::worker_fiber::id(); diff --git a/src/recursive_mutex.cpp b/src/recursive_mutex.cpp index b7546e5f..d0b60e3a 100644 --- a/src/recursive_mutex.cpp +++ b/src/recursive_mutex.cpp @@ -39,8 +39,8 @@ recursive_mutex::~recursive_mutex() void recursive_mutex::lock() { - detail::fiber_base::ptr_t n( detail::scheduler::instance()->active() ); - if ( n) + detail::fiber_base * n( detail::scheduler::instance()->active() ); + if ( 0 != n) { for (;;) { @@ -138,13 +138,13 @@ recursive_mutex::unlock() BOOST_ASSERT( this_fiber::get_id() == owner_); unique_lock< detail::spinlock > lk( splk_); - detail::fiber_base::ptr_t n; + detail::fiber_base * n = 0; if ( 0 == --count_) { if ( ! waiting_.empty() ) { - n.swap( waiting_.front() ); + n = waiting_.front(); waiting_.pop_front(); } owner_ = detail::worker_fiber::id(); diff --git a/src/recursive_timed_mutex.cpp b/src/recursive_timed_mutex.cpp index 9bd7c009..29d14b54 100644 --- a/src/recursive_timed_mutex.cpp +++ b/src/recursive_timed_mutex.cpp @@ -39,8 +39,8 @@ recursive_timed_mutex::~recursive_timed_mutex() void recursive_timed_mutex::lock() { - detail::fiber_base::ptr_t n( detail::scheduler::instance()->active() ); - if ( n) + detail::fiber_base * n( detail::scheduler::instance()->active() ); + if ( 0 != n) { for (;;) { @@ -134,7 +134,7 @@ recursive_timed_mutex::try_lock() bool recursive_timed_mutex::try_lock_until( clock_type::time_point const& timeout_time) { - detail::fiber_base::ptr_t n( detail::scheduler::instance()->active() ); + detail::fiber_base * n( detail::scheduler::instance()->active() ); if ( n) { for (;;) @@ -231,13 +231,13 @@ recursive_timed_mutex::unlock() BOOST_ASSERT( this_fiber::get_id() == owner_); unique_lock< detail::spinlock > lk( splk_); - detail::fiber_base::ptr_t n; + detail::fiber_base * n = 0; if ( 0 == --count_) { if ( ! waiting_.empty() ) { - n.swap( waiting_.front() ); + n = waiting_.front(); waiting_.pop_front(); } owner_ = detail::worker_fiber::id(); diff --git a/src/round_robin.cpp b/src/round_robin.cpp index 4b5d65c7..7af2cefe 100644 --- a/src/round_robin.cpp +++ b/src/round_robin.cpp @@ -28,7 +28,7 @@ namespace boost { namespace fibers { -bool fetch_ready( detail::worker_fiber::ptr_t & f) +bool fetch_ready( detail::worker_fiber * f) { BOOST_ASSERT( ! f->is_running() ); BOOST_ASSERT( ! f->is_terminated() ); @@ -40,23 +40,23 @@ bool fetch_ready( detail::worker_fiber::ptr_t & f) return f->is_ready(); } -detail::worker_fiber::ptr_t +detail::worker_fiber * round_robin::pick_next_() { - detail::worker_fiber::ptr_t victim; + detail::worker_fiber * victim = 0; if ( ! rqueue_.empty() ) victim = rqueue_.pop(); return victim; } void -round_robin::resume_( detail::worker_fiber::ptr_t const& f) +round_robin::resume_( detail::worker_fiber * f) { BOOST_ASSERT( f); BOOST_ASSERT( f->is_ready() ); // store active fiber in local var - detail::worker_fiber::ptr_t tmp = active_fiber_; + detail::worker_fiber * tmp( active_fiber_); // assign new fiber to active fiber active_fiber_ = f; // set active fiber to state_running @@ -65,16 +65,19 @@ round_robin::resume_( detail::worker_fiber::ptr_t const& f) // this might happend if fiber calls yield() and no // other fiber is in the ready-queue if ( tmp != active_fiber_) + { // resume active-fiber == start or yield to - active_fiber_->resume( tmp.get() ); + active_fiber_->resume( tmp); + if ( active_fiber_->detached() && active_fiber_->is_terminated() ) + active_fiber_->deallocate(); + } - //BOOST_ASSERT( f == active_fiber_); // reset active fiber to previous active_fiber_ = tmp; } round_robin::round_robin() BOOST_NOEXCEPT : - active_fiber_(), + active_fiber_( 0), wqueue_(), rqueue_(), mn_() @@ -92,7 +95,7 @@ round_robin::~round_robin() BOOST_NOEXCEPT } void -round_robin::spawn( detail::worker_fiber::ptr_t const& f) +round_robin::spawn( detail::worker_fiber * f) { rqueue_.push( f); } void @@ -106,7 +109,7 @@ round_robin::run() // pop new fiber from ready-queue which is not complete // (example: fiber in ready-queue could be canceled by active-fiber) - detail::worker_fiber::ptr_t f = pick_next_(); + detail::worker_fiber * f( pick_next_() ); if ( f) { BOOST_ASSERT_MSG( f->is_ready(), "fiber with invalid state in ready-queue"); @@ -163,7 +166,7 @@ round_robin::yield() } void -round_robin::join( detail::worker_fiber::ptr_t const& f) +round_robin::join( detail::worker_fiber * f) { BOOST_ASSERT( f); BOOST_ASSERT( f != active_fiber_); @@ -195,7 +198,7 @@ round_robin::join( detail::worker_fiber::ptr_t const& f) } void -round_robin::priority( detail::worker_fiber::ptr_t const& f, int prio) BOOST_NOEXCEPT +round_robin::priority( detail::worker_fiber * f, int prio) BOOST_NOEXCEPT { BOOST_ASSERT( f); diff --git a/src/timed_mutex.cpp b/src/timed_mutex.cpp index c4b3d49e..bd270154 100644 --- a/src/timed_mutex.cpp +++ b/src/timed_mutex.cpp @@ -37,7 +37,7 @@ timed_mutex::~timed_mutex() void timed_mutex::lock() { - detail::fiber_base::ptr_t n( detail::scheduler::instance()->active() ); + detail::fiber_base * n( detail::scheduler::instance()->active() ); if ( n) { for (;;) @@ -114,7 +114,7 @@ timed_mutex::try_lock() bool timed_mutex::try_lock_until( clock_type::time_point const& timeout_time) { - detail::fiber_base::ptr_t n( detail::scheduler::instance()->active() ); + detail::fiber_base * n( detail::scheduler::instance()->active() ); if ( n) { for (;;) @@ -199,11 +199,11 @@ timed_mutex::unlock() BOOST_ASSERT( this_fiber::get_id() == owner_); unique_lock< detail::spinlock > lk( splk_); - detail::fiber_base::ptr_t n; + detail::fiber_base * n = 0; if ( ! waiting_.empty() ) { - n.swap( waiting_.front() ); + n = waiting_.front(); waiting_.pop_front(); } owner_ = detail::worker_fiber::id();