Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58604 - in sandbox/task: boost/task boost/task/detail libs/task/src
From: oliver.kowalke_at_[hidden]
Date: 2009-12-30 16:41:21


Author: olli
Date: 2009-12-30 16:41:20 EST (Wed, 30 Dec 2009)
New Revision: 58604
URL: http://svn.boost.org/trac/boost/changeset/58604

Log:
- intrusive_ptr instead of shared_ptr

Text files modified:
   sandbox/task/boost/task/callable.hpp | 23 ++++++++++++++++++++++-
   sandbox/task/boost/task/context.hpp | 17 ++++++++++++++++-
   sandbox/task/boost/task/detail/pool_base.hpp | 17 +++++++++++++++++
   sandbox/task/boost/task/new_thread.hpp | 3 ++-
   sandbox/task/boost/task/static_pool.hpp | 3 ++-
   sandbox/task/boost/task/task.hpp | 27 ++++++++++++++++++++++++---
   sandbox/task/libs/task/src/context.cpp | 1 +
   7 files changed, 84 insertions(+), 7 deletions(-)

Modified: sandbox/task/boost/task/callable.hpp
==============================================================================
--- sandbox/task/boost/task/callable.hpp (original)
+++ sandbox/task/boost/task/callable.hpp 2009-12-30 16:41:20 EST (Wed, 30 Dec 2009)
@@ -8,6 +8,7 @@
 #define BOOST_TASKS_CALLABLE_H
 
 #include <boost/config.hpp>
+#include <boost/intrusive_ptr.hpp>
 #include <boost/move/move.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/thread.hpp>
@@ -30,9 +31,29 @@
 private:
         struct impl
         {
+ atomic< unsigned int > use_count_;
+
+ impl() :
+ use_count_( 0)
+ {}
+
                 virtual ~impl() {}
+
                 virtual void run() = 0;
+
                 virtual void reset( shared_ptr< thread > const&) = 0;
+
+ inline friend void intrusive_ptr_add_ref( impl * p)
+ { p->use_count_.fetch_add( 1, memory_order_relaxed); }
+
+ inline friend void intrusive_ptr_release( impl * p)
+ {
+ if ( p->use_count_.fetch_sub( 1, memory_order_release) == 1)
+ {
+ atomic_thread_fence( memory_order_acquire);
+ delete p;
+ }
+ }
         };
 
         template< typename T >
@@ -56,7 +77,7 @@
                 { ctx_.reset( thrd); }
         };
 
- shared_ptr< impl > impl_;
+ intrusive_ptr< impl > impl_;
 
 public:
         callable();

Modified: sandbox/task/boost/task/context.hpp
==============================================================================
--- sandbox/task/boost/task/context.hpp (original)
+++ sandbox/task/boost/task/context.hpp 2009-12-30 16:41:20 EST (Wed, 30 Dec 2009)
@@ -7,6 +7,8 @@
 #ifndef BOOST_TASKS_CONTEXT_H
 #define BOOST_TASKS_CONTEXT_H
 
+#include <boost/atomic.hpp>
+#include <boost/intrusive_ptr.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/thread.hpp>
 #include <boost/thread/mutex.hpp>
@@ -33,6 +35,7 @@
         class impl : private noncopyable
         {
         private:
+ atomic< unsigned int > use_count_;
                 bool requested_;
                 mutex mtx_;
                 shared_ptr< thread > thrd_;
@@ -49,9 +52,21 @@
                 void interrupt();
 
                 bool interruption_requested();
+
+ inline friend void intrusive_ptr_add_ref( impl * p)
+ { p->use_count_.fetch_add( 1, memory_order_relaxed); }
+
+ inline friend void intrusive_ptr_release( impl * p)
+ {
+ if ( p->use_count_.fetch_sub( 1, memory_order_release) == 1)
+ {
+ atomic_thread_fence( memory_order_acquire);
+ delete p;
+ }
+ }
         };
 
- shared_ptr< impl > impl_;
+ intrusive_ptr< impl > impl_;
 
 public:
         context();

Modified: sandbox/task/boost/task/detail/pool_base.hpp
==============================================================================
--- sandbox/task/boost/task/detail/pool_base.hpp (original)
+++ sandbox/task/boost/task/detail/pool_base.hpp 2009-12-30 16:41:20 EST (Wed, 30 Dec 2009)
@@ -62,6 +62,7 @@
                 DEACTIVE
         };
 
+ atomic< unsigned int > use_count_;
         worker_group wg_;
         shared_mutex mtx_wg_;
         atomic< state > state_;
@@ -142,6 +143,7 @@
                         posix_time::time_duration const& asleep,
                         scanns const& max_scns,
                         stacksize const& stack_size) :
+ use_count_( 0),
                 wg_(),
                 mtx_wg_(),
                 state_( ACTIVE),
@@ -164,6 +166,7 @@
                         posix_time::time_duration const& asleep,
                         scanns const& max_scns,
                         stacksize const& stack_size) :
