$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r81673 - in trunk: boost/thread libs/thread/example
From: vicente.botet_at_[hidden]
Date: 2012-12-02 13:30:01
Author: viboes
Date: 2012-12-02 13:30:00 EST (Sun, 02 Dec 2012)
New Revision: 81673
URL: http://svn.boost.org/trac/boost/changeset/81673
Log:
Thread: make synchronized_value use Boost.Move and add some value based operations
Text files modified: 
   trunk/boost/thread/synchronized_value.hpp        |   204 ++++++++++++++++++++++++++++++++++++--- 
   trunk/libs/thread/example/synchronized_value.cpp |    28 ++--                                    
   2 files changed, 202 insertions(+), 30 deletions(-)
Modified: trunk/boost/thread/synchronized_value.hpp
==============================================================================
--- trunk/boost/thread/synchronized_value.hpp	(original)
+++ trunk/boost/thread/synchronized_value.hpp	2012-12-02 13:30:00 EST (Sun, 02 Dec 2012)
@@ -9,11 +9,15 @@
 #define BOOST_THREAD_SYNCHRONIZED_VALUE_HPP
 
 #include <boost/thread/detail/config.hpp>
-#if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
 
+#include <boost/thread/detail/move.hpp>
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/lock_types.hpp>
 #include <boost/thread/lock_guard.hpp>
+#include <boost/thread/lock_algorithms.hpp>
+#include <boost/thread/lock_factories.hpp>
+#include <boost/thread/strict_lock.hpp>
+#include <boost/utility/swap.hpp>
 
 #include <boost/config/abi_prefix.hpp>
 
@@ -32,17 +36,151 @@
     T value_;
     mutable lockable_type mtx_;
   public:
+    /**
+     * Default constructor.
+     *
+     * Requires: T is DefaultConstructible
+     */
     synchronized_value()
     : value_()
     {
     }
 
-    synchronized_value(T other)
+    /**
+     * Constructor from copy constructible value.
+     *
+     * Requires: T is CopyConstructible
+     */
+    synchronized_value(T const& other)
     : value_(other)
     {
     }
 
     /**
+     * Move Constructor from movable value.
+     *
+     * Requires: T is CopyConstructible
+     */
+    synchronized_value(BOOST_THREAD_RV_REF(T) other)
+    : value_(boost::move(other))
+    {
+    }
+
+    /**
+     * Copy Constructor.
+     *
+     * Requires: T is DefaultConstructible and Assignable
+     * Effects: Assigns the value on a scope protected by the mutex of the rhs. The mutex is not copied.
+     */
+    synchronized_value(synchronized_value const& rhs)
+    {
+      strict_lock<lockable_type> lk(rhs.mtx_);
+      value_ = rhs.value_;
+    }
+
+    /**
+     * Move Constructor.
+     *
+     */
+    synchronized_value(BOOST_THREAD_RV_REF(synchronized_value) other)
+    {
+      strict_lock<lockable_type> lk(other.mtx_);
+      value_= boost::move(other);
+    }
+
+    /**
+     * Assignment operator.
+     *
+     * Effects: Copies the underlying value on a scope protected by the two mutexes.
+     * The mutexes are not copied. The locks are acquired using lock, so deadlock is avoided.
+     * For example, there is no problem if one thread assigns a = b and the other assigns b = a.
+     *
+     * Return: *this
+     */
+    synchronized_value& operator=(synchronized_value const& rhs)
+    {
+      if(&rhs != this)
+      {
+        // auto _ = make_unique_locks(mtx_, rhs.mtx_);
+        unique_lock<lockable_type> lk1(mtx_, defer_lock);
+        unique_lock<lockable_type> lk2(rhs.mtx_, defer_lock);
+        lock(lk1,lk2);
+
+        value_ = rhs.value_;
+      }
+      return *this;
+    }
+    /**
+     * Assignment operator from a T const&.
+     * Effects: The operator copies the value on a scope protected by the mutex.
+     * Return: *this
+     */
+    synchronized_value& operator=(value_type const& value)
+    {
+      {
+        strict_lock<lockable_type> lk(mtx_);
+        value_ = value;
+      }
+      return *this;
+    }
+
+    /**
+     * Explicit conversion to value type.
+     *
+     * Requires: T is CopyConstructible
+     * Return: A copy of the protected value obtained on a scope protected by the mutex.
+     *
+     */
+    T get() const
+    {
+      strict_lock<lockable_type> lk(mtx_);
+      return value_;
+    }
+    /**
+     * Explicit conversion to value type.
+     *
+     * Requires: T is CopyConstructible
+     * Return: A copy of the protected value obtained on a scope protected by the mutex.
+     *
+     */
+#if ! defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
+    explicit operator T() const
+    {
+      return get();
+    }
+#endif
+
+    /**
+     * Swap
+     *
+     * Effects: Swaps the data. Again, locks are acquired using lock(). The mutexes are not swapped.
+     * A swap method accepts a T& and swaps the data inside a critical section.
+     * This is by far the preferred method of changing the guarded datum wholesale because it keeps the lock only
+     * for a short time, thus lowering the pressure on the mutex.
+     */
+    void swap(synchronized_value & rhs)
+    {
+      if (this == &rhs) {
+        return;
+      }
+      // auto _ = make_unique_locks(mtx_, rhs.mtx_);
+      unique_lock<lockable_type> lk1(mtx_, defer_lock);
+      unique_lock<lockable_type> lk2(rhs.mtx_, defer_lock);
+      lock(lk1,lk2);
+      boost::swap(value_, rhs.value_);
+    }
+    /**
+     * Swap with the underlying type
+     *
+     * Effects: Swaps the data on a scope protected by the mutex.
+     */
+    void swap(value_type & rhs)
+    {
+      strict_lock<lockable_type> lk(mtx_);
+      boost::swap(value_, rhs.value_);
+    }
+
+    /**
      *
      */
     struct const_strict_synchronizer
