Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r77661 - in trunk: boost/thread libs/thread/doc libs/thread/test libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard
From: vicente.botet_at_[hidden]
Date: 2012-03-31 04:07:01


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

Log:
Thread: Added shared_lock_guard
Added:
   trunk/boost/thread/shared_lock_guard.hpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/
   trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/adopt_lock_pass.cpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/copy_assign_fail.cpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/copy_ctor_fail.cpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/default_pass.cpp (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/types_pass.cpp (contents, props changed)
Text files modified:
   trunk/libs/thread/doc/changes.qbk | 2 +-
   trunk/libs/thread/doc/mutex_concepts.qbk | 6 +++++-
   trunk/libs/thread/test/Jamfile.v2 | 10 ++++++++++
   3 files changed, 16 insertions(+), 2 deletions(-)

Added: trunk/boost/thread/shared_lock_guard.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/thread/shared_lock_guard.hpp 2012-03-31 04:06:57 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,59 @@
+// 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_SHARED_LOCK_GUARD_HPP
+#define BOOST_THREAD_SHARED_LOCK_GUARD_HPP
+#include <boost/thread/detail/config.hpp>
+#include <boost/thread/locks.hpp>
+
+namespace boost
+{
+
+ template<typename SharedMutex>
+ class shared_lock_guard
+ {
+ private:
+ SharedMutex& m;
+
+#ifndef BOOST_NO_DELETED_FUNCTIONS
+ public:
+ shared_lock_guard(shared_lock_guard const&) = delete;
+ shared_lock_guard& operator=(shared_lock_guard const&) = delete;
+#else // BOOST_NO_DELETED_FUNCTIONS
+ private:
+ shared_lock_guard(shared_lock_guard&);
+ shared_lock_guard& operator=(shared_lock_guard&);
+#endif // BOOST_NO_DELETED_FUNCTIONS
+ public:
+ typedef SharedMutex mutex_type;
+ explicit shared_lock_guard(SharedMutex& m_):
+ m(m_)
+ {
+ m.lock_shared();
+ }
+ shared_lock_guard(SharedMutex& m_,adopt_lock_t):
+ m(m_)
+ {}
+ ~shared_lock_guard()
+ {
+ m.unlock_shared();
+ }
+ };
+
+#ifdef BOOST_THREAD_NO_AUTO_DETECT_MUTEX_TYPES
+
+ template<typename T>
+ struct is_mutex_type<shared_lock_guard<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:06:57 EDT (Sat, 31 Mar 2012)
@@ -14,9 +14,9 @@
 
 [/
 * [@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/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.
 * [@http://svn.boost.org/trac/boost/ticket/6194 #6194] Adapt to Boost.Move.

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:06:57 EDT (Sat, 31 Mar 2012)
@@ -1524,6 +1524,8 @@
 [endsect]
 [endsect]
 
+]
+
 [section:other_locks Other Lock Types]
 
 
@@ -1591,6 +1593,7 @@
 
 [endsect]
 
+[/
 [section:reverse_lock Class template `reverse_lock`]
 
     #include <boost/thread/reverse_lock.hpp>
@@ -1639,9 +1642,10 @@
 
 
 [endsect]
+]
 
 [endsect]
-]
+
 
 [section:lock_functions Lock functions]
 

Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2 (original)
+++ trunk/libs/thread/test/Jamfile.v2 2012-03-31 04:06:57 EDT (Sat, 31 Mar 2012)
@@ -377,4 +377,14 @@
     #;
 
 
+ #explicit shared_lock_guard ;
+ test-suite shared_lock_guard
+ :
+ [ thread-compile-fail-V2 ./sync/mutual_exclusion/locks/shared_lock_guard/copy_assign_fail.cpp : : shared_lock_guard__cons__copy_assign_f ]
+ [ thread-compile-fail-V2 ./sync/mutual_exclusion/locks/shared_lock_guard/copy_ctor_fail.cpp : : shared_lock_guard__cons__copy_ctor_f ]
+ [ thread-run2 ./sync/mutual_exclusion/locks/shared_lock_guard/adopt_lock_pass.cpp : shared_lock_guard__cons__adopt_lock_p ]
+ [ thread-run2 ./sync/mutual_exclusion/locks/shared_lock_guard/default_pass.cpp : shared_lock_guard__cons__default_p ]
+ [ thread-run2 ./sync/mutual_exclusion/locks/shared_lock_guard/types_pass.cpp : shared_lock_guard__types_p ]
+ ;
+
 }

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/adopt_lock_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/adopt_lock_pass.cpp 2012-03-31 04:06:57 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/shared_lock_guard.hpp>
+
+// template <class Mutex> class shared_lock_guard;
+
+// shared_lock_guard(mutex_type& m, adopt_lock_t);
+
+#include <boost/thread/shared_lock_guard.hpp>
+#include <boost/thread/shared_mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+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;
+
+boost::shared_mutex m;
+
+void f()
+{
+ time_point t0 = Clock::now();
+ time_point t1;
+ {
+ m.lock();
+ boost::shared_lock_guard<boost::shared_mutex> lg(m, boost::adopt_lock);
+ t1 = Clock::now();
+ }
+ ns d = t1 - t0 - ms(250);
+ BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms
+}
+
+int main()
+{
+ m.lock();
+ boost::thread t(f);
+ boost::this_thread::sleep_for(ms(250));
+ m.unlock();
+ t.join();
+
+ return boost::report_errors();
+}
+

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/copy_assign_fail.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/copy_assign_fail.cpp 2012-03-31 04:06:57 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/shared_lock_guard.hpp>
+
+// template <class Mutex> class shared_lock_guard;
+
+// shared_lock_guard& operator=(shared_lock_guard const&) = delete;
+
+#include <boost/thread/shared_lock_guard.hpp>
+#include <boost/thread/shared_mutex.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+boost::shared_mutex m0;
+boost::shared_mutex m1;
+
+int main()
+{
+ boost::shared_lock_guard<boost::shared_mutex> lk0(m0);
+ boost::shared_lock_guard<boost::shared_mutex> lk1(m1);
+ lk1 = lk0;
+
+}
+

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/copy_ctor_fail.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/copy_ctor_fail.cpp 2012-03-31 04:06:57 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/shared_lock_guard.hpp>
+
+// template <class Mutex> class shared_lock_guard;
+
+// shared_lock_guard(shared_lock_guard const&) = delete;
+
+
+#include <boost/thread/shared_lock_guard.hpp>
+#include <boost/thread/shared_mutex.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+boost::shared_mutex m0;
+boost::shared_mutex m1;
+
+int main()
+{
+ boost::shared_lock_guard<boost::shared_mutex> lk0(m0);
+ boost::shared_lock_guard<boost::shared_mutex> lk1 = lk0;
+}
+

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/default_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/default_pass.cpp 2012-03-31 04:06:57 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/shared_lock_guard.hpp>
+
+// template <class Mutex> class shared_lock_guard;
+
+// shared_lock_guard(shared_lock_guard const&) = delete;
+
+#include <boost/thread/shared_lock_guard.hpp>
+#include <boost/thread/shared_mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+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;
+
+boost::shared_mutex m;
+
+void f()
+{
+ time_point t0 = Clock::now();
+ time_point t1;
+ {
+ boost::shared_lock_guard<boost::shared_mutex> lg(m);
+ t1 = Clock::now();
+ }
+ 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
+}
+
+int main()
+{
+ m.lock();
+ boost::thread t(f);
+ boost::this_thread::sleep_for(ms(250));
+ m.unlock();
+ t.join();
+
+ return boost::report_errors();
+}

Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/types_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/shared_lock_guard/types_pass.cpp 2012-03-31 04:06:57 EDT (Sat, 31 Mar 2012)
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/shared_lock_guard.hpp>
+
+// <mutex>
+
+// template <class Mutex>
+// class shared_lock_guard
+// {
+// public:
+// typedef Mutex mutex_type;
+// ...
+// };
+
+
+#include <boost/thread/shared_mutex.hpp>
+#include <boost/thread/shared_lock_guard.hpp>
+#include <boost/static_assert.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+int main()
+{
+ BOOST_STATIC_ASSERT_MSG((boost::is_same<boost::shared_lock_guard<boost::shared_mutex>::mutex_type,
+ boost::shared_mutex>::value), "");
+
+ 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