Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2008-05-04 05:52:55


Author: anthonyw
Date: 2008-05-04 05:52:54 EDT (Sun, 04 May 2008)
New Revision: 45108
URL: http://svn.boost.org/trac/boost/changeset/45108

Log:
Simplified move support
Added:
   trunk/libs/thread/test/no_implicit_assign_from_lvalue_thread.cpp (contents, props changed)
   trunk/libs/thread/test/no_implicit_move_from_lvalue_thread.cpp (contents, props changed)
Text files modified:
   trunk/boost/thread/detail/move.hpp | 17 ++++++++
   trunk/boost/thread/locks.hpp | 37 --------------------
   trunk/boost/thread/pthread/thread.hpp | 10 +----
   trunk/boost/thread/win32/thread.hpp | 9 +---
   trunk/libs/thread/test/Jamfile.v2 | 2 +
   trunk/libs/thread/test/test_move_function.cpp | 74 ++++++++++++++++++++++++++++++++++++++-
   6 files changed, 94 insertions(+), 55 deletions(-)

Modified: trunk/boost/thread/detail/move.hpp
==============================================================================
--- trunk/boost/thread/detail/move.hpp (original)
+++ trunk/boost/thread/detail/move.hpp 2008-05-04 05:52:54 EDT (Sun, 04 May 2008)
@@ -6,6 +6,9 @@
 #ifndef BOOST_THREAD_MOVE_HPP
 #define BOOST_THREAD_MOVE_HPP
 
+#include <boost/utility/enable_if.hpp>
+#include <boost/type_traits/is_convertible.hpp>
+
 namespace boost
 {
     namespace detail
@@ -14,7 +17,7 @@
         struct thread_move_t
         {
             T& t;
- thread_move_t(T& t_):
+ explicit thread_move_t(T& t_):
                 t(t_)
             {}
 
@@ -31,6 +34,18 @@
             void operator=(thread_move_t&);
         };
     }
+
+ template<typename T>
+ typename enable_if<boost::is_convertible<T&,detail::thread_move_t<T> >, detail::thread_move_t<T> >::type move(T& t)
+ {
+ return t;
+ }
+
+ template<typename T>
+ detail::thread_move_t<T> move(detail::thread_move_t<T> t)
+ {
+ return t;
+ }
     
 }
 

Modified: trunk/boost/thread/locks.hpp
==============================================================================
--- trunk/boost/thread/locks.hpp (original)
+++ trunk/boost/thread/locks.hpp 2008-05-04 05:52:54 EDT (Sun, 04 May 2008)
@@ -205,18 +205,6 @@
     };
 
     template<typename Mutex>
- inline detail::thread_move_t<unique_lock<Mutex> > move(unique_lock<Mutex> & x)
- {
- return x.move();
- }
-
- template<typename Mutex>
- inline detail::thread_move_t<unique_lock<Mutex> > move(detail::thread_move_t<unique_lock<Mutex> > x)
- {
- return x;
- }
-
- template<typename Mutex>
     class shared_lock
     {
     protected:
@@ -373,19 +361,6 @@
     };
 
     template<typename Mutex>
- inline detail::thread_move_t<shared_lock<Mutex> > move(shared_lock<Mutex> & x)
- {
- return x.move();
- }
-
- template<typename Mutex>
- inline detail::thread_move_t<shared_lock<Mutex> > move(detail::thread_move_t<shared_lock<Mutex> > x)
- {
- return x;
- }
-
-
- template<typename Mutex>
     class upgrade_lock
     {
     protected:
@@ -512,18 +487,6 @@
 
 
     template<typename Mutex>
- inline detail::thread_move_t<upgrade_lock<Mutex> > move(upgrade_lock<Mutex> & x)
- {
- return x.move();
- }
-
- template<typename Mutex>
- inline detail::thread_move_t<upgrade_lock<Mutex> > move(detail::thread_move_t<upgrade_lock<Mutex> > x)
- {
- return x;
- }
-
- template<typename Mutex>
     unique_lock<Mutex>::unique_lock(detail::thread_move_t<upgrade_lock<Mutex> > other):
         m(other->m),is_locked(other->is_locked)
     {

Modified: trunk/boost/thread/pthread/thread.hpp
==============================================================================
--- trunk/boost/thread/pthread/thread.hpp (original)
+++ trunk/boost/thread/pthread/thread.hpp 2008-05-04 05:52:54 EDT (Sun, 04 May 2008)
@@ -222,16 +222,10 @@
         bool interruption_requested() const;
     };
 
- inline detail::thread_move_t<thread> move(thread& x)
+ inline detail::thread_move_t<thread> move(detail::thread_move_t<thread> t)
     {
- return x.move();
+ return t;
     }
-
- inline detail::thread_move_t<thread> move(detail::thread_move_t<thread> x)
- {
- return x;
- }
-
 
     template<typename F>
     struct thread::thread_data<boost::reference_wrapper<F> >:

Modified: trunk/boost/thread/win32/thread.hpp
==============================================================================
--- trunk/boost/thread/win32/thread.hpp (original)
+++ trunk/boost/thread/win32/thread.hpp 2008-05-04 05:52:54 EDT (Sun, 04 May 2008)
@@ -281,14 +281,9 @@
         bool interruption_requested() const;
     };
 