@@ -50,6 +188,7 @@
     protected:
       friend class synchronized_value;
 
+      // this should be a strict_lock, but we need to be able to return it.
       boost::unique_lock<lockable_type> lk_;
       T const& value_;
 
@@ -58,10 +197,10 @@
       {
       }
     public:
-      BOOST_THREAD_NO_COPYABLE( const_strict_synchronizer )
+      BOOST_THREAD_MOVABLE_ONLY( const_strict_synchronizer )
 
-      const_strict_synchronizer(const_strict_synchronizer&& other)
-      : lk_(boost::move(other.lk_)),value_(other.value_)
+      const_strict_synchronizer(BOOST_THREAD_RV_REF(const_strict_synchronizer) other)
+      : lk_(boost::move(BOOST_THREAD_RV(other).lk_)),value_(BOOST_THREAD_RV(other).value_)
       {
       }
 
@@ -94,9 +233,9 @@
       {
       }
     public:
-      BOOST_THREAD_NO_COPYABLE( strict_synchronizer )
+      BOOST_THREAD_MOVABLE_ONLY( strict_synchronizer )
 
-      strict_synchronizer(strict_synchronizer&& other)
+      strict_synchronizer(BOOST_THREAD_RV_REF(strict_synchronizer) other)
       : const_strict_synchronizer(boost::move(other))
       {
       }
@@ -118,15 +257,36 @@
     };
 
 
+    /**
+     * Essentially calling a method obj->foo(x, y, z) calls the method foo(x, y, z) inside a critical section as
+     * long-lived as the call itself.
+     */
     strict_synchronizer operator->()
     {
       return BOOST_THREAD_MAKE_RV_REF(strict_synchronizer(*this));
     }
+    /**
+     * If the synchronized_value object involved is const-qualified, then you'll only be able to call const methods
+     * through operator->. So, for example, vec->push_back("xyz") won't work if vec were const-qualified.
+     * The locking mechanism capitalizes on the assumption that const methods don't modify their underlying data.
+     */
     const_strict_synchronizer operator->() const
     {
       return BOOST_THREAD_MAKE_RV_REF(const_strict_synchronizer(*this));
     }
 
