Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54320 - in sandbox/async: boost/async libs/async/test
From: vicente.botet_at_[hidden]
Date: 2009-06-24 18:07:25


Author: viboes
Date: 2009-06-24 18:07:24 EDT (Wed, 24 Jun 2009)
New Revision: 54320
URL: http://svn.boost.org/trac/boost/changeset/54320

Log:
Boost.Async V0.2: Adding unique_joiner to shared_joiner conversion
Text files modified:
   sandbox/async/boost/async/threader.hpp | 199 +++++++++++++++++++++++++++------------
   sandbox/async/libs/async/test/test_threader.cpp | 27 +++++
   2 files changed, 166 insertions(+), 60 deletions(-)

Modified: sandbox/async/boost/async/threader.hpp
==============================================================================
--- sandbox/async/boost/async/threader.hpp (original)
+++ sandbox/async/boost/async/threader.hpp 2009-06-24 18:07:24 EDT (Wed, 24 Jun 2009)
@@ -46,17 +46,14 @@
 template <typename ResultType>
 class unique_joiner;
 
-template<typename T>
-struct act_traits<unique_joiner<T> > : act_traits<unique_future<T> > {};
+template <typename ResultType>
+class shared_joiner;
 
 
-template <typename ResultType>
-class unique_joiner {
-public:
- typedef ResultType result_type;
- typedef typename act_traits<unique_joiner<ResultType> >::move_dest_type move_dest_type;
-private:
+namespace detail {
+ template <typename ResultType>
     struct unique_joiner_data {
+ typedef ResultType result_type;
         unique_future< result_type > fut_;
         thread th_;
         on_destruction::type on_destruction_;
@@ -100,8 +97,82 @@
 #endif
 
     };
- shared_ptr<unique_joiner_data> data_;
- typedef unique_joiner_data this_impl_type;
+
+ template <typename ResultType>
+ struct shared_joiner_data {
+ typedef ResultType result_type;
+ shared_future< result_type > fut_;
+ thread th_;
+ on_destruction::type on_destruction_;
+
+ shared_joiner_data(on_destruction::type on_destruction_do= on_destruction::do_join)
+ : on_destruction_(on_destruction_do)
+ {}
+ ~shared_joiner_data() {
+ if (th_.joinable()) {
+ if (on_destruction_== on_destruction::do_terminate) {
+ std::terminate();
+ } else if (on_destruction_== on_destruction::do_join) {
+ th_.join();
+ }
+ }
+ }
+
+ template <typename Nullary>
+#ifdef BOOST_THREAD_HAS_THREAD_ATTR
+ shared_joiner_data(thread::native_handle_attr_type& attr, Nullary f, on_destruction::type on_destruction_do) {
+ : on_destruction(on_destruction_do)
+ {
+ packaged_task<result_type> tsk(f);
+ fut_ = tsk.get_future();
+ thread th(attr, boost::move(tsk));
+ th_ = boost::move(th);
+ }
+#else
+ shared_joiner_data(Nullary f, on_destruction::type on_destruction_do)
+ : on_destruction_(on_destruction_do)
+ {
+ packaged_task<result_type> tsk(f);
+ fut_ = tsk.get_future();
+#if 0
+ th_ = boost::move(tsk);
+#else
+ thread th(boost::move(tsk));
+ th_ = boost::move(th);
+#endif
+ }
+#endif
+ shared_joiner_data(unique_joiner_data<result_type>& data)
+ : fut_(data.fut_)
+ , th_(boost::move(data.th_))
+ , on_destruction_(data.on_destruction_)
+ {}
+
+ shared_joiner_data& operator=(unique_joiner_data<result_type>& data)
+ {
+ fut_= data.fut_;
+ th_ = boost::move(data.th_);
+ on_destruction_ = data.on_destruction_;
+ return *this;
+ }
+
+ };
+
+}
+
+template<typename T>
+struct act_traits<unique_joiner<T> > : act_traits<unique_future<T> > {};
+
+template <typename T> class shared_joiner;
+
+template <typename ResultType>
+class unique_joiner {
+public:
+ typedef ResultType result_type;
+ typedef typename act_traits<unique_joiner<ResultType> >::move_dest_type move_dest_type;
+private:
+ shared_ptr<detail::unique_joiner_data<result_type> > data_;
+ typedef detail::unique_joiner_data<result_type> this_impl_type;
     typedef unique_joiner this_type;
 
     unique_joiner(unique_joiner<ResultType>& other);
@@ -109,6 +180,7 @@
 //protected:
 public:
     friend class unique_threader;
+ friend class shared_joiner<result_type>;
 
     template <typename Nullary>
     // requires result_of<Nullary>::type is convertible to ResultType
@@ -293,64 +365,21 @@
     typedef typename act_traits<shared_joiner<ResultType> >::move_dest_type move_dest_type;
 
 private:
- struct shared_joiner_data {
- shared_future< result_type > fut_;
- thread th_;
- on_destruction::type on_destruction_;
-
- shared_joiner_data(on_destruction::type on_destruction_do= on_destruction::do_join)
- : on_destruction_(on_destruction_do)
- {}
- ~shared_joiner_data() {
- if (th_.joinable()) {
- if (on_destruction_== on_destruction::do_terminate) {
- std::terminate();
- } else if (on_destruction_== on_destruction::do_join) {
- th_.join();
- }
- }
- }
-
- template <typename Nullary>
-#ifdef BOOST_THREAD_HAS_THREAD_ATTR
- shared_joiner_data(thread::native_handle_attr_type& attr, Nullary f, on_destruction::type on_destruction_do) {
- : on_destruction(on_destruction_do)
- {
- packaged_task<result_type> tsk(f);
- fut_ = tsk.get_future();
- thread th(attr, boost::move(tsk));
- th_ = boost::move(th);
- }
-#else
- shared_joiner_data(Nullary f, on_destruction::type on_destruction_do)
- : on_destruction_(on_destruction_do)
- {
- packaged_task<result_type> tsk(f);
- fut_ = tsk.get_future();
-#if 0
- th_ = boost::move(tsk);
-#else
- thread th(boost::move(tsk));
- th_ = boost::move(th);
-#endif
- }
-#endif
-
- };
- shared_ptr<shared_joiner_data> data_;
- typedef shared_joiner_data this_impl_type;
+ shared_ptr<detail::shared_joiner_data<result_type> > data_;
+ typedef detail::shared_joiner_data<result_type> this_impl_type;
     typedef shared_joiner this_type;
 
 //protected:
 public:
     friend class shared_threader;
+
     template <typename Nullary>
     // requires result_of<Nullary>::type is convertible to ResultType
 #ifdef BOOST_THREAD_HAS_THREAD_ATTR
- shared_joiner(thread::native_handle_attr_type& attr, Nullary f, on_destruction::type on_destruction_do=on_destruction::do_join)
+ shared_joiner(thread::native_handle_attr_type& attr, Nullary f, bool, on_destruction::type on_destruction_do=on_destruction::do_join)
     : data_(new this_impl_type(attr, f,on_destruction_do))
 #else
- shared_joiner(Nullary f, on_destruction::type on_destruction_do=on_destruction::do_join)
+ shared_joiner(Nullary f, bool, on_destruction::type on_destruction_do=on_destruction::do_join)
     : data_(new this_impl_type(f,on_destruction_do))
 #endif
     {
@@ -376,6 +405,30 @@
         other.data_.reset(new this_impl_type());
         return *this;
     }
+ shared_joiner(unique_joiner<ResultType> && other)
+ {
+ if (other->data_.get()!=0) {
+ this_impl_type* tmp=new this_impl_type(*other->data_.get());
+ data_.swap(tmp);
+ other->data_.reset();
+ }
+ }
+ shared_joiner& operator=(unique_joiner<ResultType> && other)
+ {
+ if (data_.get()!=0) {
+ if (other->data_.get()!=0) {
+ *data_=*other->data_;
+ } else {
+ data_.reset();
+ }
+ } else {
+ if (other->data_.get()!=0) {
+ data_ = new this_impl_type(*other->data_.get());
+ }
+ }
+ other.data_.reset();
+ return *this;
+ }
 
     shared_joiner&& move() {
         return static_cast<shared_joiner&&>(*this);
@@ -386,12 +439,38 @@
         x->data_.reset(new this_impl_type());
     }
 
+ shared_joiner(boost::detail::thread_move_t<unique_joiner<ResultType> > other)
+ {
+ if (other->data_.get()!=0) {
+ shared_ptr<this_impl_type> tmp(new this_impl_type(*other->data_.get()));
+ data_.swap(tmp);
+ }
+ other->data_.reset();
+ }
+
     shared_joiner& operator=(boost::detail::thread_move_t<shared_joiner> x) {
         this_type tmp_(x);
         swap(tmp_);
         return *this;
     }
 
+ shared_joiner& operator=(boost::detail::thread_move_t<unique_joiner<ResultType> > other)
+ {
+ if (data_.get()!=0) {
+ if (other->data_.get()!=0) {
+ *data_=*(other->data_);
+ } else {
+ data_.reset();
+ }
+ } else {
+ if (other->data_.get()!=0) {
+ data_.reset(new this_impl_type(*other->data_.get()));
+ }
+ }
+ other->data_.reset();
+ return *this;
+ }
+
     operator boost::detail::thread_move_t<shared_joiner>() {
         return move();
     }
@@ -506,9 +585,9 @@
     fork(F f) {
         typedef typename boost::result_of<F()>::type result_type;
 #ifdef BOOST_THREAD_HAS_THREAD_ATTR
- return shared_joiner<result_type>(attr(), f);
+ return shared_joiner<result_type>(attr(), f,true);
 #else
- return shared_joiner<result_type>(f);
+ return shared_joiner<result_type>(f,true);
 #endif
     }
 

Modified: sandbox/async/libs/async/test/test_threader.cpp
==============================================================================
--- sandbox/async/libs/async/test/test_threader.cpp (original)
+++ sandbox/async/libs/async/test/test_threader.cpp 2009-06-24 18:07:24 EDT (Wed, 24 Jun 2009)
@@ -238,10 +238,35 @@
     BOOST_CHECK_EQUAL(i, 3);
 }
 
+void do_test_joiner()
+{
+ typedef basync::unique_threader AE;
+ typedef basync::unique_joiner<int> UACT;
+ typedef basync::shared_joiner<int> SACT;
+ AE ae;
+ test_value=0;
+ UACT uact(ae.fork(simple_thread));
+#if 0
+ SACT sact(uact);
+#else
+ SACT sact;
+ sact=uact;
+#endif
+ int res_value = basync::get(sact);
+ BOOST_CHECK_EQUAL(test_value, 999);
+ BOOST_CHECK_EQUAL(res_value, 999);
+ BOOST_CHECK_EQUAL(basync::is_ready(sact), true);
+ BOOST_CHECK_EQUAL(basync::has_value(sact), true);
+ BOOST_CHECK_EQUAL(basync::has_exception(sact), false);
+ std::cout << "<<do_test_joiner" << std::endl;
+
+}
+
 test_suite* init_unit_test_suite(int, char*[])
 {
     test_suite* test = BOOST_TEST_SUITE("shared_threader");
 
+#if 0
 
     test->add(BOOST_TEST_CASE(&do_test_member_fork));
     test->add(BOOST_TEST_CASE(&do_test_member_fork_bind));
@@ -282,5 +307,7 @@
 
     test->add(BOOST_TEST_CASE(&do_test_member_fork_move_unique));
 #endif
+#endif
+ test->add(BOOST_TEST_CASE(&do_test_joiner));
   return test;
 }


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