Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81506 - in trunk: boost/thread libs/thread/test libs/thread/test/sync/mutual_exclusion/locks/lock_guard
From: vicente.botet_at_[hidden]
Date: 2012-11-24 07:36:47


Author: viboes
Date: 2012-11-24 07:36:46 EST (Sat, 24 Nov 2012)
New Revision: 81506
URL: http://svn.boost.org/trac/boost/changeset/81506

Log:
Thread: Added make_lock_guard
Added:
   trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/make_lock_guard_adopt_lock_pass.cpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/make_lock_guard_pass.cpp (contents, props changed)
Text files modified:
   trunk/boost/thread/lock_guard.hpp | 62 ++++++++++++++++++++++++++++++++++++++-
   trunk/libs/thread/test/Jamfile.v2 | 3 +
   trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/default_pass.cpp | 2
   3 files changed, 63 insertions(+), 4 deletions(-)

Modified: trunk/boost/thread/lock_guard.hpp
==============================================================================
--- trunk/boost/thread/lock_guard.hpp (original)
+++ trunk/boost/thread/lock_guard.hpp 2012-11-24 07:36:46 EST (Sat, 24 Nov 2012)
@@ -8,17 +8,43 @@
 #define BOOST_THREAD_LOCK_GUARD_HPP
 
 #include <boost/thread/detail/delete.hpp>
+#include <boost/thread/detail/move.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
   {
@@ -27,13 +53,14 @@
 
   public:
     typedef Mutex mutex_type;
- BOOST_THREAD_NO_COPYABLE( lock_guard)
+ BOOST_THREAD_NO_COPYABLE( lock_guard )
 
     explicit lock_guard(Mutex& m_) :
       m(m_)
     {
       m.lock();
     }
+
     lock_guard(Mutex& m_, adopt_lock_t) :
       m(m_)
     {
@@ -41,13 +68,44 @@
       BOOST_ASSERT(is_locked_by_this_thread(m));
 #endif
     }
+
+#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+ lock_guard(std::initializer_list<thread_detail::lockable_wrapper<Mutex> > l_) :
+ m(*(const_cast<thread_detail::lockable_wrapper<Mutex>*>(l_.begin())->m))
+ {
+ m.lock();
+ }
+
+ lock_guard(std::initializer_list<thread_detail::lockable_adopt_wrapper<Mutex> > l_) :
+ m(*(const_cast<thread_detail::lockable_adopt_wrapper<Mutex>*>(l_.begin())->m))
+ {
+#if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS
+ BOOST_ASSERT(is_locked_by_this_thread(m));
+#endif
+ }
+
+#endif
     ~lock_guard()
     {
       m.unlock();
     }
   };
 
+
+#if ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+ template <typename Lockable>
+ lock_guard<Lockable> make_lock_guard(Lockable& mtx)
+ {
+ return { thread_detail::lockable_wrapper<Lockable>(mtx) };
+ }
+ template <typename Lockable>
+ lock_guard<Lockable> make_lock_guard(Lockable& mtx, adopt_lock_t)
+ {
+ return { thread_detail::lockable_adopt_wrapper<Lockable>(mtx) };
+ }
+#endif
 }
+
 #include <boost/config/abi_suffix.hpp>
 
 #endif

Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2 (original)
+++ trunk/libs/thread/test/Jamfile.v2 2012-11-24 07:36:46 EST (Sat, 24 Nov 2012)
@@ -330,6 +330,8 @@
           [ thread-run2 ./sync/mutual_exclusion/locks/lock_guard/adopt_lock_pass.cpp : lock_guard__cons__adopt_lock_p ]
           [ thread-run2 ./sync/mutual_exclusion/locks/lock_guard/default_pass.cpp : lock_guard__cons__default_p ]
           [ thread-run2 ./sync/mutual_exclusion/locks/lock_guard/types_pass.cpp : lock_guard__types_p ]
+ [ thread-run2 ./sync/mutual_exclusion/locks/lock_guard/make_lock_guard_pass.cpp : make_lock_guard_p ]
+ [ thread-run2 ./sync/mutual_exclusion/locks/lock_guard/make_lock_guard_adopt_lock_pass.cpp : make_lock_guard__adopt_lock_p ]
     ;
 
     #explicit ts_unique_lock ;