+ use_count_( 0),
                 wg_(),
                 mtx_wg_(),
                 state_( ACTIVE),
@@ -184,6 +187,7 @@
                         posix_time::time_duration const& asleep,
                         scanns const& max_scns,
                         stacksize const& stack_size) :
+ use_count_( 0),
                 wg_(),
                 mtx_wg_(),
                 state_( ACTIVE),
@@ -207,6 +211,7 @@
                         posix_time::time_duration const& asleep,
                         scanns const& max_scns,
                         stacksize const& stack_size) :
+ use_count_( 0),
                 wg_(),
                 mtx_wg_(),
                 state_( ACTIVE),
@@ -306,6 +311,18 @@
                                 attr) );
                 return h;
         }
+
+ inline friend void intrusive_ptr_add_ref( pool_base * p)
+ { p->use_count_.fetch_add( 1, memory_order_relaxed); }
+
+ inline friend void intrusive_ptr_release( pool_base * p)
+ {
+ if ( p->use_count_.fetch_sub( 1, memory_order_release) == 1)
+ {
+ atomic_thread_fence( memory_order_acquire);
+ delete p;
+ }
+ }
 };
 
 }}}

Modified: sandbox/task/boost/task/new_thread.hpp
==============================================================================
--- sandbox/task/boost/task/new_thread.hpp (original)
+++ sandbox/task/boost/task/new_thread.hpp 2009-12-30 16:41:20 EST (Wed, 30 Dec 2009)
@@ -71,7 +71,8 @@
                 handle< R > h( f, ctx1);
                 callable ca( t, ctx2);
                 shared_ptr< thread > thrd(
- new thread( wrapper( ca) ),
+ //new thread( wrapper( ca) ),
+ new thread( ca),
                         detail::joiner() );
                 ctx1.reset( thrd);
 

Modified: sandbox/task/boost/task/static_pool.hpp
==============================================================================
--- sandbox/task/boost/task/static_pool.hpp (original)
+++ sandbox/task/boost/task/static_pool.hpp 2009-12-30 16:41:20 EST (Wed, 30 Dec 2009)
@@ -12,6 +12,7 @@
 #include <boost/config.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/fiber/round_robin.hpp>
+#include <boost/intrusive_ptr.hpp>
 #include <boost/move/move.hpp>
 
 #include <boost/task/detail/bind_processor.hpp>
@@ -52,7 +53,7 @@
         struct tag_bind_to_processors {};
 # endif
         
- shared_ptr< base_type > pool_;
+ intrusive_ptr< base_type > pool_;
 
 public:
         static_pool() :

Modified: sandbox/task/boost/task/task.hpp
==============================================================================
--- sandbox/task/boost/task/task.hpp (original)
+++ sandbox/task/boost/task/task.hpp 2009-12-30 16:41:20 EST (Wed, 30 Dec 2009)
@@ -7,6 +7,7 @@
 #ifndef BOOST_TASKS_TASK_H
 #define BOOST_TASKS_TASK_H
 
+#include <boost/atomic.hpp>
 #include <boost/bind.hpp>
 #include <boost/config.hpp>
 #include <boost/preprocessor/repetition.hpp>
@@ -27,10 +28,16 @@
 template< typename R >
 class task_base
 {
-protected:
- bool done_;
+private:
+ template< typename X >
+ friend void intrusive_ptr_add_ref( task_base< X > *);
+ template< typename X >
+ friend void intrusive_ptr_release( task_base< X > *);
+
+ atomic< unsigned int > use_count_;
 
 protected:
+ bool done_;
         spin::promise< R > prom_;
 
         virtual void do_run() = 0;
@@ -57,6 +64,20 @@
         { prom_.set_wait_callback( cb); }
 };
 
+template< typename R >
+void intrusive_ptr_add_ref( task_base< R > * p)
+{ p->use_count_.fetch_add( 1, memory_order_relaxed); }
+
+template< typename R >
+void intrusive_ptr_release( task_base< R > * p)
+{
+ if ( p->use_count_.fetch_sub( 1, memory_order_release) == 1)
+ {
+ atomic_thread_fence( memory_order_acquire);
+ delete p;
+ }
+}
+
 template< typename R, typename Fn >
 class task_wrapper : public task_base< R >
 {
@@ -186,7 +207,7 @@
 
         BOOST_MOVABLE_BUT_NOT_COPYABLE( task);
 
- shared_ptr< detail::task_base< R > > task_;
+ intrusive_ptr< detail::task_base< R > > task_;
 
 public:
         task() :

Modified: sandbox/task/libs/task/src/context.cpp
==============================================================================
--- sandbox/task/libs/task/src/context.cpp (original)
+++ sandbox/task/libs/task/src/context.cpp 2009-12-30 16:41:20 EST (Wed, 30 Dec 2009)
@@ -31,6 +31,7 @@
 }
 
 context::impl::impl() :
+ use_count_( 0),
         requested_( false),
         mtx_(),
         thrd_()


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