Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52407 - in sandbox/threadpool: boost boost/tp boost/tp/detail libs/tp/examples libs/tp/src
From: oliver.kowalke_at_[hidden]
Date: 2009-04-15 11:44:49


Author: olli
Date: 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
New Revision: 52407
URL: http://svn.boost.org/trac/boost/changeset/52407

Log:
* use of promise inside callable
* task_interrupted exception introduced

Added:
   sandbox/threadpool/boost/threadpool.hpp (contents, props changed)
Removed:
   sandbox/threadpool/boost/tp.hpp
   sandbox/threadpool/libs/tp/examples/launch.cpp
Text files modified:
   sandbox/threadpool/boost/tp/detail/callable.hpp | 63 ++++++++++++++++++++++++++++++++++-----
   sandbox/threadpool/boost/tp/exceptions.hpp | 3 +
   sandbox/threadpool/boost/tp/pool.hpp | 20 ++++++------
   sandbox/threadpool/libs/tp/examples/interrupt.cpp | 4 +-
   sandbox/threadpool/libs/tp/examples/shutdonw_now.cpp | 4 +-
   sandbox/threadpool/libs/tp/src/default_pool.cpp | 2
   6 files changed, 73 insertions(+), 23 deletions(-)

Added: sandbox/threadpool/boost/threadpool.hpp
==============================================================================
--- (empty file)
+++ sandbox/threadpool/boost/threadpool.hpp 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
@@ -0,0 +1,24 @@
+// Copyright (c) 2008 Oliver Kowalke. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef BOOST_TP_TP_H
+#define BOOST_TP_TP_H
+
+#include <boost/tp/bounded_channel.hpp>
+#include <boost/tp/exceptions.hpp>
+#include <boost/tp/default_pool.hpp>
+#include <boost/tp/fifo.hpp>
+#include <boost/tp/info.hpp>
+#include <boost/tp/lifo.hpp>
+#include <boost/tp/pool.hpp>
+#include <boost/tp/poolsize.hpp>
+#include <boost/tp/priority.hpp>
+#include <boost/tp/scanns.hpp>
+#include <boost/tp/smart.hpp>
+#include <boost/tp/task.hpp>
+#include <boost/tp/unbounded_channel.hpp>
+#include <boost/tp/watermark.hpp>
+
+#endif // BOOST_TP_TP_H
+

Deleted: sandbox/threadpool/boost/tp.hpp
==============================================================================
--- sandbox/threadpool/boost/tp.hpp 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
+++ (empty file)
@@ -1,24 +0,0 @@
-// Copyright (c) 2008 Oliver Kowalke. Distributed under the Boost
-// Software License, Version 1.0. (See accompanying file
-// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
-
-#ifndef BOOST_TP_TP_H
-#define BOOST_TP_TP_H
-
-#include <boost/tp/bounded_channel.hpp>
-#include <boost/tp/exceptions.hpp>
-#include <boost/tp/default_pool.hpp>
-#include <boost/tp/fifo.hpp>
-#include <boost/tp/info.hpp>
-#include <boost/tp/lifo.hpp>
-#include <boost/tp/pool.hpp>
-#include <boost/tp/poolsize.hpp>
-#include <boost/tp/priority.hpp>
-#include <boost/tp/scanns.hpp>
-#include <boost/tp/smart.hpp>
-#include <boost/tp/task.hpp>
-#include <boost/tp/unbounded_channel.hpp>
-#include <boost/tp/watermark.hpp>
-
-#endif // BOOST_TP_TP_H
-

Modified: sandbox/threadpool/boost/tp/detail/callable.hpp
==============================================================================
--- sandbox/threadpool/boost/tp/detail/callable.hpp (original)
+++ sandbox/threadpool/boost/tp/detail/callable.hpp 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
@@ -6,8 +6,10 @@
 #define BOOST_TP_DETAIL_CALLABLE_H
 
 #include <boost/future.hpp>
+#include <boost/thread.hpp>
 
 #include <boost/thread/detail/move.hpp>
