Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54980 - in sandbox/task: boost/task boost/task/detail libs/task/src
From: oliver.kowalke_at_[hidden]
Date: 2009-07-16 11:41:28


Author: olli
Date: 2009-07-16 11:41:26 EDT (Thu, 16 Jul 2009)
New Revision: 54980
URL: http://svn.boost.org/trac/boost/changeset/54980

Log:
* refactoring inside worker
* callables poped via swap operation
* swap() member function to callable added

Text files modified:
   sandbox/task/boost/task/bounded_channel.hpp | 6 ++--
   sandbox/task/boost/task/callable.hpp | 2 +
   sandbox/task/boost/task/detail/worker.hpp | 29 +++++++++++--------
   sandbox/task/boost/task/fifo.hpp | 5 +--
   sandbox/task/boost/task/priority.hpp | 49 ++++++++++++++++++--------------
   sandbox/task/boost/task/smart.hpp | 59 ++++++++++++++++++++++++---------------
   sandbox/task/boost/task/unbounded_channel.hpp | 6 ++--
   sandbox/task/libs/task/src/callable.cpp | 4 ++
   8 files changed, 94 insertions(+), 66 deletions(-)

Modified: sandbox/task/boost/task/bounded_channel.hpp
==============================================================================
--- sandbox/task/boost/task/bounded_channel.hpp (original)
+++ sandbox/task/boost/task/bounded_channel.hpp 2009-07-16 11:41:26 EDT (Thu, 16 Jul 2009)
@@ -192,7 +192,7 @@
                 }
                 if ( deactive_now_() || ( deactive_() && empty_() ) )
                         return false;
- ca = queue_.pop();
+ queue_.pop( ca);
                 if ( size_() <= lwm_)
                 {
                         if ( lwm_ == hwm_)
@@ -230,7 +230,7 @@
                 }
                 if ( deactive_now_() || ( deactive_() && empty_() ) )
                         return false;
