$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r81508 - in trunk: boost/thread libs/thread/test libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons
From: vicente.botet_at_[hidden]
Date: 2012-11-24 11:24:28
Author: viboes
Date: 2012-11-24 11:24:27 EST (Sat, 24 Nov 2012)
New Revision: 81508
URL: http://svn.boost.org/trac/boost/changeset/81508
Log:
Thread: Added unique lock factories
Added:
   trunk/boost/thread/lock_factories.hpp   (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_adopt_lock_pass.cpp   (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_defer_lock_pass.cpp   (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_mutex_pass.cpp   (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_try_to_lock_pass.cpp   (contents, props changed)
   trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_locks_mutex_pass.cpp   (contents, props changed)
Text files modified: 
   trunk/libs/thread/test/Jamfile.v2 |    12 ++++++++++++                            
   1 files changed, 12 insertions(+), 0 deletions(-)
Added: trunk/boost/thread/lock_factories.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/thread/lock_factories.hpp	2012-11-24 11:24:27 EST (Sat, 24 Nov 2012)
@@ -0,0 +1,91 @@
+// 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 2007 Anthony Williams
+// (C) Copyright 2011-2012 Vicente J. Botet Escriba
+
+#ifndef BOOST_THREAD_LOCK_FACTORIES_HPP
+#define BOOST_THREAD_LOCK_FACTORIES_HPP
+
+#include <boost/thread/lock_types.hpp>
+#include <boost/thread/lock_algorithms.hpp>
+#if ! defined(BOOST_NO_CXX11_HDR_TUPLE)
+#include <tuple> // todo change to <boost/tuple.hpp> once Boost.Tuple or Boost.Fusion provides Move semantics.
+#endif
+#include <boost/config/abi_prefix.hpp>
+
+namespace boost
+{
+
+  template <typename Lockable>
+  unique_lock<Lockable> make_unique_lock(Lockable& mtx)
+  {
+    return unique_lock<Lockable> (mtx);
+  }
+
+  template <typename Lockable>
+  unique_lock<Lockable> make_unique_lock(Lockable& mtx, adopt_lock_t)
+  {
+    return unique_lock<Lockable> (mtx, adopt_lock);
+  }
+
+  template <typename Lockable>
+  unique_lock<Lockable> make_unique_lock(Lockable& mtx, defer_lock_t)
+  {
+    return unique_lock<Lockable> (mtx, defer_lock);
+  }
+
+  template <typename Lockable>
+  unique_lock<Lockable> make_unique_lock(Lockable& mtx, try_to_lock_t)
+  {
+    return unique_lock<Lockable> (mtx, try_to_lock);
+  }
+#if ! defined(BOOST_NO_CXX11_HDR_TUPLE)
+
+#if ! defined BOOST_NO_CXX11_VARIADIC_TEMPLATES
+  template <typename ...Lockable>
+  std::tuple<unique_lock<Lockable> ...> make_unique_locks(Lockable& ...mtx)
+  {
+    boost::lock(mtx...);
+    return std::tuple<unique_lock<Lockable> ...>(unique_lock<Lockable>(mtx, adopt_lock)...);
+  }
+#else
+  template <typename L1, typename L2>
+  std::tuple<unique_lock<L1>, unique_lock<L2> > make_unique_locks(L1& m1, L2& m2)
+  {
+    boost::lock(m1, m2);
+    return std::tuple<unique_lock<L1>,unique_lock<L2> >(
+        unique_lock<L1>(m1, adopt_lock),
+        unique_lock<L2>(m2, adopt_lock)
+    );
+  }
+  template <typename L1, typename L2, typename L3>
+  std::tuple<unique_lock<L1>, unique_lock<L2>, unique_lock<L3> > make_unique_locks(L1& m1, L2& m2, L2& m3)
+  {
+    boost::lock(m1, m2, m3);
+    return std::tuple<unique_lock<L1>,unique_lock<L2>,unique_lock<L3> >(
+        unique_lock<L1>(m1, adopt_lock),
+        unique_lock<L2>(m2, adopt_lock),
+        unique_lock<L3>(m3, adopt_lock)
+    );
+  }
+
+#endif
+#endif
+//  int main()
+//  {
+//    std::mutex m1;
+//    std::mutex m2;
+//    std::mutex m3;
+//
+//    {
+//      auto lks = make_unique_locks(m1, m2, m3);
+//
+//    }
+//    return 0;
+//  }
+
+}
+
+#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 11:24:27 EST (Sat, 24 Nov 2012)
@@ -367,6 +367,18 @@
           [ thread-run2 ./sync/mutual_exclusion/locks/unique_lock/types_pass.cpp : unique_lock__types_p ]
     ;
 
+    #explicit ts_make_unique_lock ;
+    test-suite ts_make_unique_lock
+    :
+          [ thread-run2 ./sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_mutex_pass.cpp : make_unique_lock__mutex_p ]
+          [ thread-run2 ./sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_adopt_lock_pass.cpp : make_unique_lock__adopt_lock_p ]
+          [ thread-run2 ./sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_defer_lock_pass.cpp : make_unique_lock__defer_lock_p ]
+          [ thread-run2 ./sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_try_to_lock_pass.cpp : make_unique_lock__try_to_lock_p ]
+
+          [ thread-run2 ./sync/mutual_exclusion/locks/unique_lock/cons/make_unique_locks_mutex_pass.cpp : make_unique_locks__mutex_p ]
+
+    ;
+
     #explicit ts_shared_lock ;
     test-suite ts_shared_lock
     :
Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_adopt_lock_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_adopt_lock_pass.cpp	2012-11-24 11:24:27 EST (Sat, 24 Nov 2012)
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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_factories.hpp>
+
+// template <class Mutex> class unique_lock;
+// unique_lock<Mutex> make_unique_lock(Mutex&, adopt_lock_t);
+
+#include <boost/thread/lock_factories.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+
+int main()
+{
+  boost::mutex m;
+  m.lock();
+  auto lk = boost::make_unique_lock(m, boost::adopt_lock);
+  BOOST_TEST(lk.mutex() == &m);
+  BOOST_TEST(lk.owns_lock() == true);
+
+  return boost::report_errors();
+}
+
+#else
+int main()
+{
+  return boost::report_errors();
+}
+#endif
+
Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_defer_lock_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_defer_lock_pass.cpp	2012-11-24 11:24:27 EST (Sat, 24 Nov 2012)
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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_factories.hpp>
+
+// template <class Mutex> class unique_lock;
+// unique_lock<Mutex> make_unique_lock(Mutex&, defer_lock_t);
+
+// unique_lock(mutex_type& m, adopt_lock_t);
+
+#include <boost/thread/lock_factories.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+int main()
+{
+  boost::mutex m;
+  m.lock();
+  auto lk = boost::make_unique_lock(m, boost::defer_lock);
+  BOOST_TEST(lk.mutex() == &m);
+  BOOST_TEST(lk.owns_lock() == false);
+
+  return boost::report_errors();
+}
+
+#else
+int main()
+{
+  return boost::report_errors();
+}
+#endif
+
Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_mutex_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_mutex_pass.cpp	2012-11-24 11:24:27 EST (Sat, 24 Nov 2012)
@@ -0,0 +1,83 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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_factories.hpp>
+
+// template <class Mutex>
+// unique_lock<Mutex> make_unique_lock(Mutex&);
+
+#include <boost/thread/lock_factories.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+
+boost::mutex m;
+
+#if defined BOOST_THREAD_USES_CHRONO
+
+typedef boost::chrono::system_clock Clock;
+typedef Clock::time_point time_point;
+typedef Clock::duration duration;
+typedef boost::chrono::milliseconds ms;
+typedef boost::chrono::nanoseconds ns;
+#else
+#endif
+
+void f()
+{
+#if defined BOOST_THREAD_USES_CHRONO
+  time_point t0 = Clock::now();
+  time_point t1;
+  {
+    auto&& _ = boost::make_unique_lock(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
+#else
+  //time_point t0 = Clock::now();
+  //time_point t1;
+  {
+    auto _ = boost::make_unique_lock(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
+#endif
+}
+
+int main()
+{
+  m.lock();
+  boost::thread t(f);
+#if defined BOOST_THREAD_USES_CHRONO
+  boost::this_thread::sleep_for(ms(250));
+#else
+#endif
+  m.unlock();
+  t.join();
+
+  return boost::report_errors();
+}
+#else
+int main()
+{
+  return boost::report_errors();
+}
+#endif
+
Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_try_to_lock_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_try_to_lock_pass.cpp	2012-11-24 11:24:27 EST (Sat, 24 Nov 2012)
@@ -0,0 +1,110 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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/locks.hpp>
+
+// template <class Mutex> class unique_lock;
+// unique_lock<Mutex> make_unique_lock(Mutex&, try_to_lock_t);
+
+#include <boost/thread/lock_factories.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+#if ! defined(BOOST_NO_CXX11_AUTO) && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+
+boost::mutex m;
+
+#if defined BOOST_THREAD_USES_CHRONO
+typedef boost::chrono::system_clock Clock;
+typedef Clock::time_point time_point;
+typedef Clock::duration duration;
+typedef boost::chrono::milliseconds ms;
+typedef boost::chrono::nanoseconds ns;
+#else
+#endif
+
+void f()
+{
+#if defined BOOST_THREAD_USES_CHRONO
+  time_point t0 = Clock::now();
+  {
+    auto lk = boost::make_unique_lock(m, boost::try_to_lock);
+    BOOST_TEST(lk.owns_lock() == false);
+  }
+  {
+    auto lk = boost::make_unique_lock(m, boost::try_to_lock);
+    BOOST_TEST(lk.owns_lock() == false);
+  }
+  {
+    auto lk = boost::make_unique_lock(m, boost::try_to_lock);
+    BOOST_TEST(lk.owns_lock() == false);
+  }
+  while (true)
+  {
+    auto lk = boost::make_unique_lock(m, boost::try_to_lock);
+    if (lk.owns_lock()) break;
+  }
+  time_point t1 = Clock::now();
+  //m.unlock();
+  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(50000000)+ms(1000)); // within 50ms
+#else
+//  time_point t0 = Clock::now();
+//  {
+//    boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
+//    BOOST_TEST(lk.owns_lock() == false);
+//  }
+//  {
+//    boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
+//    BOOST_TEST(lk.owns_lock() == false);
+//  }
+//  {
+//    boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
+//    BOOST_TEST(lk.owns_lock() == false);
+//  }
+  while (true)
+  {
+    auto lk = boost::make_unique_lock(m, boost::try_to_lock);
+    if (lk.owns_lock()) break;
+  }
+  //time_point 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(50000000)+ms(1000)); // within 50ms
+#endif
+}
+
+int main()
+{
+  m.lock();
+  boost::thread t(f);
+#if defined BOOST_THREAD_USES_CHRONO
+  boost::this_thread::sleep_for(ms(250));
+#else
+#endif
+  m.unlock();
+  t.join();
+
+  return boost::report_errors();
+}
+
+#else
+int main()
+{
+  return boost::report_errors();
+}
+#endif
+
Added: trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_locks_mutex_pass.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_locks_mutex_pass.cpp	2012-11-24 11:24:27 EST (Sat, 24 Nov 2012)
@@ -0,0 +1,90 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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_factories.hpp>
+
+// template <class Mutex>
+// unique_lock<Mutex> make_unique_lock(Mutex&);
+
+#include <boost/thread/lock_factories.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+#if ! defined(BOOST_NO_CXX11_AUTO) && defined BOOST_NO_CXX11_HDR_TUPLE && ! defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
+
+boost::mutex m1;
+boost::mutex m2;
+boost::mutex m3;
+
+
+#if defined BOOST_THREAD_USES_CHRONO
+
+typedef boost::chrono::system_clock Clock;
+typedef Clock::time_point time_point;
+typedef Clock::duration duration;
+typedef boost::chrono::milliseconds ms;
+typedef boost::chrono::nanoseconds ns;
+#else
+#endif
+
+void f()
+{
+#if defined BOOST_THREAD_USES_CHRONO
+  time_point t0 = Clock::now();
+  time_point t1;
+  {
+    auto&& _ = boost::make_unique_locks(m1,m2,m3);
+    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
+#else
+  //time_point t0 = Clock::now();
+  //time_point t1;
+  {
+    auto&& _ = boost::make_unique_locks(m1,m2,m3);
+    //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
+#endif
+}
+
+int main()
+{
+  m1.lock();
+  m2.lock();
+  m3.lock();
+  boost::thread t(f);
+#if defined BOOST_THREAD_USES_CHRONO
+  boost::this_thread::sleep_for(ms(250));
+#else
+#endif
+  m1.unlock();
+  m2.unlock();
+  m3.unlock();
+  t.join();
+
+  return boost::report_errors();
+}
+#else
+int main()
+{
+  return boost::report_errors();
+}
+#endif
+