- inline detail::thread_move_t<thread> move(thread& x)
+ inline detail::thread_move_t<thread> move(detail::thread_move_t<thread> t)
     {
- return x.move();
- }
-
- inline detail::thread_move_t<thread> move(detail::thread_move_t<thread> x)
- {
- return x;
+ return t;
     }
 
     template<typename F>

Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2 (original)
+++ trunk/libs/thread/test/Jamfile.v2 2008-05-04 05:52:54 EDT (Sun, 04 May 2008)
@@ -53,5 +53,7 @@
           [ thread-run test_shared_mutex_part_2.cpp ]
           [ thread-run test_shared_mutex_timed_locks.cpp ]
           [ thread-run test_lock_concept.cpp ]
+ [ compile-fail no_implicit_move_from_lvalue_thread.cpp ]
+ [ compile-fail no_implicit_assign_from_lvalue_thread.cpp ]
     ;
 }

Added: trunk/libs/thread/test/no_implicit_assign_from_lvalue_thread.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/no_implicit_assign_from_lvalue_thread.cpp 2008-05-04 05:52:54 EDT (Sun, 04 May 2008)
@@ -0,0 +1,15 @@
+// Copyright (C) 2008 Anthony Williams
+//
+// 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)
+#include <boost/thread/thread.hpp>
+
+void do_nothing()
+{}
+
+void test()
+{
+ boost::thread t1(do_nothing);
+ boost::thread t2;
+ t2=t1;
+}

Added: trunk/libs/thread/test/no_implicit_move_from_lvalue_thread.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/no_implicit_move_from_lvalue_thread.cpp 2008-05-04 05:52:54 EDT (Sun, 04 May 2008)
@@ -0,0 +1,14 @@
+// Copyright (C) 2008 Anthony Williams
+//
+// 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)
+#include <boost/thread/thread.hpp>
+
+void do_nothing()
+{}
+
+void test()
+{
+ boost::thread t1(do_nothing);
+ boost::thread t2(t1);
+}

Modified: trunk/libs/thread/test/test_move_function.cpp
==============================================================================
--- trunk/libs/thread/test/test_move_function.cpp (original)
+++ trunk/libs/thread/test/test_move_function.cpp 2008-05-04 05:52:54 EDT (Sun, 04 May 2008)
@@ -1,10 +1,11 @@
-// Copyright (C) 2007 Anthony Williams
+// Copyright (C) 2007-8 Anthony Williams
 //
 // 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)
 #include <boost/thread/thread.hpp>
 #include <boost/test/unit_test.hpp>
 #include <boost/thread/mutex.hpp>
+#include <boost/shared_ptr.hpp>
 
 void do_nothing()
 {}
@@ -20,13 +21,36 @@
     dest.join();
 }
 
+void test_thread_move_from_lvalue_on_assignment()
+{
+ boost::thread src(do_nothing);
+ boost::thread::id src_id=src.get_id();
+ boost::thread dest;
+ dest=boost::move(src);
+ boost::thread::id dest_id=dest.get_id();
+ BOOST_CHECK(src_id==dest_id);
+ BOOST_CHECK(src.get_id()==boost::thread::id());
+ dest.join();
+}
+
+boost::thread start_thread()
+{
+ return boost::thread(do_nothing);
+}
+
 void test_thread_move_from_rvalue_on_construction()
 {
- boost::thread x(boost::move(boost::thread(do_nothing)));
+ boost::thread x(start_thread());
     BOOST_CHECK(x.get_id()!=boost::thread::id());
     x.join();
 }
 
+void test_thread_move_from_rvalue_using_explicit_move()
+{
+ boost::thread x(boost::move(start_thread()));
+ BOOST_CHECK(x.get_id()!=boost::thread::id());
+ x.join();
+}
 
 void test_unique_lock_move_from_lvalue_on_construction()
 {
@@ -42,6 +66,48 @@
     BOOST_CHECK(l2.mutex()==&m);
 }
 
+boost::unique_lock<boost::mutex> get_lock(boost::mutex& m)
+{
+ return boost::unique_lock<boost::mutex>(m);
+}
+
+
+void test_unique_lock_move_from_rvalue_on_construction()
+{
+ boost::mutex m;
+ boost::unique_lock<boost::mutex> l(get_lock(m));
+ BOOST_CHECK(l.owns_lock());
+ BOOST_CHECK(l.mutex()==&m);
+}
+
+namespace user
+{
+ template<typename T>
+ T move(T& t)
+ {
+ return t.move();
+ }
+
+ bool move_called=false;
+
+ struct nc:
+ public boost::shared_ptr<int>
+ {
+ nc move()
+ {
+ move_called=true;
+ return nc();
+ }
+ };
+}
+
+void test_move_for_user_defined_type_unaffected()
+{
+ user::nc src;
+ user::nc dest=move(src);
+ BOOST_CHECK(user::move_called);
+}
+
 boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
 {
     boost::unit_test_framework::test_suite* test =
@@ -49,6 +115,10 @@
 
     test->add(BOOST_TEST_CASE(test_thread_move_from_lvalue_on_construction));
     test->add(BOOST_TEST_CASE(test_thread_move_from_rvalue_on_construction));
+ test->add(BOOST_TEST_CASE(test_thread_move_from_rvalue_using_explicit_move));
+ test->add(BOOST_TEST_CASE(test_thread_move_from_lvalue_on_assignment));
     test->add(BOOST_TEST_CASE(test_unique_lock_move_from_lvalue_on_construction));
+ test->add(BOOST_TEST_CASE(test_unique_lock_move_from_rvalue_on_construction));
+ test->add(BOOST_TEST_CASE(test_move_for_user_defined_type_unaffected));
     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