Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2007-12-17 07:52:50


Author: anthonyw
Date: 2007-12-17 07:52:50 EST (Mon, 17 Dec 2007)
New Revision: 42118
URL: http://svn.boost.org/trac/boost/changeset/42118

Log:
boost::move support for locks
Text files modified:
   trunk/boost/thread/locks.hpp | 78 ++++++++++++++++++++++++++++++++++++++-
   trunk/libs/thread/test/test_move_function.cpp | 20 +++++++++-
   2 files changed, 93 insertions(+), 5 deletions(-)

Modified: trunk/boost/thread/locks.hpp
==============================================================================
--- trunk/boost/thread/locks.hpp (original)
+++ trunk/boost/thread/locks.hpp 2007-12-17 07:52:50 EST (Mon, 17 Dec 2007)
@@ -90,9 +90,20 @@
             m(other->m),is_locked(other->is_locked)
         {
             other->is_locked=false;
+ other->m=0;
         }
         unique_lock(detail::thread_move_t<upgrade_lock<Mutex> > other);
 
+ operator detail::thread_move_t<unique_lock<Mutex> >()
+ {
+ return move();
+ }
+
+ detail::thread_move_t<unique_lock<Mutex> > move()
+ {
+ return detail::thread_move_t<unique_lock<Mutex> >(*this);
+ }
+
         unique_lock& operator=(detail::thread_move_t<unique_lock<Mutex> > other)
         {
             unique_lock temp(other);
@@ -197,6 +208,18 @@
     };
 
     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:
@@ -254,6 +277,17 @@
             }
         }
 
+ operator detail::thread_move_t<shared_lock<Mutex> >()
+ {
+ return move();
+ }
+
+ detail::thread_move_t<shared_lock<Mutex> > move()
+ {
+ return detail::thread_move_t<shared_lock<Mutex> >(*this);
+ }
+
+
         shared_lock& operator=(detail::thread_move_t<shared_lock<Mutex> > other)
         {
             shared_lock temp(other);
@@ -342,6 +376,19 @@
     };
 
     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:
@@ -380,6 +427,17 @@
             }
         }
 
+ operator detail::thread_move_t<upgrade_lock<Mutex> >()
+ {
+ return move();
+ }
+
+ detail::thread_move_t<upgrade_lock<Mutex> > move()
+ {
+ return detail::thread_move_t<upgrade_lock<Mutex> >(*this);
+ }
+
+
         upgrade_lock& operator=(detail::thread_move_t<upgrade_lock<Mutex> > other)
         {
             upgrade_lock temp(other);
@@ -452,6 +510,19 @@
         friend class unique_lock<Mutex>;
     };
 
+
+ 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)
@@ -474,18 +545,18 @@
         upgrade_to_unique_lock& operator=(upgrade_to_unique_lock&);
     public:
         explicit upgrade_to_unique_lock(upgrade_lock<Mutex>& m_):
- source(&m_),exclusive(detail::thread_move(*source))
+ source(&m_),exclusive(move(*source))
         {}
         ~upgrade_to_unique_lock()
         {
             if(source)
             {
- *source=detail::thread_move(exclusive);
+ *source=move(exclusive);
             }
         }
 
         upgrade_to_unique_lock(detail::thread_move_t<upgrade_to_unique_lock<Mutex> > other):
- source(other->source),exclusive(detail::thread_move(other->exclusive))
+ source(other->source),exclusive(move(other->exclusive))
         {
             other->source=0;
         }
@@ -515,6 +586,7 @@
             return exclusive.owns_lock();
         }
     };
+
 }
 
 #endif

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 2007-12-17 07:52:50 EST (Mon, 17 Dec 2007)
@@ -4,6 +4,7 @@
 // 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>
 
 void do_nothing()
 {}
@@ -19,7 +20,7 @@
     dest.join();
 }
 
-void test_thread_move_from_rvalue()
+void test_thread_move_from_rvalue_on_construction()
 {
     boost::thread x(boost::move(boost::thread(do_nothing)));
     BOOST_CHECK(x.get_id()!=boost::thread::id());
@@ -27,12 +28,27 @@
 }
 
 
+void test_unique_lock_move_from_lvalue_on_construction()
+{
+ boost::mutex m;
+ boost::unique_lock<boost::mutex> l(m);
+ BOOST_CHECK(l.owns_lock());
+ BOOST_CHECK(l.mutex()==&m);
+
+ boost::unique_lock<boost::mutex> l2(boost::move(l));
+ BOOST_CHECK(!l.owns_lock());
+ BOOST_CHECK(!l.mutex());
+ BOOST_CHECK(l2.owns_lock());
+ BOOST_CHECK(l2.mutex()==&m);
+}
+
 boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
 {
     boost::unit_test_framework::test_suite* test =
         BOOST_TEST_SUITE("Boost.Threads: thread move test suite");
 
     test->add(BOOST_TEST_CASE(test_thread_move_from_lvalue_on_construction));
- test->add(BOOST_TEST_CASE(test_thread_move_from_rvalue));
+ test->add(BOOST_TEST_CASE(test_thread_move_from_rvalue_on_construction));
+ test->add(BOOST_TEST_CASE(test_unique_lock_move_from_lvalue_on_construction));
     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