+    /**
+     * The synchronize() factory make easier to lock on a scope.
+     * As discussed, operator-> can only lock over the duration of a call, so it is insufficient for complex operations.
+     * With synchronize() you get to lock the object in a scoped and to directly access the object inside that scope.
+     *
+     * Example
+     *   void fun(synchronized_value<vector<int>> & vec) {
+     *     auto&& vec=vec.synchronize();
+     *     vec.push_back(42);
+     *     assert(vec.back() == 42);
+     *   }
+     */
     strict_synchronizer synchronize()
     {
       return BOOST_THREAD_MAKE_RV_REF(strict_synchronizer(*this));
@@ -148,14 +308,14 @@
       T& value_;
 
     public:
-      BOOST_THREAD_NO_COPYABLE(unique_synchronizer)
+      BOOST_THREAD_MOVABLE_ONLY(unique_synchronizer)
 
       explicit unique_synchronizer(synchronized_value& outer)
       : base_type(outer.mtx_), value_(outer.value_)
       {
       }
-      unique_synchronizer(unique_synchronizer&& other):
-      base_type(static_cast<base_type&&>(other)),value_(other.value_)
+      unique_synchronizer(BOOST_THREAD_RV_REF(unique_synchronizer) other)
+      : base_type(boost::move(other)),value_(BOOST_THREAD_RV(other).value_)
       {
       }
 
@@ -207,9 +367,10 @@
       {}
 
     public:
-      BOOST_THREAD_NO_COPYABLE(deref_value)
-      deref_value(deref_value&& other):
-      lk_(boost::move(other.lk_)),value_(other.value_)
+      BOOST_THREAD_MOVABLE_ONLY(deref_value)
+
+      deref_value(BOOST_THREAD_RV_REF(deref_value) other):
+      lk_(boost::move(BOOST_THREAD_RV(other).lk_)),value_(BOOST_THREAD_RV(other).value_)
       {}
       operator T()
       {
@@ -235,9 +396,10 @@
       {}
 
     public:
-      BOOST_THREAD_NO_COPYABLE(const_deref_value)
-      const_deref_value(const_deref_value&& other):
-      lk_(boost::move(other.lk_)), value_(other.value_)
+      BOOST_THREAD_MOVABLE_ONLY(const_deref_value)
+
+      const_deref_value(BOOST_THREAD_RV_REF(const_deref_value) other):
+      lk_(boost::move(BOOST_THREAD_RV(other).lk_)), value_(BOOST_THREAD_RV(other).value_)
       {}
 
       operator T()
@@ -259,9 +421,17 @@
 
   };
 
+  /**
+   *
+   */
+  template <typename T, typename L>
+  inline void swap(synchronized_value<T,L> & lhs, synchronized_value<T,L> & rhs)
+  {
+    lhs.swap(rhs);
+  }
+
 }
 
 #include <boost/config/abi_suffix.hpp>
 
-#endif // BOOST_NO_CXX11_RVALUE_REFERENCES
 #endif // header
Modified: trunk/libs/thread/example/synchronized_value.cpp
==============================================================================
--- trunk/libs/thread/example/synchronized_value.cpp	(original)
+++ trunk/libs/thread/example/synchronized_value.cpp	2012-12-02 13:30:00 EST (Sun, 02 Dec 2012)
@@ -57,6 +57,21 @@
       std::cout<<"v1="<<*u<<std::endl;
       g(u);
     }
+    boost::synchronized_value<int> v2(2);
+    std::cout<<"v2="<<*v2<<std::endl;
+    v2 = 3;
+    std::cout<<"v2="<<*v2<<std::endl;
+
+    boost::synchronized_value<int> v3(v2);
+    std::cout<<"v3="<<*v3<<std::endl;
+    v3 = v1;
+    std::cout<<"v3="<<*v3<<std::endl;
+
+    std::cout<<"v2="<<*v3<<std::endl;
+    std::cout<<"v3="<<*v3<<std::endl;
+    swap(v3,v2);
+    v1.swap(v2);
+    std::cout<<"v3="<<*v3<<std::endl;
   }
   {
     boost::synchronized_value<std::string> s;
@@ -65,21 +80,8 @@
   }
   {
     boost::synchronized_value<std::string> s;
-#if 1
     s->append("foo/");
-#else
-    s.synchronize()->append("foo");
-#endif
-    addTrailingSlashIfMissing(s);
-    std::cout<<"s="<<std::string(*s)<<std::endl;
-  }
-  {
-    boost::synchronized_value<std::string> s;
-#if 1
-    s->append("foo");
-#else
     s.synchronize()->append("foo");
-#endif
     addTrailingSlashIfMissing(s);
     std::cout<<"s="<<std::string(*s)<<std::endl;
   }