+#include <boost/tp/exceptions.hpp>
 
 namespace boost { namespace tp {
 namespace detail
@@ -21,18 +23,58 @@
                 virtual void run() = 0;
         };
 
- template< typename T >
+ template<
+ typename Act,
+ typename T
+ >
         struct impl_wrapper
         : public impl
         {
- packaged_task< T > tsk;
+ Act act;
+ promise< T > prom;
 
- impl_wrapper( boost::detail::thread_move_t< packaged_task< T > > const& t)
- : tsk( t)
+ impl_wrapper(
+ Act const& act_,
+ promise< T > & prom_)
+ : act( act_), prom( move( prom_) )
                 {}
 
                 void run()
- { tsk(); }
+ {
+ try
+ { prom.set_value( act() ); }
+ catch ( thread_interrupted const&)
+ { prom.set_exception( copy_exception( task_interrupted() ) ); }
+ catch(...)
+ { prom.set_exception( current_exception() ); }
+ }
+ };
+
+ template< typename Act >
+ struct impl_wrapper< Act, void >
+ : public impl
+ {
+ Act act;
+ promise< void > prom;
+
+ impl_wrapper(
+ Act const& act_,
+ promise< void > & prom_)
+ : act( act_), prom( move( prom_) )
+ {}
+
+ void run()
+ {
+ try
+ {
+ act();
+ prom.set_value();
+ }
+ catch ( thread_interrupted const&)
+ { prom.set_exception( copy_exception( task_interrupted() ) ); }
+ catch(...)
+ { prom.set_exception( current_exception() ); }
+ }
         };
 
         boost::shared_ptr< impl > impl_;
@@ -40,9 +82,14 @@
 public:
         callable();
 
- template< typename T >
- callable( boost::detail::thread_move_t< packaged_task< T > > const& t)
- : impl_( new impl_wrapper< T >( t) )
+ template<
+ typename Act,
+ typename T
+ >
+ callable(
+ Act const& act,
+ promise< T > & prom)
+ : impl_( new impl_wrapper< Act, T >( act, prom) )
         {}
 
         void operator()();

Modified: sandbox/threadpool/boost/tp/exceptions.hpp
==============================================================================
--- sandbox/threadpool/boost/tp/exceptions.hpp (original)
+++ sandbox/threadpool/boost/tp/exceptions.hpp 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
@@ -46,6 +46,9 @@
         {}
 };
 
+struct task_interrupted
+{};
+
 class task_rejected
 : public std::runtime_error
 {

Modified: sandbox/threadpool/boost/tp/pool.hpp
==============================================================================
--- sandbox/threadpool/boost/tp/pool.hpp (original)
+++ sandbox/threadpool/boost/tp/pool.hpp 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
@@ -304,8 +304,8 @@
         {
                 typedef typename result_of< Act() >::type R;
                 detail::interrupter intr;
- packaged_task< R > tsk( act);
- shared_future< R > f( tsk.get_future() );
+ promise< R > prom;
+ shared_future< R > f( prom.get_future() );
                 detail::worker * w( detail::worker::tss_get() );
                 if ( w)
                 {
@@ -313,12 +313,12 @@
                                 bind(
                                         & shared_future< R >::is_ready,
                                         f) );
- tsk.set_wait_callback(
+ prom.set_wait_callback(
                                 bind(
                                         ( void ( detail::worker::*)( function< bool() > const&) ) & detail::worker::reschedule_until,
                                         w,
                                         wcb) );
- w->put( detail::callable( move( tsk) ), intr);
+ w->put( detail::callable( act, prom), intr);
                         return task< R >( f, intr);
                 }
                 else
@@ -326,7 +326,7 @@
                         if ( closed_() )
                                 throw task_rejected("pool is closed");
 
- channel_item itm( detail::callable( move( tsk) ), intr);
+ channel_item itm( detail::callable( act, prom), intr);
                         channel_.put( itm);
                         return task< R >( f, intr);
                 }
@@ -342,8 +342,8 @@
         {
                 typedef typename result_of< Act() >::type R;
                 detail::interrupter intr;
- packaged_task< R > tsk( act);
- shared_future< R > f( tsk.get_future() );
+ promise< R > prom;
+ shared_future< R > f( prom.get_future() );
                 detail::worker * w( detail::worker::tss_get() );
                 if ( w)
                 {
@@ -351,12 +351,12 @@
                                 bind(
                                         & shared_future< R >::is_ready,
                                         f) );
- tsk.set_wait_callback(
+ prom.set_wait_callback(
                                 bind(
                                         ( void ( detail::worker::*)( function< bool() > const&) ) & detail::worker::reschedule_until,
                                         w,
                                         wcb) );
- w->put( detail::callable( move( tsk) ), intr);
+ w->put( detail::callable( act, prom), intr);
                         return task< R >( f, intr);
                 }
                 else
@@ -364,7 +364,7 @@
                         if ( closed_() )
                                 throw task_rejected("pool is closed");
 
- channel_item itm( detail::callable( move( tsk) ), attr, intr);
+ channel_item itm( detail::callable( act, prom), attr, intr);
                         channel_.put( itm);
                         return task< R >( f, intr);
                 }

Modified: sandbox/threadpool/libs/tp/examples/interrupt.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/interrupt.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/interrupt.cpp 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
@@ -52,8 +52,8 @@
 
                 return EXIT_SUCCESS;
         }
