Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2008-04-28 05:00:59


Author: anthonyw
Date: 2008-04-28 05:00:58 EDT (Mon, 28 Apr 2008)
New Revision: 44838
URL: http://svn.boost.org/trac/boost/changeset/44838

Log:
Added detail::try_lock_wrapper for use as scoped_try_lock typedefs, to fix issue #1873
Text files modified:
   trunk/boost/thread/detail/move.hpp | 5 +++
   trunk/boost/thread/locks.hpp | 66 ++++++++++++++++++++++++++++++++++++++++
   trunk/boost/thread/pthread/mutex.hpp | 4 +-
   trunk/boost/thread/pthread/recursive_mutex.hpp | 4 +-
   trunk/boost/thread/win32/mutex.hpp | 4 +-
   trunk/boost/thread/win32/recursive_mutex.hpp | 4 +-
   trunk/libs/smart_ptr/test/Jamfile.v2 | 1
   trunk/libs/thread/test/test_lock_concept.cpp | 61 ++++++++++++++++++++++++++++++++++++
   8 files changed, 141 insertions(+), 8 deletions(-)

Modified: trunk/boost/thread/detail/move.hpp
==============================================================================
--- trunk/boost/thread/detail/move.hpp (original)
+++ trunk/boost/thread/detail/move.hpp 2008-04-28 05:00:58 EDT (Mon, 28 Apr 2008)
@@ -18,6 +18,11 @@
                 t(t_)
             {}
 
+ T& operator*() const
+ {
+ return t;
+ }
+
             T* operator->() const
             {
                 return &t;

Modified: trunk/boost/thread/locks.hpp
==============================================================================
--- trunk/boost/thread/locks.hpp (original)
+++ trunk/boost/thread/locks.hpp 2008-04-28 05:00:58 EDT (Mon, 28 Apr 2008)
@@ -587,6 +587,72 @@
         }
     };
 
+ namespace detail
+ {
+ template<typename Mutex>
+ class try_lock_wrapper:
+ private unique_lock<Mutex>
+ {
+ typedef unique_lock<Mutex> base;
+ public:
+ explicit try_lock_wrapper(Mutex& m):
+ base(m,try_to_lock)
+ {}
+
+ try_lock_wrapper(Mutex& m_,adopt_lock_t):
+ base(m_,adopt_lock)
+ {}
+ try_lock_wrapper(Mutex& m_,defer_lock_t):
+ base(m_,defer_lock)
+ {}
+ try_lock_wrapper(Mutex& m_,try_to_lock_t):
+ base(m,try_to_lock)
+ {}
+ try_lock_wrapper(detail::thread_move_t<try_lock_wrapper<Mutex> > other):
+ base(detail::thread_move_t<base>(*other))
+ {}
+
+ operator detail::thread_move_t<try_lock_wrapper<Mutex> >()
+ {
+ return move();
+ }
+
+ detail::thread_move_t<try_lock_wrapper<Mutex> > move()
+ {
+ return detail::thread_move_t<try_lock_wrapper<Mutex> >(*this);
+ }
+
+ try_lock_wrapper& operator=(detail::thread_move_t<try_lock_wrapper<Mutex> > other)
+ {
+ try_lock_wrapper temp(other);
+ swap(temp);
+ return *this;
+ }
+
+ void swap(try_lock_wrapper& other)
+ {
+ base::swap(other);
+ }
+ void swap(detail::thread_move_t<try_lock_wrapper<Mutex> > other)
+ {
+ base::swap(*other);
+ }
+
+ using base::lock;
+ using base::try_lock;
+ using base::unlock;
+ using base::owns_lock;
+ using base::operator!;
+ using base::mutex;
+ using base::release;
+
+ typedef void (unique_lock::*bool_type)();
+ operator bool_type() const
+ {
+ return owns_lock()?&unique_lock::lock:0;
+ }
+ };
+ }
 }
 
 #endif

Modified: trunk/boost/thread/pthread/mutex.hpp
==============================================================================
--- trunk/boost/thread/pthread/mutex.hpp (original)
+++ trunk/boost/thread/pthread/mutex.hpp 2008-04-28 05:00:58 EDT (Mon, 28 Apr 2008)
@@ -69,7 +69,7 @@
         }
 
         typedef unique_lock<mutex> scoped_lock;
- typedef scoped_lock scoped_try_lock;
+ typedef detail::try_lock_wrapper<mutex> scoped_try_lock;
     };
 
     typedef mutex try_mutex;
@@ -187,7 +187,7 @@
 #endif
 
         typedef unique_lock<timed_mutex> scoped_timed_lock;
- typedef scoped_timed_lock scoped_try_lock;
+ typedef detail::try_lock_wrapper<timed_mutex> scoped_try_lock;
         typedef scoped_timed_lock scoped_lock;
     };
 

Modified: trunk/boost/thread/pthread/recursive_mutex.hpp
==============================================================================
--- trunk/boost/thread/pthread/recursive_mutex.hpp (original)
+++ trunk/boost/thread/pthread/recursive_mutex.hpp 2008-04-28 05:00:58 EDT (Mon, 28 Apr 2008)
@@ -77,7 +77,7 @@
             return !res;
         }
         typedef unique_lock<recursive_mutex> scoped_lock;
- typedef scoped_lock scoped_try_lock;
+ typedef detail::try_lock_wrapper<recursive_mutex> scoped_try_lock;
     };
 
     typedef recursive_mutex recursive_try_mutex;
@@ -239,7 +239,7 @@
 #endif
 
         typedef unique_lock<recursive_timed_mutex> scoped_timed_lock;
- typedef scoped_timed_lock scoped_try_lock;
+ typedef detail::try_lock_wrapper<recursive_timed_mutex> scoped_try_lock;
         typedef scoped_timed_lock scoped_lock;
     };
 

Modified: trunk/boost/thread/win32/mutex.hpp
==============================================================================
--- trunk/boost/thread/win32/mutex.hpp (original)
+++ trunk/boost/thread/win32/mutex.hpp 2008-04-28 05:00:58 EDT (Mon, 28 Apr 2008)
@@ -32,7 +32,7 @@
         }
 
         typedef unique_lock<mutex> scoped_lock;
- typedef scoped_lock scoped_try_lock;
+ typedef detail::try_lock_wrapper<mutex> scoped_try_lock;
     };
 
     typedef mutex try_mutex;
@@ -53,7 +53,7 @@
         }
 
         typedef unique_lock<timed_mutex> scoped_timed_lock;
- typedef scoped_timed_lock scoped_try_lock;
+ typedef detail::try_lock_wrapper<timed_mutex> scoped_try_lock;
         typedef scoped_timed_lock scoped_lock;
     };
 }

Modified: trunk/boost/thread/win32/recursive_mutex.hpp
==============================================================================
--- trunk/boost/thread/win32/recursive_mutex.hpp (original)
+++ trunk/boost/thread/win32/recursive_mutex.hpp 2008-04-28 05:00:58 EDT (Mon, 28 Apr 2008)
@@ -32,7 +32,7 @@
         }
 
         typedef unique_lock<recursive_mutex> scoped_lock;
- typedef scoped_lock scoped_try_lock;
+ typedef detail::try_lock_wrapper<recursive_mutex> scoped_try_lock;
     };
 
     typedef recursive_mutex recursive_try_mutex;
@@ -52,7 +52,7 @@
         }
 
         typedef unique_lock<recursive_timed_mutex> scoped_timed_lock;
- typedef scoped_timed_lock scoped_try_lock;
+ typedef detail::try_lock_wrapper<recursive_timed_mutex> scoped_try_lock;
         typedef scoped_timed_lock scoped_lock;
     };
 }

Modified: trunk/libs/smart_ptr/test/Jamfile.v2
==============================================================================
--- trunk/libs/smart_ptr/test/Jamfile.v2 (original)
+++ trunk/libs/smart_ptr/test/Jamfile.v2 2008-04-28 05:00:58 EDT (Mon, 28 Apr 2008)
@@ -45,5 +45,6 @@
           [ run spinlock_pool_test.cpp ]
           [ run sp_accept_owner_test.cpp ]
           [ run sp_atomic_test.cpp ]
+ [ run sp_atomic_mt_test.cpp : : : <threading>multi <library>/boost/thread//boost_thread ]
         ;
 }

Modified: trunk/libs/thread/test/test_lock_concept.cpp
==============================================================================
--- trunk/libs/thread/test/test_lock_concept.cpp (original)
+++ trunk/libs/thread/test/test_lock_concept.cpp 2008-04-28 05:00:58 EDT (Mon, 28 Apr 2008)
@@ -7,7 +7,9 @@
 #include <boost/test/test_case_template.hpp>
 #include <boost/mpl/vector.hpp>
 #include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
 #include <boost/thread/recursive_mutex.hpp>
+#include <boost/thread/condition_variable.hpp>
 
 template<typename Mutex,typename Lock>
 struct test_initially_locked
@@ -23,6 +25,64 @@
 };
 
 template<typename Mutex,typename Lock>
+struct test_initially_unlocked_if_other_thread_has_lock
+{
+ Mutex m;
+ boost::mutex done_mutex;
+ bool done;
+ bool locked;
+ boost::condition_variable done_cond;
+
+ test_initially_unlocked_if_other_thread_has_lock():
+ done(false),locked(false)
+ {}
+
+ void locking_thread()
+ {
+ Lock lock(m);
+
+ boost::lock_guard<boost::mutex> lk(done_mutex);
+ locked=lock.owns_lock();
+ done=true;
+ done_cond.notify_one();
+ }
+
+ bool is_done() const
+ {
+ return done;
+ }
+
+
+ void operator()()
+ {
+ Lock lock(m);
+
+ typedef test_initially_unlocked_if_other_thread_has_lock<Mutex,Lock> this_type;
+
+ boost::thread t(&this_type::locking_thread,this);
+
+ try
+ {
+ {
+ boost::mutex::scoped_lock lk(done_mutex);
+ BOOST_CHECK(done_cond.timed_wait(lk,boost::posix_time::seconds(2),
+ boost::bind(&this_type::is_done,this)));
+ BOOST_CHECK(!locked);
+ }
+
+ lock.unlock();
+ t.join();
+ }
+ catch(...)
+ {
+ lock.unlock();
+ t.join();
+ throw;
+ }
+ }
+};
+
+template<typename Mutex,typename Lock>
 struct test_initially_unlocked_with_defer_lock_parameter
 {
     void operator()() const
@@ -144,6 +204,7 @@
     typedef typename Mutex::scoped_try_lock Lock;
     
     test_initially_locked<Mutex,Lock>()();
+ test_initially_unlocked_if_other_thread_has_lock<Mutex,Lock>()();
     test_initially_unlocked_with_defer_lock_parameter<Mutex,Lock>()();
     test_initially_locked_with_adopt_lock_parameter<Mutex,Lock>()();
     test_unlocked_after_unlock_called<Mutex,Lock>()();


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