Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r77662 - in trunk: boost/thread libs/thread/doc libs/thread/test libs/thread/test/sync/mutual_exclusion/locks/reverse_lock
From: vicente.botet_at_[hidden]
Date: 2012-03-31 04:39:29


Author: viboes
Date: 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
New Revision: 77662
URL: http://svn.boost.org/trac/boost/changeset/77662

Log:
Thread: Added reverse_lock
Added:
   trunk/boost/thread/reverse_lock.hpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/
   trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/copy_assign_fail.cpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/copy_ctor_fail.cpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/types_pass.cpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/unique_lock_ctor_pass.cpp (contents, props changed)
Text files modified:
   trunk/libs/thread/doc/changes.qbk | 8 --------
   trunk/libs/thread/doc/mutex_concepts.qbk | 2 --
   trunk/libs/thread/doc/overview.qbk | 5 ++---
   trunk/libs/thread/test/Jamfile.v2 | 9 +++++++++
   4 files changed, 11 insertions(+), 13 deletions(-)

Added: trunk/boost/thread/reverse_lock.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/thread/reverse_lock.hpp 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,65 @@
+// 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)
+// (C) Copyright 2012 Vicente J. Botet Escriba
+
+#ifndef BOOST_THREAD_REVERSE_LOCK_HPP
+#define BOOST_THREAD_REVERSE_LOCK_HPP
+#include <boost/thread/detail/config.hpp>
+#include <boost/thread/locks.hpp>
+
+namespace boost
+{
+
+ template<typename Lock>
+ class reverse_lock
+ {
+
+#ifndef BOOST_NO_DELETED_FUNCTIONS
+ public:
+ reverse_lock(reverse_lock const&) = delete;
+ reverse_lock& operator=(reverse_lock const&) = delete;
+#else // BOOST_NO_DELETED_FUNCTIONS
+ private:
+ reverse_lock(reverse_lock&);
+ reverse_lock& operator=(reverse_lock&);
+#endif // BOOST_NO_DELETED_FUNCTIONS
+ public:
+ typedef typename Lock::mutex_type mutex_type;
+
+ explicit reverse_lock(Lock& m_)
+ : m(m_), mtx(0)
+ {
+ if (m.owns_lock())
+ {
+ m.unlock();
+ }
+ mtx=m.release();
+ }
+ ~reverse_lock()
+ {
+ if (mtx) {
+ mtx->lock();
+ m = Lock(*mtx, adopt_lock);
+ }
+ }
+
+ private:
+ Lock& m;
+ mutex_type* mtx;
+ };
+
+
+#ifdef BOOST_THREAD_NO_AUTO_DETECT_MUTEX_TYPES
+ template<typename T>
+ struct is_mutex_type<reverse_lock<T> >
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true);
+ };
+
+#endif
+
+
+}
+
+#endif // header

Modified: trunk/libs/thread/doc/changes.qbk
==============================================================================
--- trunk/libs/thread/doc/changes.qbk (original)
+++ trunk/libs/thread/doc/changes.qbk 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
@@ -12,10 +12,7 @@
 
 New Features:
 
-[/
 * [@http://svn.boost.org/trac/boost/ticket/1850 #1850] Request for unlock_guard to compliment lock_guard.
-]
-
 * [@http://svn.boost.org/trac/boost/ticket/2637 #2637] Request for shared_mutex duration timed_lock and timed_lock_shared.
 * [@http://svn.boost.org/trac/boost/ticket/2741 #2741] Proposal to manage portable and non portable thread attributes.
 * [@http://svn.boost.org/trac/boost/ticket/3567 #3567] Request for shared_lock_guard.
@@ -188,11 +185,6 @@
   * [@http://svn.boost.org/trac/boost/ticket/6270 #6270] Add thread constructor from movable callable and movable arguments following C++11.
          
 
-# Add some complementary locks
-
- * [@http://svn.boost.org/trac/boost/ticket/1850 #1850] Request for unlock_guard (and/or unique_unlock) to compliment lock_guard/unique_lock.
- * [@http://svn.boost.org/trac/boost/ticket/3567 #3567] Request for shared_lock_guard.
-
 # Add the shared locking upward conversions.
 
   * [@http://svn.boost.org/trac/boost/ticket/6217 #6217] Enhance Boost.Thread shared mutex interface following Howard Hinnant proposal.

Modified: trunk/libs/thread/doc/mutex_concepts.qbk
==============================================================================
--- trunk/libs/thread/doc/mutex_concepts.qbk (original)
+++ trunk/libs/thread/doc/mutex_concepts.qbk 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
@@ -1593,7 +1593,6 @@
 
 [endsect]
 
-[/
 [section:reverse_lock Class template `reverse_lock`]
 
     #include <boost/thread/reverse_lock.hpp>
@@ -1642,7 +1641,6 @@
 
 
 [endsect]
-]
 
 [endsect]
 

Modified: trunk/libs/thread/doc/overview.qbk
==============================================================================
--- trunk/libs/thread/doc/overview.qbk (original)
+++ trunk/libs/thread/doc/overview.qbk 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
@@ -20,9 +20,8 @@
 [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2139.html N2139], and
 [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2094.html N2094]
 
-Vicente J. Botet Escriba started in version 2 the adaptation to comply with the accepted Thread C++11 library (Make use of Boost.Chrono and Boost.Move) and the [@http://home.roadrunner.com/~hinnant/bloomington/shared_mutex.html Shared Locking] Howard Hinnant proposal except for the upward conversions.
-
-[/ as some new features as thread attributes, unlock_guard, shared_guard, ]
+Vicente J. Botet Escriba started in version 2 the adaptation to comply with the accepted Thread C++11 library (Make use of Boost.Chrono and Boost.Move) and the [@http://home.roadrunner.com/~hinnant/bloomington/shared_mutex.html Shared Locking] Howard Hinnant proposal except for the upward conversions. Some minor features have been added also
+as thread attributes, reverse_lock, shared_lock_guard.
 
 In order to use the classes and functions described here, you can
 either include the specific headers specified by the descriptions of

Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2 (original)
+++ trunk/libs/thread/test/Jamfile.v2 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
@@ -387,4 +387,13 @@
           [ thread-run2 ./sync/mutual_exclusion/locks/shared_lock_guard/types_pass.cpp : shared_lock_guard__types_p ]
     ;
 
+ #explicit reverse_lock ;
+ test-suite reverse_lock
+ :
+ [ thread-compile-fail-V2 ./sync/mutual_exclusion/locks/reverse_lock/copy_assign_fail.cpp : : reverse_lock__copy_assign_f ]
+ [ thread-compile-fail-V2 ./sync/mutual_exclusion/locks/reverse_lock/copy_ctor_fail.cpp : : reverse_lock__copy_ctor_f ]
+ [ thread-run2 ./sync/mutual_exclusion/locks/reverse_lock/unique_lock_ctor_pass.cpp : reverse_lock__unique_lock_ctor_p ]
+ [ thread-run2 ./sync/mutual_exclusion/locks/reverse_lock/types_pass.cpp : reverse_lock__types_p ]
+ ;
+
 }

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/copy_assign_fail.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/copy_assign_fail.cpp 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,30 @@
+// Copyright (C) 2012 Vicente J. Botet Escriba
+//
+// 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)
+
+// <boost/thread/locks.hpp>
+
+// template <class Mutex> class unlock_guard;
+
+// unlock_guard& operator=(unlock_guard const&) = delete;
+
+#include <boost/thread/locks.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+
+int main()
+{
+ boost::mutex m0;
+ boost::mutex m1;
+ boost::unique_lock<boost::mutex> lk0(m0);
+ boost::unique_lock<boost::mutex> lk1(m1);
+ {
+ boost::unlock_guard<boost::unique_lock<boost::mutex> > lg0(lk0);
+ boost::unlock_guard<boost::unique_lock<boost::mutex> > lg1(lk1);
+ lk1 = lk0;
+ }
+
+}
+

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/copy_ctor_fail.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/copy_ctor_fail.cpp 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,31 @@
+// Copyright (C) 2012 Vicente J. Botet Escriba
+//
+// 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)
+
+// <boost/thread/locks.hpp>
+
+// template <class Mutex> class unlock_guard;
+
+// unlock_guard(unlock_guard const&) = delete;
+
+
+#include <boost/thread/locks.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+boost::mutex m0;
+boost::mutex m1;
+
+int main()
+{
+ boost::mutex m0;
+ boost::mutex m1;
+ boost::unique_lock<boost::mutex> lk0(m0);
+ boost::unique_lock<boost::mutex> lk1(m1);
+ {
+ boost::unlock_guard<boost::unique_lock<boost::mutex> > lg0(lk0);
+ boost::unlock_guard<boost::unique_lock<boost::mutex> > lg1(lg0);
+ }
+}
+

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/types_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/types_pass.cpp 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,33 @@
+// Copyright (C) 2012 Vicente J. Botet Escriba
+//
+// 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)
+
+// <boost/thread/mutex.hpp>
+
+// <mutex>
+
+// template <class Mutex>
+// class unlock_guard
+// {
+// public:
+// typedef Mutex mutex_type;
+// ...
+// };
+
+
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/reverse_lock.hpp>
+#include <boost/thread/locks.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/static_assert.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+int main()
+{
+ BOOST_STATIC_ASSERT_MSG((boost::is_same<boost::reverse_lock<boost::unique_lock<boost::mutex> >::mutex_type,
+ boost::mutex>::value), "");
+
+ return boost::report_errors();
+}
+

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/unique_lock_ctor_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/reverse_lock/unique_lock_ctor_pass.cpp 2012-03-31 04:39:21 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,52 @@
+// Copyright (C) 2012 Vicente J. Botet Escriba
+//
+// 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)
+
+// <boost/thread/locks.hpp>
+
+// template <class Mutex> class unlock_guard;
+
+// unlock_guard(unlock_guard const&) = delete;
+
+#include <boost/thread/reverse_lock.hpp>
+#include <boost/thread/locks.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+
+int main()
+{
+ {
+ boost::mutex m;
+ boost::unique_lock<boost::mutex> lk(m);
+ BOOST_TEST(lk.owns_lock());
+ BOOST_TEST(lk.mutex()==&m);
+
+ {
+ boost::reverse_lock<boost::unique_lock<boost::mutex> > lg(lk);
+ BOOST_TEST(!lk.owns_lock());
+ BOOST_TEST(lk.mutex()==0);
+ }
+ BOOST_TEST(lk.owns_lock());
+ BOOST_TEST(lk.mutex()==&m);
+ }
+
+ {
+ boost::mutex m;
+ boost::unique_lock<boost::mutex> lk(m, boost::defer_lock);
+ BOOST_TEST(! lk.owns_lock());
+ BOOST_TEST(lk.mutex()==&m);
+ {
+ boost::reverse_lock<boost::unique_lock<boost::mutex> > lg(lk);
+ BOOST_TEST(!lk.owns_lock());
+ BOOST_TEST(lk.mutex()==0);
+ }
+ BOOST_TEST(lk.owns_lock());
+ BOOST_TEST(lk.mutex()==&m);
+ }
+
+
+ return boost::report_errors();
+}


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