- catch ( boost::thread_interrupted const& )
- { std::cerr << "thread_interrupted: task was canceled" << std::endl; }
+ catch ( tp::task_interrupted const& )
+ { std::cerr << "task_interrupted: task was interrupted" << std::endl; }
         catch ( std::exception const& e)
         { std::cerr << "exception: " << e.what() << std::endl; }
         catch ( ... )

Deleted: sandbox/threadpool/libs/tp/examples/launch.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/launch.cpp 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
+++ (empty file)
@@ -1,50 +0,0 @@
-#include <iostream>
-#include <cstdlib>
-#include <stdexcept>
-
-#include <boost/bind.hpp>
-
-#include "boost/tp.hpp"
-
-namespace tp = boost::tp;
-
-inline
-int fibonacci_fn( int n)
-{
- if ( n == 0) return 0;
- if ( n == 1) return 1;
- int k1( 1), k2( 0);
- for ( int i( 2); i <= n; ++i)
- {
- boost::this_thread::interruption_point();
- int tmp( k1);
- k1 = k1 + k2;
- k2 = tmp;
- }
- boost::this_thread::interruption_point();
- return k1;
-}
-
-int main( int argc, char *argv[])
-{
- try
- {
- tp::pool<
- tp::unbounded_channel< tp::fifo >
- > pool( tp::poolsize( 1) );
- tp::task< int > t(
- tp::launch_in_pool(
- boost::bind(
- fibonacci_fn,
- 10) ) );
- std::cout << t.get() << std::endl;
-
- return EXIT_SUCCESS;
- }
- catch ( std::exception const& e)
- { std::cerr << "exception: " << e.what() << std::endl; }
- catch ( ... )
- { std::cerr << "unhandled" << std::endl; }
-
- return EXIT_FAILURE;
-}

Modified: sandbox/threadpool/libs/tp/examples/shutdonw_now.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/examples/shutdonw_now.cpp (original)
+++ sandbox/threadpool/libs/tp/examples/shutdonw_now.cpp 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
@@ -48,8 +48,8 @@
 
                 return EXIT_SUCCESS;
         }
- catch ( boost::thread_interrupted const& )
- { std::cerr << "thread_interrupted: thread was interrupted" << std::endl; }
+ catch ( tp::task_interrupted const& )
+ { std::cerr << "task_interrupted: task was interrupted" << std::endl; }
         catch ( std::exception const& e)
         { std::cerr << "exception: " << e.what() << std::endl; }
         catch ( ... )

Modified: sandbox/threadpool/libs/tp/src/default_pool.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/src/default_pool.cpp (original)
+++ sandbox/threadpool/libs/tp/src/default_pool.cpp 2009-04-15 11:44:48 EDT (Wed, 15 Apr 2009)
@@ -5,6 +5,6 @@
 namespace detail
 {
 default_pool
-static_pool::instance = default_pool( poolsize( thread::hardware_concurrency() ) );
+static_pool::instance( poolsize( thread::hardware_concurrency() ) );
 }
 } }
\ No newline at end of file


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