Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58656 - sandbox/fiber/boost/fiber
From: oliver.kowalke_at_[hidden]
Date: 2010-01-03 06:58:48


Author: olli
Date: 2010-01-03 06:58:48 EST (Sun, 03 Jan 2010)
New Revision: 58656
URL: http://svn.boost.org/trac/boost/changeset/58656

Log:
- internal refactoring

Text files modified:
   sandbox/fiber/boost/fiber/bounded_channel.hpp | 424 ++++++++++++++++++++-------------------
   sandbox/fiber/boost/fiber/unbounded_channel.hpp | 251 ++++++++++++-----------
   2 files changed, 344 insertions(+), 331 deletions(-)

Modified: sandbox/fiber/boost/fiber/bounded_channel.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/bounded_channel.hpp (original)
+++ sandbox/fiber/boost/fiber/bounded_channel.hpp 2010-01-03 06:58:48 EST (Sun, 03 Jan 2010)
@@ -25,284 +25,290 @@
 
 namespace boost {
 namespace fibers {
+namespace detail {
 
 template< typename T >
-class bounded_channel
+class bounded_channel_base : private noncopyable
 {
 public:
         typedef optional< T > value_type;
 
 private:
- class impl : private noncopyable
+ struct node
         {
- private:
- struct node
- {
- typedef intrusive_ptr< node > ptr;
+ typedef intrusive_ptr< node > ptr;
 
- std::size_t use_count;
- value_type va;
- ptr next;
-
- node() :
- use_count( 0),
- va(),
- next()
- {}
-
- inline friend void intrusive_ptr_add_ref( node * p)
- { ++p->use_count; }
-
- inline friend void intrusive_ptr_release( node * p)
- { if ( --p->use_count == 0) delete p; }
- };
+ std::size_t use_count;
+ value_type va;
+ ptr next;
+
+ node() :
+ use_count( 0),
+ va(),
+ next()
+ {}
 
- enum state
- {
- ACTIVE = 0,
- DEACTIVE
- };
-
- state state_;
- std::size_t count_;
- typename node::ptr head_;
- mutable mutex head_mtx_;
- typename node::ptr tail_;
- mutable mutex tail_mtx_;
- condition not_empty_cond_;
- condition not_full_cond_;
- std::size_t hwm_;
- std::size_t lwm_;
- std::size_t use_count_;
-
- bool active_() const
- { return ACTIVE == state_; }
-
- void deactivate_()
- { state_ = DEACTIVE; }
-
- std::size_t size_() const
- { return count_; }
+ inline friend void intrusive_ptr_add_ref( node * p)
+ { ++p->use_count; }
+
+ inline friend void intrusive_ptr_release( node * p)
+ { if ( --p->use_count == 0) delete p; }
+ };
 
- bool empty_() const
- { return head_ == get_tail_(); }
+ enum state
+ {
+ ACTIVE = 0,
+ DEACTIVE
+ };
 
- bool full_() const
- { return size_() >= hwm_; }
+ state state_;
+ std::size_t count_;
+ typename node::ptr head_;
+ mutable mutex head_mtx_;
+ typename node::ptr tail_;
+ mutable mutex tail_mtx_;
+ condition not_empty_cond_;
+ condition not_full_cond_;
+ std::size_t hwm_;
+ std::size_t lwm_;
+ std::size_t use_count_;
+
+ bool active_() const
+ { return ACTIVE == state_; }
+
+ void deactivate_()
+ { state_ = DEACTIVE; }
+
+ std::size_t size_() const
+ { return count_; }
 
- typename node::ptr get_tail_() const
- {
- mutex::scoped_lock lk( tail_mtx_);
- typename node::ptr tmp = tail_;
- return tmp;
- }
+ bool empty_() const
+ { return head_ == get_tail_(); }
 
- typename node::ptr pop_head_()
- {
- typename node::ptr old_head = head_;
- head_ = old_head->next;
- --count_;
- return old_head;
- }
+ bool full_() const
+ { return size_() >= hwm_; }
 
- public:
- template< typename Strategy >
- impl(
- scheduler< Strategy > & sched,
- std::size_t hwm,
- std::size_t lwm) :
- state_( ACTIVE),
- count_( 0),
- head_( new node() ),
- head_mtx_( sched),
- tail_( head_),
- tail_mtx_( sched),
- not_empty_cond_( sched),
- not_full_cond_( sched),
- hwm_( hwm),
- lwm_( lwm),
- use_count_( 0)
- {
- if ( hwm_ < lwm_)
- throw invalid_watermark();
- }
+ typename node::ptr get_tail_() const
+ {
+ mutex::scoped_lock lk( tail_mtx_);
+ typename node::ptr tmp = tail_;
+ return tmp;
+ }
 
- template< typename Strategy >
- impl(
- scheduler< Strategy > & sched,
- std::size_t wm) :
- state_( ACTIVE),
- count_( 0),
- head_( new node() ),
- head_mtx_( sched),
- tail_( head_),
- tail_mtx_( sched),
- not_empty_cond_( sched),
- not_full_cond_( sched),
- hwm_( wm),
- lwm_( wm),
- use_count_( 0)
- {}
+ typename node::ptr pop_head_()
+ {
+ typename node::ptr old_head = head_;
+ head_ = old_head->next;
+ --count_;
+ return old_head;
+ }
 
- void upper_bound( std::size_t hwm)
- {
- if ( hwm < lwm_)
- throw invalid_watermark();
- unsigned int tmp( hwm_);
- hwm_ = hwm;
- if ( hwm_ > tmp) not_full_cond_.notify_one();
- }
+public:
+ template< typename Strategy >
+ bounded_channel_base(
+ scheduler< Strategy > & sched,
+ std::size_t hwm,
+ std::size_t lwm) :
+ state_( ACTIVE),
+ count_( 0),
+ head_( new node() ),
+ head_mtx_( sched),
+ tail_( head_),
+ tail_mtx_( sched),
+ not_empty_cond_( sched),
+ not_full_cond_( sched),
+ hwm_( hwm),
+ lwm_( lwm),
+ use_count_( 0)
+ {
+ if ( hwm_ < lwm_)
+ throw invalid_watermark();
+ }
+
+ template< typename Strategy >
+ bounded_channel_base(
+ scheduler< Strategy > & sched,
+ std::size_t wm) :
+ state_( ACTIVE),
+ count_( 0),
+ head_( new node() ),
+ head_mtx_( sched),
+ tail_( head_),
+ tail_mtx_( sched),
+ not_empty_cond_( sched),
+ not_full_cond_( sched),
+ hwm_( wm),
+ lwm_( wm),
+ use_count_( 0)
+ {}
+
+ void upper_bound( std::size_t hwm)
+ {
+ if ( hwm < lwm_)
+ throw invalid_watermark();
+ unsigned int tmp( hwm_);
+ hwm_ = hwm;
+ if ( hwm_ > tmp) not_full_cond_.notify_one();
+ }
 
- std::size_t upper_bound() const
- { return hwm_; }
+ std::size_t upper_bound() const
+ { return hwm_; }
 
- void lower_bound( std::size_t lwm)
- {
- if ( lwm > hwm_ )
- throw invalid_watermark();
- unsigned int tmp( lwm_);
- lwm_ = lwm;
- if ( lwm_ > tmp) not_full_cond_.notify_one();
- }
+ void lower_bound( std::size_t lwm)
+ {
+ if ( lwm > hwm_ )
+ throw invalid_watermark();
+ unsigned int tmp( lwm_);
+ lwm_ = lwm;
+ if ( lwm_ > tmp) not_full_cond_.notify_one();
+ }
 
- std::size_t lower_bound() const
- { return lwm_; }
+ std::size_t lower_bound() const
+ { return lwm_; }
 
- void deactivate()
- { deactivate_(); }
+ void deactivate()
+ { deactivate_(); }
 
- bool empty() const
- {
- mutex::scoped_lock lk( head_mtx_);
- return empty_();
- }
+ bool empty() const
+ {
+ mutex::scoped_lock lk( head_mtx_);
+ return empty_();
+ }
 
- void put( T const& t)
+ void put( T const& t)
+ {
+ typename node::ptr new_node( new node() );
                 {
- typename node::ptr new_node( new node() );
- {
- mutex::scoped_lock lk( tail_mtx_);
+ mutex::scoped_lock lk( tail_mtx_);
 
- if ( full_() )
- {
- while ( active_() && full_() )
- not_full_cond_.wait( lk);
- }
-
- if ( ! active_() )
- throw std::runtime_error("queue is not active");
-
- tail_->va = t;
- tail_->next = new_node;
- tail_ = new_node;
- ++count_;
+ if ( full_() )
+ {
+ while ( active_() && full_() )
+ not_full_cond_.wait( lk);
                         }
- not_empty_cond_.notify_one();
+
+ if ( ! active_() )
+ throw std::runtime_error("queue is not active");
+
+ tail_->va = t;
+ tail_->next = new_node;
+ tail_ = new_node;
+ ++count_;
                 }
+ not_empty_cond_.notify_one();
+ }
 
- bool take( value_type & va)
+ bool take( value_type & va)
+ {
+ mutex::scoped_lock lk( head_mtx_);
+ bool empty = empty_();
+ if ( ! active_() && empty)
+ return false;
+ if ( empty)
                 {
- mutex::scoped_lock lk( head_mtx_);
- bool empty = empty_();
- if ( ! active_() && empty)
- return false;
- if ( empty)
+ try
                         {
- try
- {
- while ( active_() && empty_() )
- not_empty_cond_.wait( lk);
- }
- catch ( fiber_interrupted const&)
- { return false; }
+ while ( active_() && empty_() )
+ not_empty_cond_.wait( lk);
                         }
- if ( ! active_() && empty_() )
- return false;
- swap( va, head_->va);
- pop_head_();
- if ( size_() <= lwm_)
- {
- if ( lwm_ == hwm_)
- not_full_cond_.notify_one();
- else
- // more than one producer could be waiting
- // for submiting an action object
- not_full_cond_.notify_all();
- }
- return va;
+ catch ( fiber_interrupted const&)
+ { return false; }
                 }
+ if ( ! active_() && empty_() )
+ return false;
+ swap( va, head_->va);
+ pop_head_();
+ if ( size_() <= lwm_)
+ {
+ if ( lwm_ == hwm_)
+ not_full_cond_.notify_one();
+ else
+ // more than one producer could be waiting
+ // for submiting an action object
+ not_full_cond_.notify_all();
+ }
+ return va;
+ }
 
- bool try_take( value_type & va)
- {
- mutex::scoped_lock lk( head_mtx_);
- if ( empty_() )
- return false;
- swap( va, head_->va);
- pop_head_();
- bool valid = va;
- if ( valid && size_() <= lwm_)
- {
- if ( lwm_ == hwm_)
- not_full_cond_.notify_one();
- else
- // more than one producer could be waiting
- // in order to submit an task
- not_full_cond_.notify_all();
- }
- return valid;
+ bool try_take( value_type & va)
+ {
+ mutex::scoped_lock lk( head_mtx_);
+ if ( empty_() )
+ return false;
+ swap( va, head_->va);
+ pop_head_();
+ bool valid = va;
+ if ( valid && size_() <= lwm_)
+ {
+ if ( lwm_ == hwm_)
+ not_full_cond_.notify_one();
+ else
+ // more than one producer could be waiting
+ // in order to submit an task
+ not_full_cond_.notify_all();
                 }
+ return valid;
+ }
 
- friend void intrusive_ptr_add_ref( impl * p)
- { ++( p->use_count_s); }
+ friend void intrusive_ptr_add_ref( bounded_channel_base * p)
+ { ++( p->use_count_s); }
 
- friend void intrusive_ptr_release( impl * p)
- { if ( --( & p->use_count_) == 1) delete p; }
- };
+ friend void intrusive_ptr_release( bounded_channel_base * p)
+ { if ( --( & p->use_count_) == 1) delete p; }
+};
 
- intrusive_ptr< impl > impl_;
+}
+
+template< typename T >
+class bounded_channel
+{
+private:
+ intrusive_ptr< detail::bounded_channel_base< T > > base_;
 
 public:
+ typedef typename detail::bounded_channel_base< T >::value_type value_type;
+
         template< typename Strategy >
         bounded_channel(
                         scheduler< Strategy > & sched,
                         unsigned int hwm,
                         unsigned int lwm) :
- impl_( new impl( sched, hwm, lwm) )
+ base_( new detail::bounded_channel_base< T >( sched, hwm, lwm) )
         {}
         
         template< typename Strategy >
         bounded_channel(
                         scheduler< Strategy > & sched,
                         unsigned int wm) :
- impl_( new impl( sched, wm) )
+ base_( new detail::bounded_channel_base< T >( sched, wm) )
         {}
         
         void upper_bound( std::size_t hwm)
- { impl_->upper_bound( hwm); }
+ { base_->upper_bound( hwm); }
         
         std::size_t upper_bound() const
- { return impl_->upper_bound(); }
+ { return base_->upper_bound(); }
         
         void lower_bound( std::size_t lwm)
- { impl_->lower_bound( lwm); }
+ { base_->lower_bound( lwm); }
         
         std::size_t lower_bound() const
- { return impl_->lower_bound(); }
+ { return base_->lower_bound(); }
         
         void deactivate()
- { impl_->deactivate(); }
+ { base_->deactivate(); }
         
         bool empty() const
- { return impl_->empty(); }
+ { return base_->empty(); }
         
         void put( T const& t)
- { impl_->put( t); }
+ { base_->put( t); }
         
         bool take( value_type & va)
- { return impl_->take( va);}
+ { return base_->take( va);}
         
         bool try_take( value_type & va)
- { return impl_->try_take( va); }
+ { return base_->try_take( va); }
 };
 
 }}

Modified: sandbox/fiber/boost/fiber/unbounded_channel.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/unbounded_channel.hpp (original)
+++ sandbox/fiber/boost/fiber/unbounded_channel.hpp 2010-01-03 06:58:48 EST (Sun, 03 Jan 2010)
@@ -26,174 +26,181 @@
 
 namespace boost {
 namespace fibers {
+namespace detail {
 
 template< typename T >
-class unbounded_channel
+class unbounded_channel_base : private noncopyable
 {
 public:
         typedef optional< T > value_type;
 
 private:
- class impl : private noncopyable
+ struct node
         {
- private:
- struct node
- {
- typedef intrusive_ptr< node > ptr;
+ typedef intrusive_ptr< node > ptr;
 
- std::size_t use_count;
- value_type va;
- ptr next;
-
- node() :
- use_count( 0),
- va(),
- next()
- {}
-
- inline friend void intrusive_ptr_add_ref( node * p)
- { ++p->use_count; }
-
- inline friend void intrusive_ptr_release( node * p)
- { if ( --p->use_count == 0) delete p; }
- };
+ std::size_t use_count;
+ value_type va;
+ ptr next;
+
+ node() :
+ use_count( 0),
+ va(),
+ next()
+ {}
 
- enum state
- {
- ACTIVE = 0,
- DEACTIVE
- };
-
- state state_;
- typename node::ptr head_;
- mutable mutex head_mtx_;
- typename node::ptr tail_;
- mutable mutex tail_mtx_;
- condition not_empty_cond_;
- std::size_t use_count_;
-
- bool active_() const
- { return ACTIVE == state_; }
+ inline friend void intrusive_ptr_add_ref( node * p)
+ { ++p->use_count; }
+
+ inline friend void intrusive_ptr_release( node * p)
+ { if ( --p->use_count == 0) delete p; }
+ };
 
- void deactivate_()
- { state_ = DEACTIVE; }
+ enum state
+ {
+ ACTIVE = 0,
+ DEACTIVE
+ };
 
- bool empty_() const
- { return head_ == get_tail_(); }
+ state state_;
+ typename node::ptr head_;
+ mutable mutex head_mtx_;
+ typename node::ptr tail_;
+ mutable mutex tail_mtx_;
+ condition not_empty_cond_;
+ std::size_t use_count_;
+
+ bool active_() const
+ { return ACTIVE == state_; }
 
- typename node::ptr get_tail_() const
- {
- mutex::scoped_lock lk( tail_mtx_);
- typename node::ptr tmp = tail_;
- return tmp;
- }
+ void deactivate_()
+ { state_ = DEACTIVE; }
 
- typename node::ptr pop_head_()
- {
- typename node::ptr old_head = head_;
- head_ = old_head->next;
- return old_head;
- }
+ bool empty_() const
+ { return head_ == get_tail_(); }
 
- public:
- template< typename Strategy >
- impl( scheduler< Strategy > & sched) :
- state_( ACTIVE),
- head_( new node() ),
- head_mtx_( sched),
- tail_( head_),
- tail_mtx_( sched),
- not_empty_cond_( sched),
- use_count_( 0)
- {}
+ typename node::ptr get_tail_() const
+ {
+ mutex::scoped_lock lk( tail_mtx_);
+ typename node::ptr tmp = tail_;
+ return tmp;
+ }
 
- void deactivate()
- { deactivate_(); }
+ typename node::ptr pop_head_()
+ {
+ typename node::ptr old_head = head_;
+ head_ = old_head->next;
+ return old_head;
+ }
 
- bool empty() const
- {
- mutex::scoped_lock lk( head_mtx_);
- return empty_();
- }
+public:
+ template< typename Strategy >
+ unbounded_channel_base( scheduler< Strategy > & sched) :
+ state_( ACTIVE),
+ head_( new node() ),
+ head_mtx_( sched),
+ tail_( head_),
+ tail_mtx_( sched),
+ not_empty_cond_( sched),
+ use_count_( 0)
+ {}
+
+ void deactivate()
+ { deactivate_(); }
 
- void put( T const& t)
+ bool empty() const
+ {
+ mutex::scoped_lock lk( head_mtx_);
+ return empty_();
+ }
+
+ void put( T const& t)
+ {
+ typename node::ptr new_node( new node() );
                 {
- typename node::ptr new_node( new node() );
- {
- mutex::scoped_lock lk( tail_mtx_);
+ mutex::scoped_lock lk( tail_mtx_);
 
- if ( ! active_() )
- throw std::runtime_error("channel is not active");
+ if ( ! active_() )
+ throw std::runtime_error("channel is not active");
 
- tail_->va = t;
- tail_->next = new_node;
- tail_ = new_node;
- }
- not_empty_cond_.notify_one();
+ tail_->va = t;
+ tail_->next = new_node;
+ tail_ = new_node;
                 }
+ not_empty_cond_.notify_one();
+ }
 
- bool take( value_type & va)
+ bool take( value_type & va)
+ {
+ mutex::scoped_lock lk( head_mtx_);
+ bool empty = empty_();
+ if ( ! active_() && empty)
+ return false;
+ if ( empty)
                 {
- mutex::scoped_lock lk( head_mtx_);
- bool empty = empty_();
- if ( ! active_() && empty)
- return false;
- if ( empty)
+ try
                         {
- try
- {
- while ( active_() && empty_() )
- not_empty_cond_.wait( lk);
- }
- catch ( fiber_interrupted const&)
- { return false; }
+ while ( active_() && empty_() )
+ not_empty_cond_.wait( lk);
                         }
- if ( ! active_() && empty_() )
- return false;
- swap( va, head_->va);
- pop_head_();
- return va;
+ catch ( fiber_interrupted const&)
+ { return false; }
                 }
+ if ( ! active_() && empty_() )
+ return false;
+ swap( va, head_->va);
+ pop_head_();
+ return va;
+ }
 
- bool try_take( value_type & va)
- {
- mutex::scoped_lock lk( head_mtx_);
- if ( empty_() )
- return false;
- swap( va, head_->va);
- pop_head_();
- return va;
- }
+ bool try_take( value_type & va)
+ {
+ mutex::scoped_lock lk( head_mtx_);
+ if ( empty_() )
+ return false;
+ swap( va, head_->va);
+ pop_head_();
+ return va;
+ }
 
- friend void intrusive_ptr_add_ref( impl * p)
- { ++( p->use_count_); }
+ friend void intrusive_ptr_add_ref( unbounded_channel_base * p)
+ { ++( p->use_count_); }
 
- friend void intrusive_ptr_release( impl * p)
- { if ( --( p->use_count_) == 1) delete p; }
- };
+ friend void intrusive_ptr_release( unbounded_channel_base * p)
+ { if ( --( p->use_count_) == 1) delete p; }
+};
+
+}
 
- intrusive_ptr< impl > impl_;
+template< typename T >
+class unbounded_channel
+{
+private:
+ intrusive_ptr< detail::unbounded_channel_base< T > > base_;
 
 public:
+ typedef typename detail::unbounded_channel_base< T >::value_type
+ value_type;
+
         template< typename Strategy >
         unbounded_channel( scheduler< Strategy > & sched) :
- impl_( new impl( sched) )
+ base_( new detail::unbounded_channel_base< T >( sched) )
         {}
 
         void deactivate()
- { impl_->deactivate(); }
+ { base_->deactivate(); }
 
         bool empty() const
- { return impl_->empty(); }
+ { return base_->empty(); }
 
         void put( T const& t)
- { impl_->put( t); }
+ { base_->put( t); }
 
         bool take( value_type & va)
- { return impl_->take( va); }
+ { return base_->take( va); }
 
         bool try_take( value_type & va)
- { return impl_->try_take( va); }
+ { return base_->try_take( va); }
 };
 
 }}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk