Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81784 - in trunk: boost/thread boost/thread/detail libs/thread/test libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock libs/thread/test/sync/mutual_exclusion/locks/strict_lock
From: vicente.botet_at_[hidden]
Date: 2012-12-08 03:15:50


Author: viboes
Date: 2012-12-08 03:15:49 EST (Sat, 08 Dec 2012)
New Revision: 81784
URL: http://svn.boost.org/trac/boost/changeset/81784

Log:
Thread: Added make_strict_lock.
Added:
   trunk/boost/thread/detail/lockable_wrapper.hpp (contents, props changed)
Text files modified:
   trunk/boost/thread/lock_guard.hpp | 28 +------------------------
   trunk/boost/thread/strict_lock.hpp | 43 ++++++++++++++++++++++++++++++++++++---
   trunk/libs/thread/test/Jamfile.v2 | 4 +-
   trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/copy_ctor_fail.cpp | 4 +-
   trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp | 4 +-
   trunk/libs/thread/test/sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp | 2
   6 files changed, 48 insertions(+), 37 deletions(-)

Added: trunk/boost/thread/detail/lockable_wrapper.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/thread/detail/lockable_wrapper.hpp 2012-12-08 03:15:49 EST (Sat, 08 Dec 2012)
@@ -0,0 +1,43 @@
+// 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_DETAIL_LOCKABLE_WRAPPER_HPP
+#define BOOST_THREAD_DETAIL_LOCKABLE_WRAPPER_HPP
+
+#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+#include <initializer_list>
+#endif
+#include <boost/config/abi_prefix.hpp>
+
+namespace boost
+{
+
+#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+ namespace thread_detail
+ {
+ template <typename Mutex>
+ struct lockable_wrapper
+ {
+ Mutex* m;
+ explicit lockable_wrapper(Mutex& m_) :
+ m(&m_)
+ {}
+ };
+ template <typename Mutex>
+ struct lockable_adopt_wrapper
+ {
+ Mutex* m;
+ explicit lockable_adopt_wrapper(Mutex& m_) :
+ m(&m_)
+ {}
+ };
+ }
+#endif
+
+}
+
+#include <boost/config/abi_suffix.hpp>
+
+#endif // header

Modified: trunk/boost/thread/lock_guard.hpp
==============================================================================
--- trunk/boost/thread/lock_guard.hpp (original)
+++ trunk/boost/thread/lock_guard.hpp 2012-12-08 03:15:49 EST (Sat, 08 Dec 2012)
@@ -9,42 +9,18 @@
 
 #include <boost/thread/detail/delete.hpp>
 #include <boost/thread/detail/move.hpp>
+#include <boost/thread/detail/lockable_wrapper.hpp>
 #include <boost/thread/lock_options.hpp>
 #if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
 #include <boost/thread/is_locked_by_this_thread.hpp>
-#endif
 #include <boost/assert.hpp>
-#include <iostream>
-#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
-#include <initializer_list>
 #endif