- ca = queue_.pop();
+ queue_.pop( ca);
                 if ( size_() <= lwm_)
                 {
                         if ( lwm_ == hwm_)
@@ -247,7 +247,7 @@
         {
                 if ( deactive_now_() || empty_() )
                         return false;
- ca = queue_.pop();
+ queue_.pop( ca);
                 bool valid = ! ca.empty();
                 if ( valid && size_() <= lwm_)
                 {

Modified: sandbox/task/boost/task/callable.hpp
==============================================================================
--- sandbox/task/boost/task/callable.hpp (original)
+++ sandbox/task/boost/task/callable.hpp 2009-07-16 11:41:26 EDT (Thu, 16 Jul 2009)
@@ -85,6 +85,8 @@
         void reset();
 
         void reset( shared_ptr< thread > const&);
+
+ void swap( callable &);
 };
 
 class context_guard : private noncopyable

Modified: sandbox/task/boost/task/detail/worker.hpp
==============================================================================
--- sandbox/task/boost/task/detail/worker.hpp (original)
+++ sandbox/task/boost/task/detail/worker.hpp 2009-07-16 11:41:26 EDT (Thu, 16 Jul 2009)
@@ -110,13 +110,18 @@
                 BOOST_ASSERT( ca.empty() );
         }
 
- bool next_global_callable_( callable & ca)
+ bool take_global_callable_(
+ callable & ca,
+ posix_time::time_duration const& asleep)
+ { return pool_.channel_.take( ca, asleep); }
+
+ bool try_take_global_callable_( callable & ca)
         { return pool_.channel_.try_take( ca); }
 
- bool next_local_callable_( callable & ca)
+ bool try_take_local_callable_( callable & ca)
         { return wsq_.try_take( ca); }
         
- bool next_stolen_callable_( callable & ca)
+ bool try_steal_other_callable_( callable & ca)
         {
                 std::size_t idx( rnd_idx_() );
                 for ( std::size_t j( 0); j < pool_.wg_.size(); ++j)
@@ -204,12 +209,10 @@
                 callable ca;
                 while ( ! shutdown_() )
                 {
- if ( next_local_callable_( ca) ||
- next_global_callable_( ca) ||
- next_stolen_callable_( ca) )
+ if ( try_take_local_callable_( ca) ||
+ try_take_global_callable_( ca) ||
+ try_steal_other_callable_( ca) )
                         {
- BOOST_ASSERT( ! ca.empty() );
-
                                 execute_( ca);
                                 scns_ = 0;
                         }
@@ -220,10 +223,12 @@
                                 ++scns_;
                                 if ( scns_ >= max_scns_)
                                 {
+ // should the comparation be atomic or
+ // at least the read of idle_worker_ be atomic ?
                                         if ( pool_.size_() == pool_.idle_worker_)
                                         {
- pool_.channel_.take( ca, asleep_);
- if ( ! ca.empty() ) execute_( ca);
+ if ( take_global_callable_( ca, asleep_) )
+ execute_( ca);
                                         }
                                         else
                                                 this_thread::sleep( asleep_);
@@ -238,9 +243,9 @@
         void reschedule_until( function< bool() > const& pred)
         {
                 callable ca;
- while ( ! pred() && ! shutdown_() )
+ while ( ! pred() /* && ! shutdown_() */)
                 {
- if ( next_local_callable_( ca) )
+ if ( try_take_local_callable_( ca) )
                         {
                                 execute_( ca);
                                 scns_ = 0;

Modified: sandbox/task/boost/task/fifo.hpp
==============================================================================
--- sandbox/task/boost/task/fifo.hpp (original)
+++ sandbox/task/boost/task/fifo.hpp 2009-07-16 11:41:26 EDT (Thu, 16 Jul 2009)
@@ -35,11 +35,10 @@
                 void push( item const& itm)
                 { lst_.push_back( itm); }
         
- const item pop()
+ void pop( callable & ca)
                 {
- item itm( lst_.front() );
+ ca.swap( lst_.front() );
                         lst_.pop_front();
- return itm;
                 }
         
                 std::size_t size() const

Modified: sandbox/task/boost/task/priority.hpp
==============================================================================
--- sandbox/task/boost/task/priority.hpp (original)
+++ sandbox/task/boost/task/priority.hpp 2009-07-16 11:41:26 EDT (Thu, 16 Jul 2009)
@@ -12,7 +12,7 @@
 
 #include <boost/assert.hpp>
 #include <boost/multi_index_container.hpp>
-#include <boost/multi_index/mem_fun.hpp>
+#include <boost/multi_index/member.hpp>
 #include <boost/multi_index/ordered_index.hpp>
 
 #include <boost/task/callable.hpp>
@@ -38,24 +38,16 @@
                 typedef Ord ordering;
 
         public:
- class item
+ struct item
                 {
- private:
- callable ca_;
- attribute attr_;
+ callable ca;
+ attribute attr;
         
- public:
                         item(
- callable const& ca,
- attribute const& attr)
- : ca_( ca), attr_( attr)
- { BOOST_ASSERT( ! ca_.empty() ); }
-
- const callable ca() const
- { return ca_; }
-
- const attribute attr() const
- { return attr_; }
+ callable const& ca_,
+ attribute const& attr_)
+ : ca( ca_), attr( attr_)
+ { BOOST_ASSERT( ! ca.empty() ); }
                 };
         
         private:
@@ -63,9 +55,9 @@
                         item,
                         multi_index::indexed_by<
                                 multi_index::ordered_non_unique<
- multi_index::const_mem_fun<
+ multi_index::member<
                                                 item,
- const attribute,
+ attribute,
                                                 & item::attr
>,
                                         ordering
@@ -73,6 +65,20 @@
>
> list;
                 typedef typename list::template nth_index< 0 >::type index;
+
+ class swapper
+ {
+ private:
+ callable & ca_;
+
+ public:
+ swapper( callable & ca)
+ : ca_( ca)
+ {}
+
+ void operator()( item & itm)
+ { ca_.swap( itm.ca); }
+ };
         
                 list lst_;
                 index & idx_;
@@ -88,15 +94,14 @@
                 {}
         
                 void push( item const& itm)
- { idx_.insert( itm); }
+ { lst_.insert( itm); }
         
- const callable pop()
+ void pop( callable & ca)
                 {
                         iterator i( lst_.begin() );
                         BOOST_ASSERT( i != lst_.end() );
- item itm( * i);
+ lst_.modify( i, swapper( ca) );
                         lst_.erase( i);
- return itm.ca();
                 }
         
                 std::size_t size() const

Modified: sandbox/task/boost/task/smart.hpp
==============================================================================
--- sandbox/task/boost/task/smart.hpp (original)
+++ sandbox/task/boost/task/smart.hpp 2009-07-16 11:41:26 EDT (Thu, 16 Jul 2009)
@@ -11,7 +11,7 @@
 
 #include <boost/assert.hpp>
 #include <boost/multi_index_container.hpp>
-#include <boost/multi_index/mem_fun.hpp>
+#include <boost/multi_index/member.hpp>
 #include <boost/multi_index/ordered_index.hpp>
 
 #include <boost/task/callable.hpp>
@@ -41,28 +41,26 @@
                 typedef Ord ordering;
 
         public:
- class item
+ struct item
                 {
- private:
- callable ca_;
- attribute attr_;
+ callable ca;
+ attribute attr;
         
- public:
                         item()
- : ca_(), attr_()
+ : ca(), attr()
                         {}
 
                         item(
- callable const& ca,
- attribute const& attr)
- : ca_( ca), attr_( attr)
- { BOOST_ASSERT( ! ca_.empty() ); }
-
- const callable ca() const
- { return ca_; }
-
- const attribute attr() const
- { return attr_; }
+ callable const& ca_,
+ attribute const& attr_)
+ : ca( ca_), attr( attr_)
+ { BOOST_ASSERT( ! ca.empty() ); }
+
+ void swap( item & other)
+ {
+ ca.swap( other.ca);
+ std::swap( attr, other.attr);
+ }
                 };
         
         private:
@@ -70,9 +68,9 @@
                         item,
                         multi_index::indexed_by<
                                 multi_index::ordered_non_unique<
- multi_index::const_mem_fun<
+ multi_index::member<
                                                 item,
- const attribute,
+ attribute,
                                                 & item::attr
>,
                                         ordering
@@ -103,11 +101,11 @@
                 void push( item const& itm)
                 { enq_op_( idx_, itm); }
         
- const callable pop()
+ void pop( callable & ca)
                 {
                         item itm;
                         deq_op_( idx_, itm);
- return itm.ca();
+ ca.swap( itm.ca);
                 }
         
                 std::size_t size() const
@@ -142,7 +140,7 @@
         void operator()( Index & idx, Item const& itm)
         {
                 typedef typename Index::iterator iterator;
- iterator i( idx.find( itm.attr() ) );
+ iterator i( idx.find( itm.attr) );
                 if ( i == idx.end() )
                         idx.insert( itm);
                 else
@@ -152,6 +150,21 @@
 
 struct take_oldest
 {
+ template< typename Item >
+ class swapper
+ {
+ private:
+ Item & itm_;
+
+ public:
+ swapper( Item & itm)
+ : itm_( itm)
+ {}
+
+ void operator()( Item & itm)
+ { itm_.swap( itm); }
+ };
+
         template<
                 typename Index,
                 typename Item
@@ -161,7 +174,7 @@
                 typedef typename Index::iterator iterator;
                 iterator i( idx.begin() );
                 BOOST_ASSERT( i != idx.end() );
- itm = * i;
+ idx.modify( i, swapper< Item >( itm) );
                 idx.erase( i);
         }
 };

Modified: sandbox/task/boost/task/unbounded_channel.hpp
==============================================================================
--- sandbox/task/boost/task/unbounded_channel.hpp (original)
+++ sandbox/task/boost/task/unbounded_channel.hpp 2009-07-16 11:41:26 EDT (Thu, 16 Jul 2009)
@@ -135,7 +135,7 @@
                 }
                 if ( deactive_now_() || ( deactive_() && empty_() ) )
                         return false;
- ca = queue_.pop();
+ queue_.pop( ca);
                 return ! ca.empty();
         }
 
@@ -164,7 +164,7 @@
                 }
                 if ( deactive_now_() || ( deactive_() && empty_() ) )
                         return false;
- ca = queue_.pop();
+ queue_.pop( ca);
                 return ! ca.empty();
         }
 
@@ -172,7 +172,7 @@
         {
                 if ( deactive_now_() || empty_() )
                         return false;
- ca = queue_.pop();
+ queue_.pop( ca);
                 return ! ca.empty();
         }
 

Modified: sandbox/task/libs/task/src/callable.cpp
==============================================================================
--- sandbox/task/libs/task/src/callable.cpp (original)
+++ sandbox/task/libs/task/src/callable.cpp 2009-07-16 11:41:26 EDT (Thu, 16 Jul 2009)
@@ -33,4 +33,8 @@
 callable::reset( shared_ptr< thread > const& thrd)
 { impl_->reset( thrd); }
 
+void
+callable::swap( callable & other)
+{ impl_.swap( other.impl_); }
+
 }}


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