@@ -591,7 +593,6 @@
     :
           #[ thread-run test_7665.cpp ]
           #[ thread-run test_7666.cpp ]
- #[ compile ../example/tes_is_function.cpp ]
           #[ thread-run ../example/unwrap.cpp ]
     ;
 

Modified: trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/default_pass.cpp
==============================================================================
--- trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/default_pass.cpp (original)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/default_pass.cpp 2012-11-24 07:36:46 EST (Sat, 24 Nov 2012)
@@ -16,7 +16,7 @@
 
 // template <class Mutex> class lock_guard;
 
-// lock_guard(lock_guard const&) = delete;
+// lock_guard(Mutex &);
 
 #include <boost/thread/lock_guard.hpp>
 #include <boost/thread/mutex.hpp>

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/make_lock_guard_adopt_lock_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/make_lock_guard_adopt_lock_pass.cpp 2012-11-24 07:36:46 EST (Sat, 24 Nov 2012)
@@ -0,0 +1,75 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// 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/lock_guard.hpp>
+
+// template <class Lockable>
+// lock_guard<Lockable> make_lock_guard(Lockable &, adopt_lock_t);
+
+#include <boost/thread/lock_guard.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+#ifdef BOOST_THREAD_USES_CHRONO
+typedef boost::chrono::high_resolution_clock Clock;
+typedef Clock::time_point time_point;
+typedef Clock::duration duration;
+typedef boost::chrono::milliseconds ms;
+typedef boost::chrono::nanoseconds ns;
+#endif
+boost::mutex m;
+
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+
+void f()
+{
+#ifdef BOOST_THREAD_USES_CHRONO
+ time_point t0 = Clock::now();
+ time_point t1;
+ {
+ m.lock();
+ auto&& lg = boost::make_lock_guard(m, boost::adopt_lock);
+ t1 = Clock::now();
+ }
+ ns d = t1 - t0 - ms(250);
+ BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms
+#else
+ //time_point t0 = Clock::now();
+ //time_point t1;
+ {
+ m.lock();
+ auto&& lg = boost::make_lock_guard(m, boost::adopt_lock);
+ //t1 = Clock::now();
+ }
+ //ns d = t1 - t0 - ms(250);
+ //BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms
+#endif
+}
+#endif
+
+int main()
+{
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+ m.lock();
+ boost::thread t(f);
+#ifdef BOOST_THREAD_USES_CHRONO
+ boost::this_thread::sleep_for(ms(250));
+#endif
+ m.unlock();
+ t.join();
+#endif
+ return boost::report_errors();
+}
+

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/make_lock_guard_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/lock_guard/make_lock_guard_pass.cpp 2012-11-24 07:36:46 EST (Sat, 24 Nov 2012)
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// Copyright (C) 2011 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/lock_guard.hpp>
+
+// template <class Lockable>
+// lock_guard<Lockable> make_lock_guard(Lockable &);
+
+#define BOOST_THREAD_VERSION 4
+#define BOOST_THREAD_USES_LOG
+#define BOOST_THREAD_DONT_PROVIDE_NESTED_LOCKS
+
+#include <boost/thread/detail/log.hpp>
+#include <boost/thread/lock_guard.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+
+#include <boost/detail/lightweight_test.hpp>
+
+#ifdef BOOST_THREAD_USES_CHRONO
+typedef boost::chrono::high_resolution_clock Clock;
+typedef Clock::time_point time_point;
+typedef Clock::duration duration;
+typedef boost::chrono::milliseconds ms;
+typedef boost::chrono::nanoseconds ns;
+#endif
+
+boost::mutex m;
+
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+
+void f()
+{
+ time_point t0 = Clock::now();
+ time_point t1;
+ {
+ auto&& lg = boost::make_lock_guard(m);
+ t1 = Clock::now();
+ BOOST_THREAD_TRACE;
+ }
+ BOOST_THREAD_TRACE;
+ ns d = t1 - t0 - ms(250);
+ // This test is spurious as it depends on the time the thread system switches the threads
+ BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms
+}
+#endif
+
+int main()
+{
+
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+ {
+ m.lock();
+ boost::thread t(f);
+ #ifdef BOOST_THREAD_USES_CHRONO
+ boost::this_thread::sleep_for(ms(250));
+ #endif
+ m.unlock();
+ t.join();
+ }
+#endif
+ 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