+
 #include <boost/config/abi_prefix.hpp>
 
 namespace boost
 {
 
-#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
- namespace thread_detail
- {
- template <typename Mutex>
- struct lockable_wrapper
- {
- Mutex* m;
- explicit lockable_wrapper(Mutex& m_) :
- m(&m_)
- {}
- };
- template <typename Mutex>
- struct lockable_adopt_wrapper
- {
- Mutex* m;
- explicit lockable_adopt_wrapper(Mutex& m_) :
- m(&m_)
- {}
- };
- }
-#endif
-
   template <typename Mutex>
   class lock_guard
   {

Modified: trunk/boost/thread/strict_lock.hpp
==============================================================================
--- trunk/boost/thread/strict_lock.hpp (original)
+++ trunk/boost/thread/strict_lock.hpp 2012-12-08 03:15:49 EST (Sat, 08 Dec 2012)
@@ -7,6 +7,7 @@
 #define BOOST_THREAD_STRICT_LOCK_HPP
 
 #include <boost/thread/detail/delete.hpp>
+#include <boost/thread/detail/lockable_wrapper.hpp>
 #include <boost/thread/lock_options.hpp>
 #include <boost/thread/lock_traits.hpp>
 #include <boost/thread/lockable_traits.hpp>
@@ -48,6 +49,15 @@
       mtx.lock();
     } /*< locks on construction >*/
 
+
+#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+ strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lockable> > l_) :
+ mtx_(*(const_cast<thread_detail::lockable_wrapper<Lockable>*>(l_.begin())->m))
+ {
+ mtx_.lock();
+ }
+#endif
+
     /**
      * Destructor
      *
@@ -97,7 +107,7 @@
 
   /**
    * A nested strict lock is a scoped lock guard ensuring the mutex is locked on its
- * scope, by taking ownership of an nesting lock, and locking the mutex on construction if not already locked
+ * scope, by taking ownership of an nesting lock, locking the mutex on construction if not already locked
    * and restoring the ownership to the nesting lock on destruction.
    */
   //[nested_strict_lock
@@ -117,8 +127,8 @@
      *
      * __Requires: <c>lk.mutex() != null_ptr</c>
      * __Effects: Stores the reference to the lock parameter and takes ownership on it.
- * If the lock doesn't owns the mutex @mtx lock it.
- * __Postconditions: @c owns_lock()
+ * If the lock doesn't owns the mutex @c mtx lock it.
+ * __Postconditions: @c owns_lock(lk.mutex())
      * __StrongException
      * __Throws:
      *
@@ -127,7 +137,7 @@
      * - Any exception that @c lk.lock() can throw.
      *
      */
- nested_strict_lock(Lock& lk) :
+ explicit nested_strict_lock(Lock& lk) :
       lk_(lk) /*< Store reference to lk >*/
     {
       /*< Define BOOST_THREAD_DONT_CHECK_PRECONDITIONS if you don't want to check lk ownership >*/
@@ -138,6 +148,19 @@
       tmp_lk_ = move(lk); /*< Move ownership to temporary lk >*/
     }
 
+#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+ nested_strict_lock(std::initializer_list<thread_detail::lockable_wrapper<Lock> > l_) :
+ lk_(*(const_cast<thread_detail::lockable_wrapper<Lock>*>(l_.begin())->m))
+ {
+ /*< Define BOOST_THREAD_DONT_CHECK_PRECONDITIONS if you don't want to check lk ownership >*/
+ BOOST_THREAD_ASSERT_PRECONDITION( lk_.mutex() != 0,
+ lock_error()
+ );
+ if (!lk_.owns_lock()) lk_.lock(); /*< ensures it is locked >*/
+ tmp_lk_ = move(lk_); /*< Move ownership to temporary lk >*/
+ }
+#endif
+
     /**
      * Destructor
      *
@@ -180,6 +203,18 @@
   {
   };
 
+#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+ template <typename Lockable>
+ strict_lock<Lockable> make_strict_lock(Lockable& mtx)
+ {
+ return { thread_detail::lockable_wrapper<Lockable>(mtx) };
+ }
+ template <typename Lock>
+ nested_strict_lock<Lock> make_nested_strict_lock(Lock& lk)
+ {
+ return { thread_detail::lockable_wrapper<Lock>(lk) };
+ }
+#endif
 }
 #include <boost/config/abi_suffix.hpp>
 

Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2 (original)
+++ trunk/libs/thread/test/Jamfile.v2 2012-12-08 03:15:49 EST (Sat, 08 Dec 2012)
@@ -480,7 +480,7 @@
           [ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/default_pass.cpp : strict_lock__cons__default_p ]
           [ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/owns_lock_pass.cpp : strict_lock__owns_lock_p ]
           [ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/types_pass.cpp : strict_lock__types_p ]
- #[ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp : make_strict_lock_p ]
+ [ thread-run2-noit ./sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp : make_strict_lock_p ]
     ;
 
     #explicit ts_nested_strict_lock ;
@@ -491,7 +491,7 @@
           [ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/default_pass.cpp : nested_strict_lock__cons__default_p ]
           [ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/owns_lock_pass.cpp : nested_strict_lock__owns_lock_p ]
           [ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/types_pass.cpp : nested_strict_lock__types_p ]
- #[ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp : make_nested_strict_lock_p ]
+ [ thread-run2-noit ./sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp : make_nested_strict_lock_p ]
     ;
 
 

Modified: trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/copy_ctor_fail.cpp
==============================================================================
--- trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/copy_ctor_fail.cpp (original)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/copy_ctor_fail.cpp 2012-12-08 03:15:49 EST (Sat, 08 Dec 2012)
@@ -20,8 +20,8 @@
 
 int main()
 {
- boost::nested_strict_lock<boost::mutex> lk0(m0);
- boost::nested_strict_lock<boost::mutex> lk1 = lk0;
+ boost::nested_strict_lock<boost::unique_lock<boost::mutex> > lk0(m0);
+ boost::nested_strict_lock<boost::unique_lock<boost::mutex> > lk1 = lk0;
 }
 
 #include "../../../../remove_error_code_unused_warning.hpp"

Modified: trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp
==============================================================================
--- trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp (original)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/nested_strict_lock/make_nested_strict_lock_pass.cpp 2012-12-08 03:15:49 EST (Sat, 08 Dec 2012)
@@ -30,7 +30,7 @@
 
 boost::mutex m;
 
-#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && BOOST_THREAD_USES_CHRONO
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && defined BOOST_THREAD_USES_CHRONO
 
 void f()
 {
@@ -38,7 +38,7 @@
   time_point t1;
   boost::unique_lock<boost::mutex> lg(m);
   {
- const auto&& nlg = boost::make_strict_lock(lg); (void)nlg;
+ const auto&& nlg = boost::make_nested_strict_lock(lg); (void)nlg;
     t1 = Clock::now();
     BOOST_THREAD_TRACE;
   }

Modified: trunk/libs/thread/test/sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp
==============================================================================
--- trunk/libs/thread/test/sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp (original)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/strict_lock/make_strict_lock_pass.cpp 2012-12-08 03:15:49 EST (Sat, 08 Dec 2012)
@@ -29,7 +29,7 @@
 
 boost::mutex m;
 
-#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && BOOST_THREAD_USES_CHRONO
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST && defined BOOST_THREAD_USES_CHRONO
 
 void f